Sampling, Aliasing and Filtering
Nyquist, how aliases appear, anti-alias filter design, oversampling and decimation, and choosing the sample rate and record length.
Nyquist and sampling
Sampling reads a signal at regular intervals. If the sample rate is fs, the highest frequency that can be represented is fs ÷ 2, the Nyquist frequency. Components above it do not vanish: they are folded down into the range below it and look like slower signals. Once sampled, an alias cannot be told apart from a real low-frequency signal, so the only cure is to remove the high frequencies before sampling (see the filter below).
falias = | f − k · fs |, with k the whole number that makes the result lie between 0 and fs ÷ 2
How aliasing appears
| Signal frequency | Status at fs = 50 Hz | Appears at |
|---|---|---|
| 10 Hz | below Nyquist: correct | 10 Hz |
| 20 Hz | below Nyquist: correct | 20 Hz |
| 25 Hz | above Nyquist: aliased | 25 Hz |
| 40 Hz | above Nyquist: aliased | 10 Hz |
| 50 Hz | above Nyquist: aliased | 0 Hz |
| 60 Hz | above Nyquist: aliased | 10 Hz |
| 90 Hz | above Nyquist: aliased | 10 Hz |
| 110 Hz | above Nyquist: aliased | 10 Hz |
Worked example: mains hum in a 100 Hz logger
- A logger samples at 100 Hz, so its Nyquist frequency is 50 Hz. Mains pick-up at 60 Hz is above it.
- It folds to | 60 − 1 × 100 | = 40 Hz: a slow ripple that looks like a real 40 Hz vibration.
- At 50 Hz mains the alias is exactly 50 Hz (50 is Nyquist itself, which sampling cannot represent reliably), and at 120 Hz it would be 20 Hz. Because it is a fixed offset, the alias often looks like a slow “beat” in the data.
- The same trap occurs when a 60 Hz signal is sampled at 50 Hz: it shows at 10 Hz.
- Aliases look real. A slow oscillation in a log that nobody expected may be fast noise folded down.
- A suspicious frequency that changes when you change the sample rate is an alias; a real signal stays at the same frequency.
- Sampling at an exact multiple of the interference (for example 50 Hz mains at 100 Hz) maps it to a constant offset instead of an oscillation: harmless-looking, still wrong.
The anti-alias filter
A low-pass filter ahead of the ADC removes frequencies above those you want. The simplest is a resistor and capacitor with a cut-off fc = 1 ÷ (2π R C), where the signal is down 3 dB (to 71 %). A single pole falls only 20 dB per decade, so it does not stop much just above the cut-off.
fc = 1 ÷ (2π R C) |H(f)| = 1 ÷ √( 1 + (f ÷ fc)² )
Worked example: an RC anti-alias filter
- R = 1 600 Ω and C = 1 µF: fc = 1 ÷ (2π × 1 600 × 10−6) = 99.5 Hz.
- For the 100 Hz logger, the 60 Hz hum is down to 85.6 % of its size: it still gets through. A single pole is not enough at this ratio.
- How many poles? To cut a signal at 80 Hz by 40 dB with fc = 20 Hz, the stop frequency is log10(80 ÷ 20) = 0.60 decades above the corner, which is 12.0 dB for a single pole, so 4 poles (a fourth-order filter) are needed.
- Cascade or use an active filter: two RC stages with an op-amp buffer between them, a Sallen–Key or multiple-feedback design, or a switched-capacitor chip such as an MAX7400 family part. Bessel responses keep the waveform shape; Butterworth is flat in the pass band.
- The ADC itself may help: delta-sigma converters have a built-in digital filter and their own anti-alias rolloff, so a simple RC is often enough (see Sensors, Signal Conditioning and ADCs).
- Filter first, then sample. A digital filter after sampling cannot remove an alias.
- Set the corner by the signal, not the sample rate: the filter must pass everything you care about, and stop everything that would fold into it. Aim for the stop-band to start at or below fs − fmax.
- Slow signals still need a filter if the environment is noisy: a temperature signal sampled once a minute can still carry 50 Hz pick-up that folds into the data.
- Integrating converters (dual-slope, delta-sigma at a low rate) average over a window, which acts as a filter: integrating over 20 ms nulls 50 Hz and its harmonics.
Choosing the sample rate
| Goal | Rule of thumb | Example |
|---|---|---|
| Detect that a frequency exists | Just over 2 × the frequency (Nyquist) | A 10 Hz signal needs more than 20 Hz |
| See the waveform shape | 5 to 10 × the highest frequency | About 100 Hz for the same 10 Hz signal |
| Measure peaks and rise times | 10 to 20 × the highest frequency, or 5 samples across the fastest edge | A 1 ms edge needs a sample every 0.2 ms |
| Trend of a slow process | Interval about τ ÷ 5 | A 5 min time constant: every 60 s (see Planning a Logging System) |
| Power and energy | Many samples per mains cycle (at least 20 to 64); more for harmonics | At 50 Hz, at least 1 kHz for harmonics to the 9th |
| Vibration and sound | 2.56 × the analysis frequency in analyzers, higher for time waveforms | A 5 kHz band: 12.8 kHz |
- Sampling faster is cheap in the ADC and expensive in storage. Often the best design samples fast, filters or averages digitally, and stores a lower rate (see below).
- Consistency matters: a rate from a timer interrupt is regular; one from a software loop with
sleep()is not (see Time Stamps and Clocks). - Multi-channel scanning reads the channels one after another, so they are not simultaneous. For phase or power calculations use simultaneous-sampling converters or correct for the delay.
Oversampling and decimation
Oversampling samples much faster than needed, then decimates: low-pass filters digitally and keeps every M-th sample. The analog filter can then be gentle (the aliases are far above the signal), and the averaging reduces noise.
Worked example: 4 kHz down to 100 Hz
- Sample at 4 000 Hz and decimate by M = 4 000 ÷ 100 = 40 to give 100 Hz.
- Resolution: averaging 40 samples adds ½ log2(40) = 2.66 bits if the noise is random (see Sensors, Signal Conditioning and ADCs).
- Noise: random noise of 2.0 counts falls to 2.0 ÷ √40 = 0.32 counts.
- Filter before dropping samples. Simply keeping every 40th sample (“naive decimation”) aliases all the noise above 50 Hz back into the band and gains nothing. Use an average, a CIC or FIR filter, then downsample.
# Decimate by M with a simple block average (a basic low-pass), then keep one value per block
import numpy as np
M = 40
x = np.asarray(samples[: len(samples) // M * M], dtype=float)
y = x.reshape(-1, M).mean(axis=1) # 4000 Hz -> 100 Hz
# For a sharper filter use scipy.signal.decimate(x, M, ftype="fir") in stages of 10 or less
Record length and resolution
The record length sets the frequency resolution of any spectrum you compute: resolution Δf = fs ÷ N = 1 ÷ T. A short record cannot separate close frequencies, and a record that does not hold a whole number of cycles “leaks” energy across the spectrum unless a window (Hann, for example) is applied.
Worked example: how long a record?
- At fs = 1 000 Hz, N = 4 096 samples last T = 4.10 s and resolve Δf = 1000.0 ÷ 4 096 = 0.244 Hz.
- To tell two frequencies 2.0 Hz apart you need at least T = 1 ÷ 2.0 = 0.5 s; more if the lines are not equal in size. Raising the sample rate does not improve this: only a longer record does.
- Averaging spectra from several records (Welch’s method) reduces the scatter of the estimate, at the price of coarser resolution.
Digital filters for logged data
| Filter | What it does | Notes |
|---|---|---|
| Moving average | Mean of the last N samples | Simple; cut-off about 0.443 fs ÷ N (4.4 Hz for N = 10 at 100 Hz); poor stop-band; delays by (N−1) ÷ 2 samples |
| Exponential (first order IIR) | y = y + α (x − y) | One line of code, one variable; cut-off set by α; a long tail for step changes |
| Median | Middle value of the last N | Removes single-sample spikes without smearing steps (see Numerical Methods) |
| Butterworth / FIR | Designed low-pass with a defined slope | Use scipy.signal; forward and backward filtering (filtfilt) gives zero phase lag offline, which a real-time filter cannot |
- Every filter adds delay. Compare the filtered time against events; do not use a filtered signal to decide when something happened without allowing for the lag.
- Filter the data you use, store the raw data you can. Raw data can be re-filtered with better ideas; filtered data cannot be un-filtered.
- A filter cannot create information: it cannot repair an aliased signal, and a smoothing filter turns a short spike into a wide bump (check the peak is not underestimated).
Checking for aliasing
| Test | What it shows |
|---|---|
| Change the sample rate by 10 to 30 % and compare | A real frequency stays; an alias moves |
| Look at the signal with a fast scope or a burst at a high rate | The true spectrum, including high-frequency noise you did not know about |
| Add the filter and see what disappears | Whatever vanishes was out of band (or an alias of it) |
| Plot a spectrum | Lines at suspicious frequencies, and a raised noise floor near the Nyquist frequency |
| Check the numbers: the alias formula for known interferers (mains, a PWM drive, a switching supply) | Whether a mystery frequency is explained |
Timing jitter also limits fast signals: a sampling-time uncertainty of 1 µs on a 1 000 Hz sine gives an amplitude error up to 2π f t = 0.63 % of the amplitude. Use hardware timing for fast signals (see Time Stamps and Clocks).
Related
- Data Logging: the overview of this guide.
- Sensors, Signal Conditioning and ADCs: the converter and its noise.
- Storage, Formats and Data Rates: what the sample rate costs in storage.
- Numerical Methods and Embedded Systems: filters and ADCs in code.
FAQ
What sample rate should I use?
At least twice the highest frequency present, and 5 to 10 times the highest frequency you want to see, after an anti-alias filter. Check by changing the rate: a real signal does not move.
Why does 60 Hz appear as 10 Hz?
At a 50 Hz sample rate, a 60 Hz input folds to | 60 − 50 | = 10 Hz. Filter it out before the converter or sample faster.
Can I filter the aliasing out afterwards?
No. Once sampled, an alias is indistinguishable from a real low-frequency signal. The filter must act before the converter.
Is averaging the same as an anti-alias filter?
Only if the averaging is done on much faster samples (oversampling). Averaging a few slow samples does not stop high-frequency noise folding in.
References
- Oppenheim, A. V. and Schafer, R. W., Discrete-Time Signal Processing.
- Smith, S. W., The Scientist and Engineer’s Guide to Digital Signal Processing (free online).
- Analog Devices, Data Conversion Handbook and Analog Filter Design application notes; the SciPy
scipy.signaldocumentation.
Last reviewed: October 2026. Filter values are examples: compute your own corner frequency and check the stop-band attenuation.
