The FFT Algorithm
Computing the DFT from its definition costs complex multiplications: for a million samples, . The fast Fourier transform (Cooley and Tukey, 1965, though Gauss had the idea around 1805) does it in about operations, for a million samples. It is one of the most important algorithms ever found, and it is a short recursion.
Divide and conquer
Split the sum into even and odd indices. With and even:
because : and are DFTs of length . They are periodic with period , and , so
This pair of formulas is the butterfly. Recursing down to length 1 gives levels of butterflies each.
import numpy as np
def fft(x):
N = len(x)
if N == 1:
return [complex(x[0])]
E = fft(x[0::2])
O = fft(x[1::2])
X = [0j] * N
for k in range(N // 2):
t = np.exp(-2j * np.pi * k / N) * O[k] # twiddle factor times odd part
X[k] = E[k] + t
X[k + N // 2] = E[k] - t
return X
rng = np.random.default_rng(0)
x = rng.normal(size=16)
print("matches np.fft.fft:", np.allclose(fft(list(x)), np.fft.fft(x)))
print("first three bins:", [complex(round(z.real, 4), round(z.imag, 4)) for z in fft(list(x))[:3]])matches np.fft.fft: True
first three bins: [(-4.2572+0j), (-0.7823-6.1573j), (1.242+1.1786j)]
Counting operations
import math
def naive_ops(N):
return N * N
def fft_ops(N): # complex multiplications: N/2 per level, log2 N levels
return N // 2 * int(math.log2(N))
for p in (4, 10, 16, 20):
N = 2 ** p
print(f"N = 2^{p:2d} = {N:8d}: naive {naive_ops(N):.2e} FFT {fft_ops(N):.2e} speed-up {naive_ops(N) / fft_ops(N):9.0f}x")N = 2^ 4 = 16: naive 2.56e+02 FFT 3.20e+01 speed-up 8x
N = 2^10 = 1024: naive 1.05e+06 FFT 5.12e+03 speed-up 205x
N = 2^16 = 65536: naive 4.29e+09 FFT 5.24e+05 speed-up 8192x
N = 2^20 = 1048576: naive 1.10e+12 FFT 1.05e+07 speed-up 104858x
For a million samples the FFT is about 100000 times faster. That speed-up made digital audio, radar, MRI, cellular radio (OFDM) and fast convolution practical.
Beyond powers of two
Cooley-Tukey works for any factorization (mixed radix). Prime lengths use Rader's or Bluestein's algorithm, which turn the DFT into a convolution computed with other FFTs. Libraries such as FFTW and pocketfft (used by NumPy) pick the best plan for each size, but sizes with only small prime factors (2, 3, 5, 7) are still the fastest.
In software
- NumPy uses pocketfft; SciPy's
scipy.fftadds multithreading (workers=) andnext_fast_len; FFTW, cuFFT (GPU) and Intel MKL are the high-performance options. - The number-theoretic transform is an FFT modulo a prime, used for exact big-integer multiplication and in post-quantum cryptography (ML-KEM).
Exercises
Exercise 1: Butterflies by hand
Use one butterfly level to compute the DFT of from the 2-point DFTs of and .
Answer
.
Solution
, , and . Then , , , .
Exercise 2: Levels
How many butterfly levels does a 4096-point radix-2 FFT have?
Answer
Solution
, each with butterflies.
Exercise 3: Inverse FFT from the forward one (code)
Show that using the recursive fft above on .
Answer
The original sequence comes back.
Solution
import numpy as np
x = np.array([1, 0, -1, 0, 2, 0, 0, 0], dtype=float)
X = np.fft.fft(x)
back = np.conj(np.fft.fft(np.conj(X))) / len(X)
print((np.round(back.real, 10) + 0.0).tolist())[1.0, 0.0, -1.0, 0.0, 2.0, 0.0, 0.0, 0.0]
Conjugating flips the sign in the exponent, so one forward FFT routine also computes inverses.
Test yourself
The FFT computes an -point DFT in about…
- operations
- operations
- operations
- operations
The radix-2 FFT splits the input into…
- first and second halves
- even- and odd-indexed samples
- real and imaginary parts
- positive and negative values
Which FFT sizes are typically fastest?
- prime sizes
- sizes with only small prime factors
- odd sizes
- all sizes are equal