Course / Tutorials / Week 7 / Q-20
Quick tutorial Q-20 · Week 7 · about 15 min · Core
Find the lag between two signals
Find how many minutes a glucose sensor trails its reference by cross-correlation, then measure its error after aligning the clocks, on a synthetic record with a known answer.
- 1Gather (used)
- 2Statistics
- 3Visualize (used)
- 4Cleanse (used)
- 5Features (used)
- 6Balance
- 7Scale
- 8Split
- 9Deploy
- Ask
- Collect
- Check (this week)
- Interpret (this week)
- Coach
- Reassess
Download notebook ·Python file ·Read as Markdown ·Data folder ·Chapter 7 (sections 7.3)
Every cell runs in this page; the first run downloads Python (about 30 s). Nothing you load leaves your browser. Helpful first: Q-14 Heart rate from beat intervals.
The decision
A runner's glucose sensor disagrees with finger-stick readings during a long run. Is the sensor inaccurate, or just late? The answer decides how to read it during the next run.
The raw data
raw/glucose_pair_synthetic.csv: SYNTHETIC record, one row a minute: a reference blood glucose and a sensor built from it 7 minutes late, 0.4 mmol/L high, with noise (minute, reference_mmol, sensor_mmol). Made bybook/ch07-emerging/code/make_glucose.pyfor Section 7.3 of Data-Driven Athletics; the same file asbook/ch07-emerging/data/glucose_pair.csv.
Try it
A glucose sensor under the skin reads the fluid between cells, which follows blood glucose a few minutes late. A sensor that trails its reference looks inaccurate whenever glucose is changing, even if it would be exact with its clock moved. The fix is to find the lag first. The file is the synthetic record of Section 7.3: a reference blood glucose every minute through a 3.5-hour run, and a sensor built from it with a known lag, a known offset and random noise. Because the answers are known, you can check yours.
Compare the two as recorded
Type a prediction to enable Run.
Show the expected result without running
271 minutes; reference 3.5 to 6.8 mmol/L MARD as recorded 10.4%, mean difference +0.41 mmol/L
Slide one signal along the other
Shift the sensor back by k minutes, compute the correlation with the reference at each shift, and take the shift with the highest correlation. This is cross-correlation.
Show the expected result without running
best lag 7 min: r = 0.939 (r at 0 min = 0.838)

Align, then measure the error
Show the expected result without running
after shifting 7 min: MARD 8.4%, bias +0.38 mmol/L built into the synthetic sensor: lag 7 min, offset +0.4 mmol/L, plus noise
Cross-correlation found the 7-minute lag that was built in, and after aligning, the bias of +0.38 mmol/L is close to the +0.4 built in. About two points of the 10.4% MARD came from the lag alone; the rest is the offset and the noise, which no shift removes. The same method aligns two heart-rate devices, or a watch and a video, whenever two clocks disagree.
For the athlete: "Your sensor runs about 7 minutes behind your blood; read it as where your glucose was a few minutes ago, not where it is now."
Action card
- Finding
- The sensor trails the reference by 7 minutes and reads about 0.4 mmol/L high.
- Evidence
- The correlation peaks at a 7-minute shift (r 0.939 against 0.838 unshifted); after aligning, the bias is +0.38 mmol/L and MARD falls from 10.4% to 8.4%.
- How sure
- High on this synthetic record, which has the answers built in.
- Action
- Read the sensor as glucose a few minutes ago, and judge its accuracy only after aligning the clocks.
- Test again
- Repeat with finger-stick readings at known times on your next long run.
With your own data
Any two recordings of the same quantity on two clocks work: a wrist and a chest heart rate, or the strap's displayed heart rate against the rate from its beat intervals (Q-14). Put them on a common one-second or one-minute grid first.
Check yourself
Why compute the lag before the error?
Reference and sensor pairs (mg/dL): (90, 99), (110, 100), (140, 119), (100, 108). What is the MARD, in %?
References
- Data-Driven Athletics, Chapter 7, Continuous Glucose Sensing: units and accuracy, lag by cross-correlation (Section 7.3).
Built 2026-11-30 with Python packages numpy 2.5.5, pandas 3.1.1, matplotlib 3.11.2. In the browser the same code runs on Pyodide, so the last digit can differ.