Issue
I am attempting to plot the frequency response of a circuit in a Bode plot using Matplotlib in Python. The transfer function from this circuit generates constant 20dB and 180 degree responses. Originally, when I attempted this in Matplotlib, I was getting a -180 degree response. I found that I could specify wrap_phase=True
in control.bode()
to fix this. However, I realized later that, in order to plot specific points on my graph (to plot the measured response), I would need to set plot=False
, so that I could create my own plots and manage my own points (if this is incorrect and makes it more difficult, please let me know). Below is the code I am using:
import matplotlib.pyplot as plt
import numpy as np
import control
w = np.logspace(1,8,200)
H = control.tf(-10, 1)
mag, phase, omega = control.bode(H, w, dB=True, wrap_phase=True, plot=False)
freq_points = [628.318, 1256.636, 3141.59, 6283.18, 12566.36, 31415.9, 62831.8, 125663.6, 314159, 628318]
mag_points = [20, 20, 20, 20, 20, 20, 20, 19.172, 12.857, 7.824]
phase_points = [179, 180, 179, 178, 177, 175, 169, 148, 115, 101]
plt.figure(1)
plt.subplot(2, 1, 1)
plt.semilogx(omega, mag, label='Magnitude (dB)')
plt.scatter(freq_points, mag_points, color='red', marker='o', label='Points to Highlight')
plt.legend()
# Plot the phase plot
plt.subplot(2, 1, 2)
plt.semilogx(omega, phase, label='Phase (degrees)')
plt.scatter(freq_points, phase_points, color='blue', marker='o', label='Points to Highlight')
plt.legend()
# Show the Bode plots
plt.tight_layout()
plt.show()
Now that I am setting plot=False
, specifying db=True
and wrap_phase=True
do not have any effect. That is, I am getting a magnitude line of 10dB again. Thus, while my measured responses are in the correct place, my actual responses are not. What is the fix to this? Any help is appreciated.
Below is an image of my generated results:
Note that if I set plot=True
and remove all of the subplot nonsense, I get the desired figure below (but I don't think I can plot individual points on these).
Solution
Supposing you are using python-control from this github page. Apparently, you are using the functions in an old way. You can explore the documentation to find out the library's new ways. The documentation now suggests mag, phase, omega = control.frequency_response(H, w)
to obtain parameters. You'll still need to convert to dB (control.mag2db(mag)
) and/or to degrees if you want to plot with those units.
control.bode
always outputs the phase in radians (deg=True
as a parameter is only used when you create a plot). With np.degrees(phase)
you can convert the phase to degrees.
You can use plt.grid(which='both')
to show grid lines both for the major and minor tick marks.
import matplotlib.pyplot as plt
import numpy as np
import control
w = np.logspace(1,8,200)
H = control.tf(-10, 1)
mag, phase, omega = control.bode(H, w, dB=True, wrap_phase=True, plot=False)
freq_points = [628.318, 1256.636, 3141.59, 6283.18, 12566.36, 31415.9, 62831.8, 125663.6, 314159, 628318]
mag_points = [20, 20, 20, 20, 20, 20, 20, 19.172, 12.857, 7.824]
phase_points = [179, 180, 179, 178, 177, 175, 169, 148, 115, 101]
plt.figure(1)
plt.subplot(2, 1, 1)
plt.semilogx(omega, mag, label='Magnitude (dB)')
plt.scatter(freq_points, mag_points, color='red', marker='o', label='Points to Highlight')
plt.grid(which='both')
plt.legend()
# Plot the phase plot
plt.subplot(2, 1, 2)
plt.semilogx(omega, np.degrees(phase), label='Phase (degrees)')
plt.scatter(freq_points, phase_points, color='blue', marker='o', label='Points to Highlight')
plt.grid(which='both')
plt.legend()
# Show the Bode plots
plt.tight_layout()
plt.show()
Answered By - JohanC
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.