Modeling a linear system with Python
Question: I would like to simulate/model a closed-loop, linear, time-invariant system
(specifically a locked PLL approximation) with python.

Each sub-block within the model has a known transfer function which is given
in terms of complex frequency `H(s) = K / ( s * tau + 1 )`. Using the model, I
would like to see how the system response as well as the noise response is
affected as parameters (e.g. the VCO gain) are changed. This would involve
using Bode plots and root-locus plots.

What Python modules should I seek out to get the job done?


Answer: I know this is a bit old, but a search brought me to this question. I put this
together when I couldn't find a good module for it. It's not much, but it's a
good start if somebody else finds themselves here.

    
    
    import matplotlib.pylab as plt
    import numpy as np
    import scipy.signal
    
    def bode(G,f=np.arange(.01,100,.01)):
        plt.figure()
        jw = 2*np.pi*f*1j
        y = np.polyval(G.num, jw) / np.polyval(G.den, jw)
        mag = 20.0*np.log10(abs(y))
        phase = np.arctan2(y.imag, y.real)*180.0/np.pi % 360
    
        plt.subplot(211)
        #plt.semilogx(jw.imag, mag)
        plt.semilogx(f,mag)
        plt.grid()
        plt.gca().xaxis.grid(True, which='minor')
    
        plt.ylabel(r'Magnitude (db)')
    
        plt.subplot(212)
        #plt.semilogx(jw.imag, phase)
        plt.semilogx(f,phase)
        plt.grid()
        plt.gca().xaxis.grid(True, which='minor')
        plt.ylabel(r'Phase (deg)')
        plt.yticks(np.arange(0, phase.min()-30, -30))
    
        return mag, phase
    
    f=scipy.signal.lti([1],[1,1])
    bode(f)
    

Edit: I am back here because somebody upvoted this answer, you should try
[Control Systems
Library](http://www.cds.caltech.edu/~murray/wiki/index.php/Control_Systems_Library_for_Python).
They have implemented the bulk of the Matlab control systems toolbox with
matching syntax and everything.

