Added K IIR lpf filter

This commit is contained in:
Joppe Blondel
2025-10-19 17:02:29 +02:00
parent b2858ac5ee
commit 771fa58769
4 changed files with 115 additions and 1 deletions

View 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()