Question

J'ai besoin de mettre en œuvre un filtre moyenne sur un ensemble de données, mais je n'ai pas accès à la boîte à outils de traitement du signal. Est-il possible de le faire sans utiliser une boucle for? Voici le code que j'ai travail:

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

EDIT:

Après la mise en oeuvre de la solution de Merv, le procédé de filtration intégré retard par rapport au signal d'origine. Y a-t-il un moyen de contourner ceci? text alt

Était-ce utile?

La solution

Utiliser le haut- FILTRE fonction

%# generate noisy signal
x = sin(0:.1:10*pi);
x = x + 0.5*(rand(1,length(x))-0.5); 

%# moving average smoothing
window = 15;
h = ones(window,1)/window;
y = filter(h, 1, x);

%# plot
subplot(211), plot(x), ylim([-1 1]), title('noisy')
subplot(212), plot(y), ylim([-1 1]), title('filtered')

Pour résoudre le problème de lag, essayer quelque chose comme ceci:

s = ceil(window/2);
yy = y(s:end);
n = length(x);
plot(1:n, x, 'b'), hold on, plot(1:n-s+1, yy,'r'), hold off
legend({'noisy' 'filtered'})

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top