# Why does python's scipy.signal.dimpulse introduce delay in impulse response?

Consider a simple linear, time invariant system of the form:

$y_k = cy_{k-1} + (1-c)x_k$

The impulse response of this system can be computed by either dimpulse or by applying lfilter to a vector composed of a one followed by zeros:

import scipy.signal as sp_signal
import numpy as np

Ts = 1
c = 0.9
A = [1, -c]
B = [1-c]
time, imp_resp1 = sp_signal.dimpulse((B, A, Ts))
x = np.zeros(100)
x = 1
imp_resp2 = sp_signal.lfilter(B, A, x)
print(imp_resp1[:5,0])
print(imp_resp2[:5])


which yields:

array([ 0.    ,  0.1   ,  0.09  ,  0.081 ,  0.0729])

[ 0.1      0.09     0.081    0.0729   0.06561]


Why does dimpulse introduce a one-sample delay in the impulse response?

• does not the function's help say anything about this weirdness ? Any possibility that B vector is interpreted differently? For example in MATLAB/OCTAVE the following also introduces one sample delay impz( [ 0 (1-c) ] , [1 (-c)]), but that's expected... – Fat32 Apr 17 '17 at 21:47

In lfilter the transfer function is described in decreasing powers of z, as shown below copied from the python doc for signal.lfilter:

                    -1              -M
b + bz  + ... + b[M] z
Y(z) = -------------------------------- X(z)
-1              -N
a + az  + ... + a[N] z


While the parameters B and A in dimpulse are represented in increasing powers of z as described in the doc for signal.TransferFunction:

If (numerator, denominator) is passed in for *system, coefficients for both the numerator and denominator should be specified in descending exponent order (e.g. s^2 + 3s + 5 or z^2 + 3z + 5 would be represented as [1, 3, 5])

Along with the following example given for:

$$\frac{z^2+3z+3}{z^2+2z+1}$$

Contruct the transfer function with a sampling time of 0.1 seconds:

H(z)=z2+3z+3z2+2z+1
H(z)=z2+3z+3z2+2z+1

>>>>signal.TransferFunction(num, den, dt=0.1)
TransferFunctionDiscrete(
array([ 1.,  3.,  3.]),
array([ 1.,  2.,  1.]),
dt: 0.1
)


So in your case, for dimpulse:

$$H_1(z)=\frac{1-0.9z^{-1}}{0.1}$$

And for lfilter:

$$H_2(z)=\frac{z-0.9}{0.1}$$

So

$$H_1(z)= H_2(z)z^{-1}$$

And mystery solved!

I don't think it's because of differences between lfilter and dimpulse transfert function definitions. You must take care of the power of z in num and den definitions and get the same results in both cases.

Please just consider these equalities :

$$H(z)=\frac{1-0.9\cdot z^{-1}}{0.1} = \frac{z\cdot(1-0.9z^{-1})}{z\cdot0.1}=\frac{z-0.9}{0.1\cdot z-0}$$

You must then specify your numerator B taking care of z powers :

import scipy.signal as sp_signal
import numpy as np

Ts = 1
c = 0.9
A = [1, -c]
B = [1-c, 0] # <---- this is the good numerator definition
time, imp_resp1 = sp_signal.dimpulse((B, A, Ts))
x = np.zeros(100)
x = 1
imp_resp2 = sp_signal.lfilter(B, A, x)
print(imp_resp1[:5,0])
print(imp_resp2[:5])


With this change you will get the same response in both cases :

[ 0.1      0.09     0.081    0.0729   0.06561]
[ 0.1      0.09     0.081    0.0729   0.06561]


No sample delay problem. Let me know if both of you are agree with me