Fast Fourier Transform

Explore the most important algorithm in signal processing: how roots of unity enable O(n log n) polynomial multiplication

O(n log n) Polynomial Multiplication

The Fast Fourier Transform is one of the most important algorithms ever discovered. It transforms signals between time and frequency domains in O(n log n) instead of O(n²), revolutionizing signal processing, image compression, and polynomial arithmetic.

The key insight: evaluating a polynomial at the nth roots of unityhas special structure. Because ωk+n/2 = -ωk, we can split the problem in half at each step — the classic divide-and-conquer pattern.

In this section, you'll explore roots of unity on the complex plane, watch the butterfly diagram unfold, and see how FFT makes convolution efficient.

Demo 1: Roots of Unity

The nth roots of unity are complex numbers ω where ωn = 1. They sit evenly spaced on the unit circle and satisfy remarkable properties: the cancellation, halving, and summation lemmas that make FFT possible.

ReImωω1ω2ω3ω4ω5ω6ω7

Selected: ω80

Value: 1.00
Angle: 0.0°
Formula: e2πi·0/8 = cos(0°) + i·sin(0°)

All 8th Roots of Unity

ω0 = 1.00
ω1 = 0.71 + 0.71i
ω2 = 1.00i
ω3 = -0.71 + 0.71i
ω4 = -1.00
ω5 = -0.71 - 0.71i
ω6 = -1.00i
ω7 = 0.71 - 0.71i

Cancellation Lemma

ωdndk = ωnk for any positive integer d

With d = 2:
ω160 = 1.00
ω80 = 1.00
✓ Equal!

This lemma lets us relate roots of different orders, crucial for FFT recursion.

Why Roots of Unity Matter

Evaluating a degree-(n-1) polynomial at all n roots of unity takes O(n²) naively. But the roots have special structure: ωk+n/2 = -ωk. This means half the evaluations share work with the other half, giving O(n log n)!

Demo 2: Coefficient vs Point-Value

A polynomial can be represented as coefficients [a₀, a₁, ...] or as point-values [(x₀, y₀), (x₁, y₁), ...]. Multiplication is O(n²) in coefficient form but O(n) in point-value form. FFT converts between them in O(n log n)!

A(x) = 1 +2x +3x^2
B(x) = 1 +x

Naive O(n²) Multiplication

Multiply each coefficient of A with each coefficient of B, then collect like terms.
A × B:
1·x0 × 1·x0 = 1·x0
1·x0 × 1·x1 = 1·x1
2·x1 × 1·x0 = 2·x1
2·x1 × 1·x1 = 2·x2
3·x2 × 1·x0 = 3·x2
3·x2 × 1·x1 = 3·x3
Operations: 6

FFT O(n log n) Multiplication

Transform to point-value form, multiply pointwise, transform back.
1. FFT(A) → point-values
2. FFT(B) → point-values
3. Multiply pointwise
4. Inverse FFT → coefficients
Operations: ~72

Result: A(x) × B(x)

1 +3x +5x^2 +3x^3
Coefficients: [1, 3, 5, 3]
✓ Naive and FFT results match

Complexity Comparison

Operation
Coefficient
Point-Value
Addition
O(n)
O(n)
Multiplication
O(n²)
O(n)
Evaluation
O(n) per point
O(1) per point
Conversion (FFT)
O(n log n)

Demo 3: The Butterfly Diagram

The Cooley-Tukey FFT algorithm uses butterfly operations that combine pairs of values using twiddle factors (powers of ω). Step through the iterative FFT and watch how data flows through log₂(n) stages.

n = 4, log₂(n) = 2
Stage 0 / 2

Bit-Reverse Permutation

First, reorder input by reversing the binary representation of each index.

IndexBinaryReversedNew IndexValue
0000001.00
1011022.00
2100113.00
3111134.00

Butterfly Diagram

InputStage 1Stage 2Output1.003.002.004.00????????10.00-2.00 + 2.00i-2.00-2.00 - 2.00i
Add path
Subtract path
Multiply by twiddle factor

Why O(n log n)?

There are log₂(n) = 2 stages. Each stage performs n/2 = 2 butterfly operations. Total: 2 × 2 = 4 operations = O(n log n).

Demo 4: Convolution via FFT

Convolution in time = multiplication in frequency. Instead of O(n²) direct convolution, we FFT both signals, multiply pointwise, then inverse FFT — all in O(n log n). This powers audio processing, image filters, and neural network layers.

5
Input Signal (sine)
Kernel (gaussian)
Convolution Result

Convolution Formula

(f * g)[n] = Σk f[k] · g[n - k]

Gaussian blur: Smooths the signal by weighted averaging with a bell-shaped kernel.

The Convolution Theorem

Convolution in time = multiplication in frequency.
Instead of O(n²) direct convolution, we can: FFT both signals, multiply pointwise, then inverse FFT — all in O(n log n). This is the foundation of modern signal processing.

Demo 5: Number Theoretic Transform

The NTT is FFT over finite fields — using roots of unity mod a prime instead of complex numbers. No floating-point errors! NTT powers big integer multiplication, cryptography (lattice-based schemes), and competitive programming.

Number Theoretic Transform (NTT)

FFT over finite fields instead of complex numbers. NTT avoids floating-point errors entirely, making it perfect for exact integer arithmetic, cryptography, and competitive programming.

Why These Primes?

p = 998,244,353 has a special form: p = k·2m + 1

This means (p-1) is divisible by large powers of 2, allowing NTT of size up to 223.

g = 3 is a primitive root mod p, meaning g generates the multiplicative group Zp*.

8th root of unity ω = g(p-1)/8 mod p:
ω = 372528824
Powers of ω (should cycle back to 1):
ω0=1ω1=372528824ω2=911660635ω3=488723995ω4=998244352ω5=625715529ω6=86583718ω7=509520358

NTT Multiplication Result

[1, 3, 5, 3]

Exact integer multiplication with no floating-point errors!

FFT vs NTT Comparison

PropertyFFTNTT
DomainComplex numbersIntegers mod p
Roots of unitye2πi/ng(p-1)/n mod p
PrecisionFloating-point errorsExact (modular)
Size constraintPower of 2n | (p-1)
Use casesSignal processingCryptography, big integers

NTT Algorithm

1. Choose prime p = k·2m + 1 where n ≤ 2m

2. Find primitive root g mod p

3. Compute ω = g(p-1)/n mod p (nth root of unity mod p)

4. Apply same Cooley-Tukey algorithm, but all arithmetic is mod p

5. Inverse NTT uses ω-1 = gp-1-(p-1)/n mod p

Applications

  • Big integer multiplication: Multiply million-digit numbers exactly
  • Polynomial arithmetic: GCD, interpolation, division
  • Lattice cryptography: NTRU, Ring-LWE use NTT for efficiency
  • Error-correcting codes: Reed-Solomon encoding

FFT Mastered!

You now understand one of the most transformative algorithms:

  • Roots of unity: ωn = 1, evenly spaced on unit circle
  • Halving lemma: Squares of n roots = n/2 roots
  • Butterfly operations: Combine pairs with twiddle factors
  • Convolution theorem: Multiply in frequency domain
  • NTT: Exact arithmetic over finite fields

Next: We'll explore advanced tree structures — B-trees for disk optimization, persistent trees for version control, and van Emde Boas trees for O(log log U) integer operations.