我有以下scipy.lti对象,它基本上是一个表示LTI系统拉普拉斯变换的对象:
G_s = lti([1], [1, 2])
如何将这种传递函数与另一种传递函数相乘,例如:
H_s = lti([2], [1, 2])
#I_s = G_s * H_s <---- How to multiply this properly?
我想我能做到
I_s = lti(np.polymul([1], [2]), np.polymul([1, 2], [1, 2]))
但如果我想这样做:
#I_s = H_s / (1 + H_s) <---- Does not work since H_s is an lti object
使用scipy有一种简单的方法吗?
答案 0 :(得分:4)
有趣的是,Scipy似乎没有提供这种功能。另一种方法是将LTI系统转换为Sympy有理函数。 Sympy允许您轻松扩展和取消多项式:
from IPython.display import display
from scipy import signal
import sympy as sy
sy.init_printing() # LaTeX like pretty printing for IPython
def lti_to_sympy(lsys, symplify=True):
""" Convert Scipy's LTI instance to Sympy expression """
s = sy.Symbol('s')
G = sy.Poly(lsys.num, s) / sy.Poly(lsys.den, s)
return sy.simplify(G) if symplify else G
def sympy_to_lti(xpr, s=sy.Symbol('s')):
""" Convert Sympy transfer function polynomial to Scipy LTI """
num, den = sy.simplify(xpr).as_numer_denom() # expressions
p_num_den = sy.poly(num, s), sy.poly(den, s) # polynomials
c_num_den = [sy.expand(p).all_coeffs() for p in p_num_den] # coefficients
l_num, l_den = [sy.lambdify((), c)() for c in c_num_den] # convert to floats
return signal.lti(l_num, l_den)
pG, pH, pGH, pIGH = sy.symbols("G, H, GH, IGH") # only needed for displaying
# Sample systems:
lti_G = signal.lti([1], [1, 2])
lti_H = signal.lti([2], [1, 0, 3])
# convert to Sympy:
Gs, Hs = lti_to_sympy(lti_G), lti_to_sympy(lti_H)
print("Converted LTI expressions:")
display(sy.Eq(pG, Gs))
display(sy.Eq(pH, Hs))
print("Multiplying Systems:")
GHs = sy.simplify(Gs*Hs).expand() # make sure polynomials are canceled and expanded
display(sy.Eq(pGH, GHs))
print("Closing the loop:")
IGHs = sy.simplify(GHs / (1+GHs)).expand()
display(sy.Eq(pIGH, IGHs))
print("Back to LTI:")
lti_IGH = sympy_to_lti(IGHs)
print(lti_IGH)
输出结果为:
答案 1 :(得分:4)
根据您对“easy”的定义,您应该考虑从lti
派生自己的类,对传递函数实施必要的代数运算。这可能是最优雅的方法。
以下是我对这个主题的看法:
from __future__ import division
from scipy.signal.ltisys import TransferFunction as TransFun
from numpy import polymul,polyadd
class ltimul(TransFun):
def __neg__(self):
return ltimul(-self.num,self.den)
def __floordiv__(self,other):
# can't make sense of integer division right now
return NotImplemented
def __mul__(self,other):
if type(other) in [int, float]:
return ltimul(self.num*other,self.den)
elif type(other) in [TransFun, ltimul]:
numer = polymul(self.num,other.num)
denom = polymul(self.den,other.den)
return ltimul(numer,denom)
def __truediv__(self,other):
if type(other) in [int, float]:
return ltimul(self.num,self.den*other)
if type(other) in [TransFun, ltimul]:
numer = polymul(self.num,other.den)
denom = polymul(self.den,other.num)
return ltimul(numer,denom)
def __rtruediv__(self,other):
if type(other) in [int, float]:
return ltimul(other*self.den,self.num)
if type(other) in [TransFun, ltimul]:
numer = polymul(self.den,other.num)
denom = polymul(self.num,other.den)
return ltimul(numer,denom)
def __add__(self,other):
if type(other) in [int, float]:
return ltimul(polyadd(self.num,self.den*other),self.den)
if type(other) in [TransFun, type(self)]:
numer = polyadd(polymul(self.num,other.den),polymul(self.den,other.num))
denom = polymul(self.den,other.den)
return ltimul(numer,denom)
def __sub__(self,other):
if type(other) in [int, float]:
return ltimul(polyadd(self.num,-self.den*other),self.den)
if type(other) in [TransFun, type(self)]:
numer = polyadd(polymul(self.num,other.den),-polymul(self.den,other.num))
denom = polymul(self.den,other.den)
return ltimul(numer,denom)
def __rsub__(self,other):
if type(other) in [int, float]:
return ltimul(polyadd(-self.num,self.den*other),self.den)
if type(other) in [TransFun, type(self)]:
numer = polyadd(polymul(other.num,self.den),-polymul(other.den,self.num))
denom = polymul(self.den,other.den)
return ltimul(numer,denom)
# sheer laziness: symmetric behaviour for commutative operators
__rmul__ = __mul__
__radd__ = __add__
这定义了ltimul
类,lti
加上加法,乘法,除法,减法和否定;二进制的也定义为整数和浮点数作为合作伙伴。
我测试了它for the example of Dietrich:
G_s = ltimul([1], [1, 2])
H_s = ltimul([2],[1, 0, 3])
print G_s*H_s
print G_s*H_s/(1+G_s*H_s)
虽然GH
很好地等于
ltimul(
array([ 2.]),
array([ 1., 2., 3., 6.])
)
GH /(1 + GH)的最终结果不那么漂亮了:
ltimul(
array([ 2., 4., 6., 12.]),
array([ 1., 4., 10., 26., 37., 42., 48.])
)
由于我不太熟悉传递函数,我不确定它是否有可能产生与基于sympy的解决方案相同的结果,因为这个解决方案缺少一些简化。我发现已经lti
出现意外行为是可疑的:lti([1,2],[1,2])
不会简化其参数,即使我怀疑这个函数是常数1.所以我宁愿猜不出这个函数的正确性最终结果。
无论如何,主要消息本身就是继承,因此上述实现中的可能错误有望带来轻微的不便。我对类定义也很不熟悉,所以我可能没有遵循上面的最佳实践。
我最终在@ochurlaud pointed out之后重写了上述内容,我原来只适用于Python 2.原因是/
操作由__div__
/ __rdiv__
实现Python 2(并且是模棱两可的"classical division")。然而,在Python 3中,/
(真正的划分)和//
(地板划分)之间存在区别,他们称__truediv__
和__floordiv__
(及其“正确” “对应物”,分别。上面代码行中的__future__
导入即使在Python 2上也会触发正确的Python 3行为,因此上述代码适用于两个Python版本。由于floor(整数)除法对我们的类没有多大意义,我们明确表示它不能对//
做任何事情(除非另一个操作数实现它)。
还可以轻松地分别为__iadd__
,__idiv__
等定义+=
,/=
等就地操作。