I am trying to create a digital moving average filter and the code in matlab is this
J = 1;
for k = 2:nit
i = k-J;
if i<1
i = 1;
end
z(k) = z(k-1) + (1/J)*(y(k)-y(i));
end
applying for this signal
y = [0.0 -1.6791 -0.4876 -0.9474 -0.2113 -0.9565 -0.2573 0.2853 -1.363 0.872 -0.9179 0.0014 -1.063 -0.4128 0.6078 0.7077 -1.4585 -1.6188 1.2087 0.5573 1.0012 1.0482 -0.6493 0.8739 0.5137 1.0873 1.0952 -0.6277 1.5451 1.1885 -0.0875 -0.4636 1.1962 0.1815 0.0806 -1.6551 0.5082 1.5794 -0.0442 0.2183 1.1953 0.6058 0.6331 -1.1333 -0.6068 -1.4304 0.5145 -1.6648 0.0932 1.2799 1.3982 -0.4949 1.3955 0.7347 1.1491 1.4874 -1.6319 -0.8885 0.5422 -0.7988 -0.2711 -0.6432 -1.4922 -1.2955 0.7096 -1.57 1.4366 -0.9403 -0.4508 1.0971 0.6953 0.2132 -1.0466 0.6249 1.0356 -0.747 1.3935 1.2638 -0.6693 -0.3641 1.0919 1.6049 0.0464 1.2441 -1.4143 -1.3063 1.1317 -1.0371 -1.5757 1.0582 -0.3385 -0.8423 0.885 -1.3398 -1.1998 0.146 -0.1752 0.8495 -1.4087 0.1589];
And its work fine for matlab, but when I tried to create the same filter in python, something goes wrong. This is the function
def movingAVarage(window_size, sinal):
nit = len(sinal)
z = np.zeros((nit,1))
for k in range(1,nit):
i = k-window_size
if i < 1:
i = 1
z[k] = z[k-1] + (1/window_size)*(sinal[k]-sinal[i])
return z
it is as if the signal is offset on the y-axis. in the figure, we have j = 1, so no filtering and the response should be equal to the input.
