---
title: "Sensor lab: calibration, dynamics, bridges and thermistors"
heading: A least-squares calibration with its nonlinearity, a second-order system's step response and its parameters from a flush test, exact bridge outputs for quarter, half and full bridges, and a Steinhart–Hart thermistor fit against the simpler B model
eyebrow: S15 · Lesson 8 · Sensor lab
xp: 60
---

::: hook
Lessons 1 to 7 described sensors one formula at a time. Real instruments are characterized from data: a calibration run, a step test, a table of resistances. In this lab you'll write the calculations an instrument engineer runs on such data: fit a calibration line and quote its nonlinearity, find a catheter's natural frequency and damping from a flush test and check them against the step response they imply, compute exact bridge outputs, and fit a thermistor curve accurate to hundredths of a degree.
:::

::: play
- The curve is the step response of a second-order sensor, such as a catheter and transducer, with the natural frequency and damping ratio on the sliders.
- Light damping rings and overshoots; the successive peaks shrink by a fixed ratio, from which a flush test reads the damping.
- Damping near 0.7 gives the fastest response that barely overshoots.
:::

```sim plot
view: graph
x: t = 0..0.2
y: y = 0..2
slider: f = 5..100 = 20 log
slider: z = 0.05..0.95 = 0.2
plot: 1 - exp(-z*2*pi*f*t)*(cos(2*pi*f*sqrt(1 - z^2)*t) + z/sqrt(1 - z^2)*sin(2*pi*f*sqrt(1 - z^2)*t)) | label: step response
```

::: predict
## A flush test shows each peak 0.3 times the one before, 50 ms apart. A step of pressure applied to the same system would overshoot by about

- [ ] 0.3%
- [ ] 9%
- [x] 55%
- [ ] 100%

::: reveal
The peak ratio gives ζ = 0.188 (lesson 23's formula), and a step response overshoots by $e^{-\pi\zeta/\sqrt{1 - \zeta^2}}$ = 0.55: 55%. A flush test's successive peaks shrink by the square of that, 0.30, one full period apart. Task 2 confirms it by computing the step response itself.
:::
:::

::: nameit
## What the lab computes

- **Calibration**: the least-squares line through (input, output) pairs, $S = \sum(x - \bar x)(y - \bar y)/\sum(x - \bar x)^2$ and $b = \bar y - S\bar x$, and the nonlinearity: the largest deviation from the line as a percentage of the output span $S(x_\max - x_\min)$.
- **Second-order dynamics**: the underdamped step response $1 - e^{-\zeta\omega_nt}[\cos\omega_dt + \frac\zeta{\sqrt{1 - \zeta^2}}\sin\omega_dt]$ with $\omega_d = \omega_n\sqrt{1 - \zeta^2}$, and from a flush test's peak ratio d and damped frequency $f_d$: $\zeta = -\ln d/\sqrt{4\pi^2 + \ln^2d}$, $f_n = f_d/\sqrt{1 - \zeta^2}$.
- **Bridges**: with excitation V and fractional change x, a quarter bridge gives $V[(1 + x)/(2 + x) - \frac12]$, a half bridge with opposing arms $Vx/2$, and a full bridge with two pairs of opposing arms Vx.
- **Thermistors**: the Steinhart–Hart equation $1/T = a + b\ln R + c(\ln R)^3$ (T in kelvins), fitted exactly through three calibration points, against the two-parameter B model $1/T = 1/T_0 + \ln(R/R_0)/B$.
:::

::: check
kind: python

## Write `calibrate(xs, ys)` returning (sensitivity, offset, nonlinearity in percent of span).

```python starter
def calibrate(xs, ys):
    S = (ys[-1] - ys[0]) / (xs[-1] - xs[0])
    return S, ys[0], 0.0
```

```python solution
def calibrate(xs, ys):
    n = len(xs)
    mx, my = sum(xs) / n, sum(ys) / n
    S = sum((x - mx) * (y - my) for x, y in zip(xs, ys)) / sum((x - mx) ** 2 for x in xs)
    b = my - S * mx
    dev = max(abs(y - (b + S * x)) for x, y in zip(xs, ys))
    return S, b, dev / (S * (max(xs) - min(xs))) * 100
```

```python tests
S, b, nl = calibrate([0, 100, 200, 300], [0.02, 2.53, 5.01, 7.55])
assert abs(S - 0.02507) < 1e-9 and abs(b - 0.017) < 1e-9, "Lesson 1's transducer: 0.02507 mV/mmHg and 0.017 mV."
assert abs(nl - 0.2792) < 1e-3, "Its nonlinearity is 0.28% of span."
S2, b2, nl2 = calibrate([1, 2, 3], [2, 4, 6])
assert abs(S2 - 2) < 1e-12 and abs(b2) < 1e-12 and nl2 < 1e-9, "A perfect line has no nonlinearity."
```

::: right
Right. The endpoint line through the first and last points would quote a different, usually larger, nonlinearity; always say which line you used.
:::
:::

::: check
kind: python

## Write `step2(fn, z, t)`, the underdamped step response, and `flush_fit(d, fd)` returning (ζ, $f_n$) from a flush test's peak ratio and damped frequency.

```python starter
import math

def step2(fn, z, t):
    return 1 - math.exp(-2 * math.pi * fn * t)

def flush_fit(d, fd):
    return d, fd
```

```python solution
import math

def step2(fn, z, t):
    w = 2 * math.pi * fn
    wd = w * math.sqrt(1 - z * z)
    return 1 - math.exp(-z * w * t) * (math.cos(wd * t) + z / math.sqrt(1 - z * z) * math.sin(wd * t))

def flush_fit(d, fd):
    z = -math.log(d) / math.sqrt(4 * math.pi ** 2 + math.log(d) ** 2)
    return z, fd / math.sqrt(1 - z * z)
```

```python tests
import math
z, fn = flush_fit(0.3, 20)
assert abs(z - 0.18819) < 1e-4 and abs(fn - 20.364) < 1e-3, "Lesson 23's flush test: ζ = 0.188, fn = 20.4 Hz."
peak = max(step2(fn, z, k * 1e-5) for k in range(20000))
assert abs(peak - 1 - math.exp(-math.pi * z / math.sqrt(1 - z * z))) < 1e-4, "The step overshoots by 55%."
assert abs(step2(fn, z, 5.0) - 1) < 1e-6, "It settles to the final value."
```

::: right
Right. The flush test's peak ratio and the step's overshoot are two views of the same damping.
:::
:::

::: check
kind: python

## Write `bridge(V, x, kind)` for kind "quarter", "half" or "full", returning the exact output voltage.

```python starter
def bridge(V, x, kind):
    return V * x / 4
```

```python solution
def bridge(V, x, kind):
    if kind == "quarter":
        return V * ((1 + x) / (2 + x) - 0.5)
    if kind == "half":
        return V * x / 2
    return V * x
```

```python tests
assert abs(bridge(5, 0.004, "quarter") - 0.00499002) < 1e-8, "Quarter bridge: 4.990 mV, a little under Vx/4."
assert abs(bridge(5, 0.004, "half") - 0.01) < 1e-12 and abs(bridge(5, 0.004, "full") - 0.02) < 1e-12, "Half and full bridges are exactly linear."
q = bridge(1, 0.02, "quarter")
assert abs((0.02 / 4 - q) / q - 0.01) < 1e-9, "The quarter bridge's error is x/2: 1% at x = 0.02."
```

::: right
Right. Opposing arms cancel the quarter bridge's nonlinearity as well as doubling or quadrupling its output.
:::
:::

::: check
kind: python

## Write `sh_fit(points)` returning Steinhart–Hart's (a, b, c) through three (resistance, °C) points, and `sh_temp(R, abc)` returning °C.

```python starter
import math

def sh_fit(points):
    return (0.0, 0.0, 0.0)

def sh_temp(R, abc):
    return 25.0
```

```python solution
import math

def sh_fit(points):
    M = [[1.0, math.log(R), math.log(R) ** 3, 1 / (T + 273.15)] for R, T in points]
    for c in range(3):
        p = max(range(c, 3), key=lambda r: abs(M[r][c]))
        M[c], M[p] = M[p], M[c]
        for r in range(c + 1, 3):
            f = M[r][c] / M[c][c]
            for k in range(c, 4):
                M[r][k] -= f * M[c][k]
    x = [0.0] * 3
    for r in range(2, -1, -1):
        x[r] = (M[r][3] - sum(M[r][k] * x[k] for k in range(r + 1, 3))) / M[r][r]
    return tuple(x)

def sh_temp(R, abc):
    a, b, c = abc
    L = math.log(R)
    return 1 / (a + b * L + c * L ** 3) - 273.15
```

```python tests
import math
pts = [(32650, 0), (10000, 25), (3602, 50)]
abc = sh_fit(pts)
assert all(abs(sh_temp(R, abc) - T) < 1e-9 for R, T in pts), "The fit passes through all three points."
assert abs(sh_temp(6530, abc) - 35.003) < 0.001, "6530 Ω reads 35.00 °C."
B = math.log(32650 / 10000) / (1 / 273.15 - 1 / 298.15)
tB = 1 / (1 / 298.15 + math.log(6530 / 10000) / B) - 273.15
assert abs(tB - 35.163) < 0.002, "A B model fitted at 0 and 25 °C reads 35.16 °C: 0.16 °C off."
```

::: right
Right. The third term buys an order of magnitude in accuracy, enough for clinical thermometry.
:::
:::

::: explain
## Why does a least-squares calibration quote a smaller nonlinearity than a line drawn through the endpoints?

::: model
Nonlinearity is the largest deviation of the readings from a reference line, so it depends on which line is chosen. A line through the first and last calibration points matches those two exactly and leaves all the curvature as deviation at the points between, typically on one side. The least-squares line is free to move and tilt so that deviations fall on both sides and the sum of their squares is smallest, which roughly halves the largest one. Both are legitimate, but a specification must say which it used, and an instrument corrected by the least-squares line reads closest to the truth on average.
:::
:::
