![]() |
没有循环或信号处理工具箱的MATLAB中的均值滤波器
我需要在数据集上实现均值过滤器,但无法访问信号处理工具箱。有没有一种方法,而无需使用for循环?这是我正在工作的代码:
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(na/2)=mean(y(na/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') 编辑: 实施merv解决方案后,内置的滤波方法会滞后原始信号。有没有解决的办法? [IMG]https://i709.photobucket.com/albums/ww98/NBrower/filter.png[/IMG] [B]回答:[/B] 使用内置的[URL="https://www.mathworks.com/help/matlab/ref/filter.html;jsessionid=07db6e3b9e031f84c1aa0b192e91"]FILTER[/URL]功能 %# 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'}) [URL="https://i.stack.imgur.com/yo9ag.png"][IMG]https://i.stack.imgur.com/yo9ag.png[/IMG][/URL] [url=https://stackoverflow.com/questions/2569012]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 04:59。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.