import numpy as np
import matplotlib.pyplot as plt
# Generating angular frequency values
omega = np.logspace(-2, 2, 400) # for a wider frequency range in this example
# Dummy values for omega1 (ω1) and omega2 (ω2)
omega1 = 0.1
omega2 = 10
# Calculating the plateau height based on the slopes and cutoff frequencies
plateau_height = 10 * np.log10(omega2/omega1)
magnitude = np.piecewise(omega, [omega < omega1, (omega >= omega1) & (omega <= omega2), omega > omega2],
[lambda omega: plateau_height + 20 * np.log10(omega/omega1), plateau_height,
lambda omega: plateau_height + 20 * np.log10(omega2/omega)])
plt.figure(figsize=(10,6))
plt.semilogx(omega, magnitude, 'k')
plt.title("Bode Plot (Frequency Response)")
plt.xlabel("ω (angular frequency)")
plt.ylabel("|Vo/Vi| dB")
plt.grid(which='both', axis='both')
plt.ylim(-30, plateau_height+10) # Adjusting y limits for better visibility
plt.axhline(0, color='black',linewidth=0.5)
# Dashed lines for omega1 and omega2
plt.axvline(omega1, color='black', linestyle='--', linewidth=1)
plt.axvline(omega2, color='black', linestyle='--', linewidth=1)
# Dashed line for the plateau height and its label
plt.axhline(plateau_height, color='black', linestyle='--', linewidth=1)
plt.annotate('|R/R₁|', xy=(1e-2, plateau_height+2), horizontalalignment='left')
# Annotations for slopes and cutoff frequencies
plt.annotate('20dB/dec', xy=(omega1/3, 10), horizontalalignment='right')
plt.annotate('-20dB/dec', xy=(omega2*3, plateau_height-10), horizontalalignment='left')
plt.annotate('ω1', xy=(0.85*omega1, -25), horizontalalignment='center')
plt.annotate('ω2', xy=(0.85*omega2, -25), horizontalalignment='center')
plt.tight_layout()
plt.savefig("Bode Plot of Practical Differentiator.pdf")