Waves & Buckets

Sampling, bit depth, and reconstruction · an interactive tour of digital audio

Every sound played from a speaker has made the same round trip: a pressure wave became a list of numbers, was copied, streamed, and mixed as numbers, and became a pressure wave again. This course covers that pipeline in order: how often the wave is measured (sample rate), how precisely each measurement is recorded (bit depth), and how a DAC reconstructs the waveform on the way out. Most of the demonstrations here are audible at moderate volume, so the course is best taken with sound on.

Sound check: volume should start comfortably low, especially on headphones, because a few of the tones are deliberately piercing. Everything stays in the ordinary audible range (nothing above ~5 kHz, no ultrasonics). Every sound has a stop button, and switching demos stops the previous one automatically.

1 Sound, amplitude, and frequency

Sound is air pressure oscillating around its resting value. A microphone converts that pressure variation into a voltage that varies the same way, so in either form the signal is one continuous, smoothly changing quantity. The simplest sound, a pure tone, is described by two parameters: amplitude (the size of the swing, heard as loudness) and frequency (the number of swings per second, in hertz, heard as pitch).

x(t)=Asin(2πft)x(t) = A \sin(2\pi f t)

x(t)x(t) is the pressure (or voltage) at time tt; AA is the amplitude; ff is the frequency in Hz. The factor 2π2\pi converts cycles to radians: ftf\,t counts elapsed cycles, and sin\sin expects an angle parameterization ↗. The time one full cycle takes is the period, T=1/fT = 1/f; at 440 Hz each cycle lasts 1/4402.271/440 \approx 2.27 ms. The period appears as the readout beside the plot.

Drag the sliders and press play; both retune the live tone. Doubling the frequency, for instance from 220 Hz to 440 Hz, produces the same note one octave higher, because pitch perception is logarithmic in frequency. The slider's 50–1,000 Hz span is a small slice of human hearing, which covers roughly 20 Hz to 20,000 Hz.

220 Hz
0.70
4.55 period · ms per cycle
20 ms shown on screen

The slow on-screen drift is a visualization aid: a real 220 Hz wave completes 220 cycles every second, and the 20 ms window holds 4.4 of them at a time.

2 Sampling

A computer cannot store a continuous function; it stores a finite list of numbers. The wave's amplitude is therefore measured at regular instants. Each measurement is a sample, and the number of measurements per second is the sample rate fsf_s. A sample records the wave's instantaneous amplitude as a single number; frequency information exists only in how successive samples change.

x[n]=x ⁣(nfs)x[n] = x\!\left(\tfrac{n}{f_s}\right)

x(t)x(t) is the continuous signal, and x[n]x[n] is its nn-th sample, with square brackets marking the discrete sequence. n/fsn/f_s is the time of that sample, since consecutive samples are 1/fs1/f_s seconds apart.

Drag both sliders. With many samples per cycle the dots clearly trace the wave. As the rate drops toward 2 samples per cycle, the dots become too sparse to determine which wave produced them, and the readout turns red at that threshold. The next lesson examines exactly what goes wrong there.

3 Hz
24 /s
24 samples in this 1-second window
8.0 samples per cycle

Straight lines between the dots are not how playback rebuilds the wave; the toggle is included for comparison, and lesson 5 shows what actually happens between the samples. The numbers here are small only so the dots are countable: real audio runs about a thousand times faster, and CD quality takes 44,100 samples every second.

3 Nyquist and aliasing

The Nyquist–Shannon sampling theorem states the condition for sampling to be faithful: the sample rate must exceed twice the highest frequency present in the signal.

fs>2fmaxf_s > 2 f_{\max}

fsf_s is the sample rate, and fmaxf_{\max} is the highest frequency in the signal. Equivalently, every frequency component needs more than two samples per cycle. Half the sample rate, the highest representable frequency, is called the Nyquist frequency.

Violating the condition does not produce silence or noise. It produces a different, valid-looking signal. A sinusoid (lesson 1's pure tone under its formal name) above the Nyquist frequency yields exactly the same samples as some sinusoid below it, so after sampling the two are indistinguishable, and playback reproduces the lower one. This substitution is called aliasing. Film cameras have the same problem in time: a wagon wheel rotating faster than half the frame rate appears to spin slowly or backwards.

Real converters prevent aliasing with an anti-aliasing filter: a low-pass stage, a circuit that passes frequencies below its cutoff and rejects those above, placed in front of the sampler to remove everything above Nyquist. The filter must come before the sampler, because after sampling an alias is indistinguishable from a genuine low-frequency component, and no later processing can tell them apart. Real filters also cannot cut off abruptly: they need a transition band in which to roll off from passing to blocking. That margin is why CDs sample at 44,100/s: the gap between audible 20 kHz and Nyquist at 22,050 Hz holds the filter's transition band.

In the demo below the sampler is fixed at 4,000 samples/s, so Nyquist sits at 2,000 Hz. Start the digitized tone, then sweep the slider; the tones retune live. Past 2,000 Hz the input frequency keeps rising while the played-back frequency folds and descends.

1200 Hz
Nyquist limit: 2,000 Hz
1200 what went in · Hz
1200 what plays back · Hz

Past 2,000 Hz the pink dashed wave, the one sinusoid below Nyquist that passes through the same sample points, is what plays back. The played tone is synthesized at exactly that folded frequency; under the ideal pipeline described above, that is precisely what a real converter would output.

4 Bit depth and quantization

Sampling discretizes time; storing each sample discretizes amplitude. A bit is one two-way choice, so NN of them together can label 2N2^N distinct things. With NN bits per sample the amplitude range is divided into 2N2^{N} levels (“buckets”), and every reading is rounded to the nearest level. The rounding error, at most half a bucket, stays in the signal permanently and is heard as a low-level distortion called quantization noise. Each added bit doubles the level count, halves the maximum error, and improves the signal-to-noise ratio by about 6 dB decibels ↗:

SNR6.02N+1.76  dB\mathrm{SNR} \approx 6.02\,N + 1.76 \;\text{dB}

SNR is the power ratio between a full-scale sinusoid, one using the largest swing the NN-bit range can represent, and the quantization error, expressed in decibels. The 6.02 arises as follows: the error is approximately uniform over ±12\pm\tfrac12 step; each added bit halves the step and therefore halves the error amplitude; and halving an amplitude changes a level by 20log1026.0220\log_{10} 2 \approx 6.02 dB. The 1.76 dB constant comes from comparing a sinusoid's average power with uniform noise's average power.

The melody below loops continuously through a quantizer whose depth follows the slider; sweep the slider while the melody plays. At 12 bits the noise is inaudible; by 4 bits there is a clear hiss; at 1–2 bits the waveform is heavily distorted. Pitch and timing are unaffected throughout, because both are determined by the sample clock (lessons 2 and 3); quantization degrades only amplitude accuracy.

3 bits
8 buckets · 2N2^N
0.250 bucket size · of full range
19.8 SNR · dB

The top panel shows the wave snapped to bucket centers (the grid is drawn up to 32 buckets); the bottom panel shows the rounding error left behind, on a fixed scale, shrinking as the buckets multiply. CD's 16 bits give 65,536 buckets and an SNR near 98 dB; studios record at 24 bits to leave margin for later processing, then round once at the end.

5 ADC → DAC: the round trip

This lesson runs the whole pipeline. On the way in, the ADC (analog-to-digital converter) samples and quantizes, turning a waveform into a list of numbers. On the way out, the DAC (digital-to-analog converter) reconstructs: it holds each number steady for one sample period, producing a staircase voltage, and a reconstruction filter then removes the staircase's sharp edges, which in spectral terms means everything above Nyquist. The filter's output is not an arbitrary smoothing of the staircase; it is uniquely determined. A signal is band-limited when it contains no frequencies above some ceiling; lesson 3's condition guaranteed the original was band-limited below Nyquist, and the sampling theorem supplies the uniqueness that this lesson relies on: exactly one waveform band-limited below Nyquist passes through the stored samples, namely the original. That waveform is the filter's output. Written out, the ideal reconstruction is:

y(t)  =  nx[n]sinc ⁣(fstn)y(t) \;=\; \sum_n x[n]\,\cdot\, \mathrm{sinc}\!\big(f_s\,t - n\big)

x[n]x[n] are the stored samples, and sinc(u)=sin(πu)/(πu)\mathrm{sinc}(u) = \sin(\pi u)/(\pi u) is a pulse that equals 1 at u=0u = 0 and 0 at every other integer. The argument fstnf_s t - n measures how many sample periods separate time tt from sample nn. Each sample contributes one sinc pulse centered on itself, and the Σ\Sigma adds them, one term per stored sample, across all of them. At any sample instant every other pulse is zero, so the sum passes through each sample exactly, and between samples the pulses interpolate the unique band-limited curve. (This formula is the idealized version of what hold-then-filter approximates; real DACs adjust their filters to compensate for the staircase's slight droop.)

With generous rate and depth, the output panel below reproduces the input panel. Lower either slider and an earlier lesson's failure appears: too few samples produces the aliasing of lesson 3, and too-coarse levels produce the quantization noise of lesson 4.

32 /s
5 bits

The four panels show, left to right, the analog wave, the ADC's samples, the held and quantized staircase, and the reconstruction-filtered output. For content below Nyquist the round trip is exactly invertible, as the uniqueness argument above established. Bit depth is the one lossy step: the rounding error never leaves (lesson 4), but every added bit pushes it another 6 dB down. Everything in between (mixing, streaming, effects) operates on the numbers, where copies are exact.

6 Bitrate

The plain numbered samples this course has been producing are called PCM (pulse-code modulation). The storage and bandwidth cost of uncompressed PCM follows directly from the course's two parameters:

bitrate  =  fs×N×channels\text{bitrate} \;=\; f_s \times N \times \text{channels}

fsf_s is the sample rate in samples per second, NN is the bit depth in bits per sample, and channels counts the independent signals recorded together: one for mono, two (left and right) for stereo. The product is in bits per second, before any compression. The two parameters are independent: sample rate sets the frequency ceiling (lesson 3), and bit depth sets the signal-to-noise ratio (lesson 4).

For CD audio the formula gives 44,100 × 16 × 2 = 1,411,200 bits per second. Per minute that is 1,411,200 × 60 = 84,672,000 bits; eight bits make a byte, so dividing by 8 gives 10,584,000 bytes, about 10.6 MB of storage for every minute of stereo CD audio. Telephone audio (8,000 samples/s, 8 bits, mono) comes to 8,000 × 8 × 1 = 64,000 bits per second, roughly a twentieth of the CD rate; sampling at 8,000 samples/s caps speech below 4 kHz, which is why calls lack the high-frequency content that distinguishes “f” from “s”. (A real voice stacks many tones at once; that structure is the next course's subject.) Configure a format below and read off its cost.

44,100 /s
16 bits
stereo
3 min
1,411 kbps · uncompressed
10.6 MB per minute
31.8 MB for your 3-min song

These figures are for raw PCM. Real files are smaller: FLAC removes redundancy losslessly, reaching roughly half the PCM size, and MP3/AAC go further by discarding detail the ear cannot detect. The underlying ideas are covered in Bits & Surprise, lesson 5.

Final quiz

The quiz draws ten questions from a larger bank, so a retake produces a fresh set. Your best grade is saved with your course progress, and every answer comes with its reasoning.

0/10 answered