Mean filter in MATLAB without loops or signal processing toolbox

Posted by Doresoom on Stack Overflow See other posts from Stack Overflow or by Doresoom
Published on 2010-04-02T20:28:28Z Indexed on 2010/04/02 20:33 UTC
Read the original article Hit count: 826

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about matlab

Related posts about mean-filter