Terminology (5)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Integration
- Adding up a rate over time to get a total: speed over time gives distance.
- Euler method Also left rule
- Holding each sampled value until the next sample, then multiplying by the elapsed time.
- Trapezoid rule
- Using the average of the values at both ends of each interval.
- Timestep
- The time between samples or updates; larger steps make the approximation coarser.
- Moving time
- Here, the total duration of retained intervals whose average speed reaches the configured minimum; separate from elapsed time.
Why this matters
Distance from speed, battery charge from current, data used from bandwidth and position in an animation are all a rate added up over time. The arithmetic is short, and the mistakes come from the clock: assuming updates arrive on schedule, using one end of each interval, and integrating across time with no data.
This lesson assumes multiplication, averages and units of speed.
Accumulating a rate over time
Integration adds a rate over elapsed time: speed in metres per second multiplied by seconds contributes metres. With sampled speeds, each interval needs an assumption about the speed between its endpoints. Holding the first value gives a rectangle; averaging both endpoints gives a trapezoid.
The samples do not establish what happened during a long gap. Combining an integration rule with a dropout policy and a moving-time definition determines which distance and time enter the displayed average. Those choices remain part of the estimate even when the arithmetic is correct.
01Distance is speed times elapsed time
Distance is speed times elapsed time
Your cycling app records rides from a Bluetooth wheel-speed sensor. Riders report distances that are too short after the sensor drops out, a slight shortfall on every climb out of a junction, and an average speed that counts the minutes spent waiting at red lights.
The sensor is meant to report about once a second. One ride’s first readings were 4.0, 4.5 and 5.0 m/s at 0, 1 and 2 s, then nothing until 5.5 m/s at 5 s and 6.0 at 6 and 7 s. Adding the readings, as if each were one second, gives 25.0 m. Multiplying each speed by the time until the next reading gives 35.0 m: the estimate holds 5.0 m/s for three seconds. The final reading has no following interval, so it contributes no area.
Animations have the same bug when they move a fixed number of pixels per frame: on a 120 Hz display they run twice as fast as on a 60 Hz one.
Sensor events carry their own timestamps; in the browser, requestAnimationFrame passes a high-resolution time in milliseconds, so divide differences by 1000.
02Use both ends of each interval
Use both ends of each interval
Holding each speed until the next reading is the left rule, or Euler’s method. While the rider speeds up, the speed at the start of an interval is the lowest in it, so the left rule falls short. The trapezoid rule uses the average of both ends: 36.5 m for the same readings instead of 35.0. For the three-second interval alone, left gives 5 × 3 = 15 m and trapezoid gives (5 + 5.5)/2 × 3 = 15.75 m. Units are (m/s) × s = m. These are interpolation assumptions about what happened between samples.
03Smaller steps, smaller error
Smaller steps, smaller error
Predict both lines.
// A rider speeding up evenly, 2 m/s every second, for 4 seconds: the true distance is 16 m.
const speed = (t: number) => 2 * t;
for (const dt of [1, 0.25]) {
let left = 0;
let trap = 0;
for (let t = 0; t < 4; t += dt) {
left += speed(t) * dt;
trap += ((speed(t) + speed(t + dt)) / 2) * dt;
}
console.log(`dt=${dt} left ${left.toFixed(2)} trapezoid ${trap.toFixed(2)}`);
}
With readings every second the left rule is 4 m short of the true 16 m; every quarter second it is 1 m short. Its error shrinks in proportion to the step. The trapezoid is exact here because speed grows in a straight line, and close for smooth changes.
04Dropouts and moving time
Dropouts and moving time
| Measure | Trapezoid over everything | With dropouts and moving time |
|---|---|---|
| Distance | 34.70 m | 22.70 m |
| Time | 48.00 s elapsed | 7.00 s moving |
| Average speed | 2.60 km/h | 11.67 km/h |
The full (time seconds, speed m/s) trace is (0,0), (1,2.4), (2,4), (3,5), (4,5), (5,0.5), (6,0), (46,0.6), (47,3), (48,5). This sensor goes quiet while stopped. Between 0 m/s at 6 s and 0.6 m/s at 46 s the trapezoid invents 12 m. Skipping intervals longer than a limit adds neither distance nor time for them, and an interval whose average speed is below the threshold adds distance but not moving time. This app defines its displayed rate as all retained distance divided by moving time, then × 3.6 to convert m/s to km/h. That is a product statistic: it can include subthreshold distance and is not the mean speed restricted to moving intervals. Threshold crossing inside an interval is not located by this model.
05What the samples cannot tell you
What the samples cannot tell you
06Fix the ride summary
Fix the ride summary
Repair the ride recorder’s interval estimates, then its dropout and moving-time policy. A longer silent interval must trigger a different decision from a short interval with evidence at both ends.
A cycling app’s ride summary
Integrate speed over time, use the trapezoid rule, then report moving time.
In the frontend The ride summary card in Next.js and Svelte Optional
The summary card shows moving time beside elapsed time and says when dropouts were left out, so a short distance does not look like a bug.
// The ride summary card: show moving time and the average over it, and say when the sensor dropped out.
type Summary = { distance: number; moving: number; elapsed: number; dropouts: number };
export function RideSummary({ s }: { s: Summary }) {
const kmh = s.moving > 0 ? ((s.distance / s.moving) * 3.6).toFixed(1) : "n/a";
return (
<dl>
<dt>Distance</dt>
<dd>{(s.distance / 1000).toFixed(2)} km</dd>
<dt>Moving time</dt>
<dd>
{Math.round(s.moving / 60)} min of {Math.round(s.elapsed / 60)}
</dd>
<dt>Average speed</dt>
<dd>{kmh} km/h</dd>
{s.dropouts > 0 && (
<dd>Sensor dropped out {s.dropouts} times; those gaps are not counted.</dd>
)}
</dl>
);
}
Check this idea later
Sources
- Bluetooth SIG: Cycling Speed and Cadence Service
- MDN: requestAnimationFrame (use the timestamp, not the call count)
- Trapezoidal rule
Values above were run in Node 22, Go 1.27, Rust 1.99 and Apple clang 17. The app, the thresholds and the readings are a worked model.
Sign in to keep your read marks on every device. Sign in
Was this helpful?