Section 11.35

Counter quantization

Definition

Counter quantization is the rounding that happens when a digital counter measures time. The counter increases by one at every tick, a fixed period hh, so it records the time of an event only as the number of whole ticks that have passed. An interval computed by subtracting two readings of the same counter is therefore a whole number of ticks, and it differs from the true interval by less than one tick in either direction. What happened between ticks is lost, and printing more decimal places cannot recover it.

The idea

A program times a function with a counter that ticks once per millisecond. The function runs from 2.9 to 5.1 milliseconds, true times that the program never sees. The counter reads 2 at the start and 5 at the end, so the program reports 5−2=35 - 2 = 3 milliseconds for a function that took 5.1−2.9=2.25.1 - 2.9 = 2.2 milliseconds. A call from 2.1 to 5.9 milliseconds, which takes 3.8 milliseconds, is also reported as 3. One reported value stands for a whole range of true durations.

The key quantity is the phase of an event: the fraction of a tick that has passed since the counter last increased. A reading drops the phase. The start reading drops the start phase, which makes the reported interval longer, and the end reading drops the end phase, which makes it shorter. Both phases lie between 0 and 1 tick, so the interval error, start phase minus end phase, always lies strictly between −1-1 and +1+1 tick. The two rounding errors partly cancel because both readings are rounded down by the same rule.

Whether the error averages away depends on the phase. When the start phase varies at random from run to run, the reported count is sometimes the true duration rounded up and sometimes rounded down, in just the proportions that make the long-run average correct. When the start phase is the same in every run, for example because the measured work is started by the timer that drives the counter, the error is the same in every run, and no number of repeats removes it.

How it works

  1. The counter reading for an event at true time tt is the number of complete ticks since the counter started: c(t)=⌊t/h⌋c(t) = \lfloor t/h \rfloor, where ⌊x⌋\lfloor x \rfloor is xx rounded down to a whole number and tt is measured from the moment the counter read 0.

  2. The program reads the counter at the start event and at the end event and subtracts the first reading from the second. The difference is a whole number of ticks.

  3. The program converts the difference to time by multiplying it by hh.

Two practical rules follow.

Worked example

Let h=1h = 1 millisecond. The table gives true times and readings for three calls; times, intervals and errors are in milliseconds, and the error is the reported interval minus the true interval.

Call start end readings true interval reported error
A 2.9 5.1 2 and 5 2.2 3 +0.8+0.8
B 2.1 5.9 2 and 5 3.8 3 −0.8-0.8
C 4.2 4.5 4 and 4 0.3 0 −0.3-0.3

In call A the start phase is 0.9 and the end phase 0.1, so the error is (0.9−0.1)×1=+0.8(0.9 - 0.1) \times 1 = +0.8 millisecond; in call B the phases are reversed and the error is −0.8-0.8 millisecond. Calls A and B are both reported as 3 milliseconds, although their true durations differ by 3.8−2.2=1.63.8 - 2.2 = 1.6 milliseconds. Call C falls inside one tick and is reported as 0: a reported 0 means less than one tick, not no time. In general, a reported count of mm ticks means a true duration strictly between m−1m - 1 and m+1m + 1 ticks.

Random phase. Repeat an operation of exactly 2.2 milliseconds many times, with a start phase equally likely to take any value between 0 and 1 tick. The operation crosses 3 tick boundaries, and is reported as 3 milliseconds, when its start phase is at least 1−0.2=0.81 - 0.2 = 0.8, which has probability 0.2; otherwise it crosses 2 and is reported as 2 milliseconds. The average is 3×0.2+2×0.8=0.6+1.6=2.23 \times 0.2 + 2 \times 0.8 = 0.6 + 1.6 = 2.2 milliseconds, exactly right. One reading has standard deviation §11.14 0.2×0.8=0.4\sqrt{0.2 \times 0.8} = 0.4 millisecond, so the mean of 1000 readings has a standard error §11.15, the standard deviation of that mean, of 0.4/1000≈0.01260.4/\sqrt{1000} \approx 0.0126 millisecond: about 12.6 microseconds, far below the tick.

Fixed phase. If instead every run starts 0.05 millisecond after a tick, because the tick itself starts the operation, every run ends at phase 0.05+0.2=0.250.05 + 0.2 = 0.25 of a later tick, crosses 2 boundaries and is reported as 2 milliseconds. The error is 2−2.2=−0.22 - 2.2 = -0.2 millisecond in every run, and the mean of 1000 runs is still 2 milliseconds.

Stated precisely

Let h>0h > 0 be the tick period, and let the counter read c(t)=⌊t/h⌋c(t) = \lfloor t/h \rfloor at true time tt. For a start event at time ss and an end event at time f≥sf \ge s, let d=f−sd = f - s be the true interval and d̂=h(c(f)−c(s))\hat d = h\,(c(f) - c(s)) the measured interval. Write {x}=x−⌊x⌋\{x\} = x - \lfloor x \rfloor for the fractional part of xx, a number in [0,1)[0, 1); then {s/h}\{s/h\} and {f/h}\{f/h\} are the phases of the two events. Substituting ⌊x⌋=x−{x}\lfloor x \rfloor = x - \{x\} into d̂\hat d gives d̂−d=h({s/h}−{f/h}),so−h<d̂−d<h.\hat d - d = h\,(\{s/h\} - \{f/h\}), \qquad \text{so} \qquad -h < \hat d - d < h. Because c(f)−c(s)c(f) - c(s) is a whole number less than 1 away from d/hd/h, it equals ⌊d/h⌋\lfloor d/h \rfloor or ⌈d/h⌉\lceil d/h \rceil, where ⌈x⌉\lceil x \rceil is xx rounded up to a whole number. A counter whose readings are rounded to the nearest tick instead of down obeys the same bound, by the same argument.

The bound rests on four assumptions: both readings come from the same counter, with the same tick period and starting point; each reading is taken at the moment of its event, or with the same delay at both ends; the counter does not wrap around to 0 between the readings, or the subtraction uses unsigned modular arithmetic §4.16, which is correct for any interval shorter than one full counter period; and hh is the true tick period, since a rate error is a separate error.

Random phase. Let k=⌊d/h⌋k = \lfloor d/h \rfloor and r={d/h}r = \{d/h\}. If the start phase {s/h}\{s/h\} is uniformly distributed on [0,1)[0, 1) and independent of dd, the measured count c(f)−c(s)c(f) - c(s) is k+1k + 1 with probability rr and kk with probability 1−r1 - r. Its mean is k+r=d/hk + r = d/h, so on average the measurement has no bias, and its variance is r(1−r)r(1 - r) square ticks, which is at most 1/41/4.

Endpoints with separate error bounds. Let an endpoint error be the reading minus the true time. If the start error is known only to lie in [as,bs][a_s, b_s] and the end error in [af,bf][a_f, b_f], the interval error lies in [af−bs,bf−as][a_f - b_s,\ b_f - a_s]. For one counter that rounds down, both errors lie in (−h,0](-h, 0], which gives (−h,h)(-h, h) as above. If each endpoint is known only to within ±h\pm h, for example because the two readings come from different counters, the interval error lies in [−2h,2h][-2h, 2h], a range twice as wide.

Limits and common mistakes

Quantization error is not automatically random noise. Treating it as independent from reading to reading and uniformly spread over one tick is correct only when the phases of the events are spread evenly and independently over the tick. Periodic work, or work started by the timer that drives the counter, has a fixed phase and therefore a fixed error.

A reported 0 means less than one tick, not zero time. Converting a count to nanoseconds and printing many decimal places adds no information below one tick.

The same-counter bound of less than one tick does not apply to readings from two different counters, for which the bound is two ticks.

The relative error is large for short intervals. An interval of 3 ticks can be off by almost 1 tick, about 33%, while an interval of 1000 ticks is off by less than 0.1%. Short intervals need a counter with a much shorter tick, such as a processor’s cycle counter, which advances once per clock cycle.

Quantization is one error among several. Rate error, wraparound, the delay between an event and the counter read, and the time the reading itself takes (instrumentation overhead §11.32) each need their own term.

Questions answered

What error arises when subtracting two separately quantized endpoints?

Subtracting two quantized endpoints gives an interval whose error is the end reading’s rounding error minus the start reading’s rounding error. When both endpoints come from the same counter, which increases by one every tick of length hh, each reading is rounded down by the fraction of a tick that had passed since the last increase, called the phase of the event. The interval error is h×(start phase−end phase)h \times (\text{start phase} - \text{end phase}), so it lies strictly between −h-h and +h+h, and the measured number of ticks is the true duration in ticks rounded either down or up. With a 1-millisecond tick, events at 2.9 and 5.1 milliseconds read 2 and 5, so a 2.2-millisecond interval is reported as 3 milliseconds, an error of +0.8+0.8 millisecond; events at 2.1 and 5.9 milliseconds also read 2 and 5, so a 3.8-millisecond interval is also reported as 3 milliseconds, an error of −0.8-0.8 millisecond.

The two errors partly cancel because both readings are rounded by the same rule on the same counter. If each endpoint is known only to within ±h\pm h, for instance because the readings come from different counters, the bound doubles to ±2h\pm 2h. Averaging many measurements removes the error only when the phases vary at random from run to run; with a fixed phase the error is systematic. Rate error, counter wraparound and delays in reading the counter are separate errors that add to this one.

Related entries: Measurement uncertainty (§11.33), Resolution versus precision versus accuracy (§11.34), Monotonic clock (§9.35), Counter wraparound (§6.21), DWT cycle counter (§6.20), Binary floating-point representation, special values, and rounding (§4.49).