TExcelTAD
Home › Signals, Systems & Digital Filters for Engineers › The DFT and FFT for Engineers: Bins, Scaling and Amplitude

The DFT and FFT for Engineers: Bins, Scaling and Amplitude

np.fft.fft(x) returns an array of complex numbers. To turn them into "the 50 Hz50\ \text{Hz} component is 1.2 V1.2\ \text{V} peak" you need three facts that textbooks often leave out: which frequency each element belongs to, how to scale the magnitudes into volts (peak, rms or power), and what the first and last elements mean. Get any of them wrong and your spectrum is off by a factor of NN, 22 or 2\sqrt{2}. This lesson is the engineer's recipe. The definition, the properties and the FFT algorithm itself are covered in The DFT, DFT Properties and FFT in NumPy.

Bins and frequencies

For NN samples taken at fsf_s, the DFT

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

correlates the record with NN complex sinusoids that complete exactly kk cycles in the record. Bin kk therefore sits at

fk=k fsN=kT,Δf=fsN=1Tf_k = k\,\frac{f_s}{N} = \frac{k}{T}, \qquad \Delta f = \frac{f_s}{N} = \frac{1}{T}

where T=N/fsT = N/f_s is the record length. The bin spacing depends only on how long you recorded, not on the sample rate. Bins k>N/2k > N/2 are negative frequencies: X[N−k]X[N - k] belongs to −fk-f_k. For a real signal X[N−k]=X[k]∗X[N - k] = X[k]^*, so the upper half is redundant and np.fft.rfft returns only bins 0…N/20 \ldots N/2, with np.fft.rfftfreq(N, 1/fs) giving their frequencies.

Scaling the magnitudes

A real tone Acos⁡(2πfkt+ϕ)A\cos(2\pi f_k t + \phi) that falls exactly on bin kk splits into two complex exponentials of amplitude A/2A/2, at +fk+f_k and −fk-f_k. Each contributes N⋅A/2N \cdot A/2 to its bin. Hence the one-sided amplitude spectrum

Ak=2∣X[k]∣N  (0<k<N/2),A0=∣X[0]∣N,AN/2=∣X[N/2]∣NA_k = \frac{2\lvert X[k] \rvert}{N}\ \ (0 < k < N/2), \qquad A_0 = \frac{\lvert X[0] \rvert}{N}, \qquad A_{N/2} = \frac{\lvert X[N/2] \rvert}{N}

DC and the Nyquist bin are not doubled because they have no mirror partner. Other conventions follow from this one:

Want Formula (bins 0<k<N/20 < k < N/2) Unit
peak amplitude 2∣X[k]∣/N2\lvert X[k] \rvert / N V
rms amplitude 2∣X[k]∣/N\sqrt{2}\lvert X[k] \rvert / N Vrms\text{V}_\text{rms}
power of the tone 2∣X[k]∣2/N22\lvert X[k] \rvert^2 / N^2 V2\text{V}^2 (into 1 Ω1\ \Omega)
level 20log⁡10(rms/1 V)20\log_{10}(\text{rms}/1\ \text{V}) dBV

Parseval ties the record's mean-square value to the bins, 1N∑nx[n]2=1N2∑k∣X[k]∣2\frac{1}{N}\sum_n x[n]^2 = \frac{1}{N^2}\sum_k \lvert X[k] \rvert^2, which is a good check on any scaling. Tones that do not fall on a bin spread into neighbours and read low: that is leakage, handled by windows (which also change the scaling). Noise needs a density scaling: Spectral Estimation.

Worked example: reading a three-component signal

Given: x(t)=0.3 V+1.2 Vcos⁡(2π⋅50 Hz⋅t)+0.4 Vsin⁡(2π⋅120 Hz⋅t)x(t) = 0.3\ \text{V} + 1.2\ \text{V}\cos(2\pi \cdot 50\ \text{Hz}\cdot t) + 0.4\ \text{V}\sin(2\pi \cdot 120\ \text{Hz}\cdot t), sampled at fs=1 kSa/sf_s = 1\ \text{kSa/s} for N=1000N = 1000 samples.

Find: the bin spacing, the bins holding each component, their amplitudes in V peak and dBV, and the total mean-square value.

Solve: T=1 sT = 1\ \text{s}, so Δf=1 Hz\Delta f = 1\ \text{Hz} and the components sit in bins 0, 50 and 120. Expected: A0=0.3 VA_0 = 0.3\ \text{V}; A50=1.2 VA_{50} = 1.2\ \text{V} peak =0.849 Vrms=−1.43 dBV= 0.849\ \text{V}_\text{rms} = -1.43\ \text{dBV}; A120=0.4 VA_{120} = 0.4\ \text{V} peak =0.283 Vrms=−10.97 dBV= 0.283\ \text{V}_\text{rms} = -10.97\ \text{dBV}. The mean square is 0.32+1.22/2+0.42/2=0.09+0.72+0.08=0.89 V20.3^2 + 1.2^2/2 + 0.4^2/2 = 0.09 + 0.72 + 0.08 = 0.89\ \text{V}^2. The sine at 120 Hz120\ \text{Hz} appears with phase −90∘-90^\circ relative to a cosine.

Check: the code below reads the same values from the FFT and confirms Parseval.

python · try it yourself
import numpy as np

fs, N = 1000, 1000
t = np.arange(N) / fs
x = 0.3 + 1.2 * np.cos(2 * np.pi * 50 * t) + 0.4 * np.sin(2 * np.pi * 120 * t)

X = np.fft.rfft(x)
f = np.fft.rfftfreq(N, 1 / fs)
A = 2 * np.abs(X) / N                     # one-sided peak amplitude ...
A[0] /= 2                                 # ... except DC (and the Nyquist bin, N even)
A[-1] /= 2
print(f"bin spacing {f[1]:.1f} Hz, {len(X)} one-sided bins up to {f[-1]:.0f} Hz")
for k in np.flatnonzero(A > 1e-9):
    rms = A[k] / (np.sqrt(2) if 0 < k < N // 2 else 1)
    print(f"{f[k]:5.0f} Hz: {A[k]:.4f} V peak, {rms:.4f} V rms, {20 * np.log10(rms):7.2f} dBV, "
          f"phase {np.degrees(np.angle(X[k])):6.1f} deg")
print(f"mean square: time domain {np.mean(x**2):.4f} V^2, from all N bins {np.sum(np.abs(np.fft.fft(x))**2) / N**2:.4f} V^2")
bin spacing 1.0 Hz, 501 one-sided bins up to 500 Hz
    0 Hz: 0.3000 V peak, 0.3000 V rms,  -10.46 dBV, phase    0.0 deg
   50 Hz: 1.2000 V peak, 0.8485 V rms,   -1.43 dBV, phase    0.0 deg
  120 Hz: 0.4000 V peak, 0.2828 V rms,  -10.97 dBV, phase  -90.0 deg
mean square: time domain 0.8900 V^2, from all N bins 0.8900 V^2

Why the FFT matters

Computing all NN bins directly costs N2N^2 complex multiplications. The fast Fourier transform (Cooley–Tukey, 1965, FFT Algorithm) factors the DFT into log⁡2N\log_2 N stages of butterflies and needs about N2log⁡2N\frac{N}{2}\log_2 N. For a 4096-point spectrum that is 16.816.8 million against 24 57624\,576, a factor of 683, and it is the reason spectrum analysis runs in real time on a microcontroller. The FFT gives exactly the same numbers as the DFT; it is an algorithm, not an approximation.

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(3)
x = rng.standard_normal(256)
n = np.arange(256)
W = np.exp(-2j * np.pi * np.outer(n, n) / 256)    # the DFT as a 256 x 256 matrix
print(f"max |matrix DFT - FFT| = {np.max(np.abs(W @ x - np.fft.fft(x))):.1e}")
for N in (256, 1024, 4096, 65536):
    direct, fast = N**2, N // 2 * int(np.log2(N))
    print(f"N = {N:6d}: direct {direct:>13,d} multiplies, radix-2 FFT {fast:>9,d}, ratio {direct / fast:7.0f}")

# One-sided spectrum of the three-component signal, as a stem plot in dBV (rms)
fs, N = 1000, 1000
t = np.arange(N) / fs
x = 0.3 + 1.2 * np.cos(2 * np.pi * 50 * t) + 0.4 * np.sin(2 * np.pi * 120 * t)
A = 2 * np.abs(np.fft.rfft(x)) / N
A[0] /= 2
f = np.fft.rfftfreq(N, 1 / fs)
plt.figure(figsize=(7, 2.6))
plt.stem(f[:200], 20 * np.log10(np.maximum(A[:200] / np.sqrt(2), 1e-6)), bottom=-120)
plt.ylim(-120, 5); plt.xlabel("f (Hz)"); plt.ylabel("dBV (rms)"); plt.tight_layout(); plt.show()
max |matrix DFT - FFT| = 2.2e-12
N =    256: direct        65,536 multiplies, radix-2 FFT     1,024, ratio      64
N =   1024: direct     1,048,576 multiplies, radix-2 FFT     5,120, ratio     205
N =   4096: direct    16,777,216 multiplies, radix-2 FFT    24,576, ratio     683
N =  65536: direct 4,294,967,296 multiplies, radix-2 FFT   524,288, ratio    8192

(The DC line in the plot is shown divided by 2\sqrt{2} like the others; DC's rms is its value, 0.3 V0.3\ \text{V}. Empty bins are clipped at −120 dBV-120\ \text{dBV} for the plot; their true values are double-precision rounding noise, around 10−16 V10^{-16}\ \text{V}.)

Practical recipe

  1. Choose TT for the bin spacing you need (Δf=1/T\Delta f = 1/T), then fsf_s for the band (twice the highest frequency, plus margin).
  2. Remove the mean if DC is not of interest (it leaks into the low bins once you window).
  3. Window if tones are not exactly on bins (next lessons) and correct the scaling for the window.
  4. Use rfft and rfftfreq for real data; scale with 2/N2/N except DC and Nyquist.
  5. Quote what you plot: peak or rms, V or dBV, the bin spacing and the window.

Guided lab: a scope's FFT and your own

Hand calculation. A generator set to 1 kHz1\ \text{kHz}, 1 V1\ \text{V} peak into a high-impedance input gives 0.707 Vrms=−3.01 dBV0.707\ \text{V}_\text{rms} = -3.01\ \text{dBV}.

Simulation. Change the first program's signal to 1.0 * np.cos(2 * np.pi * 1000 * t) with fs = 10000, N = 10000: the 1000 Hz1000\ \text{Hz} line reads 1.0000 V1.0000\ \text{V} peak and −3.01 dBV-3.01\ \text{dBV}.

What you would measure. A scope's FFT (most report dBV rms) shows about −3.0 dBV-3.0\ \text{dBV} at 1 kHz1\ \text{kHz}, within the scope's vertical accuracy (typically ±2\pm 2 to 3 %3\,\%, ±0.2 dB\pm 0.2\ \text{dB}) and the generator's amplitude accuracy; if the generator is left in its 50 Ω50\ \Omega load mode into a high-impedance input, it reads 6 dB6\ \text{dB} high (+3.01 dBV+3.01\ \text{dBV}). Export the scope's samples and run the program on them: the two must agree once the window and scaling match (Scope FFT).

Hazards and safe setup. Generator and scope only; ground clip on the generator's ground; no mains circuits.

Pitfalls

  • Forgetting to divide by NN (NumPy's FFT is unnormalised) or doubling DC.
  • Mixing peak and rms: a −3 dB-3\ \text{dB} discrepancy is usually that.
  • Reading bins above N/2N/2 as positive frequencies.
  • Assuming more samples at the same TT improves the frequency resolution (only a longer record does).

Practice

  1. N=2048N = 2048 samples at fs=48 kSa/sf_s = 48\ \text{kSa/s}: what is the bin spacing, and which bin holds 1.5 kHz1.5\ \text{kHz}?
  2. A bin of a 1000-point FFT of a real signal has ∣X[k]∣=250\lvert X[k] \rvert = 250. What is the tone's peak amplitude (assume it is on the bin)?
  3. Why do bins 0 and N/2N/2 not get the factor 2?
Answer
  1. Δf=48000/2048=23.44 Hz\Delta f = 48000/2048 = 23.44\ \text{Hz}; 1500/23.44=641500/23.44 = 64: bin 64 exactly.
  2. 2×250/1000=0.5 V2 \times 250/1000 = 0.5\ \text{V} peak.
  3. A real tone at f≠0,fs/2f \ne 0, f_s/2 splits between +f+f and −f-f; DC and the Nyquist frequency are their own mirror images, so their whole amplitude is already in one bin.

Test yourself

The bin spacing of an FFT is determined by

  1. the sample rate only
  2. the record length TT: Δf=1/T\Delta f = 1/T
  3. the number of bits
  4. the window

A 1.2 V peak cosine exactly on bin kk of an NN-point FFT gives ∣X[k]∣=\lvert X[k] \rvert =

  1. 1.21.2
  2. 1.2N1.2N
  3. 0.6N0.6N
  4. 0.60.6

For real input, np.fft.rfft of N=1000N = 1000 samples returns

  1. 1000 bins
  2. 500 bins
  3. 501 bins
  4. 999 bins

A radix-2 FFT of 4096 points needs about how many complex multiplications?

  1. 4096
  2. 24 576
  3. 16.8 million
  4. 8192