NumPy FFT — np.fft module
WHY does this exist?
WHAT is the DFT? (derive it from scratch)
Where does come from?
WHY this exact formula? Think of testing your signal against a probe wave.
- A pure wave of frequency (over samples) is — it spins around the unit circle full turns as goes .
- To ask "how much of wave is in ?", take the inner product (correlation): multiply each sample by the conjugate of the probe and sum.
- Conjugate of is . Hence the minus sign.
We can prove orthogonality (geometric series of roots of unity): WHY: if each term is , sum . If , it's a geometric series with ratio and , so .
Inverse DFT
Because of orthogonality, we can recover : The and the sign are exactly what undo the forward transform (NumPy's default convention).
HOW NumPy organizes the output

Worked Example 1 — find a tone
import numpy as np
fs = 1000.0 # sampling rate, Hz
N = 1000 # number of samples
t = np.arange(N) / fs # time axis, seconds
x = 3*np.sin(2*np.pi*50*t) # a 50 Hz sine, amplitude 3
X = np.fft.rfft(x) # complex spectrum
f = np.fft.rfftfreq(N, d=1/fs) # frequency axis
amp = np.abs(X) * 2 / N # convert to physical amplitude
print(f[np.argmax(amp)], amp.max()) # ~50.0 Hz, ~3.0| Step | Why this step? |
|---|---|
t = arange(N)/fs |
builds correct time stamps; total window s |
rfft(x) |
real signal → half spectrum is enough |
rfftfreq(N,d=1/fs) |
tells you which frequency each bin is — never hard-code! |
*2/N |
DFT splits a real sine's energy into +f and −f; multiply by 2 and divide by to get the true amplitude 3 |
Worked Example 2 — reconstruct & verify inverse
x = np.array([1.0, 2.0, 0.0, -1.0])
X = np.fft.fft(x)
xr = np.fft.ifft(X)
print(np.allclose(x, xr.real)) # TrueWhy: ifft(fft(x)) == x exactly (up to float error) because the and sign conventions are matched. Always trust round-trips to debug.
Worked Example 3 — DC and Nyquist
x = np.array([2,2,2,2], dtype=float)
print(np.fft.fft(x)) # [8.+0.j 0 0 0]Why: a constant signal has only zero frequency (DC). Bin 0 equals . All other bins are zero — there's no oscillation. The DC bin is always the sum of the samples.
Recall Feynman: explain to a 12-year-old
Imagine a music chord — many notes played at once, smashed into a single jiggle of the air. Your job: figure out which separate notes are inside. The FFT is a magic sieve: you hum each possible note at the jiggle, and whenever your hum matches a hidden note, it "rings" loudly; when it doesn't match, it stays silent. Do this for every note really cleverly (not one-by-one but all at once), and out pops a list: "this much of low note, this much of high note." That list is the spectrum. The "Fast" part is just a smart shortcut so it finishes before lunch.
Flashcards
What does np.fft.fft transform between?
DFT formula for ?
Why is the FFT "fast" — what complexity vs naive DFT?
Frequency of bin in Hz given rate , length ?
What does np.fft.fftfreq(N, d) return?
Difference between fft and rfft?
rfft assumes real input and returns only the non-redundant bins.What is the value of DFT bin 0 (DC)?
For a real sine of amplitude , how do you recover from |rfft|?
Why is there a minus sign in the forward DFT exponent?
What's the frequency resolution of an FFT?
What does fftshift do and why?
Property linking and for real input?
Connections
- Discrete Fourier Transform — the math the FFT computes
- Sampling and Nyquist Theorem — why limits visible frequencies
- Convolution Theorem — FFT turns convolution into multiplication
- NumPy arrays — broadcasting — vectorized signal building
- Spectral Leakage and Windowing — fixing non-periodic signals
- Complex Numbers and Euler's Formula —
- scipy.signal — higher-level spectral tools built on
np.fft
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, FFT ka basic idea simple hai: koi bhi signal jo time ke saath badalta hai (jaise sound ya sensor reading), use hum alag-alag frequency ki sine/cosine waves ke sum ke roop mein likh sakte hain. FFT ek fast algorithm hai jo batata hai ki har frequency kitni "strong" hai signal ke andar. Microphone time ka data deta hai, lekin matlab to frequency mein hota hai — jaise music ka note ya pitch. Isiliye time domain se frequency domain ka bridge chahiye, aur wahi FFT karta hai.
Formula yaad rakho: . Yeh actually ek correlation hai — hum apne signal ko ek probe wave (frequency ) ke saath multiply karke sum karte hain. Agar wahi frequency signal mein present hai, to sum bada aata hai; warna doosri frequencies aapas mein cancel ho jaati hain (orthogonality). Minus sign isliye hai kyunki probe wave ka conjugate use hota hai. "Fast" ka matlab — naive DFT hota, FFT , jo crores of samples ke liye game-changer hai.
NumPy mein practical cheezein: np.fft.fft ya real signal ke liye np.fft.rfft (yeh half spectrum deta hai, kyunki real signal symmetric hota hai). Aur sabse important — bin index ko Hz mat samajh lena! Hamesha np.fft.fftfreq(N, d=1/fs) ya rfftfreq use karke actual frequency nikalo, kyunki . Amplitude recover karne ke liye real sine ko se multiply karo (DC bin ke liye ).
Galtiyan jo sab karte hain: peak ki height ko directly amplitude samajh lena, ya frequency resolution bhool jaana (resolution , yaani jitna lamba recording, utni acchi pitch detection). Round-trip test (ifft(fft(x))==x) hamesha debugging ke liye use karo — confidence aata hai ki convention sahi hai. Bas itna samajh lo to FFT tumhare liye easy ho jayega!