|
| 1 | +# canonical.py - functions for converting systems to canonical forms |
| 2 | +# RMM, 10 Nov 2012 |
| 3 | + |
| 4 | +import control |
| 5 | +from numpy import zeros, shape, poly |
| 6 | +from numpy.linalg import inv |
| 7 | + |
| 8 | +def canonical_form(sys, form): |
| 9 | + """Convert a system into canonical form |
| 10 | +
|
| 11 | + Parameters |
| 12 | + ---------- |
| 13 | + xsys : StateSpace object |
| 14 | + System to be transformed, with state 'x' |
| 15 | + form : String |
| 16 | + Canonical form for transformation. Chosen from: |
| 17 | + * 'reachable' - reachable canonical form |
| 18 | + * 'observable' - observable canonical form |
| 19 | + * 'modal' - modal canonical form [not implemented] |
| 20 | +
|
| 21 | + Outputs |
| 22 | + ------- |
| 23 | + zsys : StateSpace object |
| 24 | + System in desired canonical form, with state 'z' |
| 25 | + T : matrix |
| 26 | + Coordinate transformation matrix, z = T*x |
| 27 | + """ |
| 28 | + |
| 29 | + # Call the appropriate tranformation function |
| 30 | + if form == 'reachable': |
| 31 | + return reachable_form(xsys) |
| 32 | + else: |
| 33 | + raise control.ControlNotImplemented( |
| 34 | + "Canonical form '%s' not yet implemented" % form) |
| 35 | + |
| 36 | +# Reachable canonical form |
| 37 | +def reachable_form(xsys): |
| 38 | + # Check to make sure we have a SISO system |
| 39 | + if not control.issiso(xsys): |
| 40 | + raise control.ControlNotImplemented( |
| 41 | + "Canonical forms for MIMO systems not yet supported") |
| 42 | + |
| 43 | + # Create a new system, starting with a copy of the old one |
| 44 | + zsys = control.StateSpace(xsys) |
| 45 | + |
| 46 | + # Generate the system matrices for the desired canonical form |
| 47 | + zsys.B = zeros(shape(xsys.B)); zsys.B[0, 0] = 1; |
| 48 | + zsys.A = zeros(shape(xsys.A)) |
| 49 | + Apoly = poly(xsys.A) # characteristic polynomial |
| 50 | + for i in range(0, xsys.states): |
| 51 | + zsys.A[0, i] = -Apoly[i+1] / Apoly[0] |
| 52 | + if (i+1 < xsys.states): zsys.A[i+1, i] = 1 |
| 53 | + |
| 54 | + # Compute the reachability matrices for each set of states |
| 55 | + Wrx = control.ctrb(xsys.A, xsys.B) |
| 56 | + Wrz = control.ctrb(zsys.A, zsys.B) |
| 57 | + |
| 58 | + # Transformation from one form to another |
| 59 | + Tzx = Wrz * inv(Wrx) |
| 60 | + |
| 61 | + # Finally, compute the output matrix |
| 62 | + zsys.C = xsys.C * inv(Tzx) |
| 63 | + |
| 64 | + return zsys, Tzx |
0 commit comments