Keep sustained off-SF traffic out of the background noise estimate

Hardware captures show that a median and lower quartile can follow the interferer while the 12.5th percentile retains the quiet floor. Use that estimate with existing weighting and bounded rise recovery. Wait for settling after every RX re-arm and allow thirty seconds for a complete idle-sample block.

Add captured-sample, low-tail limitation, and real CLI setter regressions. Preserve RXPS intent when parsing an off command with trailing whitespace. All 162 native cases and 24 Python regressions pass.
This commit is contained in:
mikecarper committed 2026-09-10 13:52:21 -07:00
1 parent 238cca759a
commit 9c3b57eaa2
7 files changed
+180 -31

No files matched your search

+1 -1
View File
@@ -139,7 +139,7 @@ does not reflect the GoogleTest count -- run the built binary directly
| `test_persistent_store_format` | `src/helpers/PersistentStoreFormat.h` | contact-page headers and CRCs, dirty-page state, stable slot allocation, and bounded resumable legacy migration across power loss |
| `test_power_management` | `src/helpers/PowerManagementUtils.h` | median filtering of a brownout outlier and valid-reading requirements for the boot lock |
| `test_rx_power_saving` | `src/helpers/radiolib/RXPowerSaving.h` | guarded timer/capture bounds across SF5-SF12, bandwidths, TCXO delays and wire preambles; experimental profiles; retuning and strict CLI parsing |
| `test_noise_floor_estimator` | `src/helpers/radiolib/NoiseFloorEstimator.h` | spaced median, weighting, outliers, persistent rises, timeout/reset semantics and clock rollover |
| `test_noise_floor_estimator` | `src/helpers/radiolib/NoiseFloorEstimator.h` | spaced low percentile, captured off-SF traffic, weighting, contamination limits, persistent rises, resets and clock rollover |
| `test_region_names` | `src/helpers/RegionNameUtils.h` | canonical public-region markers while preserving distinct private and differently named regions |
| `test_datagram_payload_limits` | `src/helpers/DatagramPayloadLimits.h` | encrypted datagram plaintext ceilings, including the anonymous region-reply prefix and worst-case cipher padding |
| `test_serial_packet_log` | `src/helpers/SerialPacketLog.h` | bounded USB packet logging and dropped-line reporting |
@@ -19,20 +19,22 @@ TEST(NoiseFloorEstimator, RequiresACompleteSpacedBlock) {
EXPECT_GT(NoiseFloorEstimator::WINDOW_TIMEOUT_MS, 3150U);
}
TEST(NoiseFloorEstimator, MedianRejectsMinorityOutliersAndKeepsFractionalDbm) {
TEST(NoiseFloorEstimator, RejectsSparseLowOutliersAndMajorityTraffic) {
NoiseFloorEstimator n;
int32_t floor = 0;
for (unsigned i = 0; i < 64; ++i) {
ASSERT_TRUE(n.add(i < 15 ? -30 : i > 48 ? -127 : -103.25f, i * 50));
ASSERT_TRUE(n.add(i < 7 ? -127 : i >= 16 ? -30 : -103.25f, i * 50));
}
EXPECT_TRUE(n.publish(floor, false));
EXPECT_EQ(-10325, floor);
}
TEST(NoiseFloorEstimator, HandlesEvenMedianAndClampsAtMinus120) {
TEST(NoiseFloorEstimator, InterpolatesPercentileAndClampsAtMinus120) {
NoiseFloorEstimator n;
int32_t floor = 0;
for (unsigned i = 0; i < 64; ++i) n.add(i < 32 ? -105.5f : -104.5f, i * 50);
for (unsigned i = 0; i < 64; ++i) {
n.add(i < 7 ? -106 : i == 7 ? -105.5f : i == 8 ? -104.5f : -104, i * 50);
}
ASSERT_TRUE(n.publish(floor, false));
EXPECT_EQ(-10500, floor);
n.reset(); fill(n, -127);
@@ -97,15 +99,61 @@ TEST(NoiseFloorEstimator, SpacingSurvivesMillisRolloverAndRejectsInvalidReads) {
EXPECT_TRUE(n.add(-105, 0x22));
}
TEST(NoiseFloorEstimator, MajorityContaminationIsAnExplicitLimit) {
TEST(NoiseFloorEstimator, NearlyContinuousInterferenceIsAnExplicitLimit) {
NoiseFloorEstimator n;
int32_t floor = -10500;
for (unsigned block = 0; block < 3; ++block) {
n.reset();
for (unsigned i = 0; i < 64; ++i) n.add(i < 48 ? -60 : -105, i * 50);
for (unsigned i = 0; i < 64; ++i) n.add(i < 60 ? -60 : -105, i * 50);
EXPECT_EQ(block == 2, n.publish(floor, true));
}
EXPECT_EQ(-7125, floor); // a median cannot distinguish sustained energy from noise
EXPECT_EQ(-7125, floor); // too few quiet samples to distinguish traffic from noise
}
TEST(NoiseFloorEstimator, LowTailContaminationRequiresSettledReceiverSamples) {
NoiseFloorEstimator n;
int32_t floor = -10500;
for (unsigned i = 0; i < 64; ++i) n.add(i < 15 ? -127 : -105, i * 50);
ASSERT_TRUE(n.publish(floor, true));
EXPECT_EQ(-11625, floor); // the RX wrapper must exclude unsettled frontend readings
}
TEST(NoiseFloorEstimator, CapturedOffSfTrafficDoesNotBecomeTheBackgroundFloor) {
// Mercerwood Heltec V4: matching T1000-E frames plus dense RAK3401 traffic
// at another SF/BW. Sorted centi-dBm captures retain the sparse low outliers.
const int16_t quiet[64] = {
-8300, -8200, -8100, -8100, -7000, -7000, -7000, -7000,
-7000, -7000, -7000, -7000, -7000, -7000, -7000, -7000,
-7000, -7000, -7000, -7000, -7000, -7000, -6900, -6900,
-6900, -6900, -6900, -6900, -6900, -6900, -6900, -6900,
-6900, -6900, -6900, -6900, -6900, -6900, -6900, -6900,
-6900, -6900, -6900, -6900, -6900, -6900, -6900, -6900,
-6900, -6900, -6900, -6900, -6900, -6900, -6900, -6900,
-6900, -6900, -6900, -6900, -6900, -6800, -6500, -1100
};
const int16_t noisy[64] = {
-8200, -8200, -7000, -7000, -6900, -6900, -6900, -6900,
-6900, -6900, -6900, -6900, -6900, -6400, -4500, -4200,
-3600, -3600, -3500, -3400, -3400, -3300, -3200, -3200,
-3100, -3000, -3000, -3000, -2900, -2800, -2600, -2600,
-2500, -2400, -2400, -2400, -2300, -2300, -2300, -2300,
-2300, -2300, -2300, -2200, -2200, -2200, -2200, -2200,
-2100, -2100, -2100, -2000, -2000, -2000, -1900, -1900,
-1900, -1900, -1800, -1800, -1800, -1800, -1100, -1100
};
NoiseFloorEstimator n;
int32_t floor = 0;
for (unsigned i = 0; i < 64; ++i) n.add(quiet[i] / 100.0f, i * 50);
ASSERT_TRUE(n.publish(floor, false));
EXPECT_GE(floor, -7300);
EXPECT_LE(floor, -6800);
for (unsigned block = 0; block < 5; ++block) {
n.reset();
for (unsigned i = 0; i < 64; ++i) n.add(noisy[i] / 100.0f, i * 50);
n.publish(floor, true);
EXPECT_GE(floor, -7300);
EXPECT_LE(floor, -6800); // a median or lower quartile follows the interferer
}
}
int main(int argc, char** argv) {
+87 -2
View File
@@ -149,7 +149,7 @@ int main() {
assert(w.isChannelActive());
assert(state == (STATE_RX | STATE_INT_READY)); // preserves the new packet
}
// A quiet ten-second window completes in ~3.2 s and powersaving resumes.
// A quiet bounded window completes in ~3.2 s and powersaving resumes.
for (bool ps : {false, true}) {
RadioLibWrapper w;
w._rx_ps_enabled = w._rx_ps_armed = ps;
@@ -164,6 +164,21 @@ int main() {
assert(w._rx_ps_armed == ps);
assert(w.reads == 64); // no unnecessary SPI RSSI reads between accepted samples
}
// Each receive re-arm needs fresh frontend settling before sampling RSSI.
{
RadioLibWrapper w;
w.startRecv();
for (; now_ms < 120; ++now_ms) w.loop();
assert(w.reads == 0);
w.loop();
assert(w.reads == 1);
now_ms = 200;
w.startRecv(); // CAD/TX re-arm must settle again, retaining earlier samples
for (; now_ms < 220; ++now_ms) w.loop();
assert(w.reads == 1 && w._floor_estimator.count() == 1);
w.loop();
assert(w.reads == 2 && w._floor_estimator.count() == 2);
}
// Repeated scheduled requests must not reset a partial block.
{
RadioLibWrapper w;
@@ -177,7 +192,7 @@ int main() {
{
RadioLibWrapper w;
w.packet = true;
for (; now_ms <= 10101; ++now_ms) w.loop();
for (; now_ms <= NoiseFloorEstimator::WINDOW_TIMEOUT_MS + 101; ++now_ms) w.loop();
assert(!w._nf_refresh_requested);
assert(w._noise_floor_centi_dbm == -10500);
assert(w.reads == 0);
@@ -221,6 +236,76 @@ int main() {
'''
class RadioReceiveContractTest(unittest.TestCase):
def test_real_rxps_setter_preserves_intent_and_rejects_bad_values(self):
common = (ROOT / 'src/helpers/CommonCLI.cpp').read_text()
setter = method(common, 'if (memcmp(config, "radio.rxps ", 11) == 0)')
calculator = method(common, 'bool CommonCLI::calculateRxPowerSavingLevel(')
source = r'''
#include <cassert>
#include <cstring>
#include <initializer_list>
#include <helpers/CLICommandUtils.h>
#include <helpers/radiolib/RXPowerSaving.h>
struct NodePrefs {
uint8_t rx_powersaving_enabled = 1, rx_ps_level = 6, rx_ps_preamble = 32, sf = 8;
uint32_t rx_ps_rx_us = 40000, rx_ps_sleep_us = 32000;
float bw = 62.5;
};
struct Callbacks {
bool accept = true;
unsigned calls = 0;
bool setRxPowerSaving(bool, uint32_t, uint32_t) { ++calls; return accept; }
};
struct CommonCLI {
NodePrefs* _prefs;
Callbacks* _callbacks;
unsigned saves = 0;
void savePrefs() { ++saves; }
bool calculateRxPowerSavingLevel(uint32_t, uint8_t, float, uint32_t, uint32_t*, uint32_t*);
void set(const char* config, char* reply);
};
void appendRxPowerSavingAdjustmentNote(char*, const NodePrefs*, uint8_t, float) {}
@CALCULATOR@
void CommonCLI::set(const char* config, char* reply) { @SETTER@ }
int main() {
NodePrefs p;
Callbacks callbacks;
CommonCLI cli{&p, &callbacks};
char reply[160] = {};
cli.set("radio.rxps level 8 preamble 32", reply);
assert(p.rx_powersaving_enabled && p.rx_ps_level == 8 && p.rx_ps_preamble == 32);
const uint32_t rx = p.rx_ps_rx_us, sleep = p.rx_ps_sleep_us;
cli.set("radio.rxps off \t", reply);
assert(!p.rx_powersaving_enabled && p.rx_ps_level == 8 && p.rx_ps_preamble == 32);
assert(p.rx_ps_rx_us == rx && p.rx_ps_sleep_us == sleep && cli.saves == 2);
for (const char* command : {"radio.rxps level 264 preamble 32",
"radio.rxps level 8 preamble 288", "radio.rxps level 4294967296",
"radio.rxps 32 1000", "radio.rxps max extra"}) {
cli.set(command, reply);
assert(cli.saves == 2 && callbacks.calls == 2);
assert(!p.rx_powersaving_enabled && p.rx_ps_level == 8 && p.rx_ps_preamble == 32);
}
cli.set("radio.rxps 12345 67890", reply);
assert(p.rx_powersaving_enabled && p.rx_ps_level == 0 && p.rx_ps_preamble == 0);
assert(p.rx_ps_rx_us == 12345 && p.rx_ps_sleep_us == 67890 && cli.saves == 3);
callbacks.accept = false;
cli.set("radio.rxps max", reply);
assert(p.rx_ps_level == 0 && p.rx_ps_rx_us == 12345 && cli.saves == 3);
callbacks.accept = true;
cli.set("radio.rxps max", reply);
assert(p.rx_ps_level == 8 && p.rx_ps_preamble == 16 && cli.saves == 4);
}
'''
with tempfile.TemporaryDirectory(prefix='meshcore-rxps-cli-') as tmp:
cpp, binary = Path(tmp) / 'test.cpp', Path(tmp) / 'test'
cpp.write_text(source.replace('@CALCULATOR@', calculator).replace('@SETTER@', setter))
result = subprocess.run([os.environ.get('CXX', 'c++'), '-std=c++17', '-O1',
'-I', str(ROOT / 'test/mocks'), '-I', str(ROOT / 'src'),
str(cpp), '-o', str(binary)], capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stderr)
result = subprocess.run([str(binary)], capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stderr)
def test_production_transitions(self):
source = (ROOT / 'src/helpers/radiolib/RadioLibWrappers.cpp').read_text()
names = [('int16_t','performChannelScanWithTimeout'), ('bool','isPacketPendingOrReceiving'),