From Signals to Bits: Sampling and Quantization
An ADC turns a voltage into numbers by measuring it at regular instants and rounding each measurement to one of levels. Sample faster than twice the highest frequency and you lose nothing; each extra bit halves the rounding error, which buys about 6 dB of signal-to-noise ratio.
What the symbols mean
- Sample rate
Samples per second.
- Highest frequency
The fastest component in the signal.
What the symbols mean
- Step size
The voltage between neighboring codes: one LSB.
- Reference voltage
The ADC's full-scale range.
- Resolution
Bits per sample.
What the symbols mean
- Duty cycle
The fraction of each period the pin is high.
Explanation
Sampling means measuring a signal at regular moments, like taking snapshots, and quantization means rounding each measurement to the nearest rung on a fixed ladder of levels. The name quantization comes from "quantum," a fixed amount that can't be split: every reading has to be a whole number of steps. Together they're how a sensor's smoothly changing voltage becomes the list of numbers a processor can work with. The formulas look like a lot, but they're simple pieces: (take snapshots more than twice as fast as the fastest wiggle), (the step is the full range cut into equal pieces) and dB (each extra bit makes the result about 6 dB cleaner). It's really just "how often" and "how finely"; bear with it.
An analog-to-digital converter (ADC) does both jobs: it samples the signal at regular instants, and it quantizes each sample to one of levels.
Sampling and aliasing. Here's the catch with snapshots: between them, the ADC sees nothing. Sampling at rate records . A cosine at frequency and one at give the same samples for every integer :
So the samples only know mod , the same modular arithmetic as before, and since cosine is even, looks like too. Every frequency folds into the band from 0 to , the Nyquist frequency, and a tone above it shows up as a false, lower alias. That's the wagon wheel that spins backward on film, which samples at 24 frames per second.
The sampling theorem (Nyquist–Shannon) is the good news: if the signal has no frequencies at or above , the samples determine it completely. CD audio samples at 44.1 kHz to capture everything up to 20 kHz.
A common mistake. It's tempting to think you can clean up an alias in software after sampling, the way you'd filter any other noise. You can't: once sampled, an alias can't be told apart from a real signal at that lower frequency. That's why an analog low-pass anti-aliasing filter removes the high frequencies before the ADC ever sees them.
Quantization. An -bit ADC with reference splits its range into steps of , one LSB. A 12-bit ADC at 3.3 V has mV, and firmware turns a code back into volts with . Rounding makes an error between and that behaves like noise spread evenly over that interval. Its mean square is the average of :
Comparing a full-scale sine wave's RMS with the noise RMS gives dB: each extra bit halves the noise and buys about 6 dB. 16-bit audio reaches about 98 dB.
uint16_t code = adc_read(); // 0 to 4095 on a 12-bit ADC
float volts = code * (3.3f / 4096); // one step is about 0.81 mV
PWM goes the other way. A digital pin can only be high or low, but switching it fast, high for a fraction of each period (the duty cycle), gives an average of
A motor's inertia, your eye watching an LED, or an RC low-pass filter smooths the pulses into that average. A timer with an -bit counter sets in steps of at a PWM frequency of , so more resolution means a lower frequency.
See it
Worked examples
A 12-bit ADC with a 3.3 V reference reports code 2048. What voltage is that?
- 1
mV.
A 7 kHz tone is sampled at 10 kHz. What frequency appears in the samples?
- 1
The Nyquist frequency is 5 kHz, and 7 kHz is above it.
Practice
Type your answer and check it. Solve them all to complete the lesson.
CD audio is sampled 44,100 times per second. What's the highest frequency it can capture, in Hz?
A 9 kHz tone is sampled at 12 kHz. At what frequency does it show up in the samples, in kHz?
A 10-bit ADC has a 5 V reference. What is its step size (one LSB), in millivolts?
A 12-bit ADC with a 3.3 V reference converts with code . What code does it report for an input of 1.00 V?
How many bits does an ideal ADC need for a signal-to-noise ratio of at least 80 dB on a full-scale sine wave?
A 5 V microcontroller dims an LED with PWM. Its 8-bit timer holds the pin high for out of every 256 counts. Which gives an average of 2 V?
A timer runs on a 16 MHz clock through a prescaler that divides it by 64, and counts from 0 to 255 for each PWM period. What's the PWM frequency, in Hz?
An accelerometer on a drone frame picks up a 180 Hz vibration from the motors, and the firmware samples it at 200 Hz with no anti-aliasing filter. At what frequency does the vibration appear in the data, in Hz?
The hardest one here. Get it, and you've got this lesson.
An ideal 12-bit ADC digitizes a full-scale sine wave that swings across its whole range of steps of size . Model the rounding error as uniform on . Find the signal-to-noise ratio in dB from scratch: get the noise RMS from an integral and the sine's RMS from its amplitude, without using the 6.02 rule.
Where it's used
Every microphone, accelerometer and temperature sensor on a microcontroller goes through an ADC, so firmware engineers pick a sample rate (Nyquist) and a resolution (6 dB per bit) for each one. PWM runs the other way, dimming LEDs, driving motors and making sound from pins that can only be on or off.