A finite list of samples
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?
The DFT (discrete Fourier transform) turns N samples into N numbers X[0] to X[N−1]. Each X[k], a bin, measures one test frequency, k·fs/N. Here fs/N = 8000/8 = 1000 Hz, so bins sit 1 kHz apart.
One bin by hand
To measure bin k = 1, make a test wave that does exactly one cycle across the 8 samples: w[n] = cos(2π·1·n/8). In time that is 1000 Hz. Its 8 values fill the test-wave row.
Multiply each sample by the test wave at the same n: x[0]·w[0], x[1]·w[1], and so on. Where the signal and the test wave rise and fall together, the products come out positive.
Add the eight products: 1.50 + 1.21 + 0 + 0.50 + 0.50 − 0.21 + 0 + 0.50 = 4.00. A big sum means a strong match. The same test with a sine wave gives 0.00, so X[1] = 4.
A cosine test misses a shifted wave, so every bin also tests a sine. For k = 2 the cosine sum is 0.00 but the sine sum is 2.00. X[2] = cos sum − j·sin sum = −2j, a complex number (j = √−1).
The whole spectrum
Repeat for every k from 0 to 7, with a cosine and a sine test each time. The spectrum appears: X = 4, 4, −2j, 0, 0, 0, 2j, 4. Most bins are empty: the signal holds only a few frequencies.
Bins above N/2 = 4 are really negative frequencies: bin 7 is −1000 Hz, bin 6 is −2000 Hz. For a real signal they mirror the low bins: X[8−k] is the complex conjugate of X[k], so X[6] = 2j.
Turn bins into amplitudes. X[0]/N = 4/8 = 0.5 is the constant part. For 0 < k < N/2 the amplitude is 2·|X[k]|/N: 1 at 1000 Hz and 0.5 at 2000 Hz. That is exactly how x[n] was made.
The DTFT and the code
The DTFT of these 8 samples is a smooth curve over every frequency. The DFT values are exact samples of it, one every fs/N = 1000 Hz. Between bins the curve is not zero; the DFT simply does not look there.
In Python, np.fft.fft(x) returns the same 8 complex numbers, and np.fft.fftfreq names each bin's frequency. Done by hand, the DFT costs N² = 64 complex multiplies; the FFT gets the same answer with far fewer.
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.