import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
# Define FIR and IIR filter coefficients
# FIR Filter: H(z) = a + z^-1
a = 1 # Coefficient for FIR filter
fir_b = [a, 1] # Numerator coefficients of FIR filter (b coefficients)
fir_a = [1] # Denominator coefficients of FIR filter (a coefficients)
# IIR Filter: H(z) = (a + z^-1) / (1 - ab - bz^-1)
b = 0.5 # Coefficient for IIR filter
iir_b = [a, 1] # Numerator coefficients of IIR filter
iir_a = [1, -a*b, -b] # Denominator coefficients of IIR filter
# Calculate the frequency response of FIR filter
w_fir, h_fir = signal.freqz(fir_b, fir_a)
# Calculate the frequency response of IIR filter
w_iir, h_iir = signal.freqz(iir_b, iir_a)
# Convert from radians/sample to Hz (assuming sample rate is 1 Hz)
freq_fir = w_fir / (2 * np.pi)
freq_iir = w_iir / (2 * np.pi)
# Create Bode plots for FIR filter
plt.figure(figsize=(12, 6))
plt.subplot(2, 1, 1)
plt.plot(freq_fir, 20 * np.log10(abs(h_fir)), label='FIR Magnitude')
plt.title('Bode Plot of FIR and IIR Filters')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude (dB)')
plt.grid(True)
plt.subplot(2, 1, 2)
plt.plot(freq_fir, np.angle(h_fir, deg=True), label='FIR Phase')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Phase (degrees)')
plt.grid(True)
plt.tight_layout()
# Create Bode plots for IIR filter
plt.figure(figsize=(12, 6))
plt.subplot(2, 1, 1)
plt.plot(freq_iir, 20 * np.log10(abs(h_iir)), label='IIR Magnitude', color='orange')
plt.title('Bode Plot of IIR Filter')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude (dB)')
plt.grid(True)
plt.subplot(2, 1, 2)
plt.plot(freq_iir, np.angle(h_iir, deg=True), label='IIR Phase', color='orange')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Phase (degrees)')
plt.grid(True)
plt.tight_layout()
# Display all plots
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
# Define filter parameters
M = 5 # Set the value of M in the transfer function
# Define the filter coefficients
# H(z) = (1 - z^(-M)) / (1 - z^(-1))
num = [1] + [0] * (M - 1) + [-1] # Numerator coefficients (1 - z^-M)
den = [1, -1] # Denominator coefficients (1 - z^-1)
# Compute the frequency response of the filter
w, h = signal.freqz(num, den)
# Convert from radians/sample to frequency (assuming sample rate is 1 Hz)
frequencies = w / (2 * np.pi)
# Create Bode plots
plt.figure(figsize=(12, 6))
# Magnitude plot
plt.subplot(2, 1, 1)
plt.plot(frequencies, 20 * np.log10(abs(h)), label='Magnitude Response')
plt.title(f'Bode Plot of SINC Filter (M = {M})')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude (dB)')
plt.grid(True)
plt.legend()
# Phase plot
plt.subplot(2, 1, 2)
plt.plot(frequencies, np.angle(h, deg=True), label='Phase Response')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Phase (degrees)')
plt.grid(True)
plt.legend()
# Show the plots
plt.tight_layout()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
# Define filter coefficients for the 4-point moving average filter
# H(z) = (1/4) * (1 + z^-1 + z^-2 + z^-3)
numerator_coeffs = [1/4, 1/4, 1/4, 1/4] # Numerator coefficients of the filter
denominator_coeffs = [1] # Denominator coefficients (no feedback)
# Compute the frequency response of the moving average filter
w, h = signal.freqz(numerator_coeffs, denominator_coeffs)
# Convert from radians/sample to frequency (assuming sample rate is 1 Hz)
frequency = w / (2 * np.pi)
# Create Bode plots
plt.figure(figsize=(12, 6))
# Magnitude plot
plt.subplot(2, 1, 1)
plt.plot(frequency, 20 * np.log10(np.abs(h)), label='Magnitude Response')
plt.title('Bode Plot of 4-Point Moving Average Filter')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude (dB)')
plt.grid(True)
plt.legend()
# Phase plot
plt.subplot(2, 1, 2)
plt.plot(frequency, np.angle(h, deg=True), label='Phase Response')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Phase (degrees)')
plt.grid(True)
plt.legend()
# Show the plots
plt.tight_layout()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
# Define the parameters of the continuous-time first-order low-pass filter
tau = 0.5 # Time constant (you can change this value)
T = 0.1 # Sampling period (you can change this value)
# Define the continuous-time transfer function H(s) = 1 / (tau * s + 1)
num_s = [1]
den_s = [tau, 1]
# Use bilinear transformation to convert H(s) to H(z)
# H(z) will be represented in the form of numerator (num_z) and denominator (den_z) coefficients
num_z, den_z = signal.bilinear(num_s, den_s, fs=1/T)
# Compute the frequency response of the discrete-time filter
w, h = signal.freqz(num_z, den_z, fs=1/T)
# Create Bode plots
plt.figure(figsize=(12, 6))
# Magnitude plot
plt.subplot(2, 1, 1)
plt.plot(w, 20 * np.log10(np.abs(h)), label='Magnitude Response')
plt.title(f'Bode Plot of First-Order Low-Pass Filter (τ = {tau}, T = {T})')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude (dB)')
plt.grid(True)
plt.legend()
# Phase plot
plt.subplot(2, 1, 2)
plt.plot(w, np.angle(h, deg=True), label='Phase Response')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Phase (degrees)')
plt.grid(True)
plt.legend()
# Show the plots
plt.tight_layout()
plt.show()