Repository navigation
Statistical Adaptive SNR Thresholds - #3302
Conversation
|
Uploading ELRS Quad-2025-08-17-175240.csv… In order to address the slowly degrading SNR, the power-up threshold has been made more (linearly) sensitive when the LQ drops below 100. |
|
@SunjunKim |
I still want on 3.x because it'll solve LR1121 power adjustment problem which is already deployed quite a lot. |
|
No news is good news? Should I update my RX too or can I test this by just updating my TX? |
|
Initial testing with this PR on TX and 3.6.0 release on RX seems OK. However, when flying the one brief spot that causes my TX to ramp up from from 10mw I noticed my TX did not consistently back off/reduce output power. Sometimes it would ramp back down a few power levels in a quick burst but not typically all the way back to 10mw other times it would stay at a much higher power level than expected for a much longer period than expected. RadioMaster Nomad TX with antenna mode switch with this PR EdgeTX log sample stripped to include the link related columns. I intend to test tomorrow with Nomad TX in single antenna mode. |
|
@SunjunKim Can you suggest the correct way for me to adjust sensitivity? I would like to reduce sensitivity to SNR/RSSI changes and rely more heavily on LQ. observations: extra detail: |
|
I haven't commented on this because I've just looked at the code and have some changes I think are needed, but was waiting until I had a chance to use the code in action. I'd like to apply myself as a blocker 😆 to prevent this getting merged until I have time for it, but it seems like pitts-mo has possible things we might need to address as well |
|
This dynamic power implementation still feels too defensive for me but it did feel much better during my flights today. details: In these cases I found the behavior of dynamic TX power much more predictable. It was still bumping up to higher power levels than needed but still <250mw and staying up at those higher output powers longer than needed. However, power output levels were reducing in a much more predictable/desirable way than my previous single antenna RX tests in a thickly settled area. |
|
@SunjunKim I've retargetted this to master and made a couple changes. We can consider this as a backport for a 3.7.0 but let's work on master first where all the new features go and there's a ton of testing coming up for.
However, can we not do float operations? The result of mean and standardDeviation is like divided by 256, turned into a float, multiplied by 16, then converted back to an int. Can we just use the raw fixed value and scale it to the same integer value? diff --git a/src/lib/MeanAccumulator/StdevAccumulator.h b/src/lib/MeanAccumulator/StdevAccumulator.h
index 08027c1f..bdb05e47 100644
--- a/src/lib/MeanAccumulator/StdevAccumulator.h
+++ b/src/lib/MeanAccumulator/StdevAccumulator.h
@@ -3,12 +3,6 @@
#include <cstdint>
#include <cmath>
-// Fixed-point arithmetic constants
-// Using 16-bit fixed-point with 8 fractional bits (Q8.8 format)
-#define FIXED_POINT_SHIFT 8
-#define FIXED_POINT_SCALE (1 << FIXED_POINT_SHIFT)
-#define FIXED_POINT_MASK (FIXED_POINT_SCALE - 1)
-
// Fast integer square root using binary search
// algorithm adopted from https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_(base_2
)
uint16_t fast_sqrt_uint(uint32_t x)
@@ -43,7 +37,12 @@ uint16_t fast_sqrt_uint(uint32_t x)
class StdevAccumulator
{
public:
- static const size_t WINDOW_SIZE = 48;
+ static constexpr size_t WINDOW_SIZE = 48;
+ // Fixed-point arithmetic constants
+ // Using 16-bit fixed-point with 8 fractional bits (Q8.8 format)
+ static constexpr uint32_t FIXED_POINT_SHIFT = 8;
+ static constexpr uint32_t FIXED_POINT_SCALE = 1 << FIXED_POINT_SHIFT;
+ static constexpr uint32_t FIXED_POINT_MASK = FIXED_POINT_SCALE - 1;
StdevAccumulator()
{
@@ -80,35 +79,45 @@ public:
/// @brief Computes and returns the mean of the current buffer contents.
/// @return Mean as float, or 0.0f if no data
float mean()
+ {
+ return static_cast<float>(meanRaw()) / FIXED_POINT_SCALE;
+ }
+
+ int32_t meanRaw()
{
if (_count == 0)
{
- return 0.0f;
+ return 0;
}
if (!_dirty)
{
- return static_cast<float>(_lastMeanFixed) / FIXED_POINT_SCALE;
+ return _lastMeanFixed;
}
// Fixed-point division using bit shifting
_lastMeanFixed = (_sum << FIXED_POINT_SHIFT) / _count;
// Don't clear _dirty here, as stdev may still need update
- return static_cast<float>(_lastMeanFixed) / FIXED_POINT_SCALE;
+ return _lastMeanFixed;
}
/// @brief Computes and returns the standard deviation of the current buffer contents.
/// @return Standard deviation as float, or 0.0f if not enough data
float standardDeviation()
+ {
+ return static_cast<float>(standardDeviationRaw()) / FIXED_POINT_SCALE;
+ }
+
+ int32_t standardDeviationRaw()
{
if (_count < 2)
{
- return 0.0f;
+ return 0;
}
if (!_dirty)
{
- return static_cast<float>(_lastStdevFixed) / FIXED_POINT_SCALE;
+ return _lastStdevFixed;
}
// Calculate mean in fixed-point
@@ -136,7 +145,7 @@ public:
// Note: sqrt(variance * 256) = sqrt(variance) * 16, so we need to scale back to 256
_lastStdevFixed = fast_sqrt_uint(varianceFixed) << 4;
_dirty = false;
- return static_cast<float>(_lastStdevFixed) / FIXED_POINT_SCALE;
+ return _lastStdevFixed;
}
/// @brief Returns the number of valid entries (up to WINDOW_SIZE)
diff --git a/src/src/dynpower.cpp b/src/src/dynpower.cpp
index 6bab1ad8..24c1c06c 100644
--- a/src/src/dynpower.cpp
+++ b/src/src/dynpower.cpp
@@ -148,10 +148,6 @@ void DynamicPower_Update(uint32_t now)
// the calculation could exceed 100% during a rate change or initial connect when the LQs are not synced
lq_current = std::min(lq_current * 100 / std::max((uint32_t)LBTSuccessCalc.getLQ(), (uint32_t)1U), (uint32_t)100U);
#endif
- if (lq_current >= 99)
- {
- dynpower_stat_snr.add(snrScaled);
- }
uint32_t lq_avg = dynpower_mavg_lq;
int32_t lq_diff = lq_avg - lq_current;
dynpower_mavg_lq.add(lq_current);
@@ -195,11 +191,17 @@ void DynamicPower_Update(uint32_t now)
int8_t snr_stat_threshold_up = ExpressLRS_currAirRate_RFperfParams->DynpowerSnrThreshUp;
int8_t snr_stat_threshold_dn = ExpressLRS_currAirRate_RFperfParams->DynpowerSnrThreshDn;
+ // Incorporate the current value if LQ is meets the desired LQ standard
+ if (lq_current >= 99)
+ {
+ dynpower_stat_snr.add(snrScaled);
+ }
// is SNR stat ready? = is the buffer fully stuffed?
if(dynpower_stat_snr.getCount() >= dynpower_stat_snr.WINDOW_SIZE)
{
- int32_t snr_stat_mean = static_cast<int32_t>(dynpower_stat_snr.mean()*16);
- int32_t snr_stat_stdev = static_cast<int32_t>(dynpower_stat_snr.standardDeviation()*16);
+ static_assert(dynpower_stat_snr.FIXED_POINT_SHIFT >= 4, "StdDevAccumulator must be at least 4 bits of decimal fixed point");
+ int32_t snr_stat_mean = dynpower_stat_snr.meanRaw() >> (dynpower_stat_snr.FIXED_POINT_SHIFT - 4);
+ int32_t snr_stat_stdev = dynpower_stat_snr.standardDeviationRaw() >> (dynpower_stat_snr.FIXED_POINT_SHIFT - 4);
// Fuzzy logic: reduce scale factor when LQ is getting low for more conservative power management
// Base scale factor is 13/4 (3.25), reduce it proportionally when LQ < 100
@@ -213,6 +215,7 @@ void DynamicPower_Update(uint32_t now)
int8_t snr_thre_dn_scaled = static_cast<int8_t>((snr_stat_mean + snr_stat_stdev*3/2)/16); // +1.5 sd
int8_t snr_thre_up_limit = static_cast<int8_t>((snr_stat_mean)/16)-SNR_SCALE(1.0); // to ensure at least -1.0 dB split between thresholds
+ //DBGLN("cur=%d tup=%d tdn=%d lim=%d mean=%d sd=%d", snrScaled, snr_thre_up_scaled, snr_thre_dn_scaled, snr_thre_up_limit, snr_stat_mean, snr_stat_stdev);
snr_stat_threshold_up = std::min(snr_thre_up_scaled, snr_thre_up_limit);
snr_stat_threshold_dn = snr_thre_dn_scaled;
} |
|
@CapnBry we want to avoid float math as, much as possible as the 8285 & C3 does NOT have an FPU |
Absolutely! That's why I was asking sunjun if it was ok (and if it would preserve the correct value) to skip the intermediate floating point step and just use raw values and shifts. It seems to work just fine but I didn't want to change any of the math without checking first. |
|
This appears to work well in Master ac862ac . The new dynamic power is still bumping up to higher power levels than I am familiar with but I have not hit an instance that feels absurdly excessive. I think all my flights so far have remained >=100mw (1w dynamic). Power ramp down is also much quicker and more predictable than the last time I tested this. I plan to keep an eye on my TX power outputs for a while especially when my RX does not have a PA. |





Idea
The current dynamic power management system uses fixed SNR thresholds for power adjustments, which is not working optimal especially on LR1121 RX. This PR introduces statistical SNR threshold calculation that adapts to actual signal quality conditions.
Instead of using predetermined thresholds, the system now:
How it works
StdevAccumulator Implementation
Dynamic Threshold Calculation
mean - 3.5*stdev(for power increase when SNR is low). When LQ is less than 100, for earlier power up, the scale factor (3.5 by default) goes lower linearly, all the way down to 0.5 when LQ = 85.mean + 1.5*stdev(for power decrease when SNR is high)Performance
-4stdev -- +1.5stdevis my current go-to setting, but feel free to test other values.common.cpp) will be used as a backup.Remark