// Relative timing of a repeating acoustic reference; no phone/RCT clock synchronization required. import {ANCHOR_HZ, ORDER, SYMBOL_SECONDS} from './acoustic-pattern.js'; const cycleMs = ORDER.length * SYMBOL_SECONDS * 1000; const phaseDifference = (v, period) => v - period * Math.round(v / period); export const RATE_REVIEW_PPM = 3000; export const SHIFT_REVIEW_MS = 40; export class AcousticTiming { constructor(windowMs) { this.windowMs=windowMs; this.blockSize=Math.ceil(199.99/windowMs); this.rateCoverageMs=0; this.edgeCount=0; this.maxRatePpm=0; this.maxShiftMs=0; this.firstRateMs=null; this.firstShiftMs=null; this.basePpm=null; this.reset(); } reset() { this.lastPhase=null; this.sum=0; this.count=0; this.pairs=0; this.priorBlock=null; this.markerCandidate=-1; this.markerCount=0; this.lastMarker=-1; this.lastSeen=null; this.markerStart=null; this.markerSlot=-1; this.edgePhase=null; this.lastEdge=null; this.candidate=null; } carrier(phase, atMs) { if (phase!==null && this.lastPhase!==null) { const turns=phaseDifference(phase-this.lastPhase-ANCHOR_HZ*this.windowMs/1000,1); if (Math.abs(turns)<=0.35) { this.sum+=turns; this.pairs++; } } this.lastPhase=phase; if (++this.count=Math.ceil(0.8*this.count) ? this.sum*1e9/(measured*ANCHOR_HZ) : null; const prior=this.priorBlock; this.priorBlock=ppm; this.count=0; this.pairs=0; this.sum=0; if (ppm===null || prior===null || Math.abs(prior-ppm)>1500) return; this.rateCoverageMs+=measured; const stable=(prior+ppm)/2; this.basePpm??=stable; const change=Math.round(stable-this.basePpm); if (Math.abs(change)>Math.abs(this.maxRatePpm)) this.maxRatePpm=change; if (Math.abs(change)>=RATE_REVIEW_PPM) this.firstRateMs??=Math.max(0,Math.round(atMs-2*span)); } marker(value, atMs) { // Time a change only when both measured symbols agree over two windows. if (value!==this.markerCandidate) { this.markerCandidate=value; this.markerCount=0; } this.markerCount++; if (value<0) return; if (value===this.lastMarker) { this.lastSeen=atMs; return; } if (this.markerCount<2) return; const start=atMs-this.windowMs; const slot=ORDER.findIndex((m,i)=>m===value && ORDER[(i+ORDER.length-1)%ORDER.length]===this.lastMarker); const consecutive=this.markerSlot>=0 && slot===(this.markerSlot+1)%ORDER.length; const dwell=start-this.markerStart, symbolMs=SYMBOL_SECONDS*1000; if (consecutive && start-this.lastSeen<=2.1*this.windowMs && Math.abs(dwell-symbolMs)<=2.1*this.windowMs) this.edge(slot,start); this.markerSlot=slot; this.markerStart=start; this.lastMarker=value; this.lastSeen=atMs; } edge(slot, atMs) { this.edgeCount++; const phase=atMs-slot*SYMBOL_SECONDS*1000; if (this.lastEdge===null || atMs-this.lastEdge>=cycleMs/2) { // Reacquisition across half a cycle is ambiguous. Start a fresh timing comparison. this.edgePhase=phase; this.candidate=null; } this.lastEdge=atMs; const shift=phaseDifference(phase-this.edgePhase,cycleMs); if (Math.abs(shift)1.5*this.windowMs) { this.candidate={atMs,shift,count:1}; return; } const c=this.candidate; c.shift+=(shift-c.shift)/++c.count; if (c.count<3) return; const value=Math.round(c.shift); if (Math.abs(value)>Math.abs(this.maxShiftMs)) this.maxShiftMs=value; this.firstShiftMs??=Math.round(c.atMs); this.edgePhase=phase; this.candidate=null; } summary() { return {rateCoverageMs:Math.round(this.rateCoverageMs),edgeCount:this.edgeCount, baselinePpm:this.basePpm===null?null:Math.round(this.basePpm),maxRatePpm:this.maxRatePpm, firstRateMs:this.firstRateMs,maxShiftMs:this.maxShiftMs,firstShiftMs:this.firstShiftMs}; } }