ENع
←Decimation
Lesson 46 · Multirate and random signals

Decimation: lowering the sample rate

16 min9 steps5 questionsAdvanced
About this lesson

Lowering the sample rate means keeping fewer samples, but you cannot just drop them. Follow 48 kHz audio on its way down to 16 kHz (M = 3): keeping one sample in three lets a 10 kHz whistle fold to 6 kHz, while a digital low-pass filter first (a 61-tap FIR, cutoff 8 kHz) removes it 62 dB deep. Filter, then downsample: that is decimation. You will also see why a good decimator only computes the outputs it keeps, and do it all with scipy in Python. It builds on the lessons on aliasing, anti-alias filters, the DFT and FIR filters.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 46 · Interactive
01 / 09

48 000 samples a second

Audio arrives as samples x[n], 48 000 a second, one every 20.8 µs. It holds a 1 kHz tone we want and a 10 kHz whistle we do not, 0.5 each. A speech system needs only 16 000 samples a second.

fs = 48 000 samples per second
Ts = 1 / fs = 20.8 µs

x[n] = 0.5·sin(2π·1000·n/fs)
     + 0.5·sin(2π·10000·n/fs)

goal: fs' = 16 000 per second
      48 000 / 16 000 = 3 = M

plots: x[n] over 1 ms (48 samples),
and its spectrum (DFT of 96 samples:
1000 Hz 0.50, 10000 Hz 0.50)
Loading figure…
or ← →
Read the lesson step by step
  1. 48 000 samples a second

    Audio arrives as samples x[n], 48 000 a second, one every 20.8 µs. It holds a 1 kHz tone we want and a 10 kHz whistle we do not, 0.5 each. A speech system needs only 16 000 samples a second.

    fs = 48 000 samples per second
    Ts = 1 / fs = 20.8 µs
    
    x[n] = 0.5·sin(2π·1000·n/fs)
         + 0.5·sin(2π·10000·n/fs)
    
    goal: fs' = 16 000 per second
          48 000 / 16 000 = 3 = M
    
    plots: x[n] over 1 ms (48 samples),
    and its spectrum (DFT of 96 samples:
    1000 Hz 0.50, 10000 Hz 0.50)
  2. Keep one sample in three

    With the switch closed the filter is skipped. The ↓3 block keeps x[0], x[3], x[6] ... and drops the rest: y[m] = x[3m]. This is downsampling by M = 3. Out come 16 000 samples a second.

    downsample by M:  y[m] = x[M·m]
    
    M = 3:
      y[0] = x[0] = 0.00
      y[1] = x[3] = −0.16
      y[2] = x[6] = 0.85
      x[1], x[2], x[4], x[5] ... dropped
    
    new rate fs/M = 48 000 / 3 = 16 000
    new spacing 3·Ts = 62.5 µs
  3. The new limit: 8 kHz

    At 16 000 samples a second, only frequencies below half of that can be told apart: 8 kHz, the new Nyquist frequency fs/(2M). The 1 kHz tone fits. The 10 kHz whistle was fine at 48 kHz but is now too high.

    old Nyquist  fs/2     = 24 000 Hz
    new Nyquist  fs/(2M)  = 8 000 Hz
    
    1000 Hz  < 8000 Hz   fine
    10000 Hz > 8000 Hz   trouble
    
    shaded band: what M = 3 can no longer hold
  4. The whistle folds to 6 kHz

    Dropping samples cannot remove the whistle. It folds across 8 kHz to 16 − 10 = 6 kHz. The kept samples trace a 1 kHz tone plus a 6 kHz tone, both 0.5. A DFT of y[m] finds 0.50 at 6 kHz.

    alias: f -> fs' − f   (for fs'/2 < f < fs')
           10000 -> 16000 − 10000 = 6000 Hz
    
    sin(2π·10000·3m/48000) = −sin(2π·6000·m/16000)
    for every whole m: the same numbers
    
    DFT of 32 samples of y[m] (bins of 500 Hz):
      1000 Hz  0.50
      6000 Hz  0.50   <- the alias
      every other bin 0.00
  5. Filter first: a digital low-pass

    Open the switch: samples now pass a low-pass filter before ↓3. It is a FIR filter (finite impulse response): each output is a weighted sum of the last 61 inputs, v[n] = Σ h[k]·x[n−k]. Its cutoff sits at 8 kHz.

    FIR filter, 61 taps h[0..60]:
      v[n] = Σ h[k]·x[n − k],  k = 0 .. 60
    
    design: a sinc shaped by a Hamming window
      (scipy.signal.firwin(61, 1/3))
      cutoff = 1/3 of fs/2 = 8 000 Hz
      gain 1 at 0 Hz
      h[30] = 0.3338 (the middle tap)
    
    curve: the filter's gain |H(f)|
  6. Each tone gets its own gain

    At 1 kHz the filter's gain is 1.002: the tone passes. At 10 kHz it is 0.0008, 62 dB down. Everything above the new Nyquist frequency must be removed before a single sample is dropped.

    |H(f)| = |Σ h[k]·e^(−j2πf·k/fs)|
      1000 Hz:  1.0024   (0.02 dB)
      8 000 Hz:  0.5004   (−6.0 dB)
      10000 Hz: 0.000765 (−62.3 dB)
    
    bars: 0.5 × gain
      1000 Hz  0.501
      10000 Hz 0.00038
  7. Filter, then keep one in three

    Now ↓3 drops two samples in three of the filtered stream. The whistle still folds to 6 kHz, but at 0.0004 instead of 0.5. Low-pass plus downsampling is decimation. The filter also delays everything by 30 samples (0.625 ms).

    decimation = low-pass, then ↓M
      y[m] = v[3m]
    
    DFT of 32 samples of y[m]:
      1000 Hz  0.50
      6000 Hz  0.0004   (was 0.50)
    
    symmetric taps -> linear phase:
      delay (61 − 1)/2 = 30 samples = 0.625 ms
    
    y[0..2] = 0.35, 0.19, 0.00
  8. Compute only what you keep

    Two of every three filter outputs are thrown away, so never compute them. Work out v[n] only for n = 0, 3, 6 ...: 976 000 multiply-adds a second instead of 2 928 000. Polyphase decimators are built on this idea.

    filter every sample, then drop:
      61 × 48 000 = 2 928 000 per second
    
    compute only the kept outputs:
      61 × 16 000 = 976 000 per second
      (one third of the work, same y[m])
    
    polyphase: split h into 3 phases,
    h[0], h[3], h[6] ... / h[1], h[4] ... / h[2] ...
  9. Decimate in Python

    With scipy, firwin designs the 61-tap low-pass (cutoff 1/3 of the old Nyquist frequency), lfilter runs it and v[::3] keeps every third sample. signal.decimate(x, 3, ftype="fir") does the job in one call and also removes the delay.

    from scipy import signal
    h = signal.firwin(61, 1/3)
    v = signal.lfilter(h, 1, x)
    y = v[::3]
    
    x: a numpy array at 48 000 samples/s
    v[::3]: numpy slice start:stop:step,
            step 3 = every third sample
    
    signal.decimate(x, 3, ftype="fir")
      same 61-tap Hamming FIR (order 20·3),
      and shifts the output back by the delay
    
    (Python 3 with numpy and scipy)

Quiz Unlocks after the last step.