You are on page 1of 3

Electronics and Communications Engineering Department

Adamson University

Lab Exercise 2: Moving Average Filters: Implementation by Convolution

The moving average is the most common filter in DSP, mainly because it is the easiest digital filter to understand and
use. In spite of its simplicity, the moving average filter is optimal for a common task: reducing random noise while
retaining a sharp step response. This makes it the premier filter for time domain encoded signals. However, the
moving average is the worst filter for frequency domain encoded signals, with little ability to separate one band of
frequencies from another. Relatives of the moving average filter include the Gaussian, Blackman, and multiple-pass
moving average. These have slightly better performance in the frequency domain, at the expense of increased
computation time.

As the name implies, the moving average filter operates by averaging a number of points from the input signal to
produce each point in the output signal. In equation form, this is written:

Example, point 80 in the output of a 5 point moving average filter

Retrieved on http://www.dspguide.com/ch15/1.htm “The Scientist and Engineer's Guide to


Digital Signal Processing” By Steven W. Smith, Ph.D., 2011
Sample Program 1: Convolution
% Program for Convolution
clf;
h = [1,2,1,-1];
x = [1 2 3 -1];
y = conv(h,x);
n = 0:6;
subplot(2,1,1);
stem(n,y);
xlabel('Time index n'); ylabel('Amplitude');
title('Output Obtained by Convolution'); grid;
x1 = [x zeros(1,3)];
y1 = filter(h,1,x1);
subplot(2,1,2);
stem(n,y1);
xlabel('Time index n'); ylabel('Amplitude');
title('Output Generated by Filtering'); grid;

Sample program2: Moving Average Filter


% Program
% Simulation of an M-point Moving Average Filter
% Generate the input signal
n = 0:10;
s1 = cos(2*pi*0.05*n);
s2 = cos(2*pi*0.47*n);
x = s1+s2;
M = input('Desired length of the filter = ');
num = ones(1,M);
y = filter(num,1,x)/M;
clf;
subplot(2,2,1);
plot(n, s1);
axis([0, 10, -2, 2]);
xlabel('Time index n'); ylabel('Amplitude');
title('Signal #1');
subplot(2,2,2);
plot(n, s2);
axis([0, 10, -2, 2]);
xlabel('Time index n'); ylabel('Amplitude');
title('Signal #2');
subplot(2,2,3);
plot(n, x);
axis([0, 10, -2, 2]);
xlabel('Time index n'); ylabel('Amplitude');
title('Input Signal');
subplot(2,2,4);
plot(n, y);
axis([0, 10, -2, 2]);
xlabel('Time index n'); ylabel('Amplitude');
title('Output Signal');
axis;
Exercises:
1. The impulse response of a linear time invariant system is h[n]={1,2,1,-1}, determine the
response (convolution sum)of the system to the input signal x[n] ={1,2,3,1} using by
hand calculation and matlab simulation. (15 points)

2. Det
e rmi
n e

point 10, in the output of the 5


point moving average filter for the condition given above. (15 points)

You might also like