Signal Filtering with scipy (original) (raw)

Last Updated : 23 Jul, 2025

Signal filtering is a fundamental technique in signal processing used to enhance, clean or isolate specific components of a signal by removing unwanted noise or frequencies. It plays an important role in domains like audio processing, biomedical engineering, communications and data analysis. With Python's SciPy library, particularly scipy.signal module provides a robust set of tools to design and apply various digital filters.

Signal Filtering Techniques

1. Butterworth Low pass Filter

import numpy as np import matplotlib.pyplot as plt from scipy.signal import butter, filtfilt

fs = 1000 t = np.linspace(0, 1, fs, endpoint=False) signal = np.sin(2np.pi50t) + np.sin(2np.pi120t) + 0.5*np.random.randn(fs)

def butter_lowpass_filter(data, cutoff, fs, order=4): nyq = 0.5 * fs normal_cutoff = cutoff / nyq b, a = butter(order, normal_cutoff, btype='low') return filtfilt(b, a, data)

cutoff = 100
filtered = butter_lowpass_filter(signal, cutoff, fs)

plt.figure(figsize=(10,4)) plt.plot(t, signal, label='Original Signal') plt.plot(t, filtered, label='Low-pass Filtered', color='blue') plt.title('Butterworth Low-pass Filter') plt.xlabel('Time [s]') plt.ylabel('Amplitude') plt.legend() plt.show()

`

**Output:

Screenshot-2025-06-21-102744

Output of Butterworth Low pass Filter

2. Butterworth High pass Filter

def butter_highpass_filter(data, cutoff, fs, order=4): nyq = 0.5 * fs normal_cutoff = cutoff / nyq b, a = butter(order, normal_cutoff, btype='high') return filtfilt(b, a, data) cutoff = 100
filtered = butter_highpass_filter(signal, cutoff, fs)

plt.figure(figsize=(10,4)) plt.plot(t, signal, label='Original Signal') plt.plot(t, filtered, label='High-pass Filtered', color='red') plt.title('Butterworth High-pass Filter') plt.xlabel('Time [s]') plt.ylabel('Amplitude') plt.legend() plt.show()

`

**Output:

Screenshot-2025-06-21-102634

Output of Butterworth High pass Filter

3. Butterworth Band pass Filter

def butter_bandpass_filter(data, lowcut, highcut, fs, order=4): nyq = 0.5 * fs low = lowcut / nyq high = highcut / nyq b, a = butter(order, [low, high], btype='band') return filtfilt(b, a, data)

lowcut = 40 highcut = 130 filtered = butter_bandpass_filter(signal, lowcut, highcut, fs)

plt.figure(figsize=(10,4)) plt.plot(t, signal, label='Original Signal') plt.plot(t, filtered, label='Band-pass Filtered', color='green') plt.title('Butterworth Band-pass Filter') plt.xlabel('Time [s]') plt.ylabel('Amplitude') plt.legend() plt.show()

`

**Output:

Screenshot-2025-06-21-102645

Output of Butterworth Band pass Filter

4. Butterworth Band stop Filter

def butter_bandstop_filter(data, lowcut, highcut, fs, order=4): nyq = 0.5 * fs low = lowcut / nyq high = highcut / nyq b, a = butter(order, [low, high], btype='bandstop') return filtfilt(b, a, data)

lowcut = 55 highcut = 65 filtered = butter_bandstop_filter(signal, lowcut, highcut, fs)

plt.figure(figsize=(10,4)) plt.plot(t, signal, label='Original Signal') plt.plot(t, filtered, label='Band-stop Filtered', color='purple') plt.title('Butterworth Band-stop Filter (Notch)') plt.xlabel('Time [s]') plt.ylabel('Amplitude') plt.legend() plt.show()

`

**Output:

Screenshot-2025-06-21-102657

Output of Butterworth Band stop Filter

5. Median Filter

from scipy.signal import medfilt kernel_size = 5 filtered = medfilt(signal, kernel_size=kernel_size)

plt.figure(figsize=(10,4)) plt.plot(t, signal, label='Original Signal') plt.plot(t, filtered, label='Median Filtered', color='orange') plt.title('Median Filter') plt.xlabel('Time [s]') plt.ylabel('Amplitude') plt.legend() plt.show()

`

**Output:

Screenshot-2025-06-21-102708

Output of Median Filter

6. Savitzky Golay Filter

from scipy.signal import savgol_filter window_length = 11 polyorder = 3 filtered = savgol_filter(signal, window_length=window_length, polyorder=polyorder) plt.figure(figsize=(10,4)) plt.plot(t, signal, label='Original Signal') plt.plot(t, filtered, label='Savitzky-Golay Filtered', color='brown') plt.title('Savitzky-Golay Filter') plt.xlabel('Time [s]') plt.ylabel('Amplitude') plt.legend() plt.show()

`

**Output:

Screenshot-2025-06-21-102728

Output of Savitzky Golay Filter

Need of Signal Filtering

  1. **Noise Reduction: All signals are contaminated by unwanted noise and this noise can come from environmental interference, electronic components or transmission errors. Filtering helps clean the signal preserving the important information and discarding the irrelevant fluctuations.
  2. **Improved Analysis and Feature Detection: Noisy data can make it difficult to detect features such as peaks, troughs or edges. Filtering helps make these features more apparent and accurate which is critical for Peak detection, Edge detection etc.
  3. **Data Pre processing: Before feeding signals into algorithms it’s essential to pre process the data to remove distortions. Filters ensure the input is clean and relevant which improves model performance and stability.
  4. **Eliminating Sensor Errors and Artifacts: Sensor readings often suffer from sudden spikes, dropouts or drifts due to environmental factors, hardware limitations or calibration issues. Filters can smooth out these anomalies and make the data more reliable for decision making.