We use FIR filters to clean signals by removing unwanted parts like noise. The firwin function helps us create these filters easily.
FIR filter design (firwin) in SciPy
scipy.signal.firwin(numtaps, cutoff, window='hamming', pass_zero=True, fs=2.0)
numtaps is the number of filter coefficients (length of the filter).
cutoff is the cutoff frequency or frequencies in Hz.
firwin(31, 0.3)
firwin(51, [0.2, 0.5], pass_zero=False)
firwin(41, 1000, fs=8000)
This code creates a low-pass FIR filter with a cutoff at 1000 Hz for a signal sampled at 8000 Hz. It then plots how the filter affects different frequencies.
import numpy as np import matplotlib.pyplot as plt from scipy.signal import firwin, freqz # Design a low-pass FIR filter numtaps = 31 cutoff_hz = 1000 fs = 8000 # Sampling frequency # Create filter coefficients fir_coeff = firwin(numtaps, cutoff_hz, fs=fs) # Frequency response w, h = freqz(fir_coeff, worN=8000, fs=fs) # Plot filter response plt.plot(w, 20 * np.log10(abs(h))) plt.title('Low-pass FIR filter frequency response') plt.xlabel('Frequency (Hz)') plt.ylabel('Gain (dB)') plt.grid(True) plt.show()
The number of taps (numtaps) affects how sharp the filter is and how much delay it adds.
Cutoff frequencies must be less than half the sampling rate (Nyquist frequency).
Use pass_zero=True for low-pass or high-pass filters, and pass_zero=False for band-pass or band-stop filters.
FIR filters help clean signals by removing unwanted frequencies.
firwin creates filter coefficients easily by specifying taps and cutoff.
Always consider sampling rate and filter length for good results.