SciPy — FFT
Basic FFT
from scipy.fft import fft, ifft
import numpy as np
x = np.array([0, 1, 2, 3, 4, 5])
y = fft(x)
print(f"FFT: {y}")
print(f"Inverse FFT: {ifft(y).real}")
2D FFT
from scipy.fft import fft2, ifft2
x = np.array([[1, 2], [3, 4]])
y = fft2(x)
print(f"2D FFT:\n{y}")
FFT frequencies
from scipy.fft import fftfreq
n = 100
T = 1.0 / 100.0
freqs = fftfreq(n, T)
print(f"Frequencies: {freqs[:n//2]}")
Signal analysis
import numpy as np
from scipy.fft import fft, fftfreq
# Create signal
t = np.linspace(0, 1, 1000)
signal = np.sin(2 * np.pi * 50 * t) + 0.5 * np.sin(2 * np.pi * 120 * t)
# Compute FFT
yf = fft(signal)
xf = fftfreq(len(t), t[1] - t[0])
# Find peaks
positive_mask = xf > 0
frequencies = xf[positive_mask]
magnitudes = 2.0/len(t) * np.abs(yf[positive_mask])
Windowing
from scipy.signal import windows
# Hanning window
window = windows.hann(100)
windowed_signal = signal * window
Applications
- Audio processing
- Image filtering
- Signal analysis
- Spectrum analysis
Mini Practice
- Compute FFT of a signal
- Find frequency components
- Apply windowing
- Reconstruct signal from FFT
Up Next
Continue with Signal Processing - Signal analysis.
Related Topics
Frequently Asked Questions about FFT
What is FFT in SciPy?
FFT is a fundamental concept in SciPy. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn FFT?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn FFT.
Why is FFT important in SciPy?
FFT is essential for SciPy development. Understanding this concept will help you write better code and solve real-world problems more effectively.