45
45
import scipy as sp
46
46
import numpy as np
47
47
import math
48
+ import sys as system
48
49
from .ctrlutil import unwrap
49
50
from .bdalg import feedback
50
51
from .margins import stability_margins
@@ -85,6 +86,8 @@ def bode_plot(syslist, omega=None, dB=None, Hz=None, deg=None,
85
86
If Hz=True the limits are in Hz otherwise in rad/s.
86
87
omega_num: int
87
88
number of samples
89
+ margins : boolean
90
+ if True, plot gain and phase margin
88
91
*args, **kwargs:
89
92
Additional options to matplotlib (color, linestyle, etc)
90
93
@@ -219,7 +222,7 @@ def bode_plot(syslist, omega=None, dB=None, Hz=None, deg=None,
219
222
phase_plot = phase
220
223
ax_phase .semilogx (omega_plot , phase_plot , * args , ** kwargs )
221
224
222
- #show the phase and gain margins in the plot
225
+ # Show the phase and gain margins in the plot
223
226
if margins :
224
227
margin = stability_margins (sys )
225
228
gm , pm , Wcg , Wcp = margin [0 ], margin [1 ], margin [3 ], margin [4 ]
@@ -235,33 +238,33 @@ def bode_plot(syslist, omega=None, dB=None, Hz=None, deg=None,
235
238
236
239
if pm != float ('inf' ) and Wcp != float ('nan' ):
237
240
if dB :
238
- ax_mag .semilogx ([Wcp , Wcp ], [0 , - ( 10 ** 5. )], color = 'k' , linestyle = ':' )
241
+ ax_mag .semilogx ([Wcp , Wcp ], [0. , - 1e5 ], color = 'k' , linestyle = ':' )
239
242
else :
240
- ax_mag .loglog ([Wcp ,Wcp ], [1 , 10. ** - 20. ],color = 'k' ,linestyle = ':' )
243
+ ax_mag .loglog ([Wcp ,Wcp ], [1. , 1e-8 ],color = 'k' ,linestyle = ':' )
241
244
242
245
if deg :
243
- ax_phase .semilogx ([Wcp , Wcp ], [( 10 ** 5. ) if phase_limit > 0. else 10. ** - 20. , phase_limit + pm ], color = 'k' , linestyle = ':' )
244
- ax_phase .semilogx ([Wcp , Wcp ], [phase_limit + pm , phase_limit ], color = 'k' , linestyle = '- ' )
246
+ ax_phase .semilogx ([Wcp , Wcp ], [1e5 , phase_limit + pm ],color = 'k' , linestyle = ':' )
247
+ ax_phase .semilogx ([Wcp , Wcp ], [phase_limit + pm , phase_limit ],color = 'k' )
245
248
else :
246
- ax_phase .semilogx ([Wcp , Wcp ], [10. ** - 20. , - math .pi + math .radians (pm )], color = 'k' , linestyle = ':' )
247
- ax_phase .semilogx ([Wcp , Wcp ], [- math .pi + math .radians (pm ), - math .pi ], color = 'k' , linestyle = '- ' )
249
+ ax_phase .semilogx ([Wcp , Wcp ], [1e5 , math .radians ( phase_limit ) + math .radians (pm )],color = 'k' , linestyle = ':' )
250
+ ax_phase .semilogx ([Wcp , Wcp ], [math .radians ( phase_limit ) + math .radians (pm ), math .radians ( phase_limit )], color = 'k' )
248
251
249
252
if gm != float ('inf' ) and Wcg != float ('nan' ):
250
253
if dB :
251
- ax_mag .semilogx ([Wcg , Wcg ], [- 20. * np .log10 (gm ), - ( 10 ** 5. )], color = 'k' , linestyle = ':' )
252
- ax_mag .semilogx ([Wcg , Wcg ], [0 ,- 20 * np .log10 (gm )], color = 'k' , linestyle = '- ' )
254
+ ax_mag .semilogx ([Wcg , Wcg ], [- 20. * np .log10 (gm ), - 1e5 ], color = 'k' , linestyle = ':' )
255
+ ax_mag .semilogx ([Wcg , Wcg ], [0 ,- 20 * np .log10 (gm )],color = 'k' )
253
256
else :
254
- ax_mag .semilogx ([Wcg , Wcg ], [1. / gm , 10. ** - 20. ], color = 'k' , linestyle = ':' )
255
- ax_mag .semilogx ([Wcg , Wcg ], [1. , 1. / gm ], color = 'k' , linestyle = '- ' )
257
+ ax_mag .loglog ([Wcg , Wcg ], [1. / gm ,1e-8 ], color = 'k' , linestyle = ':' )
258
+ ax_mag .loglog ([Wcg , Wcg ], [1. ,1. / gm ],color = 'k' )
256
259
257
260
if deg :
258
- ax_phase .semilogx ([Wcg , Wcg ], [10. ** - 20. , phase_limit ], color = 'k' , linestyle = ':' )
261
+ ax_phase .semilogx ([Wcg , Wcg ], [1e-8 , phase_limit ],color = 'k' , linestyle = ':' )
259
262
else :
260
- ax_phase .semilogx ([Wcg , Wcg ], [10. ** - 20. , - math .pi ], color = 'k' , linestyle = ':' )
263
+ ax_phase .semilogx ([Wcg , Wcg ], [1e-8 , math .radians ( phase_limit )], color = 'k' , linestyle = ':' )
261
264
262
265
ax_mag .set_ylim (mag_ylim )
263
266
ax_phase .set_ylim (phase_ylim )
264
- plt .suptitle ('Gm = %.2f %s(at %.2f rad/s), Pm = %.2f %s (at %.2f rad/s)' % (20 * np .log10 (gm ) if dB else gm ,'dB ' if dB else '\b ' ,Wcg ,pm if deg else math .degrees (pm ),'deg' if deg else 'rad' ,Wcp ))
267
+ plt .suptitle ('Gm = %.2f %s(at %.2f rad/s), Pm = %.2f %s (at %.2f rad/s)' % (20 * np .log10 (gm ) if dB else gm ,'dB ' if dB else '\b ' ,Wcg ,pm if deg else math .radians (pm ),'deg' if deg else 'rad' ,Wcp ))
265
268
266
269
if nyquistfrq_plot :
267
270
ax_phase .axvline (nyquistfrq_plot , color = pltline [0 ].get_color ())
0 commit comments