[fix](trx-cw): widen tone scan range and clamp by Nyquist
Expand CW decoder tone scan/control range to 100-10000 Hz and cap the upper bound by sample-rate Nyquist to keep detection stable. Co-authored-by: Codex <codex@openai.com> Signed-off-by: Stan Grams <sjg@haxx.space>
This commit is contained in:
@@ -89,12 +89,21 @@ fn goertzel_energy(buf: &[f32], coeff: f32) -> f32 {
|
|||||||
// Tone scan bins
|
// Tone scan bins
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
const TONE_SCAN_LOW: u32 = 300;
|
const TONE_SCAN_LOW: u32 = 100;
|
||||||
const TONE_SCAN_HIGH: u32 = 1200;
|
const TONE_SCAN_HIGH: u32 = 10_000;
|
||||||
const TONE_SCAN_STEP: u32 = 25;
|
const TONE_SCAN_STEP: u32 = 25;
|
||||||
const TONE_STABLE_NEEDED: u32 = 3;
|
const TONE_STABLE_NEEDED: u32 = 3;
|
||||||
const THRESHOLD: f32 = 0.05;
|
const THRESHOLD: f32 = 0.05;
|
||||||
|
|
||||||
|
fn tone_scan_high_for_sample_rate(sample_rate: u32) -> u32 {
|
||||||
|
let nyquist = sample_rate / 2;
|
||||||
|
if nyquist <= TONE_SCAN_LOW + 1 {
|
||||||
|
TONE_SCAN_LOW
|
||||||
|
} else {
|
||||||
|
TONE_SCAN_HIGH.min(nyquist - 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct ToneScanBin {
|
struct ToneScanBin {
|
||||||
freq: u32,
|
freq: u32,
|
||||||
coeff: f32,
|
coeff: f32,
|
||||||
@@ -152,7 +161,8 @@ impl CwDecoder {
|
|||||||
// Build scan bins
|
// Build scan bins
|
||||||
let mut tone_scan_bins = Vec::new();
|
let mut tone_scan_bins = Vec::new();
|
||||||
let mut f = TONE_SCAN_LOW;
|
let mut f = TONE_SCAN_LOW;
|
||||||
while f <= TONE_SCAN_HIGH {
|
let scan_high = tone_scan_high_for_sample_rate(sample_rate);
|
||||||
|
while f <= scan_high {
|
||||||
let bk = (f as f32 * window_size as f32 / sample_rate as f32).round();
|
let bk = (f as f32 * window_size as f32 / sample_rate as f32).round();
|
||||||
let b_omega = (2.0 * std::f32::consts::PI * bk) / window_size as f32;
|
let b_omega = (2.0 * std::f32::consts::PI * bk) / window_size as f32;
|
||||||
tone_scan_bins.push(ToneScanBin {
|
tone_scan_bins.push(ToneScanBin {
|
||||||
@@ -195,7 +205,10 @@ impl CwDecoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_tone_hz(&mut self, tone_hz: u32) {
|
pub fn set_tone_hz(&mut self, tone_hz: u32) {
|
||||||
let tone_hz = tone_hz.clamp(TONE_SCAN_LOW, TONE_SCAN_HIGH);
|
let tone_hz = tone_hz.clamp(
|
||||||
|
TONE_SCAN_LOW,
|
||||||
|
tone_scan_high_for_sample_rate(self.sample_rate),
|
||||||
|
);
|
||||||
self.recompute_goertzel(tone_hz);
|
self.recompute_goertzel(tone_hz);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user