bstein: show RCT duration selector and separate carrier from marker confidence

This commit is contained in:
jenkins 2026-12-03 13:14:01 -06:00
parent 496454fe06
commit ec764ca66f
5 changed files with 55 additions and 27 deletions

View File

@ -59,7 +59,10 @@ export class AcousticDetector {
// Both tones must be distinguishable at room level, including against
// the optional RCT's separate 1 kHz output pulses.
const min = Math.max((0.0008 * this.size / 2) ** 2, 0.035 * this.size * this.energy);
if (power[0] < min || power[marker] < min || this.energy === 0) return -1;
this.carrierPresent = power[0]>=min && this.energy>0;
const runnerUp = Math.max(...power.slice(1).filter((_,i)=>i+1!==marker));
this.timingMarker = power[marker]>=min && power[marker]>=2*runnerUp ? marker-1 : -1;
if (!this.carrierPresent || power[marker] < min) return -1;
return marker - 1;
}
observeLevel(valid) {
@ -95,7 +98,6 @@ export class AcousticDetector {
this.record('marker order or timing');
valid = false;
}
if (this.locked && this.edges >= 2) this.timing.edge(pair, this.windows*this.windowMs);
this.slot = pair;
this.lastMarker = marker;
this.lastChange = this.tick;
@ -110,7 +112,9 @@ export class AcousticDetector {
this.windows++;
this.scoredEndMs = endMs;
this.observeLevel(valid);
const phase = marker>=0 && this.haveInput && !this.quietWindows ?
this.timing.marker(this.haveInput && !this.quietWindows ? this.timingMarker : -1,
this.windows*this.windowMs);
const phase = this.carrierPresent && this.haveInput && !this.quietWindows ?
Math.atan2(this.anchorSin*this.q2[0], this.q1[0]-0.5*this.coeff[0]*this.q2[0])/(2*Math.PI) : null;
this.timing.carrier(phase, this.windows*this.windowMs);
if (!valid) {

View File

@ -15,32 +15,45 @@ export class AcousticTiming {
this.reset();
}
reset() {
this.lastPhase=null; this.sum=0; this.count=0; this.priorBlock=null;
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) {
this.lastPhase=phase; this.count=0; this.sum=0; this.priorBlock=null;
return;
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++; }
}
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 span=this.count*this.windowMs, measured=this.pairs*this.windowMs;
const ppm=this.pairs>=Math.ceil(0.8*this.count) ? this.sum*1e9/(measured*ANCHOR_HZ) : null;
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;
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;

View File

@ -3,20 +3,24 @@ import {STAGE_MINUTES, TRIAL_SECONDS} from './acoustic-pattern.js';
export const phoneTest = new URLSearchParams(location.search).get('test') === 'phone';
const status = document.getElementById('path-state');
const detail = document.getElementById('path-detail');
const advance = document.getElementById('stage-next');
const duration = document.getElementById('stage-duration');
const timing = document.getElementById('path-timing');
let stageIndex = 0, device = null, attempt = 0;
let stageIndex = 0, unlockedIndex = 0, device = null, attempt = 0;
function renderStage() {
for (const [index, option] of Array.from(duration.options).entries()) {
option.disabled = index > unlockedIndex;
}
duration.selectedIndex = stageIndex;
document.getElementById('start').textContent = 'Start ' + STAGE_MINUTES[stageIndex] + '-minute receiver';
advance.hidden = true;
}
export function selectStageInput(input) {
if (device === input) return;
device = input; stageIndex = 0; attempt++;
device = input; stageIndex = 0; unlockedIndex = 0; attempt++;
renderStage();
}
export function beginStage(input) {
selectStageInput(input);
unlockedIndex = stageIndex;
renderStage();
return {minutes:STAGE_MINUTES[stageIndex], attempt:++attempt};
}
@ -25,10 +29,8 @@ export async function reviewStage(run, reason, delivered) {
const ok = await delivered;
if (attempt !== run.stage.attempt) return;
if (!ok) { status.textContent = 'Stage held · Atlas receipt unavailable'; return; }
if (stageIndex + 1 < STAGE_MINUTES.length) {
advance.textContent = 'Advance to ' + STAGE_MINUTES[stageIndex + 1] + ' minutes';
advance.hidden = false;
}
unlockedIndex = Math.min(STAGE_MINUTES.length - 1, STAGE_MINUTES.indexOf(run.stage.minutes) + 1);
renderStage();
}
const clock = ms => {
const s = Math.min(TRIAL_SECONDS, Math.floor(ms / 1000));
@ -39,8 +41,15 @@ export function setupPhoneMode() {
if (!phoneTest) return;
document.title = 'RCT microphone path test';
document.querySelector('h1').textContent = document.title;
for (const minutes of STAGE_MINUTES) duration.add(new Option(minutes+' min',String(minutes)));
document.getElementById('stage-label').hidden = false;
renderStage();
advance.onclick = () => { if (!advance.hidden) { stageIndex++; renderStage(); } };
duration.onchange = () => {
if (!duration.disabled && duration.selectedIndex>=0 && duration.selectedIndex<=unlockedIndex) {
stageIndex = duration.selectedIndex;
}
renderStage();
};
document.getElementById('start').title = 'Wait for the phone signal, then measure the selected stage.';
for (const id of ['tone','tone-stop','output-status','output-report','path-link']) {
document.getElementById(id).hidden = true;
@ -89,7 +98,8 @@ export async function attachDetector(run, isCurrent, finish) {
else if (data.timing.firstShiftMs !== null) hold = 'Relative timing shift · ' + data.timing.maxShiftMs + ' ms';
else if (data.firstUnclearMs !== null) hold = 'Reference unclear · ' + Math.round(data.gapMaxMs) + ' ms';
else if (data.complete && (data.timing.rateCoverageMs < 0.8*data.observedMs ||
data.timing.edgeCount < 0.8*data.observedMs/100)) hold = 'Insufficient reference timing coverage';
data.timing.edgeCount < 0.8*data.observedMs/100)) hold = 'Insufficient timing coverage · pitch ' + Math.round(100*data.timing.rateCoverageMs/data.observedMs) +
'% · markers ' + Math.round(10000*data.timing.edgeCount/data.observedMs) + '%';
if (hold) {
status.textContent = 'Stage held · ' + hold + ' · ' + label;
run.findingAt ??= data.observedMs;

View File

@ -10,6 +10,7 @@ function buttons() {
el('start').disabled=!!current||!el('input').value;
el('stop').disabled=!current;
el('devices').disabled=!!current||!!output;
if(phoneTest)el('stage-duration').disabled=!!current;
el('input').disabled=!!current;el('output').disabled=!!output;
el('tone').disabled=!!output||!el('output').value;
el('tone-stop').disabled=!output;

View File

@ -10,7 +10,7 @@
<label>Output <select id="output"></select></label></div>
<div><button id="start" disabled title="One uninterrupted microphone capture, up to 20 minutes. Numeric reports every ten seconds. No camera or audio upload.">Start mic watch</button>
<button id="stop" disabled>Stop</button>
<button id="stage-next" hidden title="Advance after reviewing this stage on the same setup."></button>
<span id="stage-label" hidden><label>Test duration <select id="stage-duration" title="Longer durations unlock after a completed and reported stage on this microphone."></select></label></span>
<button id="tone" disabled title="Quiet 1 kHz pulses on the explicitly selected output for two minutes. This exercises output separately; microphone audio is never monitored.">Start output pulses</button>
<button id="tone-stop" disabled>Stop output</button>
<button id="save" disabled>Save metrics</button></div>