0
0
SciPydata~5 mins

FFT-based filtering in SciPy

Choose your learning style9 modes available
Introduction

FFT-based filtering helps us remove unwanted parts of a signal or data by changing it into frequencies, then filtering, and changing it back.

You want to remove noise from a sound recording.
You need to smooth a temperature sensor's data to see trends.
You want to highlight certain patterns in an image by filtering frequencies.
You want to separate slow and fast changes in a signal.
Syntax
SciPy
from scipy.fft import fft, ifft

# fft_data = fft(original_data)
# filtered_fft = fft_data * filter_mask
# filtered_data = ifft(filtered_fft)

Use fft to convert data to frequency space.

Multiply by a filter mask to keep or remove frequencies.

Examples
This example shows a basic FFT and inverse FFT without changing frequencies.
SciPy
from scipy.fft import fft, ifft
import numpy as np

# Create a simple signal
signal = np.array([1, 2, 3, 4])

# FFT
freq_data = fft(signal)

# Simple filter: keep all frequencies
filtered_freq = freq_data * np.array([1, 1, 1, 1])

# Inverse FFT
filtered_signal = ifft(filtered_freq)
This example removes high frequencies by zeroing them out in the frequency domain.
SciPy
from scipy.fft import fft, ifft
import numpy as np

signal = np.array([1, 2, 3, 4])
freq_data = fft(signal)

# Filter: zero out high frequencies
filter_mask = np.array([1, 1, 0, 0])
filtered_freq = freq_data * filter_mask
filtered_signal = ifft(filtered_freq)
Sample Program

This program creates a signal with slow and fast parts, removes fast noise using FFT filtering, and shows both signals on a plot.

SciPy
from scipy.fft import fft, ifft
import numpy as np
import matplotlib.pyplot as plt

# Create a noisy signal: slow wave + fast noise
np.random.seed(0)
time = np.linspace(0, 1, 500)
slow_wave = np.sin(2 * np.pi * 5 * time)  # 5 Hz
noise = 0.5 * np.sin(2 * np.pi * 50 * time)  # 50 Hz noise
signal = slow_wave + noise

# FFT
freq_data = fft(signal)

# Frequency array
freq = np.fft.fftfreq(len(signal), d=time[1] - time[0])

# Create filter mask: keep frequencies below 10 Hz
filter_mask = np.abs(freq) < 10

# Apply filter
filtered_freq = freq_data * filter_mask

# Inverse FFT to get filtered signal
filtered_signal = ifft(filtered_freq)

# Plot original and filtered signals
plt.figure(figsize=(10, 6))
plt.plot(time, signal, label='Original Signal')
plt.plot(time, filtered_signal.real, label='Filtered Signal', linewidth=2)
plt.xlabel('Time (seconds)')
plt.ylabel('Amplitude')
plt.title('FFT-based Filtering Example')
plt.legend()
plt.show()
OutputSuccess
Important Notes

FFT output is complex numbers; use .real to get real part after inverse FFT.

Frequency filtering works by zeroing or reducing unwanted frequency components.

Make sure your filter mask matches the FFT frequency array size.

Summary

FFT-based filtering changes data to frequency space to remove unwanted parts.

Use fft and ifft from scipy.fft for this process.

Apply a mask to keep or remove frequencies, then convert back to original space.