From: <kk...@us...> - 2011-02-08 22:15:18
|
Revision: 67 http://python-control.svn.sourceforge.net/python-control/?rev=67&view=rev Author: kkchen Date: 2011-02-08 22:15:12 +0000 (Tue, 08 Feb 2011) Log Message: ----------- Created the "virtual" function returnScipySignalLti() in the TransferFunction and StateSpace classes. Also moved some import commands into member functions (so as not to be global) and edited some docstrings. Kevin K. Chen <kk...@pr...> Modified Paths: -------------- branches/control-0.4a/src/statesp.py branches/control-0.4a/src/xferfcn.py Modified: branches/control-0.4a/src/statesp.py =================================================================== --- branches/control-0.4a/src/statesp.py 2011-02-08 22:15:06 UTC (rev 66) +++ branches/control-0.4a/src/statesp.py 2011-02-08 22:15:12 UTC (rev 67) @@ -275,15 +275,31 @@ return StateSpace(A, B, C, D) -# -# convertToStateSpace - create a state space system from another type -# -# To allow scalar constants to be used in a simple way (k*P, 1+L), this -# function allows the dimension of the input/output system to be specified -# in the case of a scalar system -# + def returnScipySignalLti(self): + """Return a list of a list of scipy.signal.lti objects for a MIMO + system. For instance, + + >>> out = ssobject.returnSignalScipyLti() + >>> out[3][5] + + is a signal.scipy.lti object corresponding to the transfer function from + the 6th input to the 4th output.""" + + from scipy.signal import lti + + # Preallocate the output. + out = [[[] for j in range(self.inputs)] for i in range(self.outputs)] + + for i in range(self.outputs): + for j in range(self.inputs): + out[i][j] = lti(self.A, self.B[:, j], self.C[i, :], + self.D[i, j]) + + return out + def convertToStateSpace(sys, inputs=1, outputs=1): - """Convert a system to state space form (if needed).""" + """Convert a system to state space form (if needed). If sys is a scalar, + then the number of inputs and outputs can be specified manually.""" if isinstance(sys, StateSpace): # Already a state space system; just return it @@ -371,7 +387,7 @@ while True: T = randn(states, states) try: - A = numpy.dot(numpy.linalg.solve(T, A), T) # A = T \ A * T + A = numpy.dot(solve(T, A), T) # A = T \ A * T break except numpy.linalg.linalg.LinAlgError: # In the unlikely event that T is rank-deficient, iterate again. Modified: branches/control-0.4a/src/xferfcn.py =================================================================== --- branches/control-0.4a/src/xferfcn.py 2011-02-08 22:15:06 UTC (rev 66) +++ branches/control-0.4a/src/xferfcn.py 2011-02-08 22:15:12 UTC (rev 67) @@ -48,10 +48,6 @@ # External function declarations import scipy as sp -import scipy.signal as signal -import copy -import bdalg as bd -import statesp from lti2 import Lti2 class TransferFunction(Lti2): @@ -67,7 +63,7 @@ def __init__(self, num=1, den=1): """This is the constructor. The default transfer function is 1 (unit gain direct feedthrough).""" - + # Make num and den into lists of lists of arrays, if necessary. Beware: # this is a shallow copy! This should be okay, but be careful. data = [num, den] @@ -205,6 +201,8 @@ def __neg__(self): """Negate a transfer function.""" + import copy + num = copy.deepcopy(self.num) for i in range(self.outputs): for j in range(self.inputs): @@ -389,6 +387,27 @@ # But this does not work correctly because the state size will be too # large. + def returnScipySignalLti(self): + """Return a list of a list of scipy.signal.lti objects for a MIMO + system. For instance, + + >>> out = ssobject.returnSignalScipyLti() + >>> out[3][5] + + is a signal.scipy.lti object corresponding to the transfer function from + the 6th input to the 4th output.""" + + from scipy.signal import lti + + # Preallocate the output. + out = [[[] for j in range(self.inputs)] for i in range(self.outputs)] + + for i in range(self.outputs): + for j in range(self.inputs): + out[i][j] = lti(self.num[i][j], self.den[i][j]) + + return out + # Utility function to convert a transfer function polynomial to a string # Borrowed from poly1d library def _tfpolyToString(coeffs, var='s'): @@ -448,7 +467,10 @@ return num, den def convertToTransferFunction(sys, inputs=1, outputs=1): - """Convert a system to transfer function form (if needed.)""" + """Convert a system to transfer function form (if needed). If sys is a + scalar, then the number of inputs and outputs can be specified manually.""" + + import statesp if isinstance(sys, TransferFunction): return sys This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |