Skip to content

About

Turbine clock correction from the lower envelope of one-way delays, so drifting controller clocks stop putting readings in the wrong 10-minute bin or ahead of a grid event.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

driftstamp

CI

driftstamp

driftstamp re-stamps readings from wind-turbine controllers whose real-time clocks drift by tens of parts per million and sometimes jump. It fits the lower convex hull of (device time, receive time minus device time) per turbine, takes the supporting line that maximises the sum of its values at the points, and uses that line as the clock correction. The estimate is not biased by average network delay or store-and-forward bursts, it detects NTP steps and reboot resets, and it runs as a streaming corrector with a 30-minute window. The data is synthetic: a seeded generator with known clock errors, delays, outages, jumps and a grid event, so every recovered number is scored against ground truth. The CLI is a demonstration of that farm only: estimate, correct and report always generate the farm from --seed and have no option to read your own readings. To use the estimator on real data, call the library (driftstamp.envelope, driftstamp.stream) from Python.

The problem

A farm streams 1 Hz readings from controllers whose clocks drift by up to 40 ppm and start tens of seconds off. 10-minute averages for power-curve monitoring and the fleet-wide analysis of grid events assume event time is right. A turbine 30 s off puts readings in the wrong 10-minute bin and appears to react to a grid frequency dip before it happened. The gateway receive timestamp is trustworthy, but it includes cellular and store-and-forward delay, so using it only replaces clock error with delay error.

Network delay is never negative. With device clock D, receive time R and true time t, the points (D, R - D) therefore all lie on or above the line t - D + d_min, where d_min is the path delay floor. That line is exactly the correction to add to D. A mean or a least-squares fit is pulled up by the average delay and by outages; the lowest line that stays below the points is not.

How it works

flowchart LR
    A["Readings<br/>device ts + receive ts"] --> B["Decimate<br/>per-10 s minimum delay"]
    B --> C["Lower convex hull<br/>per turbine"]
    C --> D["Envelope line<br/>offset + drift (ppm)"]
    D --> E["Segment at clock jumps<br/>rise / drop vs the line"]
    E --> F["Re-stamp<br/>device_ts + offset(device_ts)"]
    F --> G["10-minute averages<br/>by corrected time"]
    F --> H["Fleet-wide grid dip<br/>onset alignment"]
Loading
  • envelope.py builds the lower hull with Andrew's monotone chain. The line is the hull edge that spans the mean device time, which is the solution of the linear program "maximise the sum of the line's values at the points subject to the line being below every point". The configured min_path_delay_ms (default 60 ms) is subtracted, because a constant delay floor cannot be seen from one-way timestamps. The drift is 1/(1+slope) - 1 in ppm.
  • decimate.py keeps the lowest-delay point of every 10 s of device time, so a 30-minute window is at most 180 points. Dropping a point moves the envelope by at most about |slope| x 10 s, under 1 ms for a 40 ppm clock (the tests check this on the generated farm, on the windows that contain no buffered readings). Bins whose minimum delay is more than 5 s above the lowest bin of the window are discarded as backlog, so a long outage cannot drag the mean device time away from the data the hull actually touches.
  • segment.py opens a new segment when a point lies far below the line, or the delay level rises above it and stays there. A backlog flush also produces large delays, so a candidate is confirmed by fitting a hull to the next 120 readings: a real jump keeps the clock's slope, a flush has slope -1.
  • stream.py keeps one corrector per turbine with a 30-minute window, refits every 60 s of receive time, and marks readings provisional until a segment has 120 points (the offset is then the window minimum delay with no drift term).
  • windows.py computes 10-minute average active power by corrected, device or receive time. baselines.py holds the three estimators the hull is compared with.
  • gen.py generates the farm, score.py scores against it.

Quick start

python3 -m venv .venv && . .venv/bin/activate
pip install -e ".[dev]"
driftstamp report --seed 13            # recovered vs true offset and drift, jumps, grid event
driftstamp estimate --turbine T19      # per-segment fit for one turbine
driftstamp correct --csv corrected.csv # stream the farm, write every corrected reading
pytest

Example output

driftstamp report --seed 13 on the synthetic farm (12 turbines, 4 hours at 1 Hz, 172,800 readings). offset_s is the clock error (device minus true time) at the middle of the turbine's longest segment, drift_ppm the clock rate error, naive_err_ms the error of the mean-offset baseline over that segment, and stream_p99_ms the 99th percentile of the streaming corrector's absolute timestamp error once warm.

synthetic farm, seed 13: 12 turbines, 172800 readings

turbine  offset_s true/est      drift_ppm true/est  naive_err_ms  stream_p99_ms  jumps
T01       +18.866  +18.866    +28.42   +28.42        3914.0            2.0  -
T03        -7.690   -7.690     +1.27    +1.27       84158.9            4.0  -
T05       -22.461  -22.460    +21.89   +21.89        2255.8            2.0  -
T07       +16.085  +16.085    -23.87   -23.87        1685.3            4.0  02:13:00.7 reset
T09        -3.561   -3.562     -8.48    -8.51        3748.8            3.0  -
T11       +26.247  +26.248    +10.57   +10.57        2178.6            2.0  -
T12       +29.590  +29.590    +22.54   +22.60        3524.0            2.0  -
T14       +18.779  +18.779    +35.23   +35.22        1946.7            2.0  -
T16       +21.029  +21.029    +17.11   +17.11        2077.1            2.0  -
T17       -11.348  -11.347    -28.34   -28.35        2131.2            6.0  -
T19       -17.648  -17.647    -10.50   -10.50        2689.5            2.0  03:40:00.4 step -3.20 s
T21       -25.458  -25.457    +31.03   +31.03        3870.4            3.0  -

detected clock jumps (time of day of the first reading after the jump)
  T07  02:13:00.7  reset  (reading 7980)
  T19  03:40:00.4  step -3.20 s  (reading 13200)

grid event: first reading below 49.8 Hz, spread across turbines
  true time          0.868 s
  device time       55.437 s
  receive time     916.902 s
  corrected time     0.868 s

driftstamp estimate --turbine T19 shows the NTP step as two segments:

turbine T19: 14400 readings, seed 13
true drift -10.50 ppm, offset at start -17.578 s
segment 0: readings 0-13199 (13200 readings, opened by start, true time from 00:00:00.4)
  clock error  true -17.648 s  estimated -17.647 s  error +0.1 ms
  clock drift  true -10.50 ppm  estimated -10.50 ppm  error +0.00 ppm
  naive mean offset is off by +2689.5 ms
segment 1: readings 13200-14399 (1200 readings, opened by rise, true time from 03:40:00.4)
  clock error  true -20.924 s  estimated -20.924 s  error +0.0 ms
  clock drift  true -10.50 ppm  estimated -12.79 ppm  error -2.29 ppm
  naive mean offset is off by +146.9 ms

Design decisions

  • Hull line instead of a robust regression. The hull edge needs no tuning, is exact on noise-free data, and is the solution of the stated linear program, so it can be checked against scipy.optimize.linprog in the tests. The cost is that it rests on a few low-delay points: the delay floor is estimated from the extreme tail of the delay distribution, and a single bad timestamp below the floor would pull the line down.
  • Subtracting a configured floor. With one-way timestamps the constant part of the path delay cannot be separated from the clock offset. The estimator subtracts min_path_delay_ms (60 ms here, from RTT/2 in a real deployment). If that value is wrong by 20 ms, every corrected timestamp is wrong by 20 ms, which estimate --min-path-delay-ms demonstrates.
  • Decimating to 10 s minima. It bounds the work per refit at 180 points and the per-turbine state at a dictionary of 180 entries. The price is a theoretical envelope change of |slope| x 10 s; the tests check it stays under that bound on the generated farm, skipping windows that contain buffered readings (their backlog bins are filtered before the fit anyway).
  • Dropping backlog bins before the fit. Without this, the 25-minute backhaul outage of T03 puts ramp points with delays up to 1,500 s inside the window and the mean device time lands where the hull has no low points, so the fit fails in those windows. The 5 s threshold treats any delay that large as a flush. A real path with sustained delays above 5 s would need a larger threshold.
  • Confirming jumps by slope. A rise of the delay level is ambiguous: a clock step and a backlog both cause it. Requiring the new slope to stay within 0.1 of the old one separates them (a flush has slope -1, a clock at most a few hundred ppm). A rise (clock stepped back, reboot) with less than 1 s of level change is not detected, and neither is a drop (clock stepped forward) with less than 0.5 s (SegmentConfig.rise_s and below_s).
  • Missing bins count as 0 kW. The 10-minute average error in windows.py (mean_abs_error) treats a bin with no readings as zero output, as a dashboard would show it. An average by device or receive time that leaves a bin empty therefore carries that bin's full reference value as error, which widens the gap to corrected time compared with scoring shared bins only. On the seed-13 farm the turbines the 10x test compares (offset above 20 s) leave no device-time bin empty, so the 10x requirement is not inflated by this rule there; it does apply to T07 in device time and to T03 in receive time.
  • Holding candidates in the stream. A reading that is far from the line waits for up to 120 further readings before it is stamped, so the stream either starts a new segment or stamps it with the old line. That costs latency only for suspicious readings; the alternative, stamping at once and fixing later, would emit wrong event times downstream.

Testing

pytest

56 tests pass. They cover the hull on a hand-computed 7-point fixture and a hypothesis cross-check against scipy.optimize.linprog, the decimation bound, recovery of offset (within 30 ms) and drift (within 3 ppm) in every 30-minute window with at least 10 clean minutes, the failure of the mean and least-squares baselines, jump detection for T19 and T07 (batch, plus a forward step through the streaming corrector) and no false jumps on the other ten turbines, streaming equivalence with the batch envelope, warm-up, 10-minute average errors, grid-event alignment, and the CLI.

Project layout

src/driftstamp/
  model.py      Reading, Series, time re-basing
  envelope.py   lower hull, LP line, clock estimate
  decimate.py   per-10 s minima, backlog filter
  baselines.py  naive mean, least squares, window minimum
  segment.py    jump detection and confirmation
  stream.py     per-turbine streaming corrector
  windows.py    10-minute averages
  gen.py        seeded farm generator with ground truth
  score.py      scoring against ground truth
  cli.py        estimate / correct / report
tests/          pytest suite

License

MIT

About

Turbine clock correction from the lower envelope of one-way delays, so drifting controller clocks stop putting readings in the wrong 10-minute bin or ahead of a grid event.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages