Created
May 17, 2025 03:57
-
-
Save futureshocked/4268abe9583596a6a6cdc736c1186ad6 to your computer and use it in GitHub Desktop.
This Python script visualizes the frequency response (Bode magnitude plot) of a first-order RC low-pass filter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
# Define resistor and capacitor values | |
R = 1e3 # Resistance in ohms | |
C = 1e-6 # Capacitance in farads | |
# Calculate the cutoff frequency | |
fc = 1 / (2 * np.pi * R * C) | |
# Define frequency range for the plot (10 Hz to 1 MHz) | |
frequencies = np.logspace(1, 6, num=500) | |
omega = 2 * np.pi * frequencies | |
# Calculate the magnitude of the transfer function for the low-pass filter | |
H_mag = 1 / np.sqrt(1 + (omega * R * C)**2) | |
H_db = 20 * np.log10(H_mag) | |
# Create the plot | |
plt.figure(figsize=(8, 4)) | |
plt.semilogx(frequencies, H_db) | |
plt.xlabel("Frequency (Hz)") | |
plt.ylabel("Magnitude (dB)") | |
plt.title("Frequency Response of RC Low-Pass Filter") | |
plt.grid(True, which="both", linestyle="--") | |
# Mark the cutoff frequency with a red dashed line | |
plt.axvline(x=fc, color="red", linestyle="--", label=f"Cutoff Frequency: {fc:.1f} Hz") | |
plt.legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment