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 , 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 milliseconds for a function that took 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 and 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
The counter reading for an event at true time is the number of complete ticks since the counter started: , where is rounded down to a whole number and is measured from the moment the counter read 0.
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.
The program converts the difference to time by multiplying it by .
Two practical rules follow.
Subtract raw counts, then convert once. Every conversion that rounds is a second quantization. A counter driven at 32,768 hertz (counts per second) has a tick of second, about 30.5 microseconds. Converting each reading to whole milliseconds before subtracting makes the millisecond the effective tick, so the interval error can approach 1 millisecond; subtracting the raw counts first keeps it below one 30.5-microsecond tick plus one final rounding.
Keep the other errors separate. An oscillator that runs at the wrong rate (clock drift §6.18), a counter that restarts from 0 between the two readings (counter wraparound §6.21), a delay between an event and the reading of the counter, and an offset between two different counters each add their own error.
Worked example
Let 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 | |
| B | 2.1 | 5.9 | 2 and 5 | 3.8 | 3 | |
| C | 4.2 | 4.5 | 4 and 4 | 0.3 | 0 |
In call A the start phase is 0.9 and the end phase 0.1, so the error is millisecond; in call B the phases are reversed and the error is millisecond. Calls A and B are both reported as 3 milliseconds, although their true durations differ by 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 ticks means a true duration strictly between and 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 , which has probability 0.2; otherwise it crosses 2 and is reported as 2 milliseconds. The average is milliseconds, exactly right. One reading has standard deviation §11.14 millisecond, so the mean of 1000 readings has a standard error §11.15, the standard deviation of that mean, of 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 of a later tick, crosses 2 boundaries and is reported as 2 milliseconds. The error is millisecond in every run, and the mean of 1000 runs is still 2 milliseconds.
Stated precisely
Let be the tick period, and let the counter read at true time . For a start event at time and an end event at time , let be the true interval and the measured interval. Write for the fractional part of , a number in ; then and are the phases of the two events. Substituting into gives Because is a whole number less than 1 away from , it equals or , where is 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 is the true tick period, since a rate error is a separate error.
Random phase. Let and . If the start phase is uniformly distributed on and independent of , the measured count is with probability and with probability . Its mean is , so on average the measurement has no bias, and its variance is square ticks, which is at most .
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 and the end error in , the interval error lies in . For one counter that rounds down, both errors lie in , which gives as above. If each endpoint is known only to within , for example because the two readings come from different counters, the interval error lies in , 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 , 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 , so it lies strictly between and , 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 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 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 , for instance because the readings come from different counters, the bound doubles to . 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).