---
title: Converter lab
heading: ENOB, DNL and INL, and a delta-sigma modulator, in Python
eyebrow: S2 · Lesson 49 · Converter lab
xp: 60
---

::: hook
Data converters are specified by a handful of numbers (SINAD and ENOB, DNL and INL, a figure of merit) and each comes from a precise measurement procedure. This lab writes those procedures in Python: quantize a coherently sampled sine and compute its SINAD, turn a converter's measured transition levels into DNL and INL, and simulate a first-order delta-sigma modulator to see a one-bit stream average out to an arbitrary input.
:::

::: play
- The curve is the ideal SQNR of a one-bit delta-sigma modulator against oversampling ratio, for loop orders 1 to 4: $6.02 + 1.76 - 10\log_{10}\frac{\pi^{2L}}{2L + 1} + (2L + 1)10\log_{10}M$.
- Each doubling of M adds $(2L + 1) \times 3$ dB: 9 dB at first order, 15 at second.
- High-order loops reach 16 bits or more at modest oversampling, at the cost of stability problems the formula doesn't show.
:::

```sim plot
view: graph
x: M = 4..512 log
y: S = 0..160 dB
slider: L = 1..4 step 1 = 2
plot: 6.02 + 1.76 - 10*log(pi^(2*L)/(2*L + 1)) + (2*L + 1)*10*log(M) | label: SQNR
readout: SQNR at M of 64 (dB) = 6.02 + 1.76 - 10*log(pi^(2*L)/(2*L + 1)) + (2*L + 1)*10*log(64) | digits: 4
```

::: predict
## A 6-level converter's transition levels are ideally 1 LSB apart, but the fourth transition sits half an LSB late. What are the largest DNL and INL?

- [x] DNL +0.5 and −0.5 LSB on the codes either side; INL 0.5 LSB at that transition
- [ ] DNL 1 LSB; INL 0
- [ ] DNL 0; INL 0.5 LSB
- [ ] DNL 0.5 LSB on one code only

::: reveal
The code below the late transition is half an LSB wide too wide (DNL +0.5) and the code above it half an LSB too narrow (−0.5). INL, the transition's distance from the straight line through the endpoints, is +0.5 LSB there and zero elsewhere. A single misplaced transition always shows up as a +/− pair in DNL.
:::
:::

::: nameit
## The procedures

- **SINAD and ENOB:** sample a sine for a whole number of cycles that is coprime with the record length (coherent sampling), quantize, reconstruct, and compare the error's power with the signal's: $\text{ENOB} = (\text{SINAD} - 1.76)/6.02$.
- **DNL and INL:** from the transition levels $T_k$, take $\text{LSB} = (T_\text{last} - T_\text{first})/(\text{number of transitions} - 1)$; then $\text{DNL}_k = (T_{k+1} - T_k)/\text{LSB} - 1$ and $\text{INL}_k = (T_k - (T_\text{first} + k\,\text{LSB}))/\text{LSB}$ (endpoint fit).
- **First-order delta-sigma:** each step, output $y = \pm1$ by the sign of an integrator v, then add $x - y$ to v. The average of y equals x.
- **Walden figure of merit:** $P/(2^\text{ENOB}f_s)$.
:::

::: check
kind: python

## Write `sinad_enob(bits, n=4096, cycles=127)`: quantize $\sin(2\pi\cdot\text{cycles}\cdot k/n)$ to `bits` bits with a mid-rise quantizer (code = ⌊(v + 1)/2 · 2^bits⌋, clamped to the top code; reconstruct at code centers) and return (SINAD in dB, ENOB).

```python starter
import math

def sinad_enob(bits, n=4096, cycles=127):
    return (0.0, 0.0)
```

```python solution
import math

def sinad_enob(bits, n=4096, cycles=127):
    q = 2 ** bits
    s = [math.sin(2 * math.pi * cycles * k / n) for k in range(n)]
    codes = [min(math.floor((v + 1) / 2 * q), q - 1) for v in s]
    rec = [(c + 0.5) / q * 2 - 1 for c in codes]
    ps = sum(v * v for v in s) / n
    pe = sum((a - b) ** 2 for a, b in zip(s, rec)) / n
    sinad = 10 * math.log10(ps / pe)
    return (sinad, (sinad - 1.76) / 6.02)
```

```python tests
s8, e8 = sinad_enob(8)
assert abs(e8 - 8.0) < 0.05, "An ideal 8-bit quantizer: ENOB 8.0."
s12, e12 = sinad_enob(12)
assert abs(s12 - 74.0) < 0.1, "12 bits: 74 dB."
```

::: right
Right. With coherent sampling and enough codes the quantization error behaves like uniform noise, and the measurement lands on $6.02N + 1.76$.
:::
:::

::: check
kind: python

## Write `dnl_inl(edges)`: from a list of transition levels, return (DNL list, INL list) in LSB, with the endpoint-fit LSB.

```python starter
def dnl_inl(edges):
    return ([], [])
```

```python solution
def dnl_inl(edges):
    lsb = (edges[-1] - edges[0]) / (len(edges) - 1)
    dnl = [(edges[k + 1] - edges[k]) / lsb - 1 for k in range(len(edges) - 1)]
    inl = [(edges[k] - (edges[0] + k * lsb)) / lsb for k in range(len(edges))]
    return (dnl, inl)
```

```python tests
e = [float(k) for k in range(7)]
e[3] += 0.5
d, i = dnl_inl(e)
assert d == [0.0, 0.0, 0.5, -0.5, 0.0, 0.0], "The predict: +0.5 and −0.5 either side of the late transition."
assert max(abs(x) for x in i) == 0.5 and i[3] == 0.5, "INL 0.5 LSB at that transition."
d2, _ = dnl_inl([0, 1.2, 2.1, 3.0])
assert abs(sum(d2)) < 1e-12, "DNL always sums to zero over the range with an endpoint LSB."
```

::: right
Right. INL is the running sum of DNL, so one big DNL error shows up as a step in INL.
:::
:::

::: check
kind: python

## Write `sd1(x, n)`: run a first-order delta-sigma modulator for n steps with input x (|x| < 1) and return the average of its ±1 output.

```python starter
def sd1(x, n):
    return 0.0
```

```python solution
def sd1(x, n):
    v, total = 0.0, 0
    for _ in range(n):
        y = 1 if v >= 0 else -1
        v += x - y
        total += y
    return total / n
```

```python tests
assert abs(sd1(0.3, 10000) - 0.3) < 1e-3, "A one-bit stream averaging 0.3."
assert abs(sd1(-0.7, 10000) + 0.7) < 1e-3, "And −0.7."
assert abs(sd1(0.3, 100) - 0.3) < 0.03, "Even over 100 samples it's within a few percent: the error stays bounded in the integrator."
```

::: right
Right. The integrator holds the running error, so the average's error falls as 1/n: that's noise shaping at work.
:::
:::

::: check
kind: python

## Write `sd_sqnr(L, M)`: the ideal one-bit SQNR of an L-th order modulator at oversampling ratio M, and `walden(P, fs, enob)`.

```python starter
import math

def sd_sqnr(L, M):
    return 0.0

def walden(P, fs, enob):
    return 0.0
```

```python solution
import math

def sd_sqnr(L, M):
    return 6.02 + 1.76 - 10 * math.log10(math.pi ** (2 * L) / (2 * L + 1)) + (2 * L + 1) * 10 * math.log10(M)

def walden(P, fs, enob):
    return P / (2 ** enob * fs)
```

```python tests
assert abs(sd_sqnr(1, 128) - 65.82) < 0.01, "First order at 128: 65.8 dB."
assert abs(sd_sqnr(2, 64) - 85.19) < 0.01, "Second order at 64: 85.2 dB."
assert abs(walden(10e-3, 100e6, 9.5) * 1e15 - 138.1) < 0.1, "9.5 bits at 100 MS/s on 10 mW: 138 fJ per step."
```

::: right
Right. The Walden figure lets converters of very different speed and resolution be compared by energy per step.
:::
:::

::: explain
## Why does a one-bit delta-sigma modulator's output average converge to its input, even though each output is only +1 or −1?

::: model
The integrator accumulates the difference between the input and the output, and the comparator always chooses the output that pushes that accumulated difference back toward zero. So the integrator's value can never grow without bound: it stays within a fixed range. Since the integrator holds the sum of all past errors, the total error after n steps is bounded, and the error in the average is that bounded amount divided by n, which shrinks as more samples are averaged. The quantization error hasn't disappeared; it has been pushed to high frequencies, where the decimation filter removes it.
:::
:::
