x[0]1.50x[1]1.71x[2]0.50x[3]−0.71x[4]−0.50x[5]0.29x[6]0.50x[7]0.71w[0]–w[1]–w[2]–w[3]–w[4]–w[5]–w[6]–w[7]–× +X[0]–X[1]–X[2]–X[3]–X[4]–X[5]–X[6]–X[7]–1import numpy as np2X = np.fft.fft(x)3f = np.fft.fftfreq(8, 1/8000)Σ = 0.00xw4
01234567-2-1012n (sample number)valuex[n] AND TEST WAVE w[n]w = cos(2π·0·n/8)w = sin(2π·0·n/8)w = cos(2π·1·n/8)w = sin(2π·1·n/8)w = cos(2π·2·n/8)w = sin(2π·2·n/8)w = cos(2π·3·n/8)w = sin(2π·3·n/8)w = cos(2π·4·n/8)w = sin(2π·4·n/8)w = cos(2π·5·n/8)w = sin(2π·5·n/8)w = cos(2π·6·n/8)w = sin(2π·6·n/8)w = cos(2π·7·n/8)w = sin(2π·7·n/8)01234567-2-1012n (sample number)x[n]·w[n]PRODUCTS x[n]·w[n]Σ = 0.0001234567012345bin k (f = k · 1000 Hz)|X[k]|SPECTRUM |X[k]|N/2−3−2−1bins 5–7: −3, −2, −1 kHz0.510.5amp = 2·|X[k]|/Ncurve = DTFT
Coloured parts: what the current step is about; everything else rests in whiteBlocks: samples x[n], test wave w[n], the multiply-and-add chip, the spectrum X[k] and a Python code cardPlots: samples with the test wave, the products x[n]·w[n], and |X[k]| with the DTFT curveTokens carry one pair of values into the chip, and each finished bin into the spectrum row8 samples at 8000 samples per second become 8 DFT bins 1000 Hz apart: X = 4, 4, −2j, 0, 0, 0, 2j, 4. Both test frequencies were chosen to land exactly on bins. A tone between bins (say 1500 Hz) spreads into every bin: that leakage, and windows that tame it, get their own lesson in this module.

A finite list of samples

  1. A converter sampled a sound 8000 times a second and kept N = 8 samples, x[0] to x[7]: one millisecond. A computer only ever holds a finite list like this. Which frequencies are inside it?

One bin by hand

The whole spectrum

The DTFT and the code

The working

# step 1/11 · samples

fs = 8000 samples per second
Ts = 1 / fs = 125 µs
N  = 8 samples  ->  8 × 125 µs = 1 ms

n     0     1     2     3     4     5     6     7
x[n]  1.50  1.71  0.50 −0.71 −0.50  0.29  0.50  0.71

(the recipe that made them stays hidden for now)

The signal is a chosen example: a constant 0.5, a 1000 Hz cosine of amplitude 1 and a 2000 Hz sine of amplitude 0.5, sampled 8000 times a second. Every value, sum and curve in the figure is computed from this model. The DFT here uses e^(−j2πkn/N) and no 1/N in front, the same convention as numpy.fft; some books put 1/N or 1/√N in front, which changes the scale of X[k] but not its shape.