2010-04-02 3 views
6

데이터 세트에 평균 필터를 구현해야하지만 신호 처리 도구 상자에 액세스 할 수 없습니다. for 루프를 사용하지 않고이 작업을 수행 할 수있는 방법이 있습니까?루프 또는 신호 처리 도구 상자가없는 MATLAB의 평균 필터

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

편집이 :

메르의 솔루션을 구현 한 후, 내장 된 필터 방법 원래 신호를 지연 여기에 내가 일하고 있어요 코드입니다. 이 문제를 해결할 방법이 있습니까? alt text

답변

5

사용하여 내장 FILTER 기능

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

이 같은 시도, 지연 문제를 해결하려면 다음

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

alt text http://img171.imageshack.us/img171/4510/45062995.png