MathsAdvanced Equations

Discrete Fourier Transform (DFT)
calculator.

Paste up to 64 evenly spaced readings to see the frequencies they hold.

How it's worked out

The DFT takes N samples and gives back N complex numbers, one per frequency bin. Smith's definition multiplies each sample by a wave that turns k times over the whole window, then adds. Here x[n] is sample n and j is the square root of −1.

X[k]\displaystyle X[k]=∑n=0N−1x[n]e−j2πkn/N\displaystyle {}= \sum_{n=0}^{N-1} x[n] \, e^{-j 2 \pi k n / N}

In plain terms, the real part of X[k] is the sum of x[n] times a cosine, and the imaginary part is minus the sum of x[n] times a sine. The size of X[k] says how much of that wave is in the samples, and its angle says where the wave starts.

From bin to hertz. Bin k is k times the bin spacing, and the spacing is the sample rate over N. With 8 samples at 1,000 per second the bins sit 125 Hz apart. Leave the rate blank and the tool gives k over N, in cycles per sample.

fk\displaystyle f_k=kfsN\displaystyle {}= \frac{k \, f_s}{N}

Back again. The inverse uses the same sum with a plus sign and divides by N. The tool runs it on every result, so you can see your samples come back. It also checks Parseval's theorem: the sum of the squared samples equals the sum of the squared bin sizes over N.

x[n]\displaystyle x[n]=1N∑k=0N−1X[k]ej2πkn/N\displaystyle {}= \frac{1}{N} \sum_{k=0}^{N-1} X[k] \, e^{j 2 \pi k n / N}

Worked examples

Priya checks a 4 point DFT by hand

Priya has the samples 1, 2, 3 and 4 on a problem set. With N = 4 the wave factors are just 1, −j, −1 and j, so each bin is a short sum.

Bin 0 is the plain sum, 10. Bin 1 is 1 + 2(−j) + 3(−1) + 4(j), which is −2 + 2j. Bin 2 is 1 − 2 + 3 − 4, which is −2. Bin 3 is −2 − 2j, the mirror image of bin 1 with the sign of j flipped.

The inverse returns 1, 2, 3, 4, and both sides of the energy check give 30. With no sample rate, bin 1 is 0.25 cycles per sample.

Open this example in the tool

Marco finds two tones in a vibration reading

Marco samples a motor mount 800 times a second and keeps 16 readings. The bins are 50 Hz apart, and the highest frequency 800 samples a second can show is 400 Hz.

The bins that are not 0 from 0 to 400 Hz are 50 Hz, with height 1 and phase −90°, and 150 Hz, with height 0.5 and phase 0°, and 250 Hz, with height 4.9781 × 10⁻⁸ and phase −124°. The heights match the 1 and 0.5 that built the signal. A phase of −90° marks a sine, and 0° marks a cosine.

Both tones complete a whole number of cycles in the 16 readings, so each one lands in a single bin.

Open this example in the tool

Ana sees a tone fall between two bins

Ana's 8 samples hold a sine that fits 1.5 cycles in the window. At 1,000 samples a second the bins are 125 Hz apart, and the tone sits halfway between bin 1 and bin 2, so no bin matches it.

The strongest bin is 125 Hz, with size 2.8478, and the next is 250 Hz, with size 2.4142. In all, 8 of the 8 bins are not 0. Smith calls this spectral leakage: cutting a wave off part way through spreads it over the whole spectrum.

Open this example in the tool

Reading your result

Wave height. For a real wave, the size of a bin is N times half its height, because the wave is split between bin k and bin N − k. So the height is 2 times |X[k]| over N, except at bin 0 and the Nyquist bin, where it is |X[k]| over N. A sine of height 3 over 8 samples gives |X[1]| = 12, and 2 × 12 over 8 is 3.

The mirror half. For real samples the spectrum is Hermitian: bin N − k has the same size as bin k and the opposite phase. In the square wave, bin 7 is 2 + 4.8284j and bin 1 is 2 − 4.8284j. Only bins 0 to N/2 carry new facts.

Sizes of the 8 bins for four signals
Signal|X[0]| to |X[7]|
Impulse1, 1, 1, 1, 1, 1, 1, 1
Constant8, 0, 0, 0, 0, 0, 0, 0
Alternating0, 0, 0, 0, 8, 0, 0, 0
Square wave0, 5.23, 0, 2.16, 0, 2.16, 0, 5.23

A single spike spreads evenly over every bin, and a flat signal puts everything in bin 0. The alternating signal is the fastest pattern 8 samples can hold, so it sits at bin 4, the Nyquist bin. The square wave has odd bins only, because its second half is the first half turned upside down.

Common mistakes

  • Reading |X[k]| as the wave height. The size grows with N, so divide by N, and double it for every bin except 0 and N/2.
  • Counting the mirror bins twice. Bins above N/2 repeat the lower half for real samples. Adding both halves to a list of tones counts each tone twice.
  • Mixing up the scaling. This tool puts no factor on the forward sum and 1/N on the inverse. Some books and libraries use 1 over the square root of N both ways, which changes every size by the same factor.
  • Expecting one bin per tone. A tone that does not fit a whole number of cycles in the window leaks into the bins around it. Choose a window length that holds whole cycles, or taper the samples first.

Questions people ask

Bin k is k times the sample rate divided by N. With 16 samples at 800 Hz the bins are 50 Hz apart, so bin 3 is 150 Hz. Without a rate, bin k is k over N cycles per sample.

Sources