apopt.comCBE 493R Data-Driven Athletics

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.

  1. 1Gather (used)
  2. 2Statistics
  3. 3Visualize (used)
  4. 4Cleanse (used)
  5. 5Features (used)
  6. 6Balance
  7. 7Scale
  8. 8Split
  9. 9Deploy
  1. Ask
  2. Collect
  3. Check (this week)
  4. Interpret (this week)
  5. Coach
  6. Reassess

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

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

PythonSensor against reference, 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.

PythonCorrelation at each trial lag

Show the expected result without running
best lag 7 min: r = 0.939 (r at 0 min = 0.838)
Correlation between reference and shifted sensor against trial lag in minutes, from 0 to 20; it rises from about 0.84 at zero to a peak near 0.94 at 7 minutes and falls after.

Align, then measure the error

PythonMARD and bias after removing the lag

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

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

  1. Why compute the lag before the error?

  2. Reference and sensor pairs (mg/dL): (90, 99), (110, 100), (140, 119), (100, 108). What is the MARD, in %?

    %

0 of 2 answered · 0 right the first time

References

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.