Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Industries Academia Support User Community Company

 

Student Center

Solution: Develop a moving average filter


figure 1
Figure 1: Corrupted Signal through a Moving Average Filter.

% Moving Average Filter Example
%
% Generate the Digital signal: Sine wave
A = 1;
f = 1;
n = 0:0.25:10;
T = 0.1;
% Amplitude
% frequency (Hz)
% samples
% period (secs)

x0 = A*sin(2*pi*f*n*T);
stem(n,x0);
title('Desired Signal: Sine Wave');
xlabel('Time (sec)');
ylabel('Amplitude');

figure 2

% Generate the Discrete White Gaussian Noise
L = length(n);
var = 0.2;
k = randn(L,1);
mean1 = 0.2;
outputs = mean1 + sqrt(var)*k;
% variance
% nonuniform random samples
% mean
% white noise

stem(outputs)
title('White Gaussian Noise');
xlabel('Time (sec)');
ylabel('Amplitude');

figure 3

% 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');

figure 4

% 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);

figure 5

% 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')

figure 6 figure 7

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.

 
Contact sales
Software evaluation for instructors
Free technical kit
E-mail this page

MATLAB & Simulink Student Version

Includes full-featured versions of both MATLAB and Simulink

From School To Industry

"The only limit to MATLAB is your imagination."
Carl Klopstein
Radar Systems Engineer

Free Code for LEGO MINDSTORMS NXT

LEGO® MINDSTORMS® NXT robotics invention system lets you create and control robots using programmable motors and sensors