From: <mur...@us...> - 2013-06-09 15:50:52
|
Revision: 275 http://sourceforge.net/p/python-control/code/275 Author: murrayrm Date: 2013-06-09 15:50:50 +0000 (Sun, 09 Jun 2013) Log Message: ----------- Added in patches from Ryan Krauss <rk...@si...>: * now possible to call a transfer function with a complex argument and get back the value of the transfer function (returns a matrix in MIMO case) * default argument for second transfer function in feedback() call is now 1. Note that this will only work for SISO systems (wasn't sure extending to MIMO was that useful). Modified Paths: -------------- trunk/ChangeLog trunk/src/bdalg.py trunk/src/frdata.py trunk/src/statesp.py trunk/src/xferfcn.py trunk/tests/bdalg_test.py trunk/tests/frd_test.py trunk/tests/xferfcn_test.py Modified: trunk/ChangeLog =================================================================== --- trunk/ChangeLog 2013-06-09 14:57:59 UTC (rev 274) +++ trunk/ChangeLog 2013-06-09 15:50:50 UTC (rev 275) @@ -1,3 +1,28 @@ +2013-06-09 Richard Murray <murray@altura-2.local> + + * tests/bdalg_test.py (TestFeedback.testScalarSS) + (TestFeedback.testScalarTF): added simple test of default argument + to feedback() for state space and transfer function systems + + * tests/frd_test.py (TestFRD.testFeedback): added test of default + argument to feedback() for FRD type + + * src/bdalg.py (feedback): + * src/frdata.py (FRD.feedback): + * src/statesp.py (StateSpace.feedback): + patched in default value of 1 for other system in feedback(). + Contributed by Ryan Krauss <rk...@si...> + + * tests/xferfcn_test.py (TestXferFcn.testEvalFrMIMO) + (TestXferFcn.testEvalFrSISO): added tests of __call__ version of + evalfr. + + * src/xferfcn.py (TransferFunction.__call__): patched in new + __call__ functionality from Ryan Krauss <rk...@si...>, but + modified to use evalfr (works with MIMO). SISO transfer functions + return a single complex number, MIMO transfer functions return a + matrix of complex numbers. + 2013-05-23 Rene van Paassen <ren...@gm...> * src/margin.py: re-vamped stability_margins function. Now Modified: trunk/src/bdalg.py =================================================================== --- trunk/src/bdalg.py 2013-06-09 14:57:59 UTC (rev 274) +++ trunk/src/bdalg.py 2013-06-09 15:50:50 UTC (rev 275) @@ -172,7 +172,8 @@ return -sys; -def feedback(sys1, sys2, sign=-1): +#! TODO: expand to allow sys2 default to work in MIMO case? +def feedback(sys1, sys2=1, sign=-1): """ Feedback interconnection between two I/O systems. Modified: trunk/src/frdata.py =================================================================== --- trunk/src/frdata.py 2013-06-09 14:57:59 UTC (rev 274) +++ trunk/src/frdata.py 2013-06-09 15:50:50 UTC (rev 275) @@ -393,7 +393,7 @@ return mag, phase, omega - def feedback(self, other, sign=-1): + def feedback(self, other=1, sign=-1): """Feedback interconnection between two FRD objects.""" other = _convertToFRD(other, omega=self.omega) Modified: trunk/src/statesp.py =================================================================== --- trunk/src/statesp.py 2013-06-09 14:57:59 UTC (rev 274) +++ trunk/src/statesp.py 2013-06-09 15:50:50 UTC (rev 275) @@ -447,7 +447,7 @@ return roots(num) # Feedback around a state space system - def feedback(self, other, sign=-1): + def feedback(self, other=1, sign=-1): """Feedback interconnection between two LTI systems.""" other = _convertToStateSpace(other) Modified: trunk/src/xferfcn.py =================================================================== --- trunk/src/xferfcn.py 2013-06-09 14:57:59 UTC (rev 274) +++ trunk/src/xferfcn.py 2013-06-09 15:50:50 UTC (rev 275) @@ -81,7 +81,7 @@ # External function declarations from numpy import angle, any, array, empty, finfo, insert, ndarray, ones, \ polyadd, polymul, polyval, roots, sort, sqrt, zeros, squeeze, exp, pi, \ - where, delete, real, poly + where, delete, real, poly, poly1d from scipy.signal import lti from copy import deepcopy from warnings import warn @@ -231,7 +231,21 @@ self.den = den self._truncatecoeff() - + + def __call__(self, s): + """Evaluate the system's transfer function for a complex vairable + + For a SISO transfer function, returns the value of the + transfer function. For a MIMO transfer fuction, returns a + matrix of values evaluated at complex variable s.""" + + if (self.inputs > 1 or self.outputs > 1): + # MIMO transfer function, return a matrix + return self.horner(s) + else: + # SISO transfer function, return a scalar + return self.horner(s)[0][0] + def _truncatecoeff(self): """Remove extraneous zero coefficients from num and den. @@ -604,7 +618,7 @@ #for now, just give zeros of a SISO tf return roots(self.num[0][0]) - def feedback(self, other, sign=-1): + def feedback(self, other=1, sign=-1): """Feedback interconnection between two LTI objects.""" other = _convertToTransferFunction(other) Modified: trunk/tests/bdalg_test.py =================================================================== --- trunk/tests/bdalg_test.py 2013-06-09 14:57:59 UTC (rev 274) +++ trunk/tests/bdalg_test.py 2013-06-09 15:50:50 UTC (rev 275) @@ -50,6 +50,14 @@ np.testing.assert_array_almost_equal(ans2.C, [[2.5, 0.]]) np.testing.assert_array_almost_equal(ans2.D, [[2.5]]) + # Make sure default arugments work as well + ans3 = feedback(self.sys2, 1) + ans4 = feedback(self.sys2) + np.testing.assert_array_almost_equal(ans3.A, ans4.A) + np.testing.assert_array_almost_equal(ans3.B, ans4.B) + np.testing.assert_array_almost_equal(ans3.C, ans4.C) + np.testing.assert_array_almost_equal(ans3.D, ans4.D) + def testScalarTF(self): """Scalar system with transfer function feedback block.""" @@ -61,6 +69,12 @@ np.testing.assert_array_almost_equal(ans2.num, [[[2.5, 5., 7.5]]]) np.testing.assert_array_almost_equal(ans2.den, [[[1., -0.5, -2.]]]) + # Make sure default arugments work as well + ans3 = feedback(self.sys1, 1) + ans4 = feedback(self.sys1) + np.testing.assert_array_almost_equal(ans3.num, ans4.num) + np.testing.assert_array_almost_equal(ans3.den, ans4.den) + def testSSScalar(self): """State space system with scalar feedback block.""" Modified: trunk/tests/frd_test.py =================================================================== --- trunk/tests/frd_test.py 2013-06-09 14:57:59 UTC (rev 274) +++ trunk/tests/frd_test.py 2013-06-09 15:50:50 UTC (rev 275) @@ -84,6 +84,11 @@ np.testing.assert_array_almost_equal( f1.feedback(1).freqresp([0.1, 1.0, 10])[0], h1.feedback(1).freqresp([0.1, 1.0, 10])[0]) + + # Make sure default argument also works + np.testing.assert_array_almost_equal( + f1.feedback().freqresp([0.1, 1.0, 10])[0], + h1.feedback().freqresp([0.1, 1.0, 10])[0]) def testFeedback2(self): h2 = StateSpace([[-1.0, 0], [0, -2.0]], [[0.4], [0.1]], Modified: trunk/tests/xferfcn_test.py =================================================================== --- trunk/tests/xferfcn_test.py 2013-06-09 14:57:59 UTC (rev 274) +++ trunk/tests/xferfcn_test.py 2013-06-09 15:50:50 UTC (rev 275) @@ -324,6 +324,11 @@ np.testing.assert_array_almost_equal(sys.evalfr(32.), np.array([[0.00281959302585077 - 0.030628473607392j]])) + # Test call version as well + np.testing.assert_almost_equal(sys(1.j), -0.5 - 0.5j) + np.testing.assert_almost_equal(sys(32.j), + 0.00281959302585077 - 0.030628473607392j) + def testEvalFrMIMO(self): """Evaluate the frequency response of a MIMO system at one frequency.""" @@ -338,6 +343,9 @@ np.testing.assert_array_almost_equal(sys.evalfr(2.), resp) + # Test call version as well + np.testing.assert_array_almost_equal(sys(2.j), resp) + # Tests for TransferFunction.freqresp. def testFreqRespSISO(self): |