Matlab:在filter()函数上表现不同

时间:2016-12-06 01:59:20

标签: matlab filtering signal-processing moving-average

我想实现滞后2的Moving AVerage模型,其功能形式为:

 y[n] = h1*x[n] + h2*x[n-1] + h3*x[n-2]

有系数h_true = [h1, h2, h3];

输出是n处的标量值数字。 Matlab具有filter()功能,可用于实现MA或AR模型。但是,当我按原样实现等式并使用filter()函数时,输出是不同的。什么是正确的方法?请在下面找到代码。

N = 10;
x = rand(1,N);
h_true = [1, 0.6, 0.3]; %h1 = 1, h2 = 0.6; h3 = 0.3
y(1) = 0.0;
y(2) = 0.0;
for i =3 : N
       y(i) = h_true(1)*x(i) + h_true(2)*x(i-1) + h_true(3)*x(i-2);

    end

filtered_y = filter(h_true,1,x);

yfiltered_y不同

1 个答案:

答案 0 :(得分:1)

虽然i<3的一些术语确实消失了,但实际并非所有术语都有。因此,当您计算y时,您仍应考虑那些非消失的术语:

y(1) = h_true(1)*x(1);
y(2) = h_true(1)*x(2) + h_true(2)*x(1);
for i =3 : N
  y(i) = h_true(1)*x(i) + h_true(2)*x(i-1) + h_true(3)*x(i-2);
end