Reputation: 124
Why aren't num_den_to_sympy(b, a)
and sy.simplify(sy.expand(tf))
the same? The expanded version contains an additional s ** 4 and I don't know where it came from.
import numpy as np
import sympy as sy
from scipy.signal import *
from IPython.display import display
sy.init_printing()
def num_den_to_sympy(num, den, symplify=True):
s = sy.Symbol('s')
G = sy.Poly(num, s) / sy.Poly(den, s)
return sy.simplify(G) if symplify else G
b, a = iirdesign(wp=2 * np.pi * 2.5e3, ws=2 * np.pi * 4.5e3, gpass=1, gstop=26,
analog=True, ftype='cheby1', output='ba')
tf = 1
for sos in tf2sos(b, a):
tf *= num_den_to_sympy(sos[0:3], sos[3:6])
display(num_den_to_sympy(b, a))
display(sy.simplify(sy.expand(tf)))
Upvotes: 2
Views: 196
Reputation:
The issue is the inconsistent order of coefficients. SciPy orders them from lowest to highest degrees. SymPy's Poly
constructor expects them to be from highest to lowest: for example, Poly([1, 0, 0], s)
is s**2
If you reverse the coefficients when using Poly, the two results are identical.
G = sy.Poly(num[::-1], s) / sy.Poly(den[::-1], s)
Upvotes: 1