Fourier Transforms (np.fft)

The Fast Fourier Transform decomposes a signal sampled in time into the frequencies it contains, turning a wiggly waveform into a clear list of which frequencies are present and how strong each one is.

Learn Fourier Transforms (np.fft) in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll run np.fft.fft and its inverse, map output bins to real frequencies with fftfreq , read a magnitude spectrum, and use the faster rfft for real-valued signals.

np.fft.fft(x) takes a 1D array of samples and returns an array of complex numbers , one per frequency bin. np.fft.ifft runs the transform backwards and returns the original samples (to floating-point precision). The pair is lossless: ifft(fft(x)) equals x .

The very first output element (bin 0) is always the sum of the inputs — the DC component , or average level. Here is a clean example where a pure oscillation makes the structure obvious.

A bare FFT result is hard to read until you know which frequency each bin represents. np.fft.fftfreq(n, d) returns those frequencies, where n is the sample count and d is the time between samples. Taking np.abs of the FFT gives the magnitude spectrum — peaks appear at exactly the frequencies present in the signal.

The peaks at bins 1 and 2 (and their mirror images at the negative frequencies) confirm both cosines, and bin 1 is twice as tall as bin 2 because the first cosine had twice the amplitude.

The spectrum of a real-valued signal is mirror-symmetric, so the negative-frequency half is redundant. np.fft.rfft returns only the unique first half — n/2 + 1 values — which is faster and cleaner to plot. Use np.fft.rfftfreq for the matching frequencies and np.fft.irfft to invert.

Replace each ___ so the program transforms a signal, prints its magnitude, and reconstructs it.

Expected output: the magnitude is [0. 2. 0. 2.] and the inverse recovers [ 1. 0. -1. 0.] . (Answers: fft , abs , ifft .)

fft returns complex numbers; printing them directly or comparing to floats is confusing.

✅ Fix: take np.abs(spec) for magnitude and np.angle(spec) for phase, and use .real after ifft .

❌ Plotting magnitude against bin index instead of frequency

Bin numbers are meaningless without the sample spacing.

✅ Fix: pair the spectrum with np.fft.fftfreq(n, d) (or rfftfreq for rfft) to get real frequency units.

Build a pure sine that completes 3 cycles over 16 samples, take its rfft magnitude, and use argmax to find which frequency bin dominates.

Lesson complete — Fourier transforms unlocked!

You can now run a forward and inverse FFT, label bins with fftfreq , read a magnitude spectrum to find the frequencies in a signal, and reach for rfft when your data is real-valued.

🚀 Up next: Interpolation (np.interp) — estimate values between known data points.

Practice quiz

What does np.fft.fft return for a 1D signal?

  • An array of complex numbers, one per frequency bin
  • A single real number
  • A sorted copy of the input
  • The signal's mean

Answer: An array of complex numbers, one per frequency bin. The FFT gives complex values describing the signal's frequency content.

Which function is the exact inverse of np.fft.fft?

  • np.fft.fftfreq
  • np.fft.rfft
  • np.fft.ifft
  • np.fft.fftshift

Answer: np.fft.ifft. ifft runs the transform backwards, so ifft(fft(x)) recovers x.

What does the first FFT output element (bin 0, the DC component) equal?

  • The maximum sample
  • The sum of the input samples
  • Always zero
  • The first sample

Answer: The sum of the input samples. Bin 0 is the sum of the inputs, i.e. the average level scaled by N.

What does np.fft.fftfreq(n, d) return?

  • The magnitude spectrum
  • The inverse transform
  • The phase of each bin
  • The frequency value for each FFT bin

Answer: The frequency value for each FFT bin. fftfreq maps each bin to a real frequency given sample count n and spacing d.

How do you get the magnitude spectrum from an FFT result spec?

  • np.abs(spec)
  • spec.real
  • np.sum(spec)
  • np.sort(spec)

Answer: np.abs(spec). np.abs gives the magnitude of each complex bin; peaks mark the frequencies present.

How many values does np.fft.rfft return for a real signal of length 8?

  • 8
  • 4
  • 5
  • 16

Answer: 5. rfft returns n/2 + 1 = 5 values, the non-redundant half of the spectrum.

When should you prefer np.fft.rfft over np.fft.fft?

  • When the input is real-valued
  • When the input is complex
  • When you need the inverse
  • Only for 2D arrays

Answer: When the input is real-valued. Real signals have a symmetric spectrum, so rfft returns just the unique half, faster.

After ifft, why do you usually take .real of the result?

  • To sort it
  • ifft returns complex values with tiny imaginary round-off
  • To normalize it
  • To make it an integer

Answer: ifft returns complex values with tiny imaginary round-off. Reconstructing a real signal leaves negligible imaginary parts; .real drops them.

Which function gives the frequencies that match an rfft result?

  • np.fft.fftfreq
  • np.fft.ifft
  • np.fft.rfftfreq
  • np.angle

Answer: np.fft.rfftfreq. rfftfreq returns the frequencies for the half-spectrum produced by rfft.

For x = [1, 0, -1, 0], what is np.abs(np.fft.fft(x))?

The magnitude spectrum is [0, 2, 0, 2], energy in the frequency-1 bins.