Moving average filter problem.

2 views (last 30 days)
Charles Moody
Charles Moody on 1 Apr 2018
Answered: Walter Roberson on 1 Apr 2018
I have an assignment that wants me to create a moving average filter using no filter functions coded into matlab.
gr is a waveform being run through the filter. It is a fourier approximation for a square wave 4/pi * SUM(sin((i*pi)/2)*(400*t+1) where i = {1,3,5,7,9}. gr also must have some .4*randn(size(t)) added to it.
The equation for an M point moving average filter is y(n) = 1/M * SUM from k=0 to k=M-1(x(n-k))
I'm doing something wrong with my indexing and I can't tell where :(
%Assignment 9 - Moving Average Filter
fs = 10000;
M = 8;
t = 0:1/fs:.05;
g1 = (4/pi)*sin((pi/2)*((400*t)+1))+.4*randn(size(t));
g3 = (4/pi)*(1/3)*sin((3*pi/2)*((400*t)+1))+.4*randn(size(t));
g5 = (4/pi)*(1/5)*sin((5*pi/2)*((400*t)+1))+.4*randn(size(t));
g7 = (4/pi)*(1/7)*sin((7*pi/2)*((400*t)+1))+.4*randn(size(t));
g9 = (4/pi)*(1/9)*sin((9*pi/2)*((400*t)+1))+.4*randn(size(t));
gr = g1 + g3 + g5 + g7 + g9 + .4*randn(size(t));
y = zeros(M,length(t));
for i = 1:M
if i == 1
y(i) = sum(gr);
else
y(i) = sum((4/pi)*sin((pi/2)*((400*(t-i)+1)))+ (4/pi)*(1/3)*sin((3*pi/2)*((400*(t-i)+1)))+ (4/pi)*(1/7)*sin((7*pi/2)*((400*(t-i)+1)))+ (4/pi)*(1/9)*sin((9*pi/2)*((400*(t-i))+1)))+.4*randn(size(t)));
end
end
plot(t,y)

Answers (1)

Walter Roberson
Walter Roberson on 1 Apr 2018
You have )))+.4*randn . That is one too many ) at that point, should be ))+.4*randn

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!