Multiply scipy.lti transfer functions

后端 未结 2 1261
暖寄归人
暖寄归人 2021-02-13 20:42

I have the following scipy.lti object that is basically an object representing a Laplace transform of an LTI system:

G_s = lti([1], [1, 2])

How

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-13 21:28

    Interestingly, Scipy does not seem to supply that functionality. An alternative is converting the LTI system into a Sympy rational function. Sympy allows you to easily expand and cancel polynomials:

    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)
    

    The output is:

提交回复
热议问题