Reconstruction and DACs: Zero-Order Hold and Sinc Droop
A waveform generator built on a microcontroller plays a sine from a table at . On the scope it looks like a staircase, the FFT shows extra tones at and only about to below the wanted one, and a tone comes out quieter than the one although both tables have the same amplitude. None of this is a bug: it is what a real digital-to-analog converter does. The ideal reconstruction of Reconstruction uses sinc interpolation, which no circuit can build. Real converters hold each sample for one period (a zero-order hold) and rely on an analog reconstruction filter afterwards. This lesson quantifies both effects and shows how to correct them.
The zero-order hold
A DAC that holds each value for produces : the ideal impulse train convolved with a pulse of width . The pulse's transform is the frequency response of the hold:
Three consequences:
- Sinc droop. Inside the band the output amplitude falls as : at , at , at the Nyquist frequency .
- Images. The sampled spectrum has copies at . The hold weights them by , so they shrink but do not vanish; the nulls fall exactly at multiples of .
- Delay. The linear phase is a pure delay of , the average lag of the staircase (the green curve). In a control loop it costs phase margin (Real-Time DSP).
The reconstruction filter after the DAC is the anti-alias filter's mirror image: pass , attenuate the first image at . The same order calculations apply (Anti-Alias Filters).
Worked example: an audio DAC at 48 kSa/s
Given: a DAC at with zero-order hold, playing a full-scale tone at .
Find: the droop at , and the level of the first image relative to the tone.
Solve: , , i.e. . The first image is at with weight , which is of the tone: only . The image is almost as loud as the tone; a sharp reconstruction filter between and (or digital oversampling before the DAC) is mandatory.
Check: the image and tone have the same numerator , so the ratio is simply .
import numpy as np
from scipy import signal
sincdb = lambda r: 20 * np.log10(np.sinc(r)) # np.sinc(x) = sin(pi x)/(pi x)
for r in (0.1, 0.25, 20 / 48, 0.5):
print(f"f/fs = {r:.4f}: ZOH gain {sincdb(r):6.2f} dB")
fs, f = 48e3, 20e3
print(f"image at {(fs - f) / 1e3:.0f} kHz relative to the tone: {sincdb((fs - f) / fs) - sincdb(f / fs):.2f} dBc")
# Inverse-sinc pre-compensation: a short linear-phase FIR that boosts the top of the band
numtaps = 15
freq = np.linspace(0, 1, 51) # fraction of Nyquist
gain = 1 / np.sinc(freq / 2) # 1/sinc(f/fs) over the whole band (freq/2 = f/fs)
h = signal.firwin2(numtaps, freq, gain)
w, H = signal.freqz(h, worN=2048, fs=1.0)
band = w <= 0.40
total = 20 * np.log10(np.abs(H[band]) * np.sinc(w[band]))
print(f"{numtaps}-tap compensator: overall response 0..0.40 fs within {total.min():+.2f} .. {total.max():+.2f} dB "
f"(ZOH alone: {sincdb(0.40):+.2f} dB at 0.40 fs)")f/fs = 0.1000: ZOH gain -0.14 dB
f/fs = 0.2500: ZOH gain -0.91 dB
f/fs = 0.4167: ZOH gain -2.64 dB
f/fs = 0.5000: ZOH gain -3.92 dB
image at 28 kHz relative to the tone: -2.92 dBc
15-tap compensator: overall response 0..0.40 fs within +0.03 .. +0.07 dB (ZOH alone: -2.42 dB at 0.40 fs)
The 15-tap filter holds the overall response within a tenth of a decibel where the hold alone loses . The compensator is applied to the samples before the DAC. The firwin2 design here is quick and approximate; the remez design of Equiripple FIR does the same job with controlled ripple. Many audio and RF DAC chips have such a filter built in.
Seeing the images
To look at the analog output in simulation, model it on a grid 32 times finer than the DAC clock: repeat each sample 32 times (the hold), then take an FFT.
import numpy as np
import matplotlib.pyplot as plt
fs, f0, up = 8000, 1000, 32 # DAC rate, tone, fine-grid factor
n = np.arange(800) # 0.1 s at 8 kSa/s
x = np.sin(2 * np.pi * f0 * n / fs)
y = np.repeat(x, up) # zero-order hold on the fine grid (256 kSa/s)
Y = np.abs(np.fft.rfft(y)) / len(y) * 2
fr = np.fft.rfftfreq(len(y), 1 / (fs * up))
ref = Y[np.argmin(abs(fr - f0))]
for fk in (1000, 7000, 9000, 15000, 17000):
k = np.argmin(abs(fr - fk))
pred = 20 * np.log10(abs(np.sinc(fk / fs) / np.sinc(f0 / fs)))
print(f"{fk / 1e3:4.0f} kHz: {20 * np.log10(Y[k] / ref):6.2f} dBc (sinc prediction {pred:6.2f} dBc)")
plt.figure(figsize=(7, 2.6))
plt.plot(fr / 1e3, 20 * np.log10(Y / ref + 1e-12))
plt.xlim(0, 40); plt.ylim(-60, 5)
plt.xlabel("f (kHz)"); plt.ylabel("dBc"); plt.title("ZOH output of a 1 kHz tone at 8 kSa/s")
plt.tight_layout(); plt.show() 1 kHz: 0.00 dBc (sinc prediction 0.00 dBc)
7 kHz: -16.89 dBc (sinc prediction -16.90 dBc)
9 kHz: -19.07 dBc (sinc prediction -19.08 dBc)
15 kHz: -23.47 dBc (sinc prediction -23.52 dBc)
17 kHz: -24.55 dBc (sinc prediction -24.61 dBc)
Practical DACs
- Architectures. Resistor-string and R-2R ladders (monotonic or fast), current-steering (high speed, RF), PWM plus a filter (cheap microcontrollers) and sigma-delta (audio, high resolution with heavy oversampling and a simple analog filter). Details belong to the embedded and chip design track; here the hold and filter model is what matters.
- Oversampling DACs. Interpolating by 4 or 8 before the converter (Multirate) moves the first image from to and makes the droop in the band tiny, so the analog filter becomes simple.
- Non-idealities. Glitches at major code transitions (worst at mid-scale in R-2R designs), finite settling time, output impedance, and clock jitter, which modulates the time each step occurs and adds noise proportional to the signal's slope.
- PWM as a DAC. The average of a PWM signal is the DAC value; the filter must suppress the PWM frequency itself, so the PWM rate is chosen far above the signal band.
Guided lab: a microcontroller DAC
Hand calculation. A table played at gives images at (, ) and (). The droop at is ; at it is (since ).
Simulation. The second program prints the image levels; change f0 to and see the droop and the images at and .
What you would measure. On a scope's FFT (Hann window, a few hundred hertz of resolution, Scope triggering and FFT): images within about of the predictions if the DAC settles quickly; extra harmonics from DAC non-linearity typically to down. Add an RC low-pass (, , ) and the image drops by about another () while the tone loses , so the image improves by about relative to the tone.
Hazards and safe setup. USB-powered board, the DAC output into a scope probe () or a high-impedance load only; do not drive a loudspeaker directly from a DAC pin.
Pitfalls
- Forgetting the sinc droop when a flat response to is required.
- Assuming images are "small": near they are almost as large as the tone.
- Forgetting the delay of the hold in a feedback loop.
- Blaming the DAC for harmonics that are really images (they move when changes; harmonics don't).
Practice
- What is the ZOH droop at ?
- A tone is played at . Where is the first image, and what is its level in dBc?
- Why does 4 times oversampling before the DAC relax the reconstruction filter?
Answer
- : .
- At ; ratio : .
- The first image moves from to , far above the band, so a low-order analog filter attenuates it enough, and the in-band droop is only .
Test yourself
The zero-order hold's gain at the Nyquist frequency is
- 0 dB
- -0.91 dB
- -3.92 dB
- dB
The images of a ZOH DAC have nulls at
- multiples of
- multiples of
- the tone frequency
- there are no nulls
The average delay of a zero-order hold is
- 0
An inverse-sinc compensator is placed
- before the DAC, on the samples
- after the reconstruction filter
- in the anti-alias filter of the ADC
- nowhere; it cannot be built