Sampling, Nyquist and Aliasing in Real Hardware
A switching regulator at an exact multiple of your sample rate arrives as a DC offset.
- Lesson 8 of 8
- Advanced
Your signal is below 1 kHz. You are sampling at 10 kSPS, which is ten times Nyquist, so you are comfortable. The switching regulator on the board runs at 2.1 MHz, which is two hundred times outside anything you care about.
It arrives in your measurement as a DC offset.
Quick answer. Sampling folds every frequency above half the sample rate down into the band below it. An interferer at frequency f appears at
|f − n·f_s|for whichever integer n brings it closest to zero. Nothing after the sampler can undo this, because by then the interferer and a real signal at the folded frequency are identical. The only defence is an analog filter in front of the converter.
Where a given interferer lands
The rule is arithmetic, not intuition. Divide the interfering frequency by the sample rate and look at the remainder:
f_alias = | f_interferer − n · f_s |Sampling at 10 kSPS, so Nyquist is 5 kHz:
| Interferer | Lands at | Comment |
|---|---|---|
| 3 kHz | 3 kHz | below Nyquist, not aliased |
| 7 kHz | 3 kHz | folds down, now indistinguishable from the above |
| 2.1003 MHz | 300 Hz | sits in the middle of your signal band |
| 2.1 MHz | 0 Hz | an exact multiple: arrives as a DC offset |
That last row is the one worth remembering. A regulator running at an exact integer multiple of the sample rate aliases to zero hertz, which means it does not look like interference at all. It looks like a calibration error. It is stable, it is repeatable, it changes if you change the sample rate, and it will survive every averaging scheme you try.
And a regulator running near an exact multiple aliases to a low frequency, which looks like drift. Three hundred hertz is in the signal band of the example; thirty millihertz would look like thermal drift over half a minute.
The filter has to be analog, and first order is usually not enough
Since aliasing happens at the sampler, the filter has to be before it. The question is how much attenuation it needs, and the answer is: enough to push the interferer below the converter's own noise floor, which for 12 bits is 74 dB down.
A single RC with its corner at 1 kHz falls at 20 dB per decade. From 1 kHz to 2.1 MHz is 3.32 decades:
attenuation = 20 · log10(2.1 MHz / 1 kHz) = 66.4 dBWhich is 7.6 dB short of what 12 bits deserves. A second-order filter gives 133 dB at the same frequency and is comfortably sufficient.
This is the honest reason anti-alias filters are usually second order or better, and it is a calculation rather than a tradition. Run it for your own numbers: corner frequency, interferer frequency, decades between them, 20 dB per decade per order.
Two caveats that keep the calculation honest:
- A real RC does not keep falling forever. Component parasitics flatten
the response at high frequency, so the attenuation at 2.1 MHz may be worse than the ideal slope suggests. For interferers far out, layout and the capacitor's own self-resonance matter more than the nominal corner.
- The filter at the ADC input is already spoken for. Lesson 6 sized that
RC from settling and DC error, and the corner it produced was 194 kHz. If you also need a 1 kHz anti-alias filter, it is a separate stage before the amplifier, not a change to the one at the pin.
Oversampling moves the problem, it does not remove it
Sampling faster pushes Nyquist up, which gives the analog filter more room to roll off before anything folds. That is genuinely useful and it is half of why delta-sigma converters are easier to filter in front of.
What it does not do is help with an interferer above the new Nyquist. At 10 kSPS the 2.1 MHz regulator folds to DC; at 100 kSPS it folds to 2.1 MHz − 21 × 100 kHz = 0 Hz as well, because 2.1 MHz is also an exact multiple of 100 kHz. Changing the rate changed nothing, which is a good illustration that the arithmetic deserves doing rather than guessing.
A rate of 90 kSPS puts it at 2.1 MHz − 23 × 90 kHz = 30 kHz, outside a 1 kHz signal band. Choosing a sample rate that is not harmonically related to the dominant interferer is a real and cheap technique.
Practical places aliasing comes from
- Switching regulators, especially ones sharing a supply or a ground
return with the analog section. Where that coupling comes from is the subject of the switch node lesson of the switching regulator course.
- Clocks and their harmonics, including the microcontroller's own.
- PWM outputs on the same board, particularly motor drive.
- Mains and its harmonics, which alias into low frequencies when the
sample rate is unrelated to 50 or 60 Hz.
- The sensor itself, if it has a mechanical resonance or an internal
oscillator.
What to do about it
- Work out the arithmetic for the interferers you actually have, rather than
filtering in general.
- Put the anti-alias filter where it can do its job, which is before the
sampler and usually before the gain stage, so the interference is attenuated before it is amplified.
- Choose a sample rate that is not an integer ratio of the dominant
interferer. This costs nothing.
- Use the converter's own filtering where it exists: a delta-sigma's
decimation filter attenuates a great deal before the final rate, which is why it needs a much gentler analog filter.
- Check by changing the sample rate. An error that moves when the rate changes
and stays when the signal is disconnected is almost always an alias.
What comes next
Lesson 9 takes the other timing problem: not when you sample relative to the signal, but how precisely you know when the sample happened.
Sources and further reading
- Texas Instruments: Understanding Data Converters (SLAA013) — application report, sampling, Nyquist and the folding of out-of-band energy.
- Texas Instruments: High-Speed, Analog-to-Digital Converter Basics (SLAA510) — application report, Nyquist zones and where a given input frequency appears.
- Microchip: AN246 Driving the Analog Inputs of a SAR A/D Converter — application note, the filter in front of a converter input and what it is really for.
- Texas Instruments: Digital Filter Types in Delta-Sigma ADCs (SBAA230) — application note, what the digital filter rejects and what it cannot, since aliasing happens before it.