【问题标题】:Mean filter in MATLAB without loops or signal processing toolboxMATLAB 中的均值滤波器,没有循环或信号处理工具箱
【发布时间】:2011-02-03 20:52:49
【问题描述】:

我需要对数据集实施均值滤波器,但我无权访问信号处理工具箱。有没有办法在不使用 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(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')

编辑:

实施merv的方案后,内置的filter方法滞后于原始信号。有没有解决的办法?

【问题讨论】:

    标签: matlab vectorization mean


    【解决方案1】:

    使用内置的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'})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 1970-01-01
      相关资源
      最近更新 更多