Mean filter in MATLAB without loops or signal processing toolbox
- by Doresoom
I need to implement a mean filter on a data set, but I don't have access to the signal processing toolbox. Is there a way to do this without using a for loop? Here's the code I've got working:
x=0:.1:10*pi;
noise=0.5*(rand(1,length(x))-0.5);
y=sin(x)+noise; %generate noisy signal
a=10; %specify moving window size
my=zeros(1,length(y)-a);
for n=a/2+1:length(y)-a/2
my(n-a/2)=mean(y(n-a/2:n+a/2)); %calculate mean for each window
end
mx=x(a/2+1:end-a/2); %truncate x array to match
plot(x,y)
hold on
plot(mx,my,'r')