Skip to content

Commit 5747e63

Browse files
committed
* Updated output arguments for matlab.py time response functions to
match MATLAB conventions (they were switched) * Added long version names for frequency plots: BodePlot, NyquistPlot, etc * Fixed some import errors in rlocus.py Details in ChangeLog.
1 parent 3354fcf commit 5747e63

5 files changed

Lines changed: 54 additions & 34 deletions

File tree

ChangeLog

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
2011-07-11 Richard Murray <murray@malabar.local>
2+
3+
* src/rlocus.py: added real() and imag() to list of functions
4+
imported from numpy
5+
6+
* src/freqplot.py: renamed plotting functions to BodePlot,
7+
NyquistPlot, GangOf4Plot and MarginPlot. Set up aliases to the more
8+
common names (bode, nyquest, gangof4, margin). Mainly playing
9+
around with idea for the eventual interface to use.
10+
11+
* tests/matlab_test.py: updated timeresp outputs to match MATLAB
12+
13+
* src/matlab.py (impulse, initial, lsim, step): switched outputs
14+
from step, impulse, initial, lsim to match MATLAB standard
15+
116
2011-07-01 Richard Murray <murray@malabar.local>
217

318
* src/rlocus.py: modified scipy import to only import those

src/freqplot.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
#
5656

5757
# Bode plot
58-
def bode(syslist, omega=None, dB=False, Hz=False, deg=True,
58+
def BodePlot(syslist, omega=None, dB=False, Hz=False, deg=True,
5959
color=None, Plot=True):
6060
"""Bode plot for a system
6161
@@ -178,7 +178,7 @@ def bode(syslist, omega=None, dB=False, Hz=False, deg=True,
178178
return mags, phases, omegas
179179

180180
# Nyquist plot
181-
def nyquist(syslist, omega=None, Plot=True):
181+
def NyquistPlot(syslist, omega=None, Plot=True):
182182
"""Nyquist plot for a system
183183
184184
Plots a Nyquist plot for the system over a (optional) frequency range.
@@ -245,7 +245,7 @@ def nyquist(syslist, omega=None, Plot=True):
245245

246246
# Gang of Four
247247
#! TODO: think about how (and whether) to handle lists of systems
248-
def gangof4(P, C, omega=None):
248+
def GangOf4Plot(P, C, omega=None):
249249
"""Plot the "Gang of 4" transfer functions for a system
250250
251251
Generates a 2x2 plot showing the "Gang of 4" sensitivity functions
@@ -302,7 +302,7 @@ def gangof4(P, C, omega=None):
302302

303303
# gain and phase margins
304304
# contributed by Sawyer B. Fuller <minster@caltech.edu>
305-
def margin(sysdata, deg=True):
305+
def MarginPlot(sysdata, deg=True):
306306
"""Calculate gain and phase margins and associated crossover frequencies
307307
308308
Usage:
@@ -457,3 +457,8 @@ def default_frequency_range(syslist):
457457

458458
return omega
459459

460+
# Function aliases
461+
bode = BodePlot
462+
nyquist = NyquistPlot
463+
gangof4 = GangOf4Plot
464+
margin = MarginPlot

src/matlab.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,12 +1165,12 @@ def step(sys, T=None, X0=0., input=0, output=0, **keywords):
11651165
11661166
Returns
11671167
-------
1168-
T: array
1169-
Time values of the output
1170-
11711168
yout: array
11721169
Response of the system
11731170
1171+
T: array
1172+
Time values of the output
1173+
11741174
See Also
11751175
--------
11761176
lsim, initial, impulse
@@ -1181,7 +1181,7 @@ def step(sys, T=None, X0=0., input=0, output=0, **keywords):
11811181
'''
11821182
T, yout = timeresp.StepResponse(sys, T, X0, input, output,
11831183
transpose = True, **keywords)
1184-
return T, yout
1184+
return yout, T
11851185

11861186
def impulse(sys, T=None, input=0, output=0, **keywords):
11871187
'''
@@ -1216,10 +1216,10 @@ def impulse(sys, T=None, input=0, output=0, **keywords):
12161216
12171217
Returns
12181218
-------
1219-
T: array
1220-
Time values of the output
12211219
yout: array
12221220
Response of the system
1221+
T: array
1222+
Time values of the output
12231223
12241224
See Also
12251225
--------
@@ -1231,7 +1231,7 @@ def impulse(sys, T=None, input=0, output=0, **keywords):
12311231
'''
12321232
T, yout = timeresp.ImpulseResponse(sys, T, 0, input, output,
12331233
transpose = True, **keywords)
1234-
return T, yout
1234+
return yout, T
12351235

12361236
def initial(sys, T=None, X0=0., input=0, output=0, **keywords):
12371237
'''
@@ -1272,10 +1272,10 @@ def initial(sys, T=None, X0=0., input=0, output=0, **keywords):
12721272
12731273
Returns
12741274
-------
1275-
T: array
1276-
Time values of the output
12771275
yout: array
12781276
Response of the system
1277+
T: array
1278+
Time values of the output
12791279
12801280
See Also
12811281
--------
@@ -1287,7 +1287,7 @@ def initial(sys, T=None, X0=0., input=0, output=0, **keywords):
12871287
'''
12881288
T, yout = timeresp.InitialResponse(sys, T, X0, input, output,
12891289
transpose = True, **keywords)
1290-
return T, yout
1290+
return yout, T
12911291

12921292
def lsim(sys, U=0., T=None, X0=0., **keywords):
12931293
'''
@@ -1324,10 +1324,10 @@ def lsim(sys, U=0., T=None, X0=0., **keywords):
13241324
13251325
Returns
13261326
-------
1327-
T: array
1328-
Time values of the output.
13291327
yout: array
13301328
Response of the system.
1329+
T: array
1330+
Time values of the output.
13311331
xout: array
13321332
Time evolution of the state vector.
13331333
@@ -1341,4 +1341,4 @@ def lsim(sys, U=0., T=None, X0=0., **keywords):
13411341
'''
13421342
T, yout, xout = timeresp.ForcedResponse(sys, T, U, X0,
13431343
transpose = True, **keywords)
1344-
return T, yout, xout
1344+
return yout, T, xout

src/rlocus.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
# $Id$
4747

4848
# Packages used by this module
49-
from scipy import array, poly1d, row_stack, zeros_like
49+
from scipy import array, poly1d, row_stack, zeros_like, real, imag
5050
import scipy.signal # signal processing toolbox
5151
import pylab # plotting routines
5252
import xferfcn # transfer function manipulation

tests/matlab_test.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,24 @@ def testStep(self):
9595
t = np.linspace(0, 1, 10)
9696
youttrue = np.array([9., 17.6457, 24.7072, 30.4855, 35.2234, 39.1165,
9797
42.3227, 44.9694, 47.1599, 48.9776])
98-
tout, yout = step(sys, T=t)
98+
yout, tout = step(sys, T=t)
9999
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
100100
np.testing.assert_array_almost_equal(tout, t)
101101

102102
# Play with arguments
103-
tout, yout = step(sys, T=t, X0=0)
103+
yout, tout = step(sys, T=t, X0=0)
104104
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
105105
np.testing.assert_array_almost_equal(tout, t)
106106

107107
X0 = np.array([0, 0]);
108-
tout, yout = step(sys, T=t, X0=X0)
108+
yout, tout = step(sys, T=t, X0=X0)
109109
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
110110
np.testing.assert_array_almost_equal(tout, t)
111111

112112
#Test MIMO system, which contains ``siso_ss1`` twice
113113
sys = self.mimo_ss1
114-
_t, y_00 = step(sys, T=t, input=0, output=0)
115-
_t, y_11 = step(sys, T=t, input=1, output=1)
114+
y_00, _t = step(sys, T=t, input=0, output=0)
115+
y_11, _t = step(sys, T=t, input=1, output=1)
116116
np.testing.assert_array_almost_equal(y_00, youttrue, decimal=4)
117117
np.testing.assert_array_almost_equal(y_11, youttrue, decimal=4)
118118

@@ -122,14 +122,14 @@ def testImpulse(self):
122122
t = np.linspace(0, 1, 10)
123123
youttrue = np.array([86., 70.1808, 57.3753, 46.9975, 38.5766, 31.7344,
124124
26.1668, 21.6292, 17.9245, 14.8945])
125-
tout, yout = impulse(sys, T=t)
125+
yout, tout = impulse(sys, T=t)
126126
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
127127
np.testing.assert_array_almost_equal(tout, t)
128128

129129
#Test MIMO system, which contains ``siso_ss1`` twice
130130
sys = self.mimo_ss1
131-
_t, y_00 = impulse(sys, T=t, input=0, output=0)
132-
_t, y_11 = impulse(sys, T=t, input=1, output=1)
131+
y_00, _t = impulse(sys, T=t, input=0, output=0)
132+
y_11, _t = impulse(sys, T=t, input=1, output=1)
133133
np.testing.assert_array_almost_equal(y_00, youttrue, decimal=4)
134134
np.testing.assert_array_almost_equal(y_11, youttrue, decimal=4)
135135

@@ -140,15 +140,15 @@ def testInitial(self):
140140
x0 = np.matrix(".5; 1.")
141141
youttrue = np.array([11., 8.1494, 5.9361, 4.2258, 2.9118, 1.9092,
142142
1.1508, 0.5833, 0.1645, -0.1391])
143-
tout, yout = initial(sys, T=t, X0=x0)
143+
yout, tout = initial(sys, T=t, X0=x0)
144144
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
145145
np.testing.assert_array_almost_equal(tout, t)
146146

147147
#Test MIMO system, which contains ``siso_ss1`` twice
148148
sys = self.mimo_ss1
149149
x0 = np.matrix(".5; 1.; .5; 1.")
150-
_t, y_00 = initial(sys, T=t, X0=x0, input=0, output=0)
151-
_t, y_11 = initial(sys, T=t, X0=x0, input=1, output=1)
150+
y_00, _t = initial(sys, T=t, X0=x0, input=0, output=0)
151+
y_11, _t = initial(sys, T=t, X0=x0, input=1, output=1)
152152
np.testing.assert_array_almost_equal(y_00, youttrue, decimal=4)
153153
np.testing.assert_array_almost_equal(y_11, youttrue, decimal=4)
154154

@@ -160,18 +160,18 @@ def testLsim(self):
160160
u = np.array([1., 1, 1, 1, 1, 1, 1, 1, 1, 1])
161161
youttrue = np.array([9., 17.6457, 24.7072, 30.4855, 35.2234, 39.1165,
162162
42.3227, 44.9694, 47.1599, 48.9776])
163-
tout, yout, _xout = lsim(self.siso_ss1, u, t)
163+
yout, tout, _xout = lsim(self.siso_ss1, u, t)
164164
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
165165
np.testing.assert_array_almost_equal(tout, t)
166-
_t, yout, _xout = lsim(self.siso_tf3, u, t)
166+
yout, _t, _xout = lsim(self.siso_tf3, u, t)
167167
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
168168

169169
#test with initial value and special algorithm for ``U=0``
170170
u=0
171171
x0 = np.matrix(".5; 1.")
172172
youttrue = np.array([11., 8.1494, 5.9361, 4.2258, 2.9118, 1.9092,
173173
1.1508, 0.5833, 0.1645, -0.1391])
174-
_t, yout, _xout = lsim(self.siso_ss1, u, t, x0)
174+
yout, _t, _xout = lsim(self.siso_ss1, u, t, x0)
175175
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
176176

177177
#Test MIMO system, which contains ``siso_ss1`` twice
@@ -184,7 +184,7 @@ def testLsim(self):
184184
[1.9092, 39.1165], [1.1508, 42.3227],
185185
[0.5833, 44.9694], [0.1645, 47.1599],
186186
[-0.1391, 48.9776]])
187-
_t, yout, _xout = lsim(self.mimo_ss1, u, t, x0)
187+
yout, _t, _xout = lsim(self.mimo_ss1, u, t, x0)
188188
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
189189

190190
def testDcgain(self):
@@ -206,7 +206,7 @@ def testDcgain(self):
206206

207207
#Compute the gain with a long simulation
208208
t = linspace(0, 1000, 1000)
209-
_t, y = step(sys_ss, t)
209+
y, _t = step(sys_ss, t)
210210
gain_sim = y[-1]
211211
print 'gain_sim:', gain_sim
212212

0 commit comments

Comments
 (0)