Added K IIR lpf filter
This commit is contained in:
42
scripts/filter_design/bode_compare_Ks_lpf_iir_q15_k.py
Normal file
42
scripts/filter_design/bode_compare_Ks_lpf_iir_q15_k.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
def bode_mag_phase(Fs, K, n=4096):
|
||||
"""Return frequency, magnitude (dB), and phase (deg) for 1-pole IIR."""
|
||||
alpha = 2.0 ** (-K)
|
||||
a = 1.0 - alpha
|
||||
w = np.linspace(0, np.pi, n)
|
||||
ejw = np.exp(-1j * w)
|
||||
H = alpha / (1 - a * ejw)
|
||||
f = (w / (2 * np.pi)) * Fs
|
||||
mag_db = 20 * np.log10(np.abs(H) + 1e-20)
|
||||
phase_deg = np.unwrap(np.angle(H)) * 180 / np.pi
|
||||
return f, mag_db, phase_deg
|
||||
|
||||
if __name__ == "__main__":
|
||||
Fs = 15e6
|
||||
Ks = [10, 8, 6, 4]
|
||||
|
||||
fig, (ax_mag, ax_phase) = plt.subplots(2, 1, sharex=True, figsize=(8, 6))
|
||||
|
||||
for K in Ks:
|
||||
f, mag_db, phase_deg = bode_mag_phase(Fs, K, n=32768)
|
||||
ax_mag.semilogx(f, mag_db, label=f"K={K}")
|
||||
ax_phase.semilogx(f, phase_deg, label=f"K={K}")
|
||||
|
||||
# ---- Styling ----
|
||||
f_max = 40e3 # 40 kHz
|
||||
ax_mag.set_xlim(10, f_max) # start at 10 Hz for nicer log scaling
|
||||
ax_mag.set_ylim(-60, 1)
|
||||
ax_mag.set_title("Ideal Bode Response vs K")
|
||||
ax_mag.set_ylabel("Magnitude [dB]")
|
||||
ax_mag.grid(True, which='both')
|
||||
ax_mag.legend()
|
||||
|
||||
ax_phase.set_xlabel("Frequency [Hz]")
|
||||
ax_phase.set_ylabel("Phase [deg]")
|
||||
ax_phase.grid(True, which='both')
|
||||
ax_phase.set_xlim(10, f_max)
|
||||
|
||||
plt.tight_layout()
|
||||
plt.show()
|
||||
Reference in New Issue
Block a user