I'm trying to model a transfer function in Python and thought I could do it by simply plotting the transfer function at many frequencies. This seemed to work for a 2nd order LPF. See the below figure.
A bit of sample code would be like:
s = np.arange(10 * 2 * np.pi, 1000 * 2 * np.pi, 10) # Creates vector of frequencies in rad/s
lpf = np.zeros(len(s)) # Initializes lowpass filter vector
for k in range(len(s)):
lpf[k] = lpfilt_func(s[k]) # Calls LPF function with TF = cutoff_freq ** 2 / (s + cutoff_freq) ** 2
To get the frequency response, instead of using s
as a real number, I changed s
to a complex number using the built-in function complex(0, s)
and calculated the magnitude and phase as follows:
for k in range(len(s)):
lpf[k] = lpfilt_func(complex(0, s[k]))
gain_lpf[k] = np.absolute(lpfilt_func(complex(0, s[k])))
phase_lpf[k] = np.angle(lpfilt_func(complex(0, s[k]))) * 180 / np.pi # Phase in deg
I think this is essentially like setting $s = j\omega$ and then calculating the magnitude as $|LPF| = \sqrt{A^2 + B^2}$ and the phase response as $\angle{LPF} = \angle{(B/A)}$.
Is this a valid method to calculate the frequency response of a transfer function? Is this conversion of s
from a real number to a complex number valid?