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

Matlab : On filter() function behaving differently

我想实现一个滞后 2 的移动平均模型,其函数形式为:

 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不同

虽然某些术语确实在 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