1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| import numpy as np import matplotlib.pyplot as plt from scipy import signal
def basic_rf_controller(Kp, Ki, notch_conf = None, plot = False, plot_pno = 1000, plot_maxf = 0.0): if (Kp < 0) or (Ki < 0): return (False,) + (None,)*4
if notch_conf is not None: if (not isinstance(notch_conf, dict)) or \ ('freq_offs' not in notch_conf.keys()) or \ ('gain' not in notch_conf.keys()) or \ ('half_bw' not in notch_conf.keys()): return (False,) + (None,)*4
plot_pno = 1000 if (plot_pno <= 0) else plot_pno plot_maxf = 1e6 if (plot_maxf <= 0) else plot_maxf
K_num, K_den = add_tf([Kp], [1.0], [Ki], [1.0, 0.0])
if notch_conf is not None: nt_f = notch_conf['freq_offs'] nt_g = notch_conf['gain'] nt_wh = notch_conf['half_bw']
for i in range(len(nt_f)): K_num, K_den = add_tf(K_num, K_den, [nt_g[i] * nt_wh[i]], [1.0, nt_wh[i] - 1j*2*np.pi*nt_f[i]]) K_num, K_den = add_tf(K_num, K_den, [nt_g[i] * nt_wh[i]], [1.0, nt_wh[i] + 1j*2*np.pi*nt_f[i]])
Akc, Bkc, Ckc, Dkc = signal.tf2ss(K_num, K_den)
if plot: w, h = signal.freqs(K_num, K_den, worN = np.linspace(-2*np.pi*plot_maxf, 2*np.pi*plot_maxf, plot_pno))
from rf_plot import plot_basic_rf_controller plot_basic_rf_controller(w, h)
return True, Akc, Bkc, Ckc, Dkc
Kp = 1.0 Ki = 0.1
notch_conf = { 'freq_offs': [50.0, 100.0], 'gain': [0.5, 0.5], 'half_bw': [10.0*np.pi, 10.0*np.pi] }
success, Akc, Bkc, Ckc, Dkc = basic_rf_controller(Kp, Ki, notch_conf, plot=True, plot_pno=1000, plot_maxf=200.0)
if success: plt.title('Frequency Response of RF Controller') plt.xlabel('Frequency (Hz)') plt.ylabel('Gain') plt.grid(True) plt.show()
|