Quiz: Fourier series and the FFT

Short questions to test your understanding of the DFT and the FFT. Try each on paper first, then open the answer.


Q1. In R, x <- c(2, 4, 6, 8). What is fft(x)[1], and how do you get the mean of x from it?

Put \(k = 0\) in \(X_k = \sum_j x_j e^{-2\pi i jk/n}\).

20+0i, the sum of the samples (every \(e^0 = 1\)). R’s fft is unnormalised, so the mean is Re(fft(x)[1]) / length(x) \(= 5\). The same holds for np.fft.fft(x)[0] in Python.


Q2. A gene is sampled every hour for 72 h (\(n = 72\)). At which R index of fft(x) do you find the 24-h rhythm? At which Python index? And what period does R index 2 correspond to?

\(k\) counts cycles per record. R adds 1 to every index.

A 24-h rhythm makes \(k = 72/24 = 3\) cycles in 72 h: R index 4, Python index 3. R index 2 is \(k = 1\): one cycle per record, period 72 h.


Q3. Why is the FFT faster than multiplying by the DFT matrix, and by roughly what factor for \(n = 2^{20}\)?

Split the samples into even and odd ones. How many times can you halve \(2^{20}\)?

The DFT of length \(n\) is two DFTs of length \(n/2\) (even and odd samples) combined with \(n\) operations: \(X_k = E_k + w^kO_k\), \(X_{k+n/2} = E_k - w^kO_k\). Repeating gives \(\log_2 n\) levels of \(n\) operations: \(n\log_2 n\) instead of \(n^2\). For \(n = 2^{20}\) the ratio is \(n/\log_2 n = 2^{20}/20 \approx 50\,000\).


Q4. You sample every 3 h. What is the Nyquist frequency, and what is the shortest period you can resolve?

The fastest wave on the grid goes up and down in two samples.

\(f_N = 1/(2\cdot3) = 1/6\) cycles per hour: periods down to 6 h. A 6-h rhythm itself sits exactly at Nyquist, and its visible amplitude depends on its phase; in practice sample clearly faster than that.


Q5. A rhythm with a 5-h period is sampled every 4 h. What period will the FFT report?

Frequencies in cycles per hour: \(f = 0.2\), \(f_s = 0.25\). Fold \(f\) back into \([0, f_s/2]\).

\(f = 0.2 > f_N = 0.125\), so it is aliased to \(|0.2 - 0.25| = 0.05\) cycles per hour: a 20-h period. The samples are identical to those of a 20-h rhythm; no analysis of these samples can tell the difference.


Q6. A colleague recorded a free-running clock with period 25.5 h for 5 days. Its spectrum has a peak near 24 h and a raised “floor” at all other frequencies. What causes the floor, and what can she do about it?

How many cycles of 25.5 h fit into 120 h? What does the DFT assume about the signal outside the record?

Leakage. 120/25.5 = 4.7 cycles, not a whole number. The DFT treats the record as one period of a periodic signal, so there is a jump where the end wraps to the start, and a jump has energy at all frequencies. Multiply by a window (Hann) that tapers both ends to zero before the FFT: the floor drops by orders of magnitude and the peak becomes a little wider. Zero-padding then helps to locate the peak (about 25.5 h) on a finer grid.


Q7. To low-pass filter a trace, a student sets Y[(kmax + 2):n] <- 0 in R and transforms back. The result has a large imaginary part, and its real part is too small. What went wrong?

Which frequencies live in the second half of fft(y)?

The second half of fft(y) holds the negative frequencies \(-k\), the mirror images of the low ones (X[n + 2 - m] = Conj(X[m])). Zeroing them removes half of every low-frequency cosine, which leaves complex exponentials: a complex result with half the amplitude. Zero only entries with \(|k| > k_\text{max}\), i.e. both the high positive and the high negative frequencies, keeping the pairs intact.


Q8. You smooth a 5-day luciferase trace with a moving average computed via the FFT (fft(y) * fft(h), transform back). The middle looks fine but the first few points are pulled towards the values at the end of the trace. Why, and how do you fix it?

What kind of convolution does a product of DFTs give?

A product of DFTs is a circular convolution: the average at the first points includes samples from the end of the record (wrap-around). Pad \(y\) and \(h\) with zeros to length at least \(n + m - 1\) to get the ordinary (linear) convolution, and then decide how to treat the edges (fewer points, mirror the data, or drop them). Removing a trend first also helps, because the start and end values then agree better.


Q9. What does this print, and what is the amplitude of the cosine?

x <- c(1, 0, -1, 0)
fft(x)
2 * Mod(fft(x)[2]) / 4

# Output:
# [1] 0+0i 2+0i 0+0i 2+0i
# [1] 1
import numpy as np

x = np.array([1.0, 0, -1, 0])
print(np.fft.fft(x))
print(2 * abs(np.fft.fft(x)[1]) / 4)

# Output:
# [0.+0.j 2.+0.j 0.+0.j 2.+0.j]
# 1.0
// rust/examples/fourier_fft_q9.rs  —  cargo run --example fourier_fft_q9
// Quiz: fft of one cycle of a cosine sampled 4 times.
use rustfft::{num_complex::Complex, FftPlanner};

fn main() {
    let mut x: Vec<Complex<f64>> = [1.0, 0.0, -1.0, 0.0].iter().map(|&v| Complex::new(v, 0.0)).collect();
    FftPlanner::<f64>::new().plan_fft_forward(4).process(&mut x);
    let s: Vec<String> = x.iter().map(|z| format!("{}{:+}i", z.re + 0.0, z.im + 0.0)).collect();
    println!("{}", s.join(" "));
    println!("{}", 2.0 * x[1].norm() / 4.0);
}

// Output:
// 0+0i 2+0i 0+0i 2+0i
// 1

\(x = (1, 0, -1, 0)\) is \(\cos(2\pi j/4)\): one cycle in four samples.

\(X = (0, 2, 0, 2)\): a cosine with \(k = 1\) appears at \(k = 1\) and at its mirror \(k = 3 \equiv -1\), each with \(nA/2 = 2\), and the phase is 0 (real, positive). The amplitude is \(2|X_1|/n = 1\). The mean (\(X_0\)) is 0.


Q10. In R (on our laptop), fft takes 0.003 s for \(n = 65536\) but over 4 s for \(n = 65537\). Why, and what is the quick fix?

Factor 65537.

65537 is prime. R’s mixed-radix FFT splits \(n\) into its prime factors; a factor \(p\) costs about \(np\) operations, so a prime \(n\) costs \(O(n^2)\). Pad with zeros to nextn(n) (here 65610 \(= 2\cdot3^8\cdot5\)) or trim the series to a length with only small factors. numpy/scipy and rustfft use Bluestein’s algorithm for primes and stay \(O(n\log n)\).


Q11. Does zero-padding a 48-h time course to 480 h let you separate a 24-h from a 22-h rhythm?

Padding adds zeros, not data. What sets the width of a peak?

No. Zero-padding evaluates the same spectrum on a 10× finer frequency grid, but each peak keeps its width of about \(1/T = 1/48\) cycles per hour. The two rhythms differ by \(1/22 - 1/24 = 1/264\), much less than that, so their peaks merge. Only a longer record (at least about 264 h) separates them.


Q12. Deblurring a microscope image by dividing fft(image) by fft(psf) gives pure noise. Why? What is the connection with the SVD?

How large is the Fourier transform of a Gaussian PSF at high frequencies?

The transform of a smooth PSF is tiny at high frequencies (around \(10^{-18}\) for a Gaussian of 2 pixels on a 128-pixel image). Dividing amplifies the measurement noise at those frequencies by the inverse of that. Blur is a linear map whose singular values are the \(|H_k|\) (the Fourier modes are its singular vectors), so this is the same problem as small singular values in least squares (The SVD, rank deficiency and regularisation): regularise (Wiener filter = ridge in frequency space, or truncate high frequencies).