From d347b493d9c5e94639b67bc05183a66a4411b5da Mon Sep 17 00:00:00 2001 From: Stan Grams Date: Sat, 1 Aug 2026 02:02:13 +0200 Subject: [PATCH] [fix](trx-frontend): make auto bandwidth modulation-aware --- .../trx-frontend-http/assets/web/app.js | 95 ++++++++++++------- 1 file changed, 62 insertions(+), 33 deletions(-) diff --git a/src/trx-client/trx-frontend/trx-frontend-http/assets/web/app.js b/src/trx-client/trx-frontend/trx-frontend-http/assets/web/app.js index b2909005..cc32a055 100644 --- a/src/trx-client/trx-frontend/trx-frontend-http/assets/web/app.js +++ b/src/trx-client/trx-frontend/trx-frontend-http/assets/web/app.js @@ -4348,64 +4348,93 @@ async function applyBandwidthFromInput() { } catch (_) {} } -function estimateBandwidthAroundPeak(data, centerHz) { +function estimateOccupiedBandwidth(data, centerHz) { 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 searchRadius = Math.max(6, Math.min(120, Math.round(maxIdx * 0.03))); - const searchLo = Math.max(1, centerIdx - searchRadius); - const searchHi = Math.min(maxIdx - 1, centerIdx + searchRadius); - - let peakIdx = centerIdx; - for (let i = searchLo; i <= searchHi; i++) { - if (bins[i] > bins[peakIdx]) peakIdx = i; - } + 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"; + // Reduce single-bin peaks and holes before finding occupied-channel edges. + // WFM needs a wider smoothing window because its energy is noise-like and + // spread across the entire channel rather than concentrated at a carrier. + 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 peak = bins[peakIdx]; - const threshold = Math.max(noise + 4, peak - Math.max(8, (peak - noise) * 0.35)); + 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 defaultBw; - let left = peakIdx; - let right = peakIdx; - let belowCount = 0; - for (let i = peakIdx; i > 1; i--) { - if (bins[i] < threshold) belowCount += 1; - else belowCount = 0; - if (belowCount >= 2) break; - left = i; + // A threshold relative to the noise floor finds occupied bandwidth much + // more reliably than one relative to the peak. The latter fails for WFM, + // whose multiplex spectrum has peaks, notches, and no narrow centre carrier. + 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 ? 12_000 : 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; } - belowCount = 0; - for (let i = peakIdx; i < maxIdx - 1; i++) { - if (bins[i] < threshold) belowCount += 1; - else belowCount = 0; - if (belowCount >= 2) break; - right = i; + let rawBw; + if (oneSided !== 0) { + rawBw = occupiedExtent(oneSided, maxSpanBins); + } else { + const leftHz = occupiedExtent(-1, searchHalfBins); + const rightHz = occupiedExtent(1, searchHalfBins); + // A symmetric RF filter must contain the larger of the two sidebands. + rawBw = 2 * Math.max(leftHz, rightHz); } - const shoulderPad = Math.max(1, Math.round((right - left) * 0.08)); - left = Math.max(0, left - shoulderPad); - right = Math.min(maxIdx, right + shoulderPad); - - const hzPerBin = data.sample_rate / maxIdx; - const rawBw = Math.max(hzPerBin, (right - left) * hzPerBin); - const [, minBw, maxBw, stepBw] = mwDefaultsForMode(modeEl ? modeEl.value : "USB"); + // Add a transition-band margin. Reject implausibly narrow WFM measurements: + // they are normally a pilot/centre feature or a neighbouring narrow signal. + rawBw *= isWfm ? 1.08 : 1.12; + if (isWfm && rawBw < defaultBw * 0.55) return defaultBw; 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 estimated = estimateBandwidthAroundPeak(lastSpectrumData, lastFreqHz); + const estimated = estimateOccupiedBandwidth(lastSpectrumData, lastFreqHz); if (!Number.isFinite(estimated) || estimated <= 0) { syncBandwidthInput(currentBandwidthHz); return;