Product Description
Generating and Filtering Digital Signals in Matlab
Introduction to Moving Average Filters
Moving averages are the simplest of all non-recursive or finite impulse response (FIR), low-pass filters. The term moving means that the average is taken over a moving or changing with time set of input samples. Also, in a moving average filter, the filter coefficients are all the same, meaning they are the reciprocal of the number of input terms to be averaged.
The difference equation for a FIR filter is
For example, if a moving average of length 5 is to be performed, the difference equation summation may be expanded into the following form.
y[n]=0.2(x[n]+x[n−1]+x[n−2]+x[n−3]+x[n−4])
All the b k coefficients are equal to the reciprocal of the length, which in this case, is equal to one fifth, or 0.2.
As with all FIR filters, this also introduces the need for storing not only the current input sample but previous or delayed samples, as well. This is usually implemented in a buffer referred to as a circular buffer. The circular designation means that when a new sample input is available, the oldest sample in the buffer is always overwritten by the newest one.
Again, if the moving average filter length is 5, that means the average is taken of the latest five samples of the input signal. To illustrate this, suppose we have the following input sample sequence shown in Table 1.
Sample
Number
First
Second
Third
Fourth
Fifth
Sixth
Seventh
Eighth
Ninth
Sample
Value
3
-2
5
1
4
-3
-1
6
2
Table 1: Input Sample Sequence
Assuming that all sampling and antialiasing filtering requirements have been met for the system and assuming that the sampling frequency equals F s , a new digital sample is provided as an input to the filter once every sampling period, which is the reciprocal of Fs , which is 1/F s = T s , where T s is the sampling period. In addition, all input samples prior to the start of the filtering process have been set to a value of 0.
Table 2 shows the moving average filter inputs and the calculated results after each new sample is received. Please note that until the fifth sample is received, the input signal buffer is in what is called a boundary condition, meaning that not enough samples have been received to calculate an accurate average yet.
Sample
Number
Filter
Input
1
2
3
4
5
6
7
8
9
x[n]
3
3
3
3
3
-3
-3
-3
-3
x[n -1]
0
-2
-2
-2
-2
-2
-1
-1
-1
x[n -2]
0
0
5
5
5
5
5
6
6
x[n -3]
0
0
0
1
1
1
1
1
2
x[n -4]
0
0
0
0
4
4
4
4
4
Mov
Avg
0.6
0.2
1.2
1.4
2.2
1.0
1.2
1.4
1.6
Table 2: Moving Average Samples and Filter Calculations
When sample 1 is received, it is stored in the x[n] element of the input buffer while the remaining elements still contain their initialized values of zero. When sample 2 is received, it is stored in the x[n-1] element. When sample 3 is received, it is stored in thex[n-2] element, and so on until the sixth sample is received. At this point, the buffer is completely filled with the five latest samples when the sixth arrives; the oldest sample in the buffer, which at this time is the x[n] sample, is overwritten with the newest sample.