Contents
Initialize parameters and generate signals
N = 50;
n = 0:N;
Omega = 2*pi/50;
v = sin(Omega*n);
r = 0.2*randn(1,N+1);
x = v + r;
Moving average filter
M = 9;
shift = fix(M/2);
y = zeros(1,N+1);
for k=1:N-M+1
y(k+shift) = (1/M)*sum(x(k:k+M-1));
end
Graphics
figure('Name',['Moving average: M = ',num2str(M)],'NumberTitle','off');
subplot(3,1,1)
stem(n,v,'filled')
axis([0 N -2 2]); ylabel('v[n] \rightarrow'); grid
subplot(3,1,2)
stem(n,x,'filled')
axis([0 N -2 2]); ylabel('x[n] \rightarrow'); grid
hold on; plot(n,v,':k'); hold off
subplot(3,1,3)
stem(n,y,'filled')
axis([0 N -2 2]); grid
xlabel('n \rightarrow')
ylabel('y[n] \rightarrow')
hold on; plot(n,v,':k'); hold off