
- 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.firwin() Function
scipy.signal.firwin() is a function in SciPy's signal processing module that designs Finite Impulse Response (FIR) filters using the window method. It creates a filter with the desired frequency response by specifying the cutoff frequencies and other parameters.
This function is useful for constructing low-pass, high-pass, band-pass and band-stop filters for signal processing tasks. By using different windowing techniques we can control the trade-off between the filter's sharpness and ripple.
Syntax
The syntax for the scipy.signal.firwin() function is as follows −
scipy.signal.firwin(numtaps, cutoff, width=None, window='hamming', pass_zero=True, scale=True, nyq=None, fs=2.0)
Parameters
Here are the parameters of the scipy.signal.firwin() function which is used to design FIR filters −
- numtaps: The number of filter taps (coefficients) in the FIR filter. Must be odd for certain filters like band-pass.
- cutoff: The cutoff frequency or frequencies of the filter. For a single frequency which specify a scalar. For band-pass or band-stop, provide a sequence.
- width(optional): Transition width for the filter. Only used for certain window types. If not specified then the default depends on the window.
- window(optional): This parameter specifies the window function used for filter design. Default value is 'hamming'.
- pass_zero(optional): Determines the type of filter. True for low-pass or high-pass and False for band-pass or band-stop.
- scale(optional): If True then the filter coefficients are scaled to ensure unity gain at a certain frequency. Default value is True.
- nyq(optional): This parameter is deprecated. Nyquist frequency of the signal and we can use fs instead.
- fs(optional): Sampling frequency of the signal. Default value is 2.0.
Return Value
The scipy.signal.firwin() function returns a 1D array containing the FIR filter coefficients.
Designing a Low-Pass Filter
Designing a low-pass filter involves selecting parameters that meet specific requirements for attenuating high-frequency components while preserving low-frequency signals. Here is an example of designing a low-pass FIR filter using scipy.signal.firwin() function −
import numpy as np from scipy.signal import firwin, freqz import matplotlib.pyplot as plt # Design a low-pass filter numtaps = 101 cutoff = 0.3 # Normalized frequency coefficients = firwin(numtaps, cutoff) # Plot the frequency response w, h = freqz(coefficients) plt.plot(w / np.pi, 20 * np.log10(abs(h))) plt.title('Low-Pass Filter Frequency Response') plt.xlabel('Normalized Frequency ( rad/sample)') plt.ylabel('Magnitude (dB)') plt.grid() plt.show()
Following is the output of the designing a low pass filter with the help of scipy.signal.firwin() function −

Designing a Band-Pass Filter
A band-pass filter allows frequencies within a specific range i.e., passband to pass through while attenuating frequencies outside this range. This can be achieved using either FIR or IIR filter design methods. Following is the example of designing a Band pass Filter by using the function scipy.signal.firwin() −
import numpy as np from scipy.signal import firwin, freqz, lfilter import matplotlib.pyplot as plt # Parameters fs = 1000 # Sampling frequency in Hz low_cutoff = 100 # Low cutoff frequency in Hz high_cutoff = 300 # High cutoff frequency in Hz numtaps = 101 # Filter order + 1 (number of coefficients) # Design FIR band-pass filter fir_coeff = firwin(numtaps, [low_cutoff, high_cutoff], fs=fs, pass_zero=False) # Frequency response w, h = freqz(fir_coeff, worN=8000, fs=fs) # Plot the frequency response plt.figure(figsize=(10, 6)) plt.plot(w, 20 * np.log10(np.abs(h)), label='FIR Band-Pass Filter') plt.title('Frequency Response of FIR Band-Pass Filter') plt.xlabel('Frequency (Hz)') plt.ylabel('Magnitude (dB)') plt.grid() plt.legend() plt.show()
Here is the output of the designing a Band pass filter with the help of scipy.signal.firwin() function −

Designing a High-Pass Filter
A high-pass filter allows frequencies above a specified cutoff frequency (fc) to pass while attenuating frequencies below (fc). High-pass filters can be implemented as either FIR (Finite Impulse Response) or IIR (Infinite Impulse Response) filters depending on the application and constraints. Here is the example of designing a High-Pass filter −
import numpy as np from scipy.signal import firwin, freqz, lfilter import matplotlib.pyplot as plt # Parameters fs = 1000 # Sampling frequency in Hz fc = 100 # Cutoff frequency in Hz numtaps = 101 # Filter order + 1 (number of coefficients) # Design FIR high-pass filter fir_coeff = firwin(numtaps, fc, fs=fs, pass_zero=False) # Frequency response w, h = freqz(fir_coeff, worN=8000, fs=fs) # Plot the frequency response plt.figure(figsize=(10, 6)) plt.plot(w, 20 * np.log10(np.abs(h)), label='FIR High-Pass Filter') plt.title('Frequency Response of FIR High-Pass Filter') plt.xlabel('Frequency (Hz)') plt.ylabel('Magnitude (dB)') plt.grid() plt.legend() plt.show()
Below is the output of the designing a High pass filter with the help of scipy.signal.firwin() function −
