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 M samples of input at a time and take the average of those Msamples and produces a single output point. It is a very simple LPF (Low Pass Filter) structure that comes handy for scientists and engineers to filter unwanted noisy component from the intended data.
As the filter length increases ( the parameter M ) the smoothness of the output increases, whereas the sharp transitions in the data are made increasingly blunt. This implies that this filter has excellent time domain response but a poor frequency response.
The MA filter perform three important functions:
1) It takes M input points, computes the average of those Mpoints and produces a single output point
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).
Matlab Code:
Following matlab code simulates the time domain response of a Mpoint Moving Average filter and also plots the frequency response for various filter lengths.
Check this book for full Matlab code.
Simulation of Digital Communication Systems Using Matlab – by Mathuranathan Viswanathan
Time Domain Response:
Input to MA filter  3point MA filter output 


51point MA filter output  101point MA filter output 


501point MA filter output  
On the first plot, we have the input that is going into the moving average filter. The input is noisy and our objective is to reduce the noise. The next figure is the output response of a 3point Moving Average filter. It can be deduced from the figure that the 3point Moving Average filter has not done much in filtering out the noise. We increase the filter taps to 51points and we can see that the noise in the output has reduced a lot, which is depicted in next figure.
We increase the taps further to 101 and 501 and we can observe that eventhough 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 transition in our input).
Frequency Response:
From the frequency response it can be asserted that the rolloff 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 know that a good performance in the time domain results in poor performance in the frequency domain, and vice versa. In short, the moving average is an exceptionally good smoothing filter (the action in the time domain), but an exceptionally bad lowpass filter (the action in the frequency domain)
External Links:
[1] Filtering – OpenCourseWare – MIT
[2] HC Chen et al.,”Moving Average Filter with its application to QRS detection”,IEEE Computers in Cardiology,2003
Pingback: Colored Noise Generation in Matlab  GaussianWaves()
Pingback: Calculating power and energy content of a signal in MATLAB  GaussianWaves()
Pingback: Relation between Eb/N0 and SNR  GaussianWaves()
Pingback: Symbol Timing Recovery for QPSK (digital modulations)  GaussianWaves()
Pingback: Central Limit Theorem  GaussianWaves()