Skip to main content

18  Spectrograms

library(rtemis.draw)

draw_spectrogram() renders an interactive time–frequency heatmap. Pass a raw numeric signal vector together with sample_rate; the function computes the STFT internally via signal::specgram(). Alternatively pass a pre-computed spectrogram matrix directly.

The first four examples use synthetic signals. The final example uses a recording bundled with the optional seewave package.

18.1 Chirp (Frequency Sweep)

A chirp sweeps linearly from a low to a high frequency. The spectrogram makes the sweep immediately visible as a diagonal ridge:

Fs  <- 8000L                          # 8 kHz sample rate
t   <- seq(0, 2, by = 1 / Fs)
sig <- signal::chirp(t, f0 = 100, t1 = 2, f1 = 3800)

draw_spectrogram(sig, sample_rate = Fs, title = "Linear Chirp (100 – 3800 Hz)")

18.2 Multiple Pure Tones

Three simultaneous sine waves appear as three horizontal bands — one per frequency:

t   <- seq(0, 2, by = 1 / Fs)
sig <- sin(2 * pi * 440  * t) +
       sin(2 * pi * 1100 * t) +
       sin(2 * pi * 2400 * t)

draw_spectrogram(sig, sample_rate = Fs, title = "440 Hz + 1100 Hz + 2400 Hz")

18.3 Log Frequency Scale

Use freq_scale = "log" to expand the low-frequency region — useful when the signal of interest spans several octaves. freq_unit = "kHz" and time_unit = "ms" rescale the axis labels:

t   <- seq(0, 2, by = 1 / Fs)
sig <- signal::chirp(t, f0 = 100, t1 = 2, f1 = 3800)

draw_spectrogram(
  sig,
  sample_rate = Fs,
  freq_scale  = "log",
  freq_unit   = "kHz",
  time_unit   = "ms",
  title       = "Chirp — log frequency scale"
)

18.4 Diverging Palette (EEG / ERSP)

For signed data — such as an Event-Related Spectral Perturbation (ERSP) matrix from an EEG experiment — use colormap = "diverging". The midpoint color maps exactly to zero. Set db = FALSE because the values are already on a meaningful signed scale:

set.seed(1)
n_freq <- 60
n_time <- 120
freq   <- seq(4, 80, length.out = n_freq)   # 4 – 80 Hz
time   <- seq(-0.5, 1.5, length.out = n_time) # −500 ms to +1500 ms

# Background: small random fluctuations
ersp <- matrix(rnorm(n_freq * n_time, sd = 0.4), nrow = n_freq)

# Alpha suppression (8–13 Hz, 200–800 ms post-stimulus)
alpha_f <- freq >= 8  & freq <= 13
alpha_t <- time >= 0.2 & time <= 0.8
ersp[alpha_f, alpha_t] <- ersp[alpha_f, alpha_t] - 2.5

# Gamma increase (40–60 Hz, 100–400 ms post-stimulus)
gamma_f <- freq >= 40 & freq <= 60
gamma_t <- time >= 0.1 & time <= 0.4
ersp[gamma_f, gamma_t] <- ersp[gamma_f, gamma_t] + 2

draw_spectrogram(
  ersp,
  frequency  = freq,
  time       = time,
  db         = FALSE,
  power      = FALSE,
  colormap   = "diverging",
  title      = "Simulated ERSP"
)

18.5 Existing time-frequency matrices

draw_spectrogram() expects frequency rows and time columns. With Plotly-style coordinates x (time), y (frequency), and a z matrix whose rows correspond to y, pass the matrix directly. A rectangular example makes the orientation explicit:

x <- c(0, 0.5, 1, 1.5)
y <- c(10, 20, 30)
z <- rbind(c(1, 2, 3, 2), c(2, 4, 6, 4), c(1, 1, 2, 1))
draw_spectrogram(z, time = x, frequency = y, db = FALSE,
                 title = "Recorded spectral values")

Transpose a matrix only if its rows represent time. Disable dB conversion for values that have already been transformed.

18.6 Real-world Data

The seewave package includes several bird song recordings as Wave objects ready to pass to draw_spectrogram():

library(seewave)
data(tico)   # Zonotrichia capensis song, 22050 Hz

draw_spectrogram(
  tico@left,
  sample_rate = tico@samp.rate,
  n_fft       = 512L,
  freq_range  = c(0, 8000),
  title       = "Zonotrichia capensis (tico)"
)

For EEG and physiological signals, PhysioNet (physionet.org) hosts thousands of freely downloadable recordings. The EDF/EDF+ format can be read into R with the edfReader package.

18.7 Configuration

setup_SpectrogramConfig() describes the transform and its display; draw() binds it to a signal:

Fs <- 8000L
t <- seq(0, 2, by = 1 / Fs)
config <- setup_SpectrogramConfig(sample_rate = Fs, freq_scale = "log",
                                  freq_unit = "kHz")
draw(config, data = signal::chirp(t, f0 = 100, t1 = 2, f1 = 3800))
© 2026 E.D. Gennatas