
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
SciPy - signal.firwin2() Function
scipy.signal.firwin2() is a function in SciPy's signal processing module that designs Finite Impulse Response (FIR) filters with arbitrary magnitude frequency responses. It allows users to specify the desired frequency response at specific frequencies and interpolates to construct the filter coefficients.
This function is particularly useful when designing filters that do not fit standard types like low-pass, high-pass or band-pass or when we need custom responses over specific frequency ranges.
Syntax
Following is the syntax for the scipy.signal.firwin2() function which is used to design Finite Impulse Response(FIR) −
scipy.signal.firwin2(numtaps, freq, gain, nfreqs=None, window='hamming', nyq=None, antisymmetric=False, fs=2.0)
Parameters
Here are the parameters of the scipy.signal.firwin2() function which is used to design custom FIR filters −
- numtaps: The number of filter taps (coefficients) in the FIR filter. Must be odd for filters with a symmetric response.
- freq: A sequence of frequency points (normalized or in Hz, based on fs) at which the filters gain is specified.
- gain: A sequence of gain values corresponding to the frequencies in freq. The gain values are interpolated to design the filter.
- nfreqs (optional): Number of frequency points to use in the interpolation. Default value is sufficient for most cases.
- window (optional): Specifies the window function to use for filter design. Default value is 'hamming'.
- nyq (optional): This parameter is Deprecated and we can use fs to specify the sampling frequency.
- antisymmetric (optional): If True then produces a Type III or Type IV linear-phase FIR filter (odd or even length) with an antisymmetric impulse response.
- fs (optional): Sampling frequency of the signal. Default value is 2.0 i.e., normalized frequency.
Return Value
The scipy.signal.firwin2() function returns a 1D array containing the FIR filter coefficients.
Designing a Custom Filter
Designing a custom FIR filter involves specifying the desired frequency response using the freq and gain parameters. Here is an example of designing a filter with custom response using scipy.signal.firwin2() function −
import numpy as np from scipy.signal import firwin2, freqz import matplotlib.pyplot as plt # Parameters numtaps = 101 # Number of filter coefficients (should be odd for symmetry) fs = 1000 # Sampling frequency in Hz freq = [0, 100, 200, 400, 500] # Frequency points in Hz (start at 0, end at Nyquist) gain = [0, 0, 1, 1, 0] # Desired gain at the corresponding frequency points # Design custom FIR filter using firwin2 coefficients = firwin2(numtaps, freq, gain, fs=fs) # Frequency response w, h = freqz(coefficients, worN=8000, fs=fs) # Plot the frequency response plt.figure(figsize=(10, 6)) plt.plot(w, 20 * np.log10(np.abs(h)), label='Custom FIR Filter') plt.title('Frequency Response of Custom FIR Filter') plt.xlabel('Frequency (Hz)') plt.ylabel('Magnitude (dB)') plt.grid() plt.legend() plt.show()
Below is the output of designing a custom FIR filter with the help of scipy.signal.firwin2() function −

Band-Pass Filter for Audio Frequencies
A band-pass filter allows a certain range of frequencies to pass through while attenuating frequencies outside this range. For audio signals, a typical band-pass filter might pass frequencies from 20 Hz to 20 kHz (the standard human hearing range) or we can design a filter to allow any specific band of frequencies. In this example let's design a simple band-pass FIR filter that passes frequencies between 500 Hz and 5000 Hz which is a common frequency range for many audio applications.
import numpy as np from scipy.signal import firwin2, freqz import matplotlib.pyplot as plt # Parameters numtaps = 101 # Number of filter coefficients (odd for linear phase) fs = 8000 # Sampling frequency in Hz (common for speech processing) lowcut = 300 # Lower cutoff frequency in Hz highcut = 3400 # Upper cutoff frequency in Hz # Frequency points (normalized to Nyquist frequency fs/2) freq = [0, lowcut, highcut, fs/2] # Frequency breakpoints gain = [0, 1, 1, 0] # Corresponding gain at those frequencies # Design band-pass FIR filter using firwin2 coefficients = firwin2(numtaps, freq, gain, fs=fs) # Frequency response w, h = freqz(coefficients, worN=8000, fs=fs) # Plot the frequency response plt.figure(figsize=(10, 6)) plt.plot(w, 20 * np.log10(np.abs(h)), label='Band-pass FIR Filter (300 Hz - 3400 Hz)') plt.title('Frequency Response of Band-pass FIR Filter (300 Hz - 3400 Hz)') plt.xlabel('Frequency (Hz)') plt.ylabel('Magnitude (dB)') plt.grid() plt.legend() plt.show()
Following is the output of designing a Band pass filter with the help of scipy.signal.firwin2() function −

High-Pass Filter with Smooth Transition
Here in this example we will design a high-pass filter with a smooth transition using scipy.signal.firwin2() function. This type of filter will allow high frequencies to pass through while attenuating the lower frequencies. The transition between passband and stopband should be smooth and can be achieved by selecting appropriate filter parameters −
import numpy as np from scipy.signal import firwin2, freqz import matplotlib.pyplot as plt # Parameters numtaps = 101 # Number of filter coefficients (odd for linear phase) fs = 5000 # Sampling frequency in Hz (for demonstration) cutoff = 1000 # Cutoff frequency in Hz (for high-pass filter) # Frequency points (normalized to Nyquist frequency fs/2) freq = [0, cutoff, cutoff, fs/2] # Frequency breakpoints gain = [0, 0, 1, 1] # Gain values at corresponding frequencies # Design high-pass FIR filter using firwin2 coefficients = firwin2(numtaps, freq, gain, fs=fs) # Frequency response w, h = freqz(coefficients, worN=8000, fs=fs) # Plot the frequency response plt.figure(figsize=(10, 6)) plt.plot(w, 20 * np.log10(np.abs(h)), label='High-pass FIR Filter (cutoff = 1000 Hz)') plt.title('Frequency Response of High-pass FIR Filter (cutoff = 1000 Hz)') plt.xlabel('Frequency (Hz)') plt.ylabel('Magnitude (dB)') plt.grid() plt.legend() plt.show()
Following is the output of designing a High pass filter with the help of scipy.signal.firwin2() function −
