fix: retain learned quiet peaks in automatic Morse receive
This commit is contained in:
1 parent
7081673bb1
commit
f1b1763dea
8 files changed
+222
-12
No files matched your search
+29
-4
@@ -174,7 +174,7 @@ public:
|
||||
_until = now + duration_ms;
|
||||
_nominal_unit = MorseTx::unitMillis(wpm);
|
||||
_unit_q5 = uint16_t(uint32_t(_nominal_unit) * UnitScale);
|
||||
_noise = _signal = 0;
|
||||
_noise = _signal = _quiet_peak = 0;
|
||||
_samples = 0;
|
||||
_last_sample_at = now;
|
||||
_max_sample_gap_ms = 0;
|
||||
@@ -258,17 +258,29 @@ public:
|
||||
void sample(float rssi_dbm, uint32_t now) {
|
||||
if (!service(now) || !isfinite(rssi_dbm) || rssi_dbm < -180.0f || rssi_dbm > 20.0f) return;
|
||||
_last_rssi = rssi_dbm;
|
||||
const uint32_t sample_gap = static_cast<uint32_t>(now - _last_sample_at);
|
||||
const float filter_gap = sample_gap > 10U ? 10U : sample_gap;
|
||||
_last_sample_at = now;
|
||||
if (!_samples || rssi_dbm < _minimum_rssi) _minimum_rssi = rssi_dbm;
|
||||
if (!_samples || rssi_dbm > _maximum_rssi) _maximum_rssi = rssi_dbm;
|
||||
++_samples;
|
||||
if (!_have_noise) {
|
||||
_have_noise = true;
|
||||
_noise = rssi_dbm;
|
||||
_noise = _quiet_peak = rssi_dbm;
|
||||
_signal = rssi_dbm + 12.0f;
|
||||
}
|
||||
if (learning(now)) {
|
||||
_noise += (rssi_dbm - _noise) * 0.05f;
|
||||
// Discard the initial receiver-settling measurements, then retain
|
||||
// sustained quiet peaks rather than a raw first sample or a spike.
|
||||
const bool settling = static_cast<uint32_t>(now - _started) < 50U;
|
||||
if (settling) {
|
||||
_noise = rssi_dbm;
|
||||
if (rssi_dbm < _quiet_peak) _quiet_peak = rssi_dbm;
|
||||
}
|
||||
else _noise += (rssi_dbm - _noise) * 0.025f * filter_gap;
|
||||
if (settling || rssi_dbm <= _quiet_peak) _candidate = false;
|
||||
else if (!_candidate) { _candidate = true; _candidate_at = now; }
|
||||
else if (static_cast<uint32_t>(now - _candidate_at) >= 5U) _quiet_peak = rssi_dbm;
|
||||
_signal = _noise + 12.0f;
|
||||
return;
|
||||
}
|
||||
@@ -277,6 +289,11 @@ public:
|
||||
float rise = span * 0.5f;
|
||||
if (rise < 6.0f) rise = 6.0f;
|
||||
float threshold = _noise + rise - (_keyed ? 3.0f : 0.0f);
|
||||
// The mean/valley alone forgets the quiet excursions observed while
|
||||
// arming. Retain their filtered upper envelope across marks and valleys;
|
||||
// a quiet receiver still has the ordinary six-dB acquisition margin.
|
||||
const float quiet_threshold = _quiet_peak + (_keyed ? 3.0f : 6.0f);
|
||||
if (threshold < quiet_threshold) threshold = quiet_threshold;
|
||||
// Before the first confirmed OFF, some receivers still report a stale
|
||||
// pre-carrier floor. Allow for a provisional floor shift without a hard
|
||||
// signal-span discontinuity. This margin is a tested startup heuristic,
|
||||
@@ -306,6 +323,14 @@ public:
|
||||
// valley. Keep the next rise anchored to the subsequently learned floor,
|
||||
// not to a transient shoulder left by the previous strong mark.
|
||||
_signal = _noise + 12.0f;
|
||||
// Only established quiet may train this envelope. An OFF edge can
|
||||
// still be on a strong mark's decay shoulder, and short symbol gaps
|
||||
// must not train a threshold which hides the following weaker mark.
|
||||
if (static_cast<uint32_t>(now - _last_edge) >= 200U) {
|
||||
_quiet_peak += (rssi_dbm - _quiet_peak)
|
||||
* (rssi_dbm > _quiet_peak ? 0.025f : 0.00005f)
|
||||
* filter_gap;
|
||||
}
|
||||
}
|
||||
// Never adapt the mark estimate down toward quiet during OFF debounce:
|
||||
// doing so moves its own threshold before the falling edge can commit.
|
||||
@@ -370,7 +395,7 @@ private:
|
||||
"fractional Morse timing must fit its existing 16-bit field");
|
||||
char _capture[CaptureCapacity + 1] = {};
|
||||
uint32_t _started = 0, _until = 0, _last_edge = 0, _candidate_at = 0;
|
||||
float _noise = 0, _signal = 0;
|
||||
float _noise = 0, _signal = 0, _quiet_peak = 0;
|
||||
float _last_rssi = NAN, _minimum_rssi = NAN, _maximum_rssi = NAN;
|
||||
uint32_t _samples = 0;
|
||||
uint32_t _last_sample_at = 0, _max_sample_gap_ms = 0;
|
||||
|
||||
+156
@@ -0,0 +1,156 @@
|
||||
// Synthetic envelopes reproduce the detector defect, not an RF qualification.
|
||||
// The shipping decoder sees the same samples in the positive/negative runs.
|
||||
#include <helpers/MorseRx.h>
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <initializer_list>
|
||||
|
||||
struct Receiver {
|
||||
mesh::MorseRx rx;
|
||||
uint32_t now;
|
||||
uint32_t cadence;
|
||||
Receiver(uint32_t step = 2, uint32_t started = 100) : now(started), cadence(step) {
|
||||
assert(rx.start(25, 300000, now) == mesh::MorseRxResult::Started);
|
||||
}
|
||||
void level(float dbm, uint32_t duration) {
|
||||
for (uint32_t elapsed = 0; elapsed < duration; elapsed += cadence, now += cadence)
|
||||
rx.sample(dbm, now);
|
||||
}
|
||||
void arm(float floor, float excursion) {
|
||||
level(floor, 100);
|
||||
level(excursion, 100);
|
||||
level(floor, 100);
|
||||
level(excursion, 100);
|
||||
level(floor, 120);
|
||||
assert(!rx.keyed() && rx.length() == 0);
|
||||
}
|
||||
};
|
||||
|
||||
static void learned_noise_cannot_prekey_a_real_dot() {
|
||||
for (uint32_t cadence = 1; cadence <= 10; ++cadence) for (float floor : {-140.0f, -126.0f, -90.0f}) {
|
||||
Receiver r(cadence);
|
||||
const float peak = floor + 15.5f;
|
||||
r.arm(floor, peak);
|
||||
r.level(peak, 100);
|
||||
assert(!r.rx.keyed() && r.rx.length() == 0);
|
||||
r.level(floor + 75, 48);
|
||||
r.level(floor, 400);
|
||||
r.rx.stop(r.now);
|
||||
assert(std::strcmp(r.rx.text(), "E ") == 0);
|
||||
mesh::MorseRxDiagnostics stats;
|
||||
r.rx.diagnostics(stats);
|
||||
assert(stats.minimum_dbm == floor && stats.maximum_dbm == floor + 75);
|
||||
assert(stats.last_dbm == floor && stats.max_sample_gap_ms == cadence);
|
||||
assert(!r.rx.active() && !r.rx.truncated());
|
||||
}
|
||||
}
|
||||
|
||||
static void recurrent_learned_excursions_stay_quiet_until_expiry() {
|
||||
for (uint32_t cadence = 1; cadence <= 10; ++cadence) for (uint32_t started : {100U, 0xffffff00U}) {
|
||||
Receiver r(cadence, started);
|
||||
r.arm(-126, -110.5f);
|
||||
while (r.rx.active()) {
|
||||
r.level(-110.5f, 100);
|
||||
r.level(-126, 320);
|
||||
assert(!r.rx.keyed() && r.rx.length() == 0);
|
||||
}
|
||||
assert(!r.rx.truncated() && r.rx.remainingMillis(r.now) == 0);
|
||||
char page[8] = {'X'};
|
||||
assert(r.rx.read(0, page, sizeof(page)) == 0 && page[0] == 0);
|
||||
mesh::MorseRxDiagnostics before, after;
|
||||
r.rx.diagnostics(before);
|
||||
r.level(-20, 1000);
|
||||
r.rx.diagnostics(after);
|
||||
assert(after.samples == before.samples && after.maximum_dbm == before.maximum_dbm);
|
||||
// A new valid session resets the learned environment and its transcript.
|
||||
assert(r.rx.start(25, 300000, r.now) == mesh::MorseRxResult::Started);
|
||||
r.level(-126, 520);
|
||||
r.level(-119, 48);
|
||||
r.level(-126, 400);
|
||||
r.rx.stop(r.now);
|
||||
assert(std::strcmp(r.rx.text(), "E ") == 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void weak_cw_is_relative_to_the_observed_environment() {
|
||||
for (uint32_t cadence = 1; cadence <= 10; ++cadence) for (float floor : {-150.0f, -127.5f, -100.0f, -70.0f}) {
|
||||
Receiver r(cadence);
|
||||
r.level(floor, 520);
|
||||
r.level(floor + 6, 48); // unchanged six-dB margin on a stable floor
|
||||
r.level(floor, 400);
|
||||
r.rx.stop(r.now);
|
||||
assert(std::strcmp(r.rx.text(), "E ") == 0);
|
||||
|
||||
Receiver noisy(cadence);
|
||||
noisy.arm(floor, floor + 15.5f);
|
||||
noisy.level(floor + 22.5f, 48); // seven dB above learned noise peaks
|
||||
noisy.level(floor, 400);
|
||||
noisy.rx.stop(noisy.now);
|
||||
assert(std::strcmp(noisy.rx.text(), "E ") == 0);
|
||||
|
||||
Receiver settled(cadence);
|
||||
settled.arm(floor, floor + 15.5f);
|
||||
settled.level(floor, 65000); // the noisier arming environment has gone away
|
||||
settled.level(floor + 7, 48);
|
||||
settled.level(floor, 400);
|
||||
settled.rx.stop(settled.now);
|
||||
assert(std::strcmp(settled.rx.text(), "E ") == 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void startup_outlier_spikes_and_late_arming_changes() {
|
||||
for (uint32_t cadence = 1; cadence <= 10; ++cadence) for (uint32_t started : {100U, 0xffffff00U}) {
|
||||
Receiver stale(cadence, started);
|
||||
stale.level(-75, cadence); // one stale initial reading, then a complete quiet arm
|
||||
stale.level(-126, 520);
|
||||
stale.level(-120, 48); // first message retains the six-dB boundary
|
||||
stale.level(-126, 400);
|
||||
stale.rx.stop(stale.now);
|
||||
assert(std::strcmp(stale.rx.text(), "E ") == 0);
|
||||
|
||||
{
|
||||
Receiver spike(cadence, started);
|
||||
spike.level(-126, 300);
|
||||
spike.level(-50, cadence); // an isolated arming spike cannot poison the envelope
|
||||
spike.level(-126, 220);
|
||||
spike.level(-119, 48);
|
||||
spike.level(-126, 400);
|
||||
spike.rx.stop(spike.now);
|
||||
assert(std::strcmp(spike.rx.text(), "E ") == 0);
|
||||
}
|
||||
|
||||
// Any final settling sample can be an outlier; the 50-ms phase boundary
|
||||
// must not retain it as the learned quiet ceiling for the whole capture.
|
||||
for (uint32_t at = 0; at < 50; at += cadence) {
|
||||
Receiver boundary(cadence, started);
|
||||
boundary.level(-126, at);
|
||||
boundary.level(-50, cadence);
|
||||
boundary.level(-126, 520);
|
||||
boundary.level(-119, 48);
|
||||
boundary.level(-126, 400);
|
||||
boundary.rx.stop(boundary.now);
|
||||
assert(std::strcmp(boundary.rx.text(), "E ") == 0);
|
||||
}
|
||||
|
||||
Receiver late(cadence, started);
|
||||
late.level(-126, 350);
|
||||
late.level(-110.5f, 170); // sustained change late in the existing arm window
|
||||
late.level(-126, 100);
|
||||
late.level(-110.5f, 100);
|
||||
assert(!late.rx.keyed() && late.rx.length() == 0);
|
||||
late.level(-103.5f, 48);
|
||||
late.level(-126, 400);
|
||||
late.rx.stop(late.now);
|
||||
assert(std::strcmp(late.rx.text(), "E ") == 0);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
learned_noise_cannot_prekey_a_real_dot();
|
||||
recurrent_learned_excursions_stay_quiet_until_expiry();
|
||||
weak_cw_is_relative_to_the_observed_environment();
|
||||
startup_outlier_spikes_and_late_arming_changes();
|
||||
std::puts("actual adaptive quiet checks passed");
|
||||
}
|
||||
Vendored
+1
-1
@@ -6,7 +6,7 @@
|
||||
#include <initializer_list>
|
||||
#include <vector>
|
||||
|
||||
static_assert(sizeof(mesh::MorseRx) == 4168, "gap fix must not add receiver memory");
|
||||
static_assert(sizeof(mesh::MorseRx) == 4172, "gap fix must not add receiver memory");
|
||||
struct Segment { bool keyed; uint32_t duration; };
|
||||
static uint32_t rounded(double value) { return uint32_t(std::lround(value)); }
|
||||
|
||||
|
||||
Vendored
+2
-2
@@ -9,9 +9,9 @@
|
||||
using mesh::MorseRx;
|
||||
|
||||
#if defined(MESH_MORSE_HIL) && MESH_MORSE_HIL
|
||||
static_assert(sizeof(MorseRx) == 4168 + sizeof(void*), "squelch must not add decoder fields");
|
||||
static_assert(sizeof(MorseRx) == ((4172 + alignof(void*) - 1) / alignof(void*)) * alignof(void*) + sizeof(void*), "squelch must not add decoder fields");
|
||||
#else
|
||||
static_assert(sizeof(MorseRx) == 4168, "shipping decoder layout must be unchanged");
|
||||
static_assert(sizeof(MorseRx) == 4172, "shipping decoder includes the learned quiet envelope");
|
||||
#endif
|
||||
|
||||
struct Receiver {
|
||||
|
||||
Vendored
+2
-2
@@ -15,9 +15,9 @@ using mesh::MorseRx;
|
||||
static_assert(std::is_same<decltype(MorseRx::_unit_q5), uint16_t>::value,
|
||||
"fractional timing must reuse the 16-bit timing field");
|
||||
#if defined(MESH_MORSE_HIL) && MESH_MORSE_HIL
|
||||
static_assert(sizeof(MorseRx) == 4168 + sizeof(void*), "HIL decoder size changed");
|
||||
static_assert(sizeof(MorseRx) == ((4172 + alignof(void*) - 1) / alignof(void*)) * alignof(void*) + sizeof(void*), "HIL decoder size changed");
|
||||
#else
|
||||
static_assert(sizeof(MorseRx) == 4168, "production decoder must remain 4168 bytes");
|
||||
static_assert(sizeof(MorseRx) == 4172, "production decoder includes one quiet-envelope float");
|
||||
#endif
|
||||
|
||||
static void begin(MorseRx& rx, uint8_t wpm, uint32_t now = 100) {
|
||||
|
||||
@@ -59,8 +59,6 @@ def legacy_estimator(original):
|
||||
header=header.replace(" if (rise < 6.0f) rise = 6.0f;",
|
||||
" if (rise < 6.0f) rise = 6.0f;\n if (rise > 24.0f) rise = 24.0f;")
|
||||
header = header.replace(" if (_keyed && threshold < _signal - 5.0f) threshold = _signal - 5.0f;\n", "")
|
||||
header = header.replace("float threshold = _noise + rise - (_keyed ? 3.0f : 0.0f);",
|
||||
"const float threshold = _noise + rise - (_keyed ? 3.0f : 0.0f);")
|
||||
header = header.replace("if (_keyed && raw_keyed && rssi_dbm > _noise + 6.0f)",
|
||||
"if (_keyed && rssi_dbm > _noise + 6.0f)")
|
||||
header = header.replace(" _signal = rssi_dbm;\n", "", 1)
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
"""Exercise adaptive quiet rejection in the actual shipping RSSI decoder."""
|
||||
from pathlib import Path
|
||||
import unittest
|
||||
|
||||
from test_morse_tx import compile_run
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
class MorseRxAdaptiveQuietTests(unittest.TestCase):
|
||||
def test_learned_excursions_weak_cw_expiry_restart_and_rollover(self):
|
||||
code = (ROOT / "test/fixtures/morse_rx_adaptive_quiet.cpp").read_text(encoding="ascii")
|
||||
for hil in (0, 1):
|
||||
with self.subTest(hil=hil):
|
||||
output = compile_run(self, code, flags=(f"-DMESH_MORSE_HIL={hil}",))
|
||||
self.assertIn("actual adaptive quiet checks passed", output)
|
||||
|
||||
def test_mean_only_negative_control_reproduces_noise_prekey(self):
|
||||
code = (ROOT / "test/fixtures/morse_rx_adaptive_quiet.cpp").read_text(encoding="ascii")
|
||||
source = (ROOT / "src/helpers/MorseRx.h").read_text(encoding="ascii")
|
||||
gate = " if (threshold < quiet_threshold) threshold = quiet_threshold;\n"
|
||||
self.assertEqual(source.count(gate), 1)
|
||||
old = source.replace(gate, " (void)quiet_threshold;\n")
|
||||
with self.assertRaisesRegex(AssertionError, "Assertion|assert"):
|
||||
compile_run(self, code, flags=("-DMESH_MORSE_HIL=0",),
|
||||
headers={"helpers/MorseRx.h": old,
|
||||
"helpers/MorseTx.h": (ROOT / "src/helpers/MorseTx.h").read_text(encoding="ascii")})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -11,7 +11,7 @@ class MorseRxTimingTests(unittest.TestCase):
|
||||
def test_actual_q5_drift_classification_bounds_fractional_gaps_rollover_and_size(self):
|
||||
code = (ROOT / "test/fixtures/morse_rx_timing.cpp").read_text(encoding="ascii")
|
||||
output = compile_run(self, code, flags=("-DMESH_MORSE_MAX_WPM=240", "-DMESH_MORSE_HIL=0"))
|
||||
self.assertIn("Q5 timing checks passed; MorseRx size: 4168 bytes", output)
|
||||
self.assertIn("Q5 timing checks passed; MorseRx size: 4172 bytes", output)
|
||||
|
||||
def test_hil_fast_off_keeps_the_same_fractional_timing_and_allocation_layout(self):
|
||||
code = (ROOT / "test/fixtures/morse_rx_timing.cpp").read_text(encoding="ascii")
|
||||
|
||||
Reference in new issue
Block a user