Decimation: lowering the sample rate
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.
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)Read the lesson step by step
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)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 µsThe 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 holdThe 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.00Filter 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)|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.00038Filter, 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.00Compute 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] ...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)
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01A signal sampled at 96 kHz is downsampled by M = 4. What are the new sample rate and the new Nyquist frequency?
The new rate is fs/M = 96/4 = 24 kHz, and the new Nyquist frequency is half of that, fs/(2M) = 12 kHz.
02At 48 kHz, a 10 kHz tone is downsampled by 3 with no filter. Where does it appear in the 16 kHz signal?
10 kHz is above the new Nyquist frequency of 8 kHz, so it folds to fs' − f = 16 − 10 = 6 kHz. The kept samples are exactly those of a 6 kHz tone.
03Why must the low-pass filter come before the downsampler, not after it?
Once samples are dropped, the 10 kHz whistle and a real 6 kHz tone give the very same numbers. The whistle must be removed while it is still at 10 kHz.
04A 61-tap FIR filter runs before a ↓3 at 48 kHz input. If you compute only the outputs you keep, how many multiply-adds a second do you need?
Each kept output needs 61 multiply-adds, and there are 16 000 kept outputs a second: 61 × 16 000 = 976 000, one third of filtering every sample.
05In numpy, which expression keeps every third sample of v, starting with v[0]?
Basic slicing is start:stop:step. v[::3] takes the default start 0 and stop (the end) with step 3: v[0], v[3], v[6] ...
Glossary
- Downsampling (↓M)
- Keeping every M-th sample and dropping the rest: y[m] = x[M·m]. The rate falls from fs to fs/M.
- Decimation
- Lowering the sample rate the safe way: a digital low-pass filter first, then downsampling by M.
- New Nyquist frequency fs/(2M)
- Half the new sample rate. Anything above it folds into the band below it after downsampling.
- Alias
- A tone that, after sampling or downsampling, gives the same numbers as a lower tone; here 10 kHz shows up as 6 kHz.
- FIR filter
- Finite impulse response: each output is a weighted sum of the last N inputs, v[n] = Σ h[k]·x[n−k]. The weights h[k] are the taps.
- Linear phase, delay
- A FIR filter with symmetric taps delays every frequency by the same (N − 1)/2 samples, so wave shapes are kept.
- Polyphase decimator
- A decimator that computes only the outputs it keeps, by splitting the taps into M interleaved sets (phases).
Notes and sources
- 48 000 samples a second is the standard rate of professional video and audio; 16 000 is the wideband speech rate of ITU-T G.722 (audio up to 7 kHz). The two tones of 0.5 each are a chosen example. The filter is scipy.signal.firwin(61, 1/3), the same filter scipy.signal.decimate(x, 3, ftype="fir") uses. Filtered values are the steady state, after the first 60 samples. Every number is computed from this model.
- The motion is slowed down a great deal: real samples arrive every 20.8 µs.
- Real decimation filters put the cutoff a little below fs/(2M) so the transition band (here 8 kHz is already −6 dB) does not let anything fold into the band you keep; this filter is fine for a 7 kHz speech band.
- A. V. Oppenheim, R. W. Schafer, J. R. Buck, Discrete-Time Signal Processing, 2nd ed., Prentice Hall 1999 (course text on MIT OCW 6.341; ch. 4 changing the sampling rate, ch. 7 FIR design by windowing)
- MIT OpenCourseWare 6.341 Discrete-Time Signal Processing (A. V. Oppenheim, Fall 2005): decimation, interpolation, sampling rate conversion
- SciPy documentation: scipy.signal.decimate (FIR order 20 × q, Hamming window, zero_phase)
- SciPy documentation: scipy.signal.firwin (Hamming default, fs = 2 by default, gain 1 at 0 Hz)
- SciPy documentation: scipy.signal.lfilter
- NumPy documentation: indexing and basic slicing (start:stop:step)
- Downsampling (signal processing) (Wikipedia): filter then keep every M-th sample; compute only kept outputs; polyphase
- Finite impulse response (Wikipedia): weighted sum of recent inputs; symmetric taps give linear phase
- Sampling (signal processing) (Wikipedia): 48 kHz in professional video, 16 kHz wideband speech
- G.722 (Wikipedia): wideband speech sampled at 16 kHz, 50 Hz to 7 kHz
Quiz Unlocks after the last step.