How the Fourier transform turns samples over time into amplitudes over frequency - the discrete Fourier transform (DFT) that defines it, the fast Fourier transform (FFT) that computes it fast, and the four things that decide what a spectrum can show.

Contents
  1. What is the Fourier transform: what frequencies are in my data
  2. Methods
    1. DFT (direct sum)
    2. FFT (Cooley-Tukey radix-2)
  3. Which one should you pick
  4. Key ideas
    1. Sampling rate sets the top frequency (Nyquist, aliasing)
    2. Record length sets the spacing (frequency resolution)
    3. Spectral leakage, and the window that tames it
    4. Reading the output: magnitude, phase, the mirrored half
  5. REFs

What is the Fourier transform: what frequencies are in my data

You have a column of numbers measured over time - a vibration sensor, a voltage, a sound level, a daily count. Plotted, it wiggles. The question this page answers is the one you actually have: which frequencies is that wiggle made of, and how strong is each one?

The idea behind the Fourier transform is that a signal can be rebuilt as a sum of sine waves, each with its own frequency and its own amplitude. The transform finds that recipe. It turns a list of values over time into a list of amplitudes over frequency - a spectrum. A tall bar in the spectrum means a strong frequency in your data; an empty stretch means there is nothing there (Fig 1).

The same data twice: 16 samples over one second on the left, an arrow labelled Fourier transform, and on the right bars of 1.00 at 1 Hz and 0.50 at 3 Hz

Fig 1. From time to frequency: the same data before and after the transform

Take an example. Sample two waves 16 times a second for one second - 16 points each - and add them: a 1 Hz wave of amplitude 1.0 and a 3 Hz wave of amplitude 0.5. The sum is what a sensor would record (Table 1).

nt (s)1 Hz3 Hzsum
00.00000.0000.0000.000
10.06250.3830.4620.845
20.12500.7070.3541.061
30.18750.924-0.1910.733
40.25001.000-0.5000.500
50.31250.924-0.1910.733
60.37500.7070.3541.061
70.43750.3830.4620.845
80.50000.0000.0000.000
90.5625-0.383-0.462-0.845
100.6250-0.707-0.354-1.061
110.6875-0.9240.191-0.733
120.7500-1.0000.500-0.500
130.8125-0.9240.191-0.733
140.8750-0.707-0.354-1.061
150.9375-0.383-0.462-0.845

Table 1. The example: 16 samples of a 1 Hz wave, a 3 Hz wave and their sum

The bold rows show the amplitudes. At $n = 4$ and $n = 12$ each wave sits at its full height - $\pm 1.0$ for the 1 Hz wave, $\pm 0.5$ for the 3 Hz wave. At $n = 8$ both cross zero, halfway through the second.

Chart 1 plots all three data sets at every sample point. Only the black sum is what you get to measure; the two colored waves are hidden inside it. The same example is used for the rest of this page, so every number can be checked against it.

The 1 Hz wave, the 3 Hz wave and their sum over one second, each marked at all 16 sample points

Chart 1. The three data sets of Table 1 at every sample point

And Chart 2 is its spectrum. Two bars, exactly where the two tones are, exactly as tall as their amplitudes: 1.0 at 1 Hz and 0.5 at 3 Hz. Nothing anywhere else.

Amplitude spectrum of the same data: a bar of 1.00 at 1 Hz, a bar of 0.50 at 3 Hz, zero elsewhere

Chart 2. The spectrum of the sum: amplitude per frequency

Getting from Chart 1 to Chart 2 is the whole job. The rest of this page is how that is done, how to do it fast, and the four things that decide what a spectrum can and cannot show you. The way back, from Chart 2 to Chart 1, is the inverse Fourier transform.

Methods

There are two ways to compute the same spectrum. The DFT is the definition itself, computed term by term. The FFT is an algorithm that produces the identical numbers with far less work. They are not two different answers; they are two routes to one answer. On this data they agree to within $2 \times 10^{-14}$, which is floating-point round-off and nothing else.

DFT (direct sum)

In plain words. For every frequency you want to check, hold a test wave of that frequency up against your data, multiply them sample by sample, and add the products up. If your data contains that frequency, the products mostly agree in sign and the sum is large. If it does not, they cancel and the sum is zero.

Basic idea. With $N$ samples $y_0, \dots, y_{N-1}$, the discrete Fourier transform gives one complex number $Y_k$ for each frequency bin $k$ (Math 1). The second line writes the test wave with the twiddle factor $W_N$, the form the FFT below is built on:

$$\begin{aligned}Y_k &= \sum_{n=0}^{N-1} y_n\, e^{-2\pi i k n / N} \\ &= \sum_{n=0}^{N-1} y_n\, W_N^{kn}, \qquad W_N = e^{-2\pi i / N} \\ &= y_0 + y_1 W_N^{k} + \dots + y_{N-1} W_N^{k(N-1)}\end{aligned}$$

Math 1.

The factor $W_N^{kn}$ is the test wave: a cosine and a sine of frequency $k$, packed into one complex number. Bin $k$ sits at frequency $k \cdot f_s / N$, where $f_s$ is the sampling rate - with 16 samples at 16 per second, bin $k$ is exactly $k$ Hz.

Here is the whole procedure by hand, on the same 16 samples as the example above ($N = 16$) - the data is in Table 1, plotted in Chart 1 - for bins 1, 2 and 3, that is, 1 Hz, 2 Hz and 3 Hz.

Step 1 - split the test wave. Since $e^{-i\theta} = \cos\theta - i\sin\theta$, each bin is two ordinary sums, one against a cosine and one against a sine (Math 2):

$$Y_k = \sum_{n=0}^{N-1} y_n \cos\frac{2\pi k n}{N} \;-\; i \sum_{n=0}^{N-1} y_n \sin\frac{2\pi k n}{N}$$
$$\begin{aligned}Y_k = &\sum_{n=0}^{N-1} y_n \cos\frac{2\pi k n}{N} \\ &- i \sum_{n=0}^{N-1} y_n \sin\frac{2\pi k n}{N}\end{aligned}$$

Math 2.

Step 2 - multiply every sample by both test waves. Each sample $y_n$ (the sum column of Table 1) times $\cos(2\pi k n/16)$ and times $\sin(2\pi k n/16)$, in Table 2:

n$y_n$cos (k=1)sin (k=1)cos (k=2)sin (k=2)cos (k=3)sin (k=3)
00.0000.0000.0000.0000.0000.0000.000
10.8450.7800.3230.5970.5970.3230.780
21.0610.7500.7500.0001.061-0.7500.750
30.7330.2800.677-0.5180.518-0.677-0.280
40.5000.0000.500-0.5000.0000.000-0.500
50.733-0.2800.677-0.518-0.5180.677-0.280
61.061-0.7500.7500.000-1.0610.7500.750
70.845-0.7800.3230.597-0.597-0.3230.780
80.0000.0000.0000.0000.0000.0000.000
9-0.8450.7800.323-0.597-0.5970.3230.780
10-1.0610.7500.7500.000-1.061-0.7500.750
11-0.7330.2800.6770.518-0.518-0.677-0.280
12-0.5000.0000.5000.5000.0000.000-0.500
13-0.733-0.2800.6770.5180.5180.677-0.280
14-1.061-0.7500.7500.0001.0610.7500.750
15-0.845-0.7800.323-0.5970.597-0.3230.780
sum0.0008.0000.0000.0000.0004.000

Table 2. Each sample times the cosine and sine test waves, bins 1 to 3

Step 3 - add each column. The sin (k=1) column never goes negative: the data moves with the 1 Hz sine, so every product agrees and they pile up to 8. In the two k=2 columns every value is matched by its opposite, so they cancel to 0 - there is no 2 Hz in the data. The sin (k=3) column adds to 4. The cosine columns all cancel, because the data is built from sines only. Putting each pair of sums back into Math 2 gives Math 3:

$$Y_1 = 0 - 8i = -8i, \qquad Y_2 = 0 - 0i = 0, \qquad Y_3 = 0 - 4i = -4i$$

Math 3.

Step 4 - turn each bin into an amplitude. Take the size $|Y_k|$, divide by $N$ and double (the reason for the doubling is in Reading the output below), in Math 4:

$$A_k = \frac{2\,|Y_k|}{N}: \qquad A_1 = \frac{2 \cdot 8}{16} = 1.0, \qquad A_2 = \frac{2 \cdot 0}{16} = 0, \qquad A_3 = \frac{2 \cdot 4}{16} = 0.5$$
$$\begin{aligned}A_k &= \frac{2\,|Y_k|}{N} \\ A_1 &= \frac{2 \cdot 8}{16} = 1.0 \\ A_2 &= \frac{2 \cdot 0}{16} = 0 \\ A_3 &= \frac{2 \cdot 4}{16} = 0.5\end{aligned}$$

Math 4.

Those are the bars of Chart 2: 1.0 at 1 Hz, nothing at 2 Hz, 0.5 at 3 Hz. The other bins work the same way and all come out 0.

Important characteristics. Each of the $N$ bins needs a sum over all $N$ samples, so the DFT costs $N \times N = N^2$ complex multiplications. That is nothing at 16 samples and a real problem at a million.

Advantages

Disadvantages

FFT (Cooley-Tukey radix-2)

In plain words.

This is the idea behind almost every FFT in use, published by Cooley and Tukey in 1965.

Basic idea. For radix-2, split the samples into the even-numbered ones and the odd-numbered ones (Fig 2):

N-point DFT
    ↓
even samples + odd samples
    ↓
two (N/2)-point DFTs:  E_k and O_k
    ↓
combine with twiddle factors
    ↓
N-point result

Fig 2. Radix-2 FFT: one level of the split

Each half is a DFT of its own, $N/2$ points long - Math 1 applied to the even samples gives $E_k$, applied to the odd samples gives $O_k$ (Math 5):

$$\begin{aligned}E_k &= \sum_{m=0}^{N/2-1} y_{2m}\, W_{N/2}^{km} \\ O_k &= \sum_{m=0}^{N/2-1} y_{2m+1}\, W_{N/2}^{km}\end{aligned}$$

Math 5.

The combining step is the butterfly (Math 6):

$$Y_k = E_k + W_N^k\, O_k, \qquad Y_{k+N/2} = E_k - W_N^k\, O_k$$

Math 6.

One multiplication, $W_N^k O_k$, serves two outputs: $Y_k$ takes it with a plus, $Y_{k+N/2}$ with a minus. So only $k = 0, \dots, N/2-1$ need a multiplication; the other half of the outputs costs a subtraction, almost nothing.

Example of Fig 2, with 32 samples. Count the complex multiplications, one split at a time (Math 7 to Math 9, each with its count below). The sample values do not matter here, only how many products each step needs.

Step 0 - the DFT itself

$$Y_k = \sum_{n=0}^{31} y_n\, W_{32}^{kn}, \qquad k = 0, \dots, 31$$

Math 7.

Step 1 - split once

$$\begin{aligned} E_k,\ O_k &:\ \text{two 16-point DFTs (Math 5)} \\ Y_k &= E_k + W_{32}^k\, O_k, \qquad k = 0, \dots, 15 \\ Y_{k+16} &= E_k - W_{32}^k\, O_k \end{aligned}$$

Math 8.

Step 2 - split again

$$\begin{aligned} EE_k,\ EO_k,\ OE_k,\ OO_k &:\ \text{four 8-point DFTs} \\ E_k &= EE_k + W_{16}^k\, EO_k, \qquad k = 0, \dots, 7 \\ E_{k+8} &= EE_k - W_{16}^k\, EO_k \\ O_k &= OE_k + W_{16}^k\, OO_k \\ O_{k+8} &= OE_k - W_{16}^k\, OO_k \\ Y_k,\ Y_{k+16} &:\ \text{as in step 1} \end{aligned}$$

Math 9.

Keep splitting until every DFT is a single sample, which costs nothing. That takes $\log_2 32 = 5$ splits and leaves only 5 combine levels of 16 multiplications: 80 in all, which is $\tfrac{N}{2}\log_2 N$ (Table 3).

stepDFTssizedirect DFTscombinestotal
01321,02401,024
121651216528
24825632288
38412848176
41626464128
532108080

Table 3. FFT cost for 32 samples, split by split, in complex multiplications

Important characteristics. There are $\log_2 N$ combine levels of $N/2$ multiplications each, so the cost is $\tfrac{N}{2}\log_2 N$ complex multiplications instead of $N^2$. The result is the same DFT, not an approximation of it.

Advantages

Disadvantages

Which one should you pick

For real data, the FFT - always. It gives the same numbers, and the saving is not a percentage but orders of magnitude (Chart 3, Table 4):

Complex multiplications against N on a log scale: the DFT's N squared line pulls away from the FFT's N/2 log2 N line as N grows

Chart 3. Cost against N: DFT vs FFT

NDFT: $N^2$FFT: $\tfrac{N}{2}\log_2 N$
1625632
1,0241,048,5765,120
65,5364,294,967,296524,288

Table 4. Cost in complex multiplications

The DFT still earns its place in two cases: when you are learning what the transform does, because every number traces back to one visible sum, and when you need only a handful of bins from a short record. That is why the basic calculator runs a DFT in your browser on up to 1,024 samples, and the advanced calculator offers the FFT for longer records.

Key ideas

The transform itself is exact. What limits a spectrum is how the data was recorded - and the first limit is why aliasing happens at all.

Why aliasing happens. Think of a film of a spinning wheel. The camera takes a picture 24 times a second. If the wheel turns almost a full circle between two pictures, the film shows it turning a little backwards - a slow wheel that is not there. Sampling does the same to a wave: it sees only where the wave is at each sample, never how many times it went round in between. A fast wave and a slow one that happen to sit at the same places at every sample look identical.

In the spectrum, this shows up as copies. The spectrum of sampled data is the true spectrum repeated at every multiple of the sampling rate $f_s$ (Fig 3). If the signal stays below $f_s / 2$, the copies stand apart and the real one can be read off. If it reaches past $f_s / 2$, the copies overlap - and that overlap is aliasing.

Concept diagram: a spectrum repeated at 0, plus and minus f_s. Top: a narrow spectrum, the copies stand apart. Bottom: a wide spectrum reaching past f_s/2, neighbouring copies overlap, the overlap marked as aliasing

Fig 3. The sampled spectrum: copies at every multiple of $f_s$

Four parameters decide what a spectrum can show.

Sampling rate sets the top frequency (Nyquist, aliasing)

Sampling interval $T_s$ - the time between two samples. Sampling rate $f_s = 1 / T_s$ - how many samples per second. The two say the same thing: a short interval is a high rate.

A 3 Hz wave sampled 16 times a second: the dots follow the wave

Chart 4. Good sampling rate: 3 Hz at 16 samples per second

The same 3 Hz wave sampled 4 times a second: the four dots trace a slow 1 Hz wave instead

Chart 5. Bad sampling rate: 3 Hz at 4 samples per second, seen as 1 Hz

Nyquist frequency (folding frequency) $f_s / 2$ - the highest frequency a spectrum can show. Anything above it folds back to a lower one (Math 10):

$$f_{\text{alias}} = |\,f - k f_s\,|, \qquad k \text{ chosen so that } f_{\text{alias}} \le f_s / 2$$

Math 10.

The samples in Chart 5 really are the same as a 1 Hz wave's. At $t = n / 4$ (Math 11):

$$\sin\!\left(2\pi \cdot 3 \tfrac{n}{4}\right) = \sin\!\left(2\pi n - 2\pi \cdot 1 \tfrac{n}{4}\right) = -\sin\!\left(2\pi \cdot 1 \tfrac{n}{4}\right)$$

Math 11.

Filter before sampling, not after. Once a tone has folded, its samples are the same numbers as the alias's, so no digital filter can tell them apart - only an analog low-pass filter in front of the converter can. See Digital Filters: a filter cannot undo aliasing.

Record length sets the spacing (frequency resolution)

Frequency resolution $\Delta f$ - the spacing between two bins, one over the length of the record (Math 12):

$$\Delta f = \frac{f_s}{N} = \frac{1}{\text{record length}}$$

Math 12.

A period needs no bins. When the record cannot be made longer and only the period of one repeating signal is wanted, autocorrelation reads it in seconds, between the bins - see Cross-Correlation: a period in a short record.

Spectrum of 3 Hz plus 3.5 Hz from a 1 second record: bins 1 Hz apart, the two tones merge into one lumpy peak around 3 to 4 Hz

Chart 6. Bad resolution: 1 s record, bins 1 Hz apart

Spectrum of 3 Hz plus 3.5 Hz from a 4 second record: bins 0.25 Hz apart, two separate bars of 1.00 at 3 Hz and 3.5 Hz

Chart 7. Good resolution: 4 s record, bins 0.25 Hz apart

Spectral leakage, and the window that tames it

Spectral leakage - a tone that does not complete a whole number of cycles in the record leaves a jump where the DFT joins the record's end to its start, and that jump spreads the tone into bins where there is no signal (spectral leakage).

Detrend first. A slow drift makes the same kind of jump: the record ends higher or lower than it starts. A window softens that jump, but the drift itself stays in the lowest bins. Remove it before the transform - see Detrending: trend and the FFT.

Two panels over 2 seconds: top, a 3.5 Hz tone and its repeat jump from minus 1 to plus 1 at the join; bottom, the same tone with a Hann window fades to 0 at both ends and joins smoothly

Chart 8. Why leakage happens: the record and its repeat, joined at 1 s

Window - a curve that fades to zero at both ends, multiplied into the data before the transform, so the end and the start meet at zero.

A 3.5 Hz tone between bins: the rectangular spectrum spreads over every bin, the Hann spectrum stays concentrated at 3 and 4 Hz

Chart 9. Leakage: a 3.5 Hz tone, no window vs Hann

The four windows differ in how they fade the record's ends (Chart 10):

The four window curves over one record: rectangular flat at 1, Hann and Blackman falling to 0 at both ends, Hamming stopping at 0.08, Blackman the narrowest

Chart 10. The four windows: the curve each one multiplies into the record

The more a window fades the ends, the less leakage - and the wider each peak. Every window trades one against the other (Table 5):

windowpeak widthleakageuse it when
rectangular (none)narrowestmosttones land exactly on bins, or the record is a transient that starts and ends at zero
Hannmediumlowthe default for unknown data
Hammingmediumlow near the peaka strong tone sits next to a weaker one
Blackmanwidestlowesta very weak tone must be seen beside a strong one

Table 5. The four windows compared

When a window is needed

When a window is not needed (rectangular)

A window also shrinks the data, so it shrinks the amplitudes. The calculators on this site correct for that: a sine of amplitude $A$ still reads $A$ whatever window you pick, as long as it lands on a bin.

Reading the output: magnitude, phase, the mirrored half

Each $Y_k$ is a complex number, and it carries two facts about frequency $k$:

For real-valued data, the upper half of the bins is a mirror image of the lower half: $Y_{N-k}$ is the complex conjugate of $Y_k$, with the same magnitude. Chart 11 shows all 16 bins of our data:

All 16 bins: 0.50 at bins 1 and 15, 0.25 at bins 3 and 13; bins above the Nyquist bin 8 are drawn faded as the mirror image

Chart 11. All 16 bins of the example: the mirrored half

Each tone appears twice - at bin $k$ and at bin $N-k$ - with half its amplitude in each: 0.5 and 0.5 for the 1 Hz tone, 0.25 and 0.25 for the 3 Hz one. The mirror holds no new information, so a spectrum is usually shown from bin 0 to bin $N/2$ only, with each bin doubled to put the two halves back together. That is the doubling in Math 4. Bin 0 (the mean of the data) and bin $N/2$ (the Nyquist bin) have no partner, so they are not doubled.

REFs

Inverse Fourier Transform (IDFT, IFFT): What Signal Does This Spectrum Make? - the inverse half of this page Cooley & Tukey (1965), "An Algorithm for the Machine Calculation of Complex Fourier Series" Harris (1978), "On the Use of Windows for Harmonic Analysis with the Discrete Fourier Transform" NumPy numpy.fft SciPy scipy.fft SciPy scipy.signal.get_window