import numpy as np
import matplotlib.pyplot as plt
def bode_plots(freqs, amp, phi, freq_line=False):
"""Function that plots AC analysis of an RLC circuit.
Args:
freqs (Numpy array double): The frequencies the AC sweep covered.
amp (Numpy array double): The output voltage at the node for each frequency.
phi (Numpy array double): The output phase shift at the node for each frequency.
freq_line (List doubles): Frequencies at which to plot a red marker line.
Returns:
Nothing.
"""
fig, axs = plt.subplots(1,2)
plt.sca(axs[0])
plt.loglog(freqs, amp)
axs[0].set_title('Amplitude')
axs[0].set_xlabel('frequency (Hz)')
axs[0].set_ylabel('gain')
try:
for line in freq_line:
plt.axvline(x=line, color='red')
except TypeError:
if freq_line:
plt.axvline(x=freq_line, color='red')
plt.sca(axs[1])
plt.semilogx(freqs, phi)
axs[1].set_title('Phase shift')
axs[1].set_xlabel('frequency (Hz)')
axs[1].set_ylabel('phase shift (degrees)')
try:
for line in freq_line:
plt.axvline(x=line, color='red')
except TypeError:
if freq_line:
plt.axvline(x=freq_line, color='red')
fig.set_size_inches(12,4)
fig.tight_layout()
plt.show()
def H_low_pass(freq,R,C):
omega = 2*np.pi*freq
H = (1)/(1+(1j*omega*R*C))
return H
def freq_analysis(transfer,freq_low,freq_high):
# define an array, log spaced, between freq_low and freq_high
freqs = np.logspace(np.log10(freq_low),np.log10(freq_high),1000)
# use these frequencies to find the amplitude as a function of frequency
amp = transfer(freqs)
# find the phase angle using numpy's built in function np.angle( ) and convert to degrees
phi = np.angle(amp) * 180 / np.pi
return freqs, amp, phi
R=1e3
C=1e-6
def H_low_pass(freq):
omega = 2*np.pi*freq
H = (1)/(1+(1j*omega*R*C))
return H
R=1e3
C=1e-6
def H_low_pass(freq):
omega = 2*np.pi*freq
H = (1)/(1+(1j*omega*R*C))
return H
freq_low = 1
freq_high = 1e4
freq_rolloff = None
freqs, amp, phi = freq_analysis(H_low_pass,freq_low,freq_high)
bode_plots(freqs, np.abs(amp), phi, freq_rolloff)