Skip to content

Commit ea93bb5

Browse files
author
Kevin Chen
committed
Modified freqplot.py to fix Nyquist, Nichols and Gangof4 to handle SISO systems again.
Steven Brunton <sbrunton@princeton.edu>
1 parent e614fa7 commit ea93bb5

1 file changed

Lines changed: 67 additions & 43 deletions

File tree

src/freqplot.py

Lines changed: 67 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,22 @@ def nyquist(syslist, omega=None):
179179
omega = default_frequency_range(syslist)
180180

181181
for sys in syslist:
182-
# Get the magnitude and phase of the system
183-
mag, phase, omega = sys.freqresp(omega)
184-
185-
# Compute the primary curve
186-
x = sp.multiply(mag, sp.cos(phase));
187-
y = sp.multiply(mag, sp.sin(phase));
182+
if (sys.inputs > 1 or sys.outputs > 1):
183+
#TODO: Add MIMO nyquist plots.
184+
raise NotImplementedError("Nyquist is currently only implemented for SISO systems.")
185+
else:
186+
# Get the magnitude and phase of the system
187+
mag_tmp, phase_tmp, omega = sys.freqresp(omega)
188+
mag = np.squeeze(mag_tmp)
189+
phase = np.squeeze(phase_tmp)
190+
191+
# Compute the primary curve
192+
x = sp.multiply(mag, sp.cos(phase));
193+
y = sp.multiply(mag, sp.sin(phase));
188194

189-
# Plot the primary curve and mirror image
190-
plt.plot(x, y, '-');
191-
plt.plot(x, -y, '--');
192-
195+
# Plot the primary curve and mirror image
196+
plt.plot(x, y, '-');
197+
plt.plot(x, -y, '--');
193198
# Mark the -1 point
194199
plt.plot([-1], [0], 'r+')
195200

@@ -225,17 +230,24 @@ def nichols(syslist, omega=None):
225230
if (omega == None):
226231
omega = default_frequency_range(syslist)
227232

233+
228234
for sys in syslist:
229-
# Get the magnitude and phase of the system
230-
mag, phase, omega = sys.freqresp(omega)
231-
232-
# Convert to Nichols-plot format (phase in degrees,
233-
# and magnitude in dB)
234-
x = unwrap(sp.degrees(phase), 360)
235-
y = 20*sp.log10(mag)
235+
if (sys.inputs > 1 or sys.outputs > 1):
236+
#TODO: Add MIMO nichols plots.
237+
raise NotImplementedError("Nichols is currently only implemented for SISO systems.")
238+
else:
239+
# Get the magnitude and phase of the system
240+
mag_tmp, phase_tmp, omega = sys.freqresp(omega)
241+
mag = np.squeeze(mag_tmp)
242+
phase = np.squeeze(phase_tmp)
243+
244+
# Convert to Nichols-plot format (phase in degrees,
245+
# and magnitude in dB)
246+
x = unwrap(sp.degrees(phase), 360)
247+
y = 20*sp.log10(mag)
236248

237-
# Generate the plot
238-
plt.plot(x, y)
249+
# Generate the plot
250+
plt.plot(x, y)
239251

240252
plt.xlabel('Phase (deg)')
241253
plt.ylabel('Magnitude (dB)')
@@ -267,30 +279,42 @@ def gangof4(P, C, omega=None):
267279
-------------
268280
None
269281
"""
270-
271-
# Select a default range if none is provided
272-
#! TODO: This needs to be made more intelligent
273-
if (omega == None):
274-
omega = default_frequency_range((P,C))
275-
276-
# Compute the senstivity functions
277-
L = P*C;
278-
S = feedback(1, L);
279-
T = L * S;
280-
281-
# Plot the four sensitivity functions
282-
#! TODO: Need to add in the mag = 1 lines
283-
mag, phase, omega = T.freqresp(omega);
284-
plt.subplot(221); plt.loglog(omega, mag);
285-
286-
mag, phase, omega = (P*S).freqresp(omega);
287-
plt.subplot(222); plt.loglog(omega, mag);
288-
289-
mag, phase, omega = (C*S).freqresp(omega);
290-
plt.subplot(223); plt.loglog(omega, mag);
291-
292-
mag, phase, omega = S.freqresp(omega);
293-
plt.subplot(224); plt.loglog(omega, mag);
282+
if (P.inputs > 1 or P.outputs > 1 or C.inputs > 1 or C.outputs >1):
283+
#TODO: Add MIMO go4 plots.
284+
raise NotImplementedError("Gang of four is currently only implemented for SISO systems.")
285+
else:
286+
287+
# Select a default range if none is provided
288+
#! TODO: This needs to be made more intelligent
289+
if (omega == None):
290+
omega = default_frequency_range((P,C))
291+
292+
# Compute the senstivity functions
293+
L = P*C;
294+
S = feedback(1, L);
295+
T = L * S;
296+
297+
# Plot the four sensitivity functions
298+
#! TODO: Need to add in the mag = 1 lines
299+
mag_tmp, phase_tmp, omega = T.freqresp(omega);
300+
mag = np.squeeze(mag_tmp)
301+
phase = np.squeeze(phase_tmp)
302+
plt.subplot(221); plt.loglog(omega, mag);
303+
304+
mag_tmp, phase_tmp, omega = (P*S).freqresp(omega);
305+
mag = np.squeeze(mag_tmp)
306+
phase = np.squeeze(phase_tmp)
307+
plt.subplot(222); plt.loglog(omega, mag);
308+
309+
mag_tmp, phase_tmp, omega = (C*S).freqresp(omega);
310+
mag = np.squeeze(mag_tmp)
311+
phase = np.squeeze(phase_tmp)
312+
plt.subplot(223); plt.loglog(omega, mag);
313+
314+
mag_tmp, phase_tmp, omega = S.freqresp(omega);
315+
mag = np.squeeze(mag_tmp)
316+
phase = np.squeeze(phase_tmp)
317+
plt.subplot(224); plt.loglog(omega, mag);
294318

295319
#
296320
# Utility functions

0 commit comments

Comments
 (0)