Hi Lawrence, Keep in mind that for a constant window width, an average really isn't all that different than a sum. For example, take the series sum average 2, 3, 4 9 3 3, 4, 5 12 4 4, 5, 6 15 5 If, say, you wanted to exclude windows with average > 4, and you knew the window width was constant at 3, you could simply exclude windows with sum > 12. That would save you the computational cost of division. Cheers, Craig ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ craign@uic.edu ~~~~~~~~~~~~~~~~~~~~~~~~~ Craig Niederberger MD FACS | Chief, Division of Andrology University of Illinois at Chicago | Director Urologic Basic Research Department of Urology M/C 958 | 312-996-2779 Fax: 312-996-1291 840 South Wood St, Chicago IL 60612 | http://godot.urol.uic.edu On Tue, 23 Sep 1997, Lawrence Lile wrote: > Anybody got a good averaging algorithm? > > I've got a stream of numbers from 0 to 15 coming out of a PIC '620 > comparator representing temperature of an appliance. When I turn on > the flourescent lamp on my bench, the program picks up a spike, and > the relay in this thing fires momentarily, because it reads the > noise. "NO PROBLEM" I sez, and sat down to write an averaging > algoritm. > > This ought to be so easy. The first "Algorithm" anybody learns in > kindy-garden Math 1 is how to average numbers. I've been checking > out verious ways of averaging a stream of data, though, and don't > have one that satisfies on a PIC with Two byte arithmetic and no > floating point math. > > Try this. Make a spreadsheet with 300 or 400 random integers between > 0 and 16. Take the arithmetic average of all of 'em, probably > around 8. Then try to simulate a PIC program processing each number > in succession that would get near the average, without floating point > math. I tried a half dozen ways this morning. > > One trick I worked out is that the average of N numbers, given the > NTH piece of data is being processed now, is (N-1)*(old average)/N > plus (Nth piece of data)/N. If you pick 256 data points, given the > old average AVG and the new data point DATA, > > NEWAVG = {(AVG*255)+DATA}/256 > > That oughta work! Not so fast. Using only integer values,no > floating point math, the average climbs slowly toward zero and stays > there. PICs can probably do floating point math, but there's gotta > be an easier way! > > > Best Regards, > > Lawrence Lile >