Skip to content

Instantly share code, notes, and snippets.

@brilliant-ember
Created April 27, 2021 21:52
Show Gist options
  • Save brilliant-ember/df96b551873a2b8a605099f8285369f5 to your computer and use it in GitHub Desktop.
Save brilliant-ember/df96b551873a2b8a605099f8285369f5 to your computer and use it in GitHub Desktop.
How to do inverse Z transform with python, this function will break up the partial fraction assuming Z transform. Note that it doesn't handle complex roots, if you have complex roots use the scipy residuez function to find the poles and coefficients.
### Solving Z domain partial fraction with sympy
from sympy import *
z = symbols('z')
def inverse_z(h):
'''finds X(z)/z for the inverse z transform table look up
'''
h = h/z
h = h.apart() * z
return expand(h)
n = z**2 + z + (z/(z-1))
d = z**2 - 3*z/2 + 1/2
a = n/d
a = inverse_z(a)
print(a)
#a
# a.cancel().apart()
'''
Note, whenever you try to do partial fractions for z inv, you must first divide the system by z
and then multiply the partial fraction result by z. So you perform partials on X(z)/z = LHS/z, and answer after partial will be multiplied by z so that your final answer is X(z)
'''
@brilliant-ember
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment