ENع
←DACs and reconstruction
Lesson 14 · Sampling and conversion

DACs and reconstruction

14 min10 steps5 questionsBeginner
About this lesson

A DAC (digital-to-analog converter) turns stored numbers back into a voltage. Follow one period of a 1000 Hz tone stored as 8 codes at 8000 samples a second: the DAC adds up the bit weights of each code, holds the voltage until the next one and so draws a staircase. The staircase's sharp corners add copies of the tone, called images, at 8 ± 1 kHz and higher. A low-pass reconstruction filter removes them and a smooth wave comes out. You will see why, compare it with the ideal sinc-pulse reconstruction, and check the images in Python with numpy. It follows "Inside an ADC" and uses the Circuits lesson on filters.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 14 · Interactive
01 / 10

A sound stored as numbers

Memory holds a 1 kHz tone as numbers: 8 samples per wave, one every 125 µs (fs = 8000 per second). A DAC, a digital-to-analog converter, turns numbers like these back into a voltage.

fs = 8000 samples per second
Ts = 1 / fs = 125 µs

one period of a 1000 Hz tone = 8 samples
code[n] = round(128 + 100·sin(2π·n/8))

 n        0   1   2   3   4   5   6   7
 code   128 199 228 199 128  57  28  57

8-bit codes: 0 .. 255, middle = 128
how the codes were made: see "Inside an ADC"
Loading figure…
or ← →
Read the lesson step by step
  1. A sound stored as numbers

    Memory holds a 1 kHz tone as numbers: 8 samples per wave, one every 125 µs (fs = 8000 per second). A DAC, a digital-to-analog converter, turns numbers like these back into a voltage.

    fs = 8000 samples per second
    Ts = 1 / fs = 125 µs
    
    one period of a 1000 Hz tone = 8 samples
    code[n] = round(128 + 100·sin(2π·n/8))
    
     n        0   1   2   3   4   5   6   7
     code   128 199 228 199 128  57  28  57
    
    8-bit codes: 0 .. 255, middle = 128
    how the codes were made: see "Inside an ADC"
  2. Each bit has a weight

    The DAC gives every bit a weight: 128, 64, 32 down to 1. It adds the weights of the 1-bits and multiplies by one step, 10 mV. Code 199 = 11000111 = 128 + 64 + 4 + 2 + 1, so 1.99 V.

    bit weights   128  64  32  16   8   4   2   1
    code 199  =    1   1   0   0   0   1   1   1
    
    128 + 64 + 4 + 2 + 1 = 199
    voltage = 199 × 10 mV = 1.99 V
    
    step (LSB) = 10 mV,  2⁸ = 256 codes
    full scale = 256 × 10 mV = 2.56 V
    (a binary-weighted DAC; real chips often
     use an R-2R resistor ladder for the weights)
  3. Hold each value: a staircase

    The DAC holds each voltage until the next code arrives, 125 µs later. This is called a zero-order hold. Code after code, the output becomes a staircase z(t) that follows the wave.

    zero-order hold (ZOH):
      z(t) = v[n]   for  n·Ts ≤ t < (n+1)·Ts
    
     t (µs)      code   z (V)
       0 .. 125    128   1.28
     125 .. 250    199   1.99
     250 .. 375    228   2.28
     375 .. 500    199   1.99
     ...
    
    flat between samples, a jump at each one
    motion in the figure: slowed down a lot
  4. Sharp corners, a little late

    Compare the staircase with the smooth wave through the samples. The stairs have sharp corners, and on average they run half a step, 62.5 µs, late. Sharp corners mean extra, higher tones are mixed in.

    each stair starts at its sample and lasts Ts,
    so its middle is Ts/2 later:
      delay = 125 µs / 2 = 62.5 µs
    
    a sudden jump cannot be made from slow
    waves alone: it needs fast (high-frequency)
    ones added in
    
    dashed: the smooth wave through the samples
  5. The steps add images

    A spectrum shows which tones a signal holds. The staircase holds the 1 kHz tone plus copies called images, at 8 ± 1 kHz, 16 ± 1 kHz and on: 7, 9, 15, 17 kHz. The 7 kHz image is 1/7 as big.

    tones in the staircase (amplitude, V)
      0 kHz   1.28   (the middle level)
      1 kHz   0.976   the tone we want
      7 kHz   0.139   = 0.976 / 7
      9 kHz   0.108   = 0.976 / 9
     15 kHz   0.065
     17 kHz   0.057
    
    images sit at k·fs ± f0
    the hold scales tone f by sinc(f/fs):
      1 kHz × 0.974 (−0.22 dB, a slight droop)
      zero at 8 kHz and 16 kHz
  6. A low-pass filter

    A reconstruction filter is a low-pass filter: it passes tones below its cutoff, 3.5 kHz here, and blocks tones above. The 1 kHz tone gets through; the 7 kHz image shrinks 16 times.

    4th-order Butterworth low-pass
    cutoff fc = 3500 Hz
    |H(f)| = 1 / √(1 + (f/fc)⁸)
    
      1 kHz   1.0000
      7 kHz   0.0624  (1/16)
      9 kHz   0.0229
    
    fc sits between the tone (1 kHz)
    and the first image (7 kHz)
    filters in depth: see Circuits, "Filters"
  7. Out comes a smooth wave

    After the filter the 7 kHz image is 0.0087 V, 0.89% of the tone. The staircase has become a smooth 1 kHz wave, and the speaker plays it. It comes out slightly late: the filter takes time too.

    after the filter (amplitude, V)
      1 kHz   0.976 × 1.0000 = 0.976
      7 kHz   0.139 × 0.0624 = 0.0087
      9 kHz   0.108 × 0.0229 = 0.0025
    
    delay at 1 kHz
      hold    62.5 µs
      filter  123 µs (its group delay)
    
    the y(t) curve is computed from the
    staircase's tones through |H| and its phase
  8. The ideal: one sinc pulse per sample

    A perfect low-pass filter, passing all below fs/2 = 4 kHz and nothing above, would rebuild the wave exactly. Each sample becomes a sinc pulse; the pulses add up to a smooth wave through every sample.

    sinc(u) = sin(πu) / (πu):  1 at u = 0,
    0 at every other whole number
    
    s(t) = Σ v[n]·sinc((t − n·Ts) / Ts)
    
    at t = n·Ts only pulse n is not zero,
    so s passes through every sample
    
    exact when every tone is below fs/2
    (the Nyquist rate lesson)
    real filters only come close,
    which is why fc sits well below 4 kHz
  9. Check it in Python

    numpy can find the images. np.repeat holds each voltage for 16 points: a zero-order hold. np.fft.rfft measures the tones: 0.98 V at 1 kHz, 0.14 V at 7 kHz and 0.11 V at 9 kHz.

    import numpy as np
    n = np.arange(8)
    codes = np.round(128 + 100*np.sin(2*np.pi*n/8))
    v = codes * 0.01
    held = np.repeat(v, 16)
    X = np.fft.rfft(held) / 64
    abs(X[[1, 7, 9]]).round(2)
    
    >>> codes.astype(int)
    array([128, 199, 228, 199, 128, 57, 28, 57])
    >>> abs(X[[1, 7, 9]]).round(2)
    array([0.98, 0.14, 0.11])
    
    128 points span 1 ms, so bin k = k kHz
    (Python 3 with numpy)
  10. Numbers back to sound

    The whole path: codes leave memory, the DAC turns each into a voltage and holds it, and the low-pass filter removes the images. Numbers become a smooth wave again, ready for a speaker.

    memory   codes, one every 125 µs
    DAC      code × 10 mV, held for Ts
             -> staircase + images at k·fs ± f0
    filter   low-pass, keeps below fc
             -> smooth wave
    speaker  turns voltage into sound
    
    sampling (ADC) and reconstruction (DAC)
    are the two halves of digital audio

Quiz Unlocks after the last step.