Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ofan666
Created February 22, 2012 06:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ofan666/1882562 to your computer and use it in GitHub Desktop.
Save ofan666/1882562 to your computer and use it in GitHub Desktop.
LTI Transient-Response Analysis using Python(scipy, matplotlib) - http://ofan666.blogspot.com/2011/04/lti-transient-response-analysis-using.html
from numpy import min, max
from scipy import linspace
from scipy.signal import lti, step, impulse
# making transfer function
# example from Ogata Modern Control Engineering
# 4th edition, International Edition page 307
# num and den, can be list or numpy array type
num = [6.3223, 18, 12.811]
den = [1, 6, 11.3223, 18, 12.811]
tf = lti(num, den)
# get t = time, s = unit-step response
t, s = step(tf)
# recalculate t and s to get smooth plot
t, s = step(tf, T = linspace(min(t), t[-1], 500))
# get i = impulse
t, i = impulse(tf, T = linspace(min(t), t[-1], 500))
from matplotlib import pyplot as plt
plt.plot(t, s, t, i)
plt.title('Transient-Response Analysis')
plt.xlabel('Time(sec)')
plt.ylabel('Amplitude')
plt.hlines(1, min(t), max(t), colors='r')
plt.hlines(0, min(t), max(t))
plt.xlim(xmax=max(t))
plt.legend(('Unit-Step Response', 'Unit-Impulse Response'), loc=0)
plt.grid()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment