Section 11.42

Calibration

Definition

Calibration is the procedure that measures reference quantities of known size with an instrument, establishes the relation between the known values and the instrument’s readings, and then uses that relation to convert later readings into corrected values with a stated uncertainty. An instrument here is anything that produces a reading, from a thermometer to a software timer. Calibration does not change the instrument. Changing the instrument so that its readings match the references is a separate step, called adjustment.

The idea

A program measures how long a function takes by reading a hardware timer before and after the call. The timer counts ticks, not microseconds, and its oscillator, the circuit that sets the tick rate, may run slightly fast. The code that reads the timer also takes time, so even an empty interval gives a reading above zero. A raw reading is therefore off in two ways: by a scale, the number of ticks per microsecond, and by an offset, the reading for an interval of zero length. Neither error shrinks when the measurement is repeated, because both recur identically every time.

Calibration finds both errors by measuring intervals whose true length is already known, such as pulses from a signal generator, an instrument that produces electrical signals of precisely set timing, which has itself been checked against a better reference. A kitchen scale checked with a 1 kilogram and a 2 kilogram weight is a fair picture. If it shows 1.05 and 2.05 kilograms, it reads 0.05 kilogram too high, and every later reading can be corrected by that amount. The picture stops being accurate in one respect: a correction holds only in conditions that match those in which it was found. The scale may read differently on a cold day, and a timer correction found in an empty loop may not hold inside a busy program.

How it works

  1. Choose references. Pick reference values of known size that span the range in which the instrument will be used. Each reference has its own uncertainty, which should be several times smaller than the uncertainty wanted for the corrected readings. Measurement science calls the ideal metrological traceability: each reference was itself calibrated against a better one, in an unbroken chain up to a national or international standard, each link with a stated uncertainty.

  2. Measure the references in the conditions of later use: the same temperature range, supply voltage, clock settings, software build and load.

  3. Fit a model that relates readings to true values, most simply a straight line with a scale and an offset. Use more than two references, so that the residuals, the differences between the readings and the fitted line, show whether a straight line fits.

  4. Invert the model to correct each later reading.

  5. State the uncertainty of each corrected value, combining the scatter of the new reading with the uncertainty of the fitted scale and offset.

  6. Recalibrate when conditions change, and at intervals set by how fast the instrument drifts.

In software timing, calibration usually takes the form of a null measurement. A probe §14.26 is a piece of code inserted into a program to record an observation, such as a timestamp. A null probe has the same entry, exit and timer reads as a real probe but an empty body. Its reading estimates the fixed cost of the measuring code, part of the instrumentation overhead §11.32, which is the time and other resources that measuring code adds to the program it measures. That fixed cost is then subtracted from real readings. The subtraction is valid only for the costs the null probe shares with the real probe. A check that needs no null probe runs the workload with 0, 1, 2 and 4 copies of the real probe on the measured path: if the total time grows in a straight line with the number of probes, the slope is the cost of one real probe in its real surroundings.

Worked example

A device timer is checked against a signal generator that produces pulses of known width, 100 microseconds and 1,100 microseconds, with an uncertainty far below a microsecond. The device times each pulse with its own start and stop code and reads 3,212 ticks and 35,212 ticks.

So each reading equals 32 ticks per true microsecond plus 12 ticks. The fitted line predicts 12 ticks at zero duration. That intercept can include start/stop overhead and a fixed delay in detecting the reference edges. An independent measurement is needed to separate those contributions.

A later reading of 16,012 ticks corrects to (16,012−12)/32=16,000/32=500(16{,}012 - 12) / 32 = 16{,}000 / 32 = 500 microseconds. Ignoring the offset would give 16,012/32=500.37516{,}012 / 32 = 500.375 microseconds, an error of 0.375 microseconds, which matters when the intervals of interest are a few microseconds long.

Now probes are placed inside the measured code. A null probe is measured by putting 0 and then 4 copies on the path: the readings are 16,012 and 16,060 ticks, so one null probe costs (16,060−16,012)/4=48/4=12(16{,}060 - 16{,}012) / 4 = 48 / 4 = 12 ticks. A real probe also copies a 16-byte record into a log buffer while holding a mutex §9.5, a lock that lets only one thread (one sequence of execution in a program) use the buffer at a time. With 0 and 4 real probes the readings are 16,012 and 16,172 ticks, so one real probe costs (16,172−16,012)/4=160/4=40(16{,}172 - 16{,}012) / 4 = 160 / 4 = 40 ticks, that is 40/32=1.2540 / 32 = 1.25 microseconds. Subtracting the null-probe cost alone would leave 40−12=2840 - 12 = 28 ticks per probe, 28/32=0.87528 / 32 = 0.875 microseconds, uncorrected in every measurement, and more whenever another thread holds the lock.

Stated precisely

Let XX be the true value of the measured quantity, YY the instrument’s reading, aa the scale (reading units per unit of XX), bb the offset (the reading when X=0X = 0) and ε\varepsilon (epsilon) the random part of the reading, with mean zero. The linear calibration model is Y=aX+b+ε.Y = aX + b + \varepsilon. From two references X1≠X2X_1 \ne X_2 with readings Y1Y_1 and Y2Y_2, ignoring ε\varepsilon, a=(Y2−Y1)/(X2−X1)a = (Y_2 - Y_1)/(X_2 - X_1) and b=Y1−aX1b = Y_1 - aX_1. With more references, aa and bb are usually fitted by least squares: the values that make the sum of the squared residuals smallest. A later reading YY is corrected to X̂=Y−ba,a≠0,\hat{X} = \frac{Y - b}{a}, \qquad a \ne 0, where X̂\hat{X} (X-hat) is the corrected value.

Write u(⋅)u(\cdot) for a standard uncertainty §11.33, an uncertainty expressed as a standard deviation §11.14, and u(a,b)u(a, b) for the covariance §15.44 of the fitted aa and bb, a measure of how their errors vary together. The two are correlated because they come from the same references. If the new reading is independent of the calibration, the law of propagation of uncertainty stated in NIST Technical Note 1297 gives, to first order, u2(X̂)=u2(Y)+u2(b)+X̂2u2(a)+2X̂u(a,b)a2.u^2(\hat{X}) = \frac{u^2(Y) + u^2(b) + \hat{X}^2\, u^2(a) + 2\hat{X}\, u(a, b)}{a^2}.

The correction is valid under three assumptions.

  1. Model: the response is linear over the range used, which the residuals of a fit to more than two references can test.

  2. Stability: aa and bb have not changed since the calibration.

  3. Transfer: the conditions of use match the conditions of calibration in every factor that affects aa or bb.

Limits and common mistakes

Calibration removes a systematic error, one that repeats the same way every time. It does not reduce the random scatter of a single reading; averaging repeated readings does that. The entry on resolution versus precision versus accuracy §11.34 separates these properties.

No number of repetitions repairs a wrong reference or a wrong model. If a reference pulse is in truth 1% longer than its stated width, the fitted scale is off by about 1%, and so is every corrected value, however many readings are averaged.

Two references always fit a straight line exactly, so they cannot reveal a curved response. Outside the calibrated range the model is an extrapolation that the calibration does not support.

Calibrations go stale. A change of temperature, supply voltage, clock frequency, firmware build or compiler setting can change the scale or the offset, and clock drift §6.18, the slow change of an oscillator’s frequency, changes the scale over time.

A compiler optimization §3.39 can remove a null probe whose body does nothing, and the null measurement then reports zero. Check the generated machine code, or give the empty body an effect the compiler must keep.

Calibration differs from verification, which checks that an instrument meets a stated requirement, for example an error within 0.1%, and produces no correction.

Questions answered

Does a null probe capture the same overhead as a full probe?

No, not in general: a null probe captures only the part of the overhead that it shares with the full probe. A probe is code inserted into a program to record an observation. A null probe keeps the probe’s entry, exit and timer reads but has an empty body, so its measured cost is the fixed cost of the probe mechanism. A full probe also does its real work, which brings costs the null probe never has:

Use the null-probe cost as a calibration of the fixed part only. To measure the whole cost, place 0, 1, 2 and 4 copies of the full probe on the measured path under the real workload and fit a straight line to the total time; the slope is the cost of one full probe there. If a null probe costs 12 timer ticks and the slope shows 40, subtracting the null cost alone leaves 28 ticks per probe in every measurement.

Related entries: Instrumentation overhead (§11.32), Measurement uncertainty (§11.33), Resolution versus precision versus accuracy (§11.34), Probe and hook (§14.26), Clock source and drift (§6.18). Further reading: [77].