Student Center
Solution: Develop a moving average filter
![]() |
| Figure 1: Corrupted Signal through a Moving Average Filter. |
% Moving Average Filter Example
%
% Generate the Digital signal: Sine wave
A = 1; |
% Amplitude |
x0 = A*sin(2*pi*f*n*T);
stem(n,x0);
title('Desired Signal: Sine Wave');
xlabel('Time (sec)');
ylabel('Amplitude');

% Generate the Discrete White Gaussian Noise
L = length(n);
var = 0.2; |
% variance |
stem(outputs)
title('White Gaussian Noise');
xlabel('Time (sec)');
ylabel('Amplitude');

% Generate the Signal with Noise (corrupted signal)
% Here the discrete signal is plotted using PLOT
% this is to better visualize the corruption
nn = reshape(noise,1,41);
x = x0 + nn;
plot(n,x)
title('Signal with Noise Corruption');
xlabel('Time (sec)');
ylabel('Amplitude');

% Create the M-point Average Moving Filter
M = 6;
B = ones(M,1)/M;
% Analyze the filter characteristics using FVTOOL: Verify that it is a lowpass filter fvtool(B,1);
% You can also do the following to analyze the filter % and implement it directly
% Hd = dfilt.dffir(B,1);
% fvtool(Hd);
% y = filter(Hd,x);

% Filter the corrupted signal
% Here the discrete filtered signal is plotted using
PLOT; this is for better visualization.
y = filter(B,1,x);
plot(n,y)
title('Filtered Signal through an Average Moving
Filter');
xlabel('Time (sec)');
ylabel('Amplitude');
% Plot both corrupted and uncorrupted signals
plot(n,x,'-',n,y,':')
title('Uncorrupted and Corrupted Signals');
xlabel('Time (sec)');
ylabel('Amplitude');
legend('x[n]: Corrupted','y[n]: Smoothed')

Looking at the filtered signal, we can tell that it is smoother since a lot of the noise was removed. There is a slight delay in the filtered signal from the original, but that is characteristic of filtering. Also, note that an even smoother signal can be obtained if the number of points (M) used to average is increased, but realize that this is will further increase the delay in the filtered signal.
Store
