# Evaluate filter matlab function on scipy

I want to reproduce the result given by filter() MATLAB function using scipy.signal functions. The result is not consistent:

Matlab example:

bb = 0:.1:9.;

aa = 1;

xx = repmat(0.9234e-18, 100, 1);

[y, zf] = filter(bb,aa,xx);

y(1:10)
zf(1:10)

# print result:
ans =

1.0e-17 *

0
0.0092
0.0277
0.0554
0.0923
0.1385
0.1939
0.2586
0.3324
0.4155

ans =

1.0e-15 *

0.3781
0.3780
0.3779
0.3776
0.3772
0.3767
0.3762
0.3755
0.3748
0.3740


Python code:

import numpy as np
from scipy.signal import lfilter, lfilter_zi

bb = np.arange(0.0, 10.0, .1)[:91]

aa = 1

xx = np.tile(0.9234e-18, (100, 1))

z_i = lfilter_zi(b=bb, a=aa)
y, zf = lfilter(b=bb, x=xx, a=aa, zi=z_i.reshape(len(bb)-1, 1), axis=0)

print(y[:10])
print(zf[:10])

# Print result
[[409.5]
[409.4]
[409.2]
[408.9]
[408.5]
[408. ]
[407.4]
[406.7]
[405.9]
[405. ]]

[[3.7813230e-16]
[3.7803996e-16]
[3.7785528e-16]
[3.7757826e-16]
[3.7720890e-16]
[3.7674720e-16]
[3.7619316e-16]
[3.7554678e-16]
[3.7480806e-16]
[3.7397700e-16]]


Zf's are the same however y's are not the same! Comments?

• how are they different? Do you have any indication which one you want to consider correct? Oct 7 at 11:48

lfilter_zi() calculates the filter state for a unit step response. Your actual signal is 17 orders of magnitude smaller, the initial state will completely dominate the result (for a while).
• If you see in MATLAB, filter() can return zf without any initialization. What I understand is that it initializes the state itself(inside the function). However, lfilter() needs an initialization to return zf Oct 7 at 12:31
• Matlab assumes zero state, so you can probably reproduce it by making zi all zeros Oct 7 at 13:34