Fourier Transform (FFT) in Python
Learn the Fast Fourier Transform in Python. Decompose signals into frequencies with numpy.fft, plot the spectrum, and filter noise — all runnable in your browser.
Try it yourself
Run this code directly in your browser. Click "Open in full editor" to experiment further.
Click Run to see output
Or press Ctrl + Enter
How it works
The Fast Fourier Transform is one of those rare algorithms that quietly runs the modern world. Every time you stream music, make a phone call, look at a JPEG, or use Wi-Fi, an FFT is somewhere in the loop. Once you see what it does, a lot of seemingly-magical tech suddenly makes sense.
The Big Idea (No Math Required)
Any signal that varies over time — audio, vibrations, stock prices, an EKG — can be thought of as a mix of pure sine waves at different frequencies. The FFT takes that signal in and tells you exactly which sine waves it's made of and how loud each one is.
It's a translator between two ways of looking at the same data:
The two views carry the same information. The FFT (and its sibling, the inverse FFT) lets you flip between them in milliseconds.
Why The "Fast" In Fast Fourier Transform Matters
The naive way to compute a discrete Fourier transform takes O(N²) operations. For an audio clip with a million samples, that's a trillion operations. The FFT pulls the same answer out in O(N log N) — about 20 million operations for the same clip. That difference is why real-time audio processing exists at all.
You never need to implement it yourself. NumPy's np.fft.fft and SciPy's scipy.fft.fft are wrappers around extremely well-tuned C/Fortran libraries.
The Two Functions You Need
fft_values = np.fft.fft(signal) # the complex amplitudes
freqs = np.fft.fftfreq(N, d=1/sample_rate) # the Hz value of each binA few things to know about what comes back:
np.abs(...) to get amplitudes (how loud each frequency is) and np.angle(...) for phase.N // 2 bins.2 / N.The Sample Rate Rule You Cannot Ignore
The Nyquist theorem says: to capture a frequency of f Hz cleanly, you need to sample at least 2f times per second. In other words, with a sample rate of 1000 Hz, the highest frequency you can possibly see in your FFT is 500 Hz. Anything above that gets aliased — it folds back down and shows up as a fake lower frequency.
This is why CD audio uses 44.1 kHz — slightly more than 2× the upper limit of human hearing (~20 kHz).
What You Can Actually Do With It
Common Gotchas
[:N//2].np.hanning(N) * signal) before the FFT to clean this up.When To Reach For SciPy Instead
scipy.fft has the same API as numpy.fft but is generally faster and supports more variants (real FFT, multi-dimensional FFT, DCT). For 2D image work, scipy.fft.fft2 is what you want.
Run the snippet above and you'll see the FFT cleanly recover three tones from a mixed signal, then watch a noisy waveform get rebuilt into something almost identical to the clean original — using nothing but FFT, threshold, inverse FFT.
Related examples
NumPy Array Operations in Python
Learn NumPy basics in Python! A fun and easy guide to super-fast arrays, matrices, and data science math without using slow for-loops.
Matrix Multiplication in Python
Multiply matrices in Python the right way. Compare @, np.dot, np.matmul, and pure-Python loops with runnable examples — no setup required.
Data Visualization with Matplotlib
Learn data visualization in Python with Matplotlib! A fun guide to creating line plots, scatter plots, and bar charts the recommended object-oriented way.