Fast Fourier Transform


Fast Fourier Transform

Corey
Imagine someone plays a guitar chord, you hold up your phone, and a music app tells you the notes are C, E, and G. The phone didn’t receive notes—it received air-pressure measurements from a microphone, sampled tens of thousands of times per second. That signal is just a long list of numbers, and the notes have to be recovered from the wiggles.

Simon
And the surprising part is that this happens fast, on a battery-powered device. The key tool is the Fast Fourier Transform, or FFT. It helps a computer find the frequencies hidden in a signal without doing the enormous amount of work a straightforward method would require.

Corey
The underlying Fourier transform is more than two hundred years old. The mathematics wasn’t the obstacle; calculating the answer took so much work that it became practical only when computers needed it and better algorithms were available. Gauss used an early version around 1805 while studying asteroid orbits, and Cooley and Tukey rediscovered and published the method in 1965.

Simon
Let’s get a picture of what a transform is finding. A jagged recording can look chaotic when you plot pressure against time, but it can be understood as several smooth, pure waves added together. The spectrum is a kind of side view: it shows which frequencies are present and how much of each there is.

Corey
A pure wave can itself be pictured as the shadow of a point moving steadily around a circle. Its height traces a wave; a faster rotation makes a higher-frequency wave. Combine several rotating arrows, and their shadows combine into a more complicated signal. So the spectrum is like a recipe describing the size and starting position of the rotations that make up the signal.

Simon
To describe those rotations neatly, we need complex numbers. We can picture a complex number as a point: its real part tells us how far right it is, and its imaginary part tells us how far up. For example, three plus two i is the point three units right and two units up.

Corey
Multiplication is where complex numbers become especially useful. Multiplying a number by i rotates it a quarter-turn counterclockwise without changing its length. Do that twice and you’ve rotated halfway around, which takes you to the negative of the original number—that’s the geometric meaning of i squared equals minus one.

Simon
More generally, when you multiply complex numbers, their lengths multiply and their angles add. Euler’s formula expresses a rotation directly: e to the i theta equals cosine theta plus i sine theta. That’s the point on the unit circle at angle theta, with cosine giving its horizontal position and sine its vertical position.

Corey
As theta moves from zero to pi, that point reaches minus one; at two pi it returns to one. And multiplying e to the i a by e to the i b adds the angles, giving e to the i times a plus b. You can picture that rotation continuing as time advances: it traces a coil, whose shadows from different viewpoints look like cosine and sine waves. A clockwise coil uses a negative angle, which is why the FFT convention we’ll use has negative exponents.

Simon
Now divide the unit circle into N equal steps. The first clockwise step is called W sub N, and it equals e to the minus two pi i over N. Its powers move around the circle: W sub N squared is two steps, W sub N cubed is three, and after N steps you’re back at one. These evenly spaced points are called roots of unity because raising any one to the Nth power gives one.

Corey
There’s one more useful connection: polynomials. Put the signal samples in as coefficients, so P of z equals x zero plus x one z plus x two z squared, continuing through the samples. Evaluating that polynomial just means plugging in a value for z. It turns out the DFT evaluates this single polynomial at the roots of unity.

Simon
Here’s the discrete Fourier transform, or DFT: X of k equals the sum over n of x of n times W sub N raised to n k. Each X of k is one output frequency. To calculate it, every input sample is multiplied by a circle-based rotation, called a twiddle factor, and all those results are added. There are N outputs, and each one uses all N inputs.

Corey
One way to picture that sum is to turn the signal around a circle. Each sample becomes an arrow whose length comes from the sample’s value, and the twiddle factor tells us how to rotate it. As we change k, the arrows wind around at different speeds. If the signal contains a tone matching that speed, its contributions line up; if not, they tend to spread around the circle and cancel.

Simon
That alignment gives us a peak in the spectrum. A mixture of tones can produce a peak for each tone, which is how the transform can pull apart a chord. If the signal has a nonzero average, k equals zero produces another peak: with no rotation, the sum measures that average level. Strictly speaking, X of k is the sum of the arrows, not their average, so its magnitude scales with the number of samples.

Corey
Let’s calculate one small output. Take four samples: one, two, three, and four. For four points, W sub four is a quarter-turn clockwise, or minus i. For X of one, the terms are one times one, two times minus i, three times minus one, and four times plus i.

Simon
Adding those gives minus two plus two i. That’s a good example of what the direct method does: one output takes four sample-by-sample multiplications. We need four such outputs, so the four-sample DFT takes sixteen multiplications.

Corey
The polynomial connection now becomes clear. The DFT multiplies x of n by W sub N to the n k, while the polynomial uses x of n times z to the n. Those match when z equals W sub N to the k. So X of k is P evaluated at W sub N to the k: one polynomial evaluated at all N points around the circle.

Simon
That also explains the cost of the direct DFT. Imagine a grid with one row per output and one column per input. Every cell is one sample-and-twiddle multiplication, so the grid has N times N, or N squared, cells. Double the signal length and you get four times as much work.

Corey
For a frame of 4,096 samples, N squared is nearly seventeen million multiplications. At a million samples it’s about a trillion. Real-time audio, medical images, and other large signals make that grid too expensive to fill directly. The FFT gives exactly the same DFT answer while reusing work between outputs.

Simon
The strategy resembles divide and conquer, like merge sort. Instead of tackling one big problem all at once, split it into smaller problems, solve those, and combine the results. For the FFT, the key split is not into a left half and a right half of the signal. We separate the samples at even positions from those at odd positions.

Corey
For eight samples, the even group is x zero, x two, x four, and x six; the odd group is x one, x three, x five, and x seven. Each group has four samples. They contain the signal on coarser grids, with the odd group offset by one sample in time.

Simon
A delay changes the phase of a rotating frequency. The odd samples are one sample later than the even grid, so their frequency arrows have an extra rotation relative to the even samples. For frequency k, that adjustment is W sub N to the k. Transform the two groups separately, rotate the odd group’s outputs by that twiddle, and add them to combine the results.

Corey
We can prove that with the DFT sum. Split the index n into even values, n equals two r, and odd values, n equals two r plus one. The even terms become a sum over the even samples; the odd terms contain W sub N to the two r k, multiplied by an extra W sub N to the k.

Simon
That extra factor doesn’t depend on r, so it can be pulled outside the odd sum. And W sub N squared is W sub N over two: squaring doubles the angle, so the N-point circle collapses onto the circle with half as many points. Each of the two sums is therefore a half-size DFT.

Corey
Call those smaller transforms E of k for the even samples and O of k for the odd samples. Then the full output is X of k equals E of k plus W sub N to the k times O of k. In plain language: the full transform is the even transform plus a twiddled odd transform.

Simon
The polynomial view shows the same structure. Group the even powers of z together, and the odd powers together after pulling out one z. Then P of z equals an even polynomial evaluated at z squared, plus z times an odd polynomial evaluated at z squared. Squaring the circle points gives the smaller circle, so we evaluate two smaller polynomials rather than starting from scratch at every point.

Corey
There seems to be a bookkeeping problem: E and O each have only N over two outputs, but X needs N. The circle handles it. The half-size transforms repeat every N over two steps, so the second half of X reuses the same E and O values as the first half.

Simon
And the twiddle factors have a special half-turn symmetry. Moving half a circle further multiplies a point by minus one, so W sub N to the k plus N over two is the negative of W sub N to the k. That gives a pair of outputs: X of k is E plus W times O, while X of k plus N over two is E minus W times O.

Corey
So you calculate the product of the twiddle and O just once, then add it for one output and subtract it for the other. That operation is called a butterfly. It takes two values, a and b, multiplies b by a twiddle W, and produces a plus Wb and a minus Wb.

Simon
A butterfly uses one complex multiplication and a pair of additions or subtractions. Its two outputs share the expensive product. That’s the central saving: the direct DFT pays separately for every output, while the FFT shares work between pairs.

Corey
To get the full FFT, repeat the even-odd split recursively. For eight samples, the problem becomes two four-point transforms, then four two-point transforms, then eight one-point transforms. A one-point DFT is just the sample itself, since its only twiddle is one; that’s the recursion’s base case.

Simon
The actual work happens as we combine results back up the tree. At the bottom, two-point transforms use sums and differences. The next level combines them into four-point transforms, and the top level combines those into the eight-point answer. At every level, it’s the same butterfly pattern, with twiddles from the circle appropriate to that transform size.

Corey
For a transform of size N, one level needs N over two butterflies. There are log base two of N levels, because each level halves the problem size until it reaches one. So the total work is proportional to N log N, rather than N squared.

Simon
For eight samples, the three levels have four butterflies each, for twelve total. The direct DFT would need sixty-four multiplications. At 1,024 samples, the direct method takes about a million multiplications, compared with roughly 5,000 for the FFT. At a million samples, the comparison is about a trillion against ten million.

Corey
You can reach the same complexity using a recurrence. If T of N is the work for one FFT, then T of N equals two T of N over two, plus work proportional to N to combine the halves. That’s the same pattern as merge sort, so it solves to N log N. In the tree picture, each level costs about N work and there are log N levels.

Simon
The recursive tree also tells us something surprising about data order. For eight samples, read the leaves from left to right and you get x zero, x four, x two, x six, x one, x five, x three, x seven. If we want to run the butterflies directly in stages, we need to put the inputs in that order first.

Corey
The order is called bit-reversed order. Write each index in three-bit binary and reverse the bits: 001 becomes 100, so index one maps to four; 011 becomes 110, so three maps to six. Doing that for all the indices gives zero, four, two, six, one, five, three, seven.

Simon
Why does reversal work? The first even-odd split sorts according to whether an index’s last bit is zero or one. The next split looks at the next bit, and so on. The recursion therefore makes choices from the index’s bits right to left, which corresponds to reading the reversed-bit indices in order. The rearrangement can be done in one pass, often by swapping index pairs in place.

Corey
Once the inputs are arranged, the whole algorithm can be drawn as a butterfly diagram. For eight points there are three stages. The first joins neighboring values, the second joins values two rows apart, and the third joins values four rows apart. The inputs are bit-reversed, and the outputs emerge in normal order.

Simon
The twiddles change with the stage. The first stage’s two-point butterflies use one, so they just form sums and differences. The next stage uses the four-point circle, including one and minus i. Multiplying by minus i is just a quarter-turn, so it can be implemented by swapping real and imaginary parts and changing a sign, rather than doing a general multiplication.

Corey
You can turn that diagram into an iterative algorithm without recursive function calls. First arrange the input in bit-reversed order. Then loop through transform sizes two, four, eight, and onward, doubling the size each time. Within each stage, visit each group and each butterfly, using the diagram’s twiddle and writing the sum and difference back into the two slots.

Simon
That version runs in place. A butterfly reads two values and overwrites those same two positions, and after it has read them, the old values aren’t needed again. So one array can be rewritten stage by stage, without keeping copies of the full data. That matters in practice because moving data can cost more than the arithmetic.

Corey
The radix-two version we’ve described assumes N is a power of two. Each even-odd split halves the size: eight becomes four, then two, then one. With six samples, the first split makes two groups of three, and three can’t be divided evenly into even and odd halves, so this particular recursion gets stuck.

Simon
A simple way to handle a length that isn’t a power of two is zero padding: add zero-valued samples until the length reaches the next power of two. The zeros contribute nothing to the transform sum, but the padded transform samples the spectrum at more frequency points. It gives a finer grid, not new information about the signal.

Corey
Padding has a practical caveat: the frequency-bin spacing is determined by the padded length. If you pad six samples to eight, the spacing is based on eight, not six, so using the old spacing would label a peak incorrectly. And padding can’t separate two tones that are genuinely very close; that requires a longer recording.

Simon
Let’s run an eight-point example. Take the samples one, two, one, followed by five zeros. The zero-frequency output should be the sum, which is four. Since this is a smooth, short bump, we’d also expect more energy at low frequencies than at the high-frequency end.

Corey
First arrange the input in bit-reversed order. The resulting values are one, zero, one, zero, two, zero, zero, zero. At stage one, the butterflies use twiddle one: the first pair, one and zero, becomes one and one; the next pair does the same; the pair two and zero becomes two and two; and the final zero pair stays zero and zero.

Simon
At stage two, the butterflies are two rows apart and use the four-point twiddles one and minus i. The top group becomes two, one minus i, zero, and one plus i. The bottom group becomes two, two, two, and two. Those are the four-point transforms of the even samples one, one, zero, zero and the odd samples two, zero, zero, zero.

Corey
At stage three, each top-half value is combined with its matching bottom-half value. The first butterfly has inputs two and two and twiddle one, so X zero is four and X four is zero. That confirms our initial guess for X zero: it’s the sum of the input samples.

Simon
For the next butterfly, E one is one minus i, O one is two, and the twiddle is W eight, about 0.707 minus 0.707 i. Multiplying the twiddle by two and then adding or subtracting gives X one about 2.414 minus 2.414 i, and X five about minus 0.414 plus 0.414 i.

Corey
The next pair uses twiddle minus i with E two equal to zero and O two equal to two, giving X two equal to minus two i and X six equal to plus two i. The final pair gives X three about minus 0.414 minus 0.414 i and X seven about 2.414 plus 2.414 i.

Simon
The magnitudes drop off after the low-frequency terms, as we expected, and X four is zero. Because the input samples are real, the spectrum has conjugate symmetry: X seven is the complex conjugate of X one, X six of X two, and X five of X three. That’s why many libraries can return only half the spectrum for real input.

Corey
We can verify X one directly from the DFT formula. Only the nonzero samples matter, so it is one plus two times W eight plus W eight squared. That equals about 2.414 minus 2.414 i, matching the butterfly result. The example takes twelve butterflies rather than the direct method’s sixty-four multiplications, and several twiddles are one or minus i, which are especially cheap.

Simon
So far we’ve split samples into even and odd positions. That’s called decimation in time, because we’re separating time samples. There’s a complementary approach, decimation in frequency, which splits the outputs into even- and odd-numbered frequencies instead.

Corey
For decimation in frequency, split the input into its first half and second half. The second half’s twiddle has an extra half-turn factor, which is minus one to the power k. That factor is positive for even k and negative for odd k. As a result, the even-frequency outputs come from a transform of sums of paired samples, while the odd-frequency outputs come from a transform of their differences, with the differences twiddled first.

Simon
That reverses the order of operations compared with decimation in time. In time decimation, you twiddle and then add or subtract; in frequency decimation, you add or subtract first, then twiddle the difference. The time-decimation version takes bit-reversed input and gives normal-order output. The frequency-decimation version takes normal-order input and gives bit-reversed output.

Corey
The two approaches have the same N log N cost and are mirror images in their butterfly diagrams. Decimation in frequency can be handy when the output order doesn’t matter. For example, in fast convolution, a forward transform and inverse transform can be arranged so the scrambled order in the middle is acceptable, avoiding a bit-reversal step.

Simon
Now for the applications. A spectrogram, like the display in a music app, chops audio into short overlapping frames, takes an FFT of each frame, and stacks the spectra. Time runs left to right, frequency runs bottom to top, and brightness represents how strong a frequency is.

Corey
At a sample rate of 44.1 kilohertz, using 2,048-sample frames that advance by 512 samples, you get about 86 frames per second. A direct DFT would need over four million multiplications per frame—more than 360 million per second. The FFT needs around eleven thousand per frame, under a million per second, which makes a responsive live display far more practical.

Simon
Voice assistants use this kind of frame-by-frame frequency analysis too. Another major application is convolution, the operation behind things like reverb, image blurring, and filtering. Directly sliding one signal across another and multiplying and adding at each position can take about N squared work.

Corey
The convolution theorem gives us a shortcut: convolution in time corresponds to multiplication in frequency. Take the FFT of each signal, multiply corresponding frequency values, and take an inverse FFT. For long signals and filters, that can reduce a huge direct calculation to a few FFTs and a pass of multiplications.

Simon
There’s a related connection to polynomial multiplication. Multiplying two polynomials is a convolution of their coefficients. So we can evaluate both polynomials at roots of unity, multiply the values point by point, and transform back. The same general idea helps multiply very large integers.

Corey
Compression uses related transforms. In JPEG, each small image block is transformed using a close relative of the DFT called the discrete cosine transform. Most of the energy is often concentrated in a few low-frequency patterns, so strong coefficients can be retained and weaker ones rounded away with little visible change.

Simon
MP3 and AAC use similar ideas for sound. These cosine transforms use the same kinds of divide-and-conquer techniques we’ve been discussing. Fast transforms help make saving and sending photos and audio practical.

Corey
Wi-Fi and mobile networks use FFTs as well. Wi-Fi’s OFDM scheme divides a signal among many narrow subcarriers; the transmitter uses an inverse FFT to build a symbol, and the receiver uses an FFT to separate the subcarriers. A classic Wi-Fi system uses a 64-point FFT hundreds of thousands of times per second.

Simon
MRI scanners use a two-dimensional inverse FFT to turn frequency information measured from the body into an image. Radar uses FFTs to analyze echoes for distance and speed, radio telescopes use them to extract faint signals from noise, and seismologists use them to study earthquakes.

Corey
Let’s tie the whole story together. The direct DFT compares the signal against every frequency and costs N squared work. Seeing the DFT as a polynomial evaluated on a circle reveals structure; splitting samples into even and odd creates two half-size transforms; and the circle’s half-turn symmetry lets one butterfly multiplication produce two outputs.

Simon
Repeat the split and you get log N stages, with about N over two butterflies at each stage, for N log N work. The recursive structure explains bit-reversed input order; unrolling it gives a staged diagram and an in-place loop. Flip the diagram around and you get decimation in frequency.

Corey
And that brings us back to the phone listening to a chord. It tests all the frequencies efficiently, reusing the work made available by the circle’s symmetry, and reveals the component notes quickly enough to be useful in real time.

Simon
That’s the elegance of the FFT: its speed isn’t a mysterious shortcut. It comes from noticing the structure of the problem and repeatedly using a simple fact about rotations and circles. When a calculation looks expensive, looking for symmetry can reveal a much smarter way to do it.

Ask anything
We've read this whole study set, so questions about it get the best answers.