TExcelTAD
Home › Fourier Analysis & Signals › The FFT Algorithm

The FFT Algorithm

Computing the DFT from its definition costs N2N^2 complex multiplications: for a million samples, 101210^{12}. The fast Fourier transform (Cooley and Tukey, 1965, though Gauss had the idea around 1805) does it in about Nlog⁡2NN \log_2 N operations, 2⋅1072 \cdot 10^7 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 ωN=e−2πi/N\omega_N = e^{-2\pi i / N} and NN even:

X[k]=∑m=0N/2−1x[2m] ωN2mk+ωNk∑m=0N/2−1x[2m+1] ωN2mk=E[k]+ωNk O[k]\begin{aligned} X[k] &= \sum_{m=0}^{N/2-1} x[2m]\, \omega_N^{2mk} + \omega_N^k \sum_{m=0}^{N/2-1} x[2m+1]\, \omega_N^{2mk} \\ &= E[k] + \omega_N^k\, O[k] \end{aligned}

because ωN2=ωN/2\omega_N^2 = \omega_{N/2}: EE and OO are DFTs of length N/2N/2. They are periodic with period N/2N/2, and ωNk+N/2=−ωNk\omega_N^{k + N/2} = -\omega_N^k, so

X[k]=E[k]+ωNkO[k],X[k+N/2]=E[k]−ωNkO[k],0≤k<N/2X[k] = E[k] + \omega_N^k O[k], \qquad X[k + N/2] = E[k] - \omega_N^k O[k], \qquad 0 \le k < N/2

This pair of formulas is the butterfly. Recursing down to length 1 gives log⁡2N\log_2 N levels of N/2N/2 butterflies each.

python · try it yourself
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

python · try it yourself
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 N=N1N2N = N_1 N_2 (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.fft adds multithreading (workers=) and next_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 [1,2,3,4][1, 2, 3, 4] from the 2-point DFTs of [1,3][1, 3] and [2,4][2, 4].

Answer

[10,−2+2i,−2,−2−2i][10, -2 + 2i, -2, -2 - 2i].

Solution

E=[4,−2]E = [4, -2], O=[6,−2]O = [6, -2], and ω4=−i\omega_4 = -i. Then X[0]=4+6=10X[0] = 4 + 6 = 10, X[2]=4−6=−2X[2] = 4 - 6 = -2, X[1]=−2+(−i)(−2)=−2+2iX[1] = -2 + (-i)(-2) = -2 + 2i, X[3]=−2−2iX[3] = -2 - 2i.

Exercise 2: Levels

How many butterfly levels does a 4096-point radix-2 FFT have?

Answer
Solution

log⁡24096=12\log_2 4096 = 12, each with 4096/2=20484096/2 = 2048 butterflies.

Exercise 3: Inverse FFT from the forward one (code)

Show that ifft⁡(X)=fft⁡(X‾)‾/N\operatorname{ifft}(X) = \overline{\operatorname{fft}(\overline{X})}/N using the recursive fft above on X=fft⁡([1,0,−1,0,2,0,0,0])X = \operatorname{fft}([1, 0, -1, 0, 2, 0, 0, 0]).

Answer

The original sequence comes back.

Solution
python · try it yourself
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 NN-point DFT in about…

  1. N2N^2 operations
  2. Nlog⁡NN \log N operations
  3. NN operations
  4. log⁡N\log N operations

The radix-2 FFT splits the input into…

  1. first and second halves
  2. even- and odd-indexed samples
  3. real and imaginary parts
  4. positive and negative values

Which FFT sizes are typically fastest?

  1. prime sizes
  2. sizes with only small prime factors
  3. odd sizes
  4. all sizes are equal