Moving average filter problem.

조회 수: 4 (최근 30일)
Charles Moody
Charles Moody 2018년 4월 1일
답변: Walter Roberson 2018년 4월 1일
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)

답변 (1개)

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

카테고리

Help CenterFile Exchange에서 Frequency Transformations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by