Trace
Interactive FFT Trace
Step through recursive and iterative FFT on an 8-point input, with live variable inspection and highlighted code.
The recursive FFT (Cooley-Tukey) solves DFT in O(n log n) by divide-and-conquer. It splits the input in half, recurses on each half independently, then merges the results with a butterfly operation.
Split
Separate even-indexed and odd-indexed coefficients into two halves.
Recurse
Recursively compute the DFT of each half — the tree reaches depth log₂ n.
Butterfly
Combine: X[k] = E[k] + ω·O[k] and X[k+n/2] = E[k] − ω·O[k], where ω = e^(−2πi/n).
Input: [1, 1, 0, 0, 0, 0, 0, 0] (coefficients of 1+x, zero-padded to 8 points)
Step Legend
call
Entering a recursive FFT subproblem
base case
n = 1 — single element is its own DFT
split
Separating even & odd indexed coefficients
butterfly
Butterfly: combining two half-DFTs via twiddle factor
return
Returning the combined result up the call stack
Visualization
Step Explanation
fft(n=8, start=0)
Entering FFT on subarray of length 8 starting at index 0. We will recursively split this into even- and odd-indexed elements.
Variables
| Name | Value |
|---|---|
| n | 8 |
| depth | 0 |
| start | 0 |
| stack depth | 1 |
| step kind | call |
Code
← annotation
1function fft(a, inv = false) {Enter: FFT on subarray of length n2 const n = a.length;Store the current subarray length3 if (n === 1) return a; // base caseBase case — single element is its own DFT4 const even = a.filter((_, i) => i % 2 === 0);Extract even-indexed coefficients → A_even5 const odd = a.filter((_, i) => i % 2 !== 0);Extract odd-indexed coefficients → A_odd6 const E = fft(even, inv);Recurse on the even half7 const O = fft(odd, inv);Recurse on the odd half8 const out = new Array(n);Allocate combined output array9 for (let k = 0; k < n/2; k++) {Butterfly combine loop (k = 0 … n/2 − 1)10 const w = omega(k, n, inv); // twiddle factorCompute twiddle factor ω^k_n = e^{−2πik/n}11 out[k] = E[k] + w * O[k];Upper output: X[k] = E[k] + ω · O[k]12 out[k + n/2] = E[k] - w * O[k];Lower output: X[k+n/2] = E[k] − ω · O[k]13 }End butterfly loop14 return out;Return combined DFT result up the call stack15}End of function