The moving average filter is a simple Low Pass FIR (Finite Impulse Response) filter commonly used for smoothing an array of sampled data/signal. It takes
As the filter length increases (the parameter
The MA filter performs three important functions:
1) It takes
2) Due to the computation/calculations involved, the filter introduces a definite amount of delay
3) The filter acts as a Low Pass Filter (with poor frequency domain response and a good time domain response).
Implementation
The difference equation for a
For example, a
The unit delay shown in Figure 1 is realized by either of the two options:
- Representing the input samples as an array in the computer memory and processing them
- Using D-Flip flop shift registers for digital hardware implementation. If each discrete value of the input
is represented as a -bit signal line from ADC (analog to digital converter), then we would require 4 sets of 12-Flip flops to implement the -point moving average filter shown in Figure 1.
Z-Transform and Transfer function
In signal processing, delaying a signal
Similarly, the Z-transform of the generic
The transfer function describes the input-output relationship of the system and for the
Simulating the filter in Matlab and Python
In Matlab, we can use the filter function or conv (convolution) to implement the moving average FIR filter.
In general, the Z-transform
where,
For implementing equation (6) using the filter function, the Matlab function is called as
B = [b_0, b_1, b_2, ..., b_n] %numerator coefficients A = [a_0, a_1, a_2, ..., a_m] %denominator coefficients y = filter(B,A,x) %filter input x and get result in y
We can note from the difference equation and transfer function of the
Therefore, the
B = [0.2, 0.2, 0.2, 0.2, 0.2] %numerator coefficients A = [1] %denominator coefficients y = filter(B,A,x) %filter input x and get result in y
The numerator coefficients for the moving average filter can be conveniently expressed in short notion as shown below
L = 5 B = ones(1,L)/L %numerator coefficients A = [1] %denominator coefficients x = rand(1,10) %random samples for x y = filter(B,A,x) %filter input x and get result in y
When using conv function to implement the moving average filter, the following code can be used
L = 5; x = rand(1,10) %random samples for x; y = conv(x,ones(1,L)/L)
There exists a difference between using conv function and filter function for implementing an FIR filter. The conv function gives the result of complete convolution and the length of the result is length(x)+ L -1. Whereas, the filter function gives the output that is of same length as that of the input
In python, the filtering operation can be performed using the lfilter and convolve functions available in the scipy signal processing package. The equivalent python code is shown below.
import numpy as np from scipy import signal L=5 #L-point filter b = (np.ones(L))/L #numerator co-effs of filter transfer function a = np.ones(1) #denominator co-effs of filter transfer function x = np.random.randn(10) #10 random samples for x y = signal.convolve(x,b) #filter output using convolution y = signal.lfilter(b,a,x) #filter output using lfilter function
Pole-zero plot and frequency response
A pole-zero plot for a filter transfer function
In a pole-zero plot, the locations of the poles are usually marked by cross (
In Matlab, the pole-zero plot and the frequency response of the
L=11 zplane([ones(1,L)]/L,1); %pole-zero plot w = -pi:(pi/100):pi; %to plot frequency response freqz([ones(1,L)]/L,1,w); %plot magnitude and phase response
The magnitude and phase frequency responses can be coded in Python as follows
[updated] See my Google colab for full Python code
import numpy as np from scipy import signal import matplotlib.pyplot as plt L=11 #L-point filter b = (np.ones(L))/L #numerator co-effs of filter transfer function a = np.ones(1) #denominator co-effs of filter transfer function w, h = signal.freqz(b,a) plt.subplot(2, 1, 1) plt.plot(w, 20 * np.log10(abs(h))) plt.ylabel('Magnitude [dB]') plt.xlabel('Frequency [rad/sample]') plt.subplot(2, 1, 2) angles = np.unwrap(np.angle(h)) plt.plot(w, angles) plt.ylabel('Angle (radians)') plt.xlabel('Frequency [rad/sample]') plt.show()
Case study:
Following figures depict the time domain & frequency domain responses of a
On the first plot, we have the noisy square wave signal that is going into the moving average filter. The input is noisy and our objective is to reduce the noise as much as possible. The next figure is the output response of a 3-point Moving Average filter. It can be deduced from the figure that the 3-point Moving Average filter has not done much in filtering out the noise. We increase the filter taps to 10-points and we can see that the noise in the output has reduced a lot, which is depicted in next figure.
With L=51 tap filter, though the noise is almost zero, the transitions are blunted out drastically (observe the slope on the either side of the signal and compare them with the ideal brick wall transitions in the input signal).
From the frequency response of lower order filters (L=3, L=10), it can be asserted that the roll-off is very slow and the stop band attenuation is not good. Given this stop band attenuation, clearly, the moving average filter cannot separate one band of frequencies from another. As we increase the filter order to 51, the transitions are not preserved in time domain. Good performance in the time domain results in poor performance in the frequency domain, and vice versa. Compromise need for optimal filter design.
Rate this article: Note: There is a rating embedded within this post, please visit this post to rate it.
References:
[1] Filtering – OpenCourseWare – MIT.↗
[2] HC Chen et al.,”Moving Average Filter with its application to QRS detection”,IEEE Computers in Cardiology, 2003.↗
Similar topics
Books by the author
Wireless Communication Systems in Matlab Second Edition(PDF) Note: There is a rating embedded within this post, please visit this post to rate it. | Digital Modulations using Python (PDF ebook) Note: There is a rating embedded within this post, please visit this post to rate it. | Digital Modulations using Matlab (PDF ebook) Note: There is a rating embedded within this post, please visit this post to rate it. |
Hand-picked Best books on Communication Engineering Best books on Signal Processing |