73 lines
3.3 KiB
JavaScript
73 lines
3.3 KiB
JavaScript
// 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.priorBlock=null;
|
|
this.edgePhase=null; this.lastEdge=null; this.candidate=null;
|
|
}
|
|
carrier(phase, atMs) {
|
|
if (phase===null || this.lastPhase===null) {
|
|
this.lastPhase=phase; this.count=0; this.sum=0; this.priorBlock=null;
|
|
return;
|
|
}
|
|
const turns=phaseDifference(phase-this.lastPhase-ANCHOR_HZ*this.windowMs/1000,1);
|
|
this.lastPhase=phase;
|
|
// A large phase jump can also be a splice; avoid turning it into a pitch estimate.
|
|
if (Math.abs(turns)>0.35) {this.sum=0; this.count=0; this.priorBlock=null; return;}
|
|
this.sum+=turns;
|
|
if (++this.count<this.blockSize) return;
|
|
const span=this.count*this.windowMs;
|
|
const ppm=this.sum*1e9/(span*ANCHOR_HZ);
|
|
const prior=this.priorBlock;
|
|
this.priorBlock=ppm; this.count=0; this.sum=0;
|
|
if (prior===null || Math.abs(prior-ppm)>1500) return;
|
|
this.rateCoverageMs+=span;
|
|
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));
|
|
}
|
|
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)<SHIFT_REVIEW_MS-0.01) {
|
|
this.edgePhase+=0.2*shift; this.candidate=null; return;
|
|
}
|
|
if (this.candidate===null || Math.abs(shift-this.candidate.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};
|
|
}
|
|
}
|