Signal Processing with SciPy
$SciPy$ provides a range of tools for signal processing through its scipy.signal module.
These tools are widely used in applications like audio processing, communications, and control systems.
A common signal processing task is filtering, where we remove unwanted noise or frequencies from a signal.
Example Problem: Low-Pass Filtering a Noisy Signal
In this example, we will apply a low-pass filter to a noisy signal.
A low-pass filter allows frequencies below a certain cutoff frequency to pass through while attenuating higher frequencies.
This can help to reduce noise from a signal.
Problem
- Generate a noisy sine wave signal.
- Apply a Butterworth low-pass filter to remove high-frequency noise.
- Visualize the original and filtered signals.
Approach
We will use scipy.signal.butter to design a Butterworth low-pass filter and scipy.signal.filtfilt to apply it to the noisy signal.
Steps
- Generate the Signal: Create a sine wave and add random noise to it.
- Design the Filter: Use the Butterworth filter to create a low-pass filter.
- Filter the Signal: Apply the filter to remove high-frequency noise.
- Visualize the Original and Filtered Signals.
Code Implementation
1 | import numpy as np |
Explanation
Signal Generation:
- We create a sine wave with a frequency of $5$ $Hz$ and add random $Gaussian$ noise to it to simulate a noisy signal.
The sampling frequency is set to $500$ $Hz$, and the time vector covers $1$ second.
- We create a sine wave with a frequency of $5$ $Hz$ and add random $Gaussian$ noise to it to simulate a noisy signal.
Butterworth Low-Pass Filter:
- The
butterfunction is used to design a low-pass Butterworth filter.
This filter has a cutoff frequency of $10$ $Hz$, meaning that it allows frequencies below $10$ $Hz$ to pass and attenuates higher frequencies. - The filter order is set to $4$, meaning it has a relatively sharp cutoff.
- The
Filtering:
- The
filtfiltfunction applies the filter to the noisy signal.
This function applies the filter twice (forward and backward) to eliminate any phase shift introduced by the filtering process, ensuring the signal remains aligned with the original.
- The
Visualization:
- Two plots are generated: the first shows the original noisy signal, and the second shows the filtered signal, where much of the high-frequency noise has been removed.
Output

- Noisy Signal: The first plot displays a noisy sine wave with high-frequency random fluctuations (noise) added to the underlying clean sine wave.
- Filtered Signal: The second plot shows the result of applying the low-pass filter.
The high-frequency noise is significantly reduced, leaving the smooth sine wave intact.
Applications
- Audio Processing: Removing noise from audio recordings.
- Communications: Filtering out unwanted frequency components from signals in wireless communication systems.
- Control Systems: Smoothing sensor data to improve system stability.
Low-pass filters are widely used to enhance signal quality in numerous real-world applications, and the Butterworth filter provides a good balance between performance and simplicity.







