bodeplot.py 985 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import matplotlib.pyplot as plt
  2. from scipy import interpolate
  3. from si_prefix import si_format
  4. import numpy as np
  5. def bodeplot(f, g, p, with_fc=False):
  6. fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 6))
  7. fig.subplots_adjust(hspace=0.5)
  8. ax1.set_title("Bode plot")
  9. ax1.set_ylabel('gain (dB)')
  10. ax1.set_xlabel('f (Hz)')
  11. ax1.grid(True)
  12. ax1.semilogx(f, g, 'C2')
  13. ax1.axhline(y=-3.0)
  14. if with_fc:
  15. try:
  16. # find fc at -3db
  17. yreduced = np.array(g) - (-3.0)
  18. freduced = interpolate.UnivariateSpline(f, yreduced, s=0)
  19. fc = freduced.roots()[0]
  20. ax1.scatter([fc], [-3.0], c = 'red')
  21. ax1.annotate("fc=" + si_format(fc, precision=2) + "Hz", xy= (fc, -3.0), xytext=(fc, -2.5) )
  22. except:
  23. print("Warning: can't find fc")
  24. ax2.set_ylabel('phase (°)')
  25. ax2.set_xlabel('f (Hz)')
  26. ax2.semilogx(f, p, 'C2')
  27. ax2.grid(True)
  28. return plt.show()