Ch.24: Algorithms: How the Fast Fourier Transform Works

Outline

Transcript

0:00 A Fourier transform tells us how repeating patterns combine to make a list of measurements. The fast Fourier transform calculates that answer while sharing work between the patterns. So we'll follow 8 values through the algorithm, including the calculation it reuses. We'll see exactly where one calculation gives us two answers. Imagine an engineer debugging a recording with a steady hum. The microphone gives us pressure measurements in time order; frequency describes how often a pattern repeats.

0:27 Yeah. A frequency view can reveal repeating components that are hard to pick out from the raw measurements. Our example will be synthetic so we can actually finish the arithmetic. And we'll use the published Cooley and Tukey method, with the papers and documentation linked in the description. What exactly goes into this transform? Evenly spaced samples, meaning measurements taken at regular intervals. The transform starts with ordinary numbers we can track through the calculation. Now take a concrete example with 8 samples: 1, 0, 1, 0, then 0, 1, 0, 1. Number their positions from 0 to 7.

1:02 The discrete Fourier transform, or DFT, produces one result for each whole-cycle pattern across that block. At output position 1, we're testing 1 cycle across the block. That position is called a frequency bin; it names the pattern, not its calculated contribution. My first thought is a list of loudness readings, one per pattern. Would that preserve everything we need to describe the original input? No, each result needs 2 coordinates. A repeating component has both a size and a starting position in its cycle; keeping only loudness would lose information.

1:36 Like knowing the width and height of an arrow, rather than only its length. How do those coordinates come out of our 8 samples? Start with an arrow 1 unit long, pointing right. Each sample multiplies its length, and its direction turns as we move through the positions. Add those arrows. For bin 1, each turn is 1/8 of a revolution. Okay, let's calculate that one. We'll turn clockwise, matching NumPy, a Python library for numerical calculations. So that 1/8 turn becomes 45 degrees at each sample position.

2:10 Take the first arrow: at position 0, it points right and hasn't turned yet. Its sample is 1, so we keep that arrow. The next sample is 0, which contributes nothing even though its arrow has turned. Yeah. Position 2 has a 1 and points down. Position 5 contributes up-left, and position 7 contributes up-right. Those are our four nonzero contributions. Yeah. The diagonal horizontal parts cancel, leaving 1 unit right. Each upward diagonal contributes about 0.71 vertically, against the other arrow's 1 unit down.

2:42 Exactly. The two upward parts total square root of 2, about 1.41. Add the 1-unit downward arrow and we're left about 0.41 up. Okay, I can picture that result, but the written answer usually has an i in it. What is that letter doing to our 2 coordinates? It's a compact label for the vertical unit. 1 plus i times 0.41 means 1 right and about 0.41 up. That's a complex number. Ah, okay. The final arrow's length carries the component's size, and its direction carries the cycle's starting point. Right. Now do that sum for every bin: 8 visits to the samples for each of 8 outputs.

3:25 So a bin's result is an arrow sum, and calculating every bin separately repeats the full walk. The FFT saves work by sharing smaller pieces of those sums. Those repeated sums lead to an even-and-odd split. Take the original even positions: 0, 2, 4 and 6. Their values are 1, 1, 0, 0. Suppose I split into the first 4 values and the last 4. That would be the usual split. Why do alternating positions matter? Because a step through the even list moves 2 places in the original list. For our 1-cycle pattern, that's 2 45-degree turns, or a quarter turn.

4:05 So its 4 positions use the same turns as an ordinary 4-value transform. The original 8-value calculation contains a smaller transform we already know how to define. Yes. The odd list, 0, 0, 1, 1, advances 2 original places too. Its first position is 1 step later, so its contribution needs an extra 45-degree rotation. At another bin, that extra rotation changes with the pattern's speed. We'll calculate the even and odd transforms once, then use the appropriate rotation when combining their results.

4:38 Splitting a job usually leaves two jobs and a meeting to put them back together. We need the combining work to buy us something. Right, splitting alone doesn't save us. The combining step will produce two output bins from each pair of smaller results. First we need to calculate those smaller transforms. Next, calculate the even branch on its own. The original even samples are 1, 1, 0, 0. Splitting their alternating positions gives two copies of 1, 0. Each can split into single values. A single-value transform returns that value, so recursion stops there.

5:12 Then we build this even branch back up. What does joining 1 and 0 produce in a 2-value transform? Their sum and their difference. For the 0-cycle pattern, both arrows point right. For its other pattern, the second arrow makes a half turn, so we subtract. Oh, that makes sense. 1 plus 0 and 1 minus 0 both give 1. Each small transform therefore returns 1, 1. Right. We're still rebuilding the even-position samples. Use the first entry from each small result, both 1. What do their sum and difference give?

5:47 2 and 0. Those become bins 0 and 2 of this 4-value transform. For bin 1, use each result's remaining entry, both 1. Keep one rightward arrow fixed and turn the other a quarter turn clockwise. That leaves a rightward arrow and a downward arrow. Their sum points 1 right and 1 down: 1 minus i at bin 1. Subtract that downward arrow instead and we get 1 right and 1 up: 1 plus i at bin 3. The even branch's 4 outputs are complete. Okay, we can leave that finished branch there. Which small lists come from the other original samples?

6:23 The original odd-position samples are 0, 0, 1, 1. Both smaller lists are 0, 1. Then let's rebuild the odd branch. What does each 0, 1 transform return? 1, minus 1. The sum is positive 1; 0 minus 1 points 1 unit left. Their first entries are again 1. The 2 outputs go to bin 0 with value 2, and bin 2 with value 0. Yes. The second entries are minus 1, so picture two leftward arrows. Which way does a quarter turn clockwise send one of them? Up. Leave the other arrow pointing left and add the upward arrow.

6:59 The result points 1 left and 1 up. That's minus 1 plus i at bin 1. What does subtraction give if we leave that leftward input unchanged? 1 left and 1 down: minus 1 minus i at bin 3. Both 4-value transforms are finished. Now we can follow a nonzero pair, which exposes the rotation instead of letting a convenient zero hide it. Now combine the completed branches at bin 1. Take the even result, 1 minus i, and its odd partner, minus 1 plus i. Okay, geometrically that's a down-right arrow from the even side and an up-left arrow from the odd side.

7:37 The original 1-position offset still needs to be applied. Yeah. Rotate that odd arrow clockwise 45 degrees. Its up-left direction becomes straight up, with length square root of 2. Then adding it to the even arrow leaves 1 right and square root of 2 minus 1 up. That's the result we calculated directly. It really is. We reached the same answer through the smaller transforms. Keep that rotated odd result, because subtracting it gives another output. Okay, which other bin can use this subtraction? Bin 5. Keep the original even arrow and subtract the upward square-root-two arrow. We end 1 right and about 2.41 down.

8:18 Nice. So the rotation wasn't a cost paid for just 1 bin. We used it in an addition and a subtraction, giving two different answers. Yeah. That paired operation is called a butterfly. Suppose a developer overwrites its even input with the first output; the second calculation would then use the wrong value. So keep both inputs until the pair is finished. But what makes bin 5's answer the subtraction instead of another independent calculation? Because bin 5 makes 4 more cycles across 8 positions than bin 1, it adds half a revolution for every original position we advance.

8:51 For instance, position 0 gets no extra turn, 1 gets a half turn, and 2 gets a whole turn. The extra direction keeps alternating. Yeah, and the pattern continues. Even positions complete whole extra revolutions, returning their arrows to the same direction. Odd positions finish halfway around, so their arrows reverse. Ah, that's why the even calculation survives unchanged. The odd contribution has the same size but the opposite direction, which converts the addition into a subtraction. Exactly. We solved 2 bins because we identified what their weights share.

9:26 We didn't throw out samples or settle for a less accurate mathematical transform. I like that the saving has a reason we can check at each position. Just being told to split an array would leave me wondering where the missing work went. Sure. That half-turn relationship works at every combine level. At size 4, the partner is 2 output positions away; at size 8, it's 4 away. That explains both outputs of our butterfly. The remaining pairs follow the same rule, so we can finish the transform without another full walk for each bin.

10:00 Next, for bin 0, add the two unrotated partial results, 2 and 2. The sum is 4. Taking their difference gives 0 at bin 4. Yeah. Bin 2 and its partner, bin 6, are both 0 because their smaller even and odd results are 0. Rotating 0 still gives 0. That 0 bin holds our input sum, 4. What would we lose if we removed that bin? The original baseline. Dividing its sum, 4, by 8 gives the average, 1/2. Removing that bin would change the measurements we reconstructed. The unscaled bin itself holds the sum. Now finish the last pair.

10:37 Its even result is 1 right and 1 up, 1 plus i. The odd result is 1 left and 1 down, minus 1 minus i. What's the rotation for bin 3? 3 45-degree steps, or 135 degrees clockwise. That down-left arrow rotates to point straight up, again with length square root of 2. Adding to the even arrow, which was already 1 unit up, gives 1 right and about 2.41 up at bin 3. Subtraction lands at bin 7, 1 right and about 0.41 down. That's all 8 outputs; NumPy gives the same array. Now bins 1 and 7 have the same horizontal coordinate and opposite vertical coordinates. Their whole arrows are mirrored.

11:22 But the butterfly paired 1 with 5. Why would 1 and 7 mirror each other? Because our input values are real numbers: they have no vertical part. For real input, reversing every rotation preserves the horizontal sum and flips the vertical sum. Those results are complex conjugates. So we're mirroring the whole result. How does reversing the rotation connect bin 1 with bin 7? 7 clockwise steps around 8 positions land at the same sample directions as 1 counterclockwise step. So bin 7 reverses bin 1's upward part but leaves the rightward part unchanged.

11:56 And 3 and 5 mirror each other too. These opposite-rotation results are called negative-frequency partners. Here they don't mean the microphone measured another independent hum. So the two pairings have different centers. Picture two equal-length seesaw arms with their hinge at 1 right, 1 down. Our butterfly outputs sit at those opposite ends. Right. That hinge is the even result. We add or subtract the rotated odd contribution around it. Conjugates instead mirror across the horizontal axis. Suppose the input already had vertical parts as well.

12:28 Could the butterfly still work without that automatic output mirror? Yes. The mirror relies on real-valued input; sharing the smaller calculations doesn't. Those are different reasons for linking a pair of outputs. Then let's count the shared calculations. How much work did building all 8 results actually take? With the transform complete, count the combine levels: 8 halved to 4, then 2, then 1. Building back up takes 3 levels with 4 butterflies apiece. 4 pairs producing 8 values per level. Doubling the input adds another halving level rather than doubling the number of levels. Exactly. For an input of size N, there are log-base-2 N levels, and each level does work proportional to N.

13:12 Together that gives N log N growth. Right. The direct formula combines N samples for each of N bins, so its work grows as N squared. Our 8-value case has 64 weighted sample terms by that method. But 12 butterflies aren't 12 of those terms. Each butterfly rotates a value and forms two combinations, so dividing 64 by 12 wouldn't give a speedup measurement. The mathematical answer stays the same, though a computer's limited digits introduce small rounding errors. We used powers of 2 to make every split the same. Does using an FFT library mean the input always has to fit that restriction? No. This is one FFT variant. FFTW is another Fourier-transform library.

13:55 Its authors describe different ways to split the calculation and timing possible execution plans, because fewer arithmetic operations don't always mean faster execution. Can we use those outputs to recover our original 8 values? Finally, take the inverse transform to recover the input. Under NumPy's default convention, reverse the rotations and divide by 8 to reconstruct our original values, within rounding. So our 1, 0, 1, 0, 0, 1, 0, 1 input isn't lost when it becomes those 2-coordinate results.

14:26 We changed its representation. Yes. One convention detail matters if you use FFTW: its backward routine is unscaled too, so you must supply that division yourself. NumPy's default inverse includes it. Right, the division still has to happen. I think recovering the input is why we need both coordinates. Yeah. And we computed those coordinates by splitting positions, solving smaller transforms and using each rotation in two outputs. That's how the fast Fourier transform earns its name. Thanks for listening to Learning Podcasts.