Signal Generator: Plot a Signal and Its Spectrum

Write x(t) as an equation, choose the sample rate and duration, and see the samples and their spectrum side by side. Nothing is sent anywhere.

Use t, pi, + − × / ^ and sin, cos, tan, exp, log, abs, sqrt, square, sawtooth, u (unit step).

Time domain
-10100.20.40.60.8t (s)x(t)
Amplitude spectrum
00.250.50.751020406080100f (Hz)amplitude

200 samples, bins every 1 Hz up to 100 Hz (the Nyquist frequency). Largest peaks: 5 Hz (1), 20 Hz (0.5).

The formulas

x[n]=x(n/fs),n=0,1,…,N−1x[n] = x(n / f_s), \quad n = 0, 1, \dots, N - 1

Sampling at fs for T seconds gives N = fs T samples.

X[k]=∑n=0N−1x[n] e−j2πkn/N,fk=kfsNX[k] = \sum_{n=0}^{N-1} x[n]\, e^{-j 2\pi k n / N}, \quad f_k = \dfrac{k f_s}{N}

The DFT, with bin k at frequency k fs / N. The tool plots 2|X[k]|/N, the amplitude of each sinusoid, up to fs / 2.

Worked example: two sines

  1. x(t) = sin(2π·5t) + 0.5 sin(2π·20t), sampled at fs = 200 Hz for 1 s, gives N = 200 samples.
  2. The bins are fs / N = 1 Hz apart, so 5 Hz and 20 Hz fall exactly on bins 5 and 20.
  3. The spectrum shows 1.0 at 5 Hz and 0.5 at 20 Hz and nothing else, matching the two amplitudes.
  4. Change the duration to 1.05 s and the peaks spread into neighboring bins: 5 Hz no longer fits a whole number of cycles (spectral leakage).
  • Frequencies above fs / 2 fold back (alias): a 150 Hz sine sampled at 200 Hz shows up at 50 Hz.
  • A square wave shows peaks at the odd harmonics, with amplitudes 4/π, 4/(3π), 4/(5π) and so on.

Sources