On 2011-02-15 10:28, Olin Lathrop wrote: > N. T. wrote: >> You may call it whatever you want, but it is "A simple moving average >> (SMA) is the unweighted mean of the previous n data points". > Yes, which is also known as a box filter. The properties of such a filte= r > are well known. It has one important characteristic in that the gain at = one > ... > it's conceptually obvious, not that there aren't better tradeoffs for mos= t > applications. > What are the tradeoffs? Why is a moving average a poor filter? Why is=20 it called a "box"? How could you make a better choice? A filter where some number of previous samples are weighted and summed=20 up to produce the next output is a Finite Impulse Response (FIR)=20 filter. A moving average takes a number of previous samples and sums=20 them, thus it is a FIR filter. Those samples that are included in the sum are said to be in the=20 "window". If you make a graph with the sample number on the X axis and=20 the relative weight of each sample on the Y axis, you can visualize the=20 shape of the "window". A moving average assigns the same weight to every sample in the window,=20 and then the weight drops to zero for any sample old enough to be=20 outside the window. Thus the window shape is rectangular, aka a=20 "box". If you used a FIR filter where each previous sample is weighed=20 differently, you can achieve better filter performance. Of course then=20 the "window" will have a different shape because the weight of each=20 previous sample is different. See http://www.filter-solutions.com/FIR.html to see some example of=20 different window shapes. Notice the graph to the right of each window=20 shape. Some window shapes achieve much better stopband attenuation, or=20 have much less stopband ripple. There is a tradeoff with how steep the filter response is. For example=20 the Blackman-Harris window shape achieves very deep attenuation of the=20 stopband, but the filter falls off quite slowly. Which one you choose=20 is an engineering choice that depends on how far the desired signals lie=20 from the undesired noise, what the nature of the noise is, how sensitive=20 the output is, how much CPU you have to do multiplications, how much RAM=20 to store past samples, etc... For example if your noise is around twice the frequency of your signal,=20 a filter with a gentle slope may not exclude the noise. You need a=20 steeper filter. But if the noise is 10x the frequency of the signal,=20 you can choose a filter with a slow rolloff and very good stopband=20 attenuation. If your output only has a dynamic range of 8 bits, then=20 50dB of attenuation is plenty because it's under the quantization noise=20 floor. Many times you can compute the filter shape beforehand. Some filter=20 shapes allow very simple math at runtime (as mentioned, you can right=20 shift 2^N previous samples N bits and sum them for a very fast=20 rectangular filter). Others require fixed or floating point math which=20 may not be fast enough. The DSP chips have hardware that makes these=20 particular operations fast. They can traverse two areas of memory at the=20 same time, one which has your sample values and one which has your=20 filter coefficients. They can multiply the two, sum the result into a=20 high-precision accumulator, advance the addresses (while properly=20 wrapping) and prefetch the next two values , all in one clock cycle... =20 (This is the merged MAC instruction - see=20 http://192.197.62.35/courses/ctec1631/lab3/MathDemo/ctec1631-dsPIC_MathDemo= ..pdf=20 ) How this applies to the measurement noise in the RPM application? I'll=20 have to think about that for a bit! Joe Koberg --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .