On Thu, 15 Jun 2000, Andrew Warren wrote: > Plunkett, Dennis wrote: > > > A short time ago there was a thread on filters, and a suggestion > > for a long term averaging filter was proposed that went along the > > lines of:- > > > > average = (((temp*width)-average)+newsample)/width > > Actually, Dennis, it was probably: > > average = (((average*width)-average)+newsample)/width > > which is usually simplified to: > > average = (average * (width-1) + newsample) / width. Which is EXACTLY the form discussed just a few weeks ago! E.g.: avg = (K1*avg + K2*newsample)/(K1+K2) In this case, K1 = 15, K2 = 1 and their sum is 16. > > > 1/ This filter assumes that the processor is a floating point > > No; the equation is EASILY implemented using integer math. It > works like this: > > 1. Since integer division by a power of 2 is trivial, we make > "width" equal to a power of 2. > > 2. Since multiplication by a power of 2 is also trivial, we > rewrite the equation thusly: > > average = ((newsample-average) + (width*average)) / width > > If we can make width = 256, the code is absurdly simple... But > that's a special case. Here's the code for width = 16, which is > more representative of what'll usually be required: > > ; Written by Andrew Warren (fastfwd@ix.netcom.com). > ; > ; (NEW - AVE) + 16*AVE > ; AVE = -------------------- > ; 16 If you wish to generalize this then check out: http://www.dattalo.com/technical/software/pic/twist.asm Which weighs in at 22 cycles for a divisor of 16. Pros: the filter coefficients are arbitrary and the filter is scalable. Cons: The 'Average' is only 8-bits instead of 12. Scott PS. I doubt it's only me, but it's really refreshing to see Andy posting PIC code again!