refactor: extract typed auto bandwidth estimator
This commit is contained in:
@@ -390,6 +390,77 @@
|
||||
else window.history.pushState({}, "", nextUrl);
|
||||
}
|
||||
|
||||
// src/features/radio/auto-bandwidth.ts
|
||||
function clampPercent(value) {
|
||||
const numeric = Number(value) || 0;
|
||||
return Math.max(0, Math.min(100, numeric)) / 100;
|
||||
}
|
||||
function estimateOccupiedBandwidth(data, centerHz, mode, limits, interference = {}) {
|
||||
if (!data || !Array.isArray(data.bins) && !ArrayBuffer.isView(data.bins) || !Number.isFinite(centerHz)) return null;
|
||||
const bins = Array.from(data.bins);
|
||||
if (bins.length < 3) return null;
|
||||
const maxIdx = bins.length - 1;
|
||||
const hzPerBin = data.sample_rate / maxIdx;
|
||||
const fullLoHz = data.center_hz - data.sample_rate / 2;
|
||||
const centerIdx = Math.max(
|
||||
1,
|
||||
Math.min(maxIdx - 1, Math.round((centerHz - fullLoHz) / data.sample_rate * maxIdx))
|
||||
);
|
||||
const normalizedMode = mode.toUpperCase();
|
||||
const [defaultBw, minBw, maxBw, stepBw] = limits;
|
||||
const oneSided = ["USB", "DIG", "CW"].includes(normalizedMode) ? 1 : ["LSB", "CWR"].includes(normalizedMode) ? -1 : 0;
|
||||
const isWfm = normalizedMode === "WFM";
|
||||
const smoothRadius = isWfm ? 3 : 1;
|
||||
const smoothed = bins.map((_, index) => {
|
||||
let sum = 0;
|
||||
let count = 0;
|
||||
for (let adjacent = Math.max(0, index - smoothRadius); adjacent <= Math.min(maxIdx, index + smoothRadius); adjacent += 1) {
|
||||
sum += bins[adjacent] ?? 0;
|
||||
count += 1;
|
||||
}
|
||||
return sum / count;
|
||||
});
|
||||
const sorted = [...bins].sort((left, right) => left - right);
|
||||
const noise = sorted[Math.floor(sorted.length * 0.2)] ?? -Infinity;
|
||||
const maxSpanBins = Math.max(2, Math.ceil(maxBw / hzPerBin));
|
||||
const searchHalfBins = oneSided === 0 ? Math.ceil(maxSpanBins / 2) : maxSpanBins;
|
||||
const searchLo = Math.max(1, centerIdx - (oneSided > 0 ? 2 : searchHalfBins));
|
||||
const searchHi = Math.min(maxIdx - 1, centerIdx + (oneSided < 0 ? 2 : searchHalfBins));
|
||||
let peak = -Infinity;
|
||||
for (let index = searchLo; index <= searchHi; index += 1) {
|
||||
peak = Math.max(peak, smoothed[index] ?? -Infinity);
|
||||
}
|
||||
const snr = peak - noise;
|
||||
if (!Number.isFinite(snr) || snr < (isWfm ? 5 : 4)) return isWfm ? minBw : defaultBw;
|
||||
const threshold = noise + Math.max(3, Math.min(isWfm ? 6 : 10, snr * (isWfm ? 0.18 : 0.28)));
|
||||
const allowedGap = Math.max(isWfm ? 4 : 2, Math.ceil((isWfm ? 12e3 : stepBw) / hzPerBin));
|
||||
const occupiedExtent = (direction, limitBins) => {
|
||||
let lastOccupied = centerIdx;
|
||||
let gap = 0;
|
||||
for (let offset = 0; offset <= limitBins; offset += 1) {
|
||||
const index = centerIdx + direction * offset;
|
||||
if (index <= 0 || index >= maxIdx) break;
|
||||
if ((smoothed[index] ?? -Infinity) >= threshold) {
|
||||
lastOccupied = index;
|
||||
gap = 0;
|
||||
} else if (++gap > allowedGap) break;
|
||||
}
|
||||
return Math.abs(lastOccupied - centerIdx) * hzPerBin;
|
||||
};
|
||||
let rawBw = oneSided !== 0 ? occupiedExtent(oneSided, maxSpanBins) : 2 * Math.max(occupiedExtent(-1, searchHalfBins), occupiedExtent(1, searchHalfBins));
|
||||
rawBw *= isWfm ? 1.08 : 1.12;
|
||||
if (isWfm) {
|
||||
const aci = clampPercent(interference.aci);
|
||||
const cci = clampPercent(interference.cci);
|
||||
const aciCap = maxBw - (maxBw - minBw) * aci;
|
||||
const cciFloor = minBw + (defaultBw - minBw) * 0.65;
|
||||
const cciCap = maxBw - (maxBw - cciFloor) * cci;
|
||||
rawBw = Math.min(rawBw, aciCap, cciCap);
|
||||
}
|
||||
const clamped = Math.max(minBw, Math.min(maxBw, rawBw));
|
||||
return Math.max(stepBw, Math.round(clamped / stepBw) * stepBw);
|
||||
}
|
||||
|
||||
// src/app.js
|
||||
void loadDecoderRegistry(refreshOperatorLayoutCapabilities);
|
||||
var authRole = null;
|
||||
@@ -4089,84 +4160,18 @@ ${unsupportedBandSummary()}`;
|
||||
window.trxUi?.notify("Bandwidth could not be changed", { kind: "error", action: { label: "Retry", run: applyBandwidthFromInput } });
|
||||
}
|
||||
}
|
||||
function estimateOccupiedBandwidth(data, centerHz, interference = {}) {
|
||||
if (!data || !isBinsArray(data.bins) || data.bins.length < 3 || !Number.isFinite(centerHz)) {
|
||||
return null;
|
||||
}
|
||||
const bins = data.bins;
|
||||
const maxIdx = bins.length - 1;
|
||||
const hzPerBin = data.sample_rate / maxIdx;
|
||||
const fullLoHz = data.center_hz - data.sample_rate / 2;
|
||||
const centerIdx = Math.max(
|
||||
1,
|
||||
Math.min(maxIdx - 1, Math.round((centerHz - fullLoHz) / data.sample_rate * maxIdx))
|
||||
);
|
||||
const mode = (modeEl ? modeEl.value : "USB").toUpperCase();
|
||||
const [defaultBw, minBw, maxBw, stepBw] = mwDefaultsForMode(mode);
|
||||
const oneSided = mode === "USB" || mode === "DIG" || mode === "CW" ? 1 : mode === "LSB" || mode === "CWR" ? -1 : 0;
|
||||
const isWfm = mode === "WFM";
|
||||
const smoothRadius = isWfm ? 3 : 1;
|
||||
const smoothed = bins.map((_, i) => {
|
||||
let sum = 0;
|
||||
let count = 0;
|
||||
for (let j = Math.max(0, i - smoothRadius); j <= Math.min(maxIdx, i + smoothRadius); j++) {
|
||||
sum += bins[j];
|
||||
count += 1;
|
||||
}
|
||||
return sum / count;
|
||||
});
|
||||
const sorted = [...bins].sort((a, b) => a - b);
|
||||
const noise = sorted[Math.floor(sorted.length * 0.2)];
|
||||
const maxSpanBins = Math.max(2, Math.ceil(maxBw / hzPerBin));
|
||||
const searchHalfBins = oneSided === 0 ? Math.ceil(maxSpanBins / 2) : maxSpanBins;
|
||||
const searchLo = Math.max(1, centerIdx - (oneSided > 0 ? 2 : searchHalfBins));
|
||||
const searchHi = Math.min(maxIdx - 1, centerIdx + (oneSided < 0 ? 2 : searchHalfBins));
|
||||
let peak = -Infinity;
|
||||
for (let i = searchLo; i <= searchHi; i++) peak = Math.max(peak, smoothed[i]);
|
||||
const snr = peak - noise;
|
||||
if (!Number.isFinite(snr) || snr < (isWfm ? 5 : 4)) return isWfm ? minBw : defaultBw;
|
||||
const threshold = noise + Math.max(3, Math.min(isWfm ? 6 : 10, snr * (isWfm ? 0.18 : 0.28)));
|
||||
const allowedGap = Math.max(isWfm ? 4 : 2, Math.ceil((isWfm ? 12e3 : stepBw) / hzPerBin));
|
||||
function occupiedExtent(direction, limitBins) {
|
||||
let lastOccupied = centerIdx;
|
||||
let gap = 0;
|
||||
for (let n = 0; n <= limitBins; n++) {
|
||||
const i = centerIdx + direction * n;
|
||||
if (i <= 0 || i >= maxIdx) break;
|
||||
if (smoothed[i] >= threshold) {
|
||||
lastOccupied = i;
|
||||
gap = 0;
|
||||
} else if (++gap > allowedGap) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return Math.abs(lastOccupied - centerIdx) * hzPerBin;
|
||||
}
|
||||
let rawBw;
|
||||
if (oneSided !== 0) {
|
||||
rawBw = occupiedExtent(oneSided, maxSpanBins);
|
||||
} else {
|
||||
const leftHz = occupiedExtent(-1, searchHalfBins);
|
||||
const rightHz = occupiedExtent(1, searchHalfBins);
|
||||
rawBw = 2 * Math.max(leftHz, rightHz);
|
||||
}
|
||||
rawBw *= isWfm ? 1.08 : 1.12;
|
||||
if (isWfm) {
|
||||
const aci = Math.max(0, Math.min(100, Number(interference.aci) || 0)) / 100;
|
||||
const cci = Math.max(0, Math.min(100, Number(interference.cci) || 0)) / 100;
|
||||
const aciCap = maxBw - (maxBw - minBw) * aci;
|
||||
const cciFloor = minBw + (defaultBw - minBw) * 0.65;
|
||||
const cciCap = maxBw - (maxBw - cciFloor) * cci;
|
||||
rawBw = Math.min(rawBw, aciCap, cciCap);
|
||||
}
|
||||
const clamped = Math.max(minBw, Math.min(maxBw, rawBw));
|
||||
return Math.max(stepBw, Math.round(clamped / stepBw) * stepBw);
|
||||
}
|
||||
async function applyAutoBandwidth() {
|
||||
if (!lastSpectrumData || lastFreqHz == null) return;
|
||||
const onVirtual = window.trx.modules.vchan?.isOnVirtual() === true;
|
||||
const interference = onVirtual ? {} : { cci: lastWfmCci, aci: lastWfmAci };
|
||||
const estimated = estimateOccupiedBandwidth(lastSpectrumData, lastFreqHz, interference);
|
||||
const mode = (modeEl?.value || "").toUpperCase();
|
||||
const estimated = estimateOccupiedBandwidth(
|
||||
lastSpectrumData,
|
||||
lastFreqHz,
|
||||
mode,
|
||||
mwDefaultsForMode(mode),
|
||||
interference
|
||||
);
|
||||
if (!Number.isFinite(estimated) || estimated <= 0) {
|
||||
syncBandwidthInput(currentBandwidthHz);
|
||||
return;
|
||||
@@ -4178,7 +4183,6 @@ ${unsupportedBandSummary()}`;
|
||||
if (lastSpectrumData) {
|
||||
scheduleSpectrumDraw();
|
||||
}
|
||||
const mode = (modeEl?.value || "").toUpperCase();
|
||||
let reason = "measured occupied spectrum";
|
||||
if (mode === "WFM") {
|
||||
if (estimated === 6e4 && lastWfmAci >= 20) reason = `high adjacent-channel interference (${Math.round(lastWfmAci)}% ACI)`;
|
||||
|
||||
Reference in New Issue
Block a user