|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# freqresp_test.py - test frequency response functions |
| 4 | +# RMM, 30 May 2016 (based on timeresp_test.py) |
| 5 | +# |
| 6 | +# This is a rudimentary set of tests for frequency response functions, |
| 7 | +# including bode plots. |
| 8 | + |
| 9 | +import unittest |
| 10 | +import numpy as np |
| 11 | +from control.statesp import StateSpace |
| 12 | +from control.matlab import ss, tf, bode |
| 13 | +import matplotlib.pyplot as plt |
| 14 | + |
| 15 | +class TestFreqresp(unittest.TestCase): |
| 16 | + def setUp(self): |
| 17 | + self.A = np.matrix('1,1;0,1') |
| 18 | + self.C = np.matrix('1,0') |
| 19 | + self.omega = np.linspace(10e-2,10e2,1000) |
| 20 | + |
| 21 | + def test_siso(self): |
| 22 | + B = np.matrix('0;1') |
| 23 | + D = 0 |
| 24 | + sys = StateSpace(self.A,B,self.C,D) |
| 25 | + |
| 26 | + # test frequency response |
| 27 | + frq=sys.freqresp(self.omega) |
| 28 | + |
| 29 | + # test bode plot |
| 30 | + bode(sys) |
| 31 | + |
| 32 | + # Convert to transfer function and test bode |
| 33 | + systf = tf(sys) |
| 34 | + bode(systf) |
| 35 | + |
| 36 | + def test_doubleint(self): |
| 37 | + # 30 May 2016, RMM: added to replicate typecast bug in freqresp.py |
| 38 | + A = np.matrix('0, 1; 0, 0'); |
| 39 | + B = np.matrix('0; 1'); |
| 40 | + C = np.matrix('1, 0'); |
| 41 | + D = 0; |
| 42 | + sys = ss(A, B, C, D); |
| 43 | + bode(sys); |
| 44 | + |
| 45 | + def test_mimo(self): |
| 46 | + # MIMO |
| 47 | + B = np.matrix('1,0;0,1') |
| 48 | + D = np.matrix('0,0') |
| 49 | + sysMIMO = ss(self.A,B,self.C,D) |
| 50 | + |
| 51 | + frqMIMO = sysMIMO.freqresp(self.omega) |
| 52 | + tfMIMO = tf(sysMIMO) |
| 53 | + |
| 54 | + #bode(sysMIMO) # - should throw not implemented exception |
| 55 | + #bode(tfMIMO) # - should throw not implemented exception |
| 56 | + |
| 57 | + #plt.figure(3) |
| 58 | + #plt.semilogx(self.omega,20*np.log10(np.squeeze(frq[0]))) |
| 59 | + |
| 60 | + #plt.figure(4) |
| 61 | + #bode(sysMIMO,self.omega) |
| 62 | + |
| 63 | +def suite(): |
| 64 | + return unittest.TestLoader().loadTestsFromTestCase(TestTimeresp) |
| 65 | + |
| 66 | +if __name__ == '__main__': |
| 67 | + unittest.main() |
0 commit comments