Skip to main content
Back to module
Power, Electronics & Signals · Signals and Systems

Signals and frequency

Reading49mLesson 1 of 3 in this module

Describe signals in the time and frequency domains, sample them correctly and compute an amplitude spectrum with the FFT.

Why it matters

Engineers constantly ask "what frequencies are in this signal?" Is that noise on a sensor line 60 Hz hum from the mains wiring, or switching noise from a converter at 100 kHz? How much harmonic distortion does a motor drive put back on the grid? What bandwidth does an audio link need? The frequency domain answers those questions, and the fast Fourier transform (FFT) is how you get there from sampled data.

Time domain and frequency domain

A signal is a quantity that changes over time, such as a voltage. In the time domain you see it as a waveform. A pure sinusoid is described by

x(t) = A * sin(2 pi f t + phi)

with amplitude A, frequency f in hertz (period T = 1/f) and phase phi. Fourier's insight is that almost any signal is a sum of sinusoids. A square wave of frequency f, for example, is the sum of odd harmonics f, 3f, 5f, ... with amplitudes falling as 1, 1/3, 1/5, .... The spectrum lists how much of each frequency is present.

Sampling

A computer stores a signal as samples taken every 1/fs seconds, where fs is the sampling rate. Two rules matter:

  • Nyquist: you can represent frequencies only up to fs / 2. Anything above that aliases to a false lower frequency: f_alias = |f - fs * round(f / fs)|. A 900 Hz tone sampled at 1 kHz looks like 100 Hz. Filter before sampling.
  • Resolution: with N samples, the FFT gives frequency bins spaced fs / N apart. One second of data gives 1 Hz resolution, whatever the sample rate.

The DFT and the FFT

The discrete Fourier transform turns N samples into N complex numbers, one per frequency bin; each one's magnitude says how much of that frequency is present and its angle gives the phase. The FFT is just a fast algorithm for it (N log N operations instead of N^2). For real-valued signals, NumPy's np.fft.rfft returns the N/2 + 1 non-negative frequency bins, and np.fft.rfftfreq(N, d=1/fs) gives their frequencies.

To read amplitudes in volts, scale the magnitudes: divide by N, then double every bin except DC (bin 0) and, for even N, the Nyquist bin, because the energy of each real sinusoid is split between a positive and a negative frequency.

Worked example: hum and a harmonic

A signal has a 0.5 V DC offset, 3 V of 50 Hz and 1 V of 120 Hz. Sample at fs = 1000 Hz for one second (N = 1000).

import numpy as np

fs, n = 1000, 1000
t = np.arange(n) / fs
x = 0.5 + 3 * np.sin(2 * np.pi * 50 * t) + 1 * np.sin(2 * np.pi * 120 * t)

X = np.fft.rfft(x)
freqs = np.fft.rfftfreq(n, d=1 / fs)
amps = 2 * np.abs(X) / n
amps[0] /= 2            # DC is not doubled
amps[-1] /= 2           # nor is the Nyquist bin (n is even)

for f in (0, 50, 120):
    print(f, round(amps[freqs == f][0], 3))   # 0.5, 3.0, 1.0

The peaks are exactly 3.0 and 1.0 because each tone completes a whole number of cycles in the one-second window.

Leakage and windows

If a tone does not fit a whole number of cycles in the record, for example 50.5 Hz in one second, its energy smears into neighbouring bins (spectral leakage) and the peak looks lower. Multiplying the data by a window such as Hann (np.hanning(N)) before the FFT reduces leakage at the cost of slightly wider peaks. For plotting spectra in real work you might use matplotlib (plt.semilogy(freqs, amps)), and scipy.signal.welch averages many windowed FFTs to give a smoother power spectral density for noisy signals.

Common mistakes

  • Sampling too slowly and analysing aliases.
  • Forgetting to scale the FFT, then reporting amplitudes that grow with N.
  • Doubling the DC bin.
  • Expecting fine resolution from short records: 0.1 s of data gives 10 Hz bins.
  • Reading leakage as extra signals, where a window would have shown one clean peak.

Recap

  • Any periodic signal is a sum of sinusoids; the spectrum shows their amplitudes.
  • Sample above 2 f_max; bin spacing is fs / N.
  • np.fft.rfft plus correct scaling gives amplitudes in signal units; windows reduce leakage.

Try it

Open the Do tab to write a spectrum function, find the dominant frequency and predict aliases.

Also in this lesson: