TExcelTAD
Home › Fourier Analysis & Signals › The Discrete Fourier Transform

The Discrete Fourier Transform

Computers cannot integrate over all time; they have NN samples. The discrete Fourier transform (DFT) is the Fourier transform made finite: NN numbers in, NN frequencies out, and an exact inverse. It is a change of basis in CN\mathbb{C}^N, to a basis of sampled complex exponentials, and the FFT computes it fast.

Definition

For x[0],…,x[N−1]x[0], \ldots, x[N-1]:

X[k]=∑n=0N−1x[n] e−2πikn/N,x[n]=1N∑k=0N−1X[k] e2πikn/NX[k] = \sum_{n=0}^{N-1} x[n]\, e^{-2\pi i k n / N}, \qquad x[n] = \frac{1}{N} \sum_{k=0}^{N-1} X[k]\, e^{2\pi i k n / N}

X[k]X[k] measures the frequency kk cycles per NN samples; with sampling rate fsf_s that is kfs/Nk f_s / N Hz. Indices k>N/2k > N/2 represent negative frequencies k−Nk - N, because e2πikn/Ne^{2\pi i k n/N} is periodic in kk.

As a matrix

The DFT is multiplication by the N×NN \times N matrix FF with entries Fkn=ωknF_{kn} = \omega^{kn}, where ω=e−2πi/N\omega = e^{-2\pi i / N}. Its columns are orthogonal, and F−1=1NF‾F^{-1} = \frac{1}{N}\overline{F} (the conjugate, since FF is symmetric).

python · try it yourself
import numpy as np

N = 4
k, n = np.meshgrid(np.arange(N), np.arange(N), indexing="ij")
F = np.exp(-2j * np.pi * k * n / N)
print("F for N = 4 (rounded):")
for row in np.round(F, 3):
    print("  ", [complex(z.real + 0.0, z.imag + 0.0) for z in row])

x = np.array([1.0, 2.0, 0.0, -1.0])
print("F @ x       :", np.round(F @ x, 6).tolist())
print("np.fft.fft  :", np.round(np.fft.fft(x), 6).tolist())
print("F^H F / N = I:", np.allclose(F.conj().T @ F / N, np.eye(N)))
F for N = 4 (rounded):
   [(1+0j), (1+0j), (1+0j), (1+0j)]
   [(1+0j), -1j, (-1+0j), 1j]
   [(1+0j), (-1+0j), (1+0j), (-1+0j)]
   [(1+0j), 1j, (-1+0j), -1j]
F @ x       : [(2+0j), (1-3j), 0j, (1+3j)]
np.fft.fft  : [(2+0j), (1-3j), 0j, (1+3j)]
F^H F / N = I: True

For N=4N = 4 the matrix contains only 1,−i,−1,i1, -i, -1, i; computing it by hand is a good exercise.

Reading the output

python · try it yourself
import numpy as np

fs, N = 64, 64                                 # 1 second at 64 Hz: bin k is k Hz
t = np.arange(N) / fs
x = 0.5 + np.cos(2 * np.pi * 5 * t) + 2 * np.sin(2 * np.pi * 12 * t)
X = np.fft.fft(x)
big = np.where(np.abs(X) > 1e-9)[0]
for k in big:
    freq = k if k <= N // 2 else k - N
    print(f"bin {k:2d} ({freq:+3d} Hz): X = {complex(round(X[k].real, 3) + 0.0, round(X[k].imag, 3) + 0.0)}")
bin  0 ( +0 Hz): X = (32+0j)
bin  5 ( +5 Hz): X = (32+0j)
bin 12 (+12 Hz): X = -64j
bin 52 (-12 Hz): X = 64j
bin 59 ( -5 Hz): X = (32+0j)
  • X[0]=N⋅mean=32X[0] = N \cdot \text{mean} = 32: the DC term.
  • The cosine of amplitude 1 gives N/2=32N/2 = 32 at ±5\pm 5 Hz (real).
  • The sine of amplitude 2 gives ∓iN=∓64i\mp i N = \mp 64i at ±12\pm 12 Hz (imaginary).

So amplitude =2∣X[k]∣/N= 2\lvert X[k] \rvert / N for 0<k<N/20 < k < N/2.

In software

  • np.fft.fft / ifft use exactly these formulas (no scaling forward, 1/N1/N backward); norm="ortho" uses 1/N1/\sqrt{N} both ways, making FF unitary.
  • The DFT assumes the NN samples are one period of a periodic signal. When they are not, spectral leakage appears.

Exercises

Exercise 1: A 2-point DFT

Compute the DFT of [a,b][a, b].

Answer

[a+b,a−b][a + b, a - b].

Solution

With N=2N = 2, e−2πikn/2=(−1)kne^{-2\pi i k n/2} = (-1)^{kn}. So X[0]=a+bX[0] = a + b (sum) and X[1]=a−bX[1] = a - b (difference). This butterfly is the building block of the FFT.

Exercise 2: Bin frequency

With fs=44100f_s = 44100 Hz and N=4096N = 4096, what frequency does bin 100 represent, and what is the bin spacing?

Answer

About 1076.7 Hz; spacing about 10.77 Hz.

Solution

Spacing fs/N=44100/4096≈10.767f_s / N = 44100/4096 \approx 10.767 Hz, so bin 100 is at about 1076.7 Hz.

Exercise 3: Inverse by the matrix (code)

Build FF for N=8N = 8 and recover x=[3,1,4,1,5,9,2,6]x = [3, 1, 4, 1, 5, 9, 2, 6] from X=FxX = Fx using 1NF‾\frac{1}{N}\overline{F}.

Answer

The original vector comes back.

Solution
python · try it yourself
import numpy as np
N = 8
k, n = np.meshgrid(np.arange(N), np.arange(N), indexing="ij")
F = np.exp(-2j * np.pi * k * n / N)
x = np.array([3, 1, 4, 1, 5, 9, 2, 6], dtype=float)
X = F @ x
print((np.round((F.conj() @ X / N).real, 10) + 0.0).tolist())
[3.0, 1.0, 4.0, 1.0, 5.0, 9.0, 2.0, 6.0]

Test yourself

Bin kk of an NN-point DFT at sampling rate fsf_s corresponds to…

  1. kk Hz
  2. kfs/Nk f_s / N Hz
  3. kN/fsk N / f_s Hz
  4. fs/kf_s / k Hz

For real input, X[N−k]X[N - k] equals…

  1. X[k]X[k]
  2. X[k]‾\overline{X[k]}
  3. −X[k]-X[k]
  4. 0

The DFT matrix FF satisfies…

  1. F−1=FF^{-1} = F
  2. F−1=1NF‾F^{-1} = \frac{1}{N}\overline{F}
  3. FF is singular
  4. F−1=FTF^{-1} = F^T