The inverse DFT and Fourier series

Rebuilding a signal from its spectrum — spinning circles that draw a square wave, and a sign error that runs time backwards.

What you'll learn
  • Write the inverse DFT and the scaling it needs for a half spectrum
  • Build a square wave from odd harmonics and explain the Gibbs overshoot
  • Find the sign error in 3_IDFT.py and what it does to the output

The DFT measured how much of each rotating phasor a signal contains. The inverse DFT adds those phasors back up:

\[x[n] = \frac{1}{N} \sum_{k=0}^{N-1} X[k]\, e^{+2\pi i k n / N}.\]

Same sum, opposite rotation, and a factor \(1/N\). Nothing is lost in either direction: the \(N\) samples and the \(N\) coefficients are two descriptions of the same signal.

Circles that draw a square wave

Fourier’s claim was that any periodic shape is a sum of sinusoids. For a square wave of height \(\pm 1\) only the odd harmonics appear:

\[\text{square}(x) = \frac{4}{\pi} \left( \sin x + \frac{\sin 3x}{3} + \frac{\sin 5x}{5} + \cdots \right).\]

Each term is a phasor: a circle of radius \(4/(\pi n)\) spinning \(n\) times as fast. Chain the circles tip to tail and the height of the last tip draws the sum.

Odd harmonics as a chain of spinning circles; the tip's height traces the partial Fourier sum (orange) against the target square wave (grey). Add circles to sharpen the corners — the overshoot beside each jump narrows but never drops below about 9% of the jump.

Two things to notice. The edges sharpen as circles are added, because high harmonics are what make fast changes possible. And the overshoot beside each jump never goes away: the peak of the partial sum tends to 1.179, an overshoot of 8.95% of the jump from −1 to +1. This is the Gibbs phenomenon, and it is the same ringing a sharp-cutoff filter puts next to every step in a signal (lesson 7). Waveforms/2_squarewave.py generates the target itself with signal.square(2*np.pi*5*t).

The half-spectrum inverse in 3_IDFT.py

3_IDFT.py inverts the half spectrum from lesson 3 with real arithmetic:

for x in range(len(sig_src_imx_arr)):
    sig_src_rex_arr[x] = sig_src_rex_arr[x]/len(sig_src_rex_arr)
    sig_src_imx_arr[x] = sig_src_imx_arr[x]/len(sig_src_rex_arr)

for k in range(len(sig_src_rex_arr)):
    for i in range(len(sig_src_rex_arr)*2):
        sig_dest_idft_arr[i] += sig_src_rex_arr[k]*math.cos(2*math.pi*k*i/(len(sig_src_rex_arr)*2))
        sig_dest_idft_arr[i] += sig_src_imx_arr[k]*math.sin(2*math.pi*k*i/(len(sig_src_imx_arr)*2))

Dividing by \(N/2\) is right for a half spectrum: each bin stands for itself and its mirror image. Two details are not:

  • The sine term needs a minus sign. The forward loop stored \(\operatorname{Im}X[k] = -\sum x[n]\sin(\cdot)\). Adding it back with a plus sign flips the odd part of the signal, and the result is the input played backwards: \(x[-n]\) instead of \(x[n]\). Checked numerically on the test signal: the output matches the time-reversed input, not the input.
  • The DC bin must be divided by \(N\), not \(N/2\) — it has no mirror image. As written the mean is doubled, here an offset of 0.037. (The Nyquist bin \(k = N/2\), which the loop also omits, needs the same treatment.)
The first 80 samples of the input signal, the output of 3_IDFT.py which does not match it, and the corrected inverse which lies exactly on top of the input.
With −Im and the DC term divided by N, the inverse reproduces the input to 4 × 10⁻¹⁴. As written it returns a different-looking signal: the input reversed in time.

The bug is easy to miss because for many test signals a time-reversed copy looks plausible. Comparing against np.fft.ifft — or simply subtracting the input — catches it at once.

Try it yourself. Fix the two lines in 3_IDFT.py and confirm that max(abs(idft - input)) drops to around \(10^{-13}\). Then feed it an asymmetric signal such as np.arange(32) % 8 and plot the as-written output: the ramps now run downhill.

View source on GitHub →