Skip to content

Commit c4dba11

Browse files
authored
Improve peak estimation
fix an issue where the old estimation can cause some very off very bad numbers. When the old denom would be near 0 it would create wildly inaccurate peak estimations, this fixes that.
1 parent 28dd82e commit c4dba11

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/main/flight/dyn_notch_filter.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
// Each SDFT output bin has width sdftSampleRateHz/72, ie 18.5Hz per bin at 1333Hz.
8484
// Usable bandwidth is half this, ie 666Hz if sdftSampleRateHz is 1333Hz, i.e. bin 1 is 18.5Hz, bin 2 is 37.0Hz etc.
8585

86-
#define DYN_NOTCH_SMOOTH_HZ 4
86+
#define DYN_NOTCH_SMOOTH_HZ 8
8787
#define DYN_NOTCH_CALC_TICKS (XYZ_AXIS_COUNT * STEP_COUNT) // 3 axes and 4 steps per axis
8888
#define DYN_NOTCH_OSD_MIN_THROTTLE 20
8989
#define DYN_NOTCH_UPDATE_MIN_HZ 2000
@@ -343,14 +343,16 @@ static FAST_CODE_NOINLINE void dynNotchProcess(void)
343343
float meanBin = peaks[p].bin;
344344

345345
// Height of peak bin (y1) and shoulder bins (y0, y2)
346-
const float y0 = sdftData[peaks[p].bin - 1];
346+
const float y0 = sdftData[peaks[p].bin - 1] * 0.95;
347347
const float y1 = sdftData[peaks[p].bin];
348-
const float y2 = sdftData[peaks[p].bin + 1];
348+
const float y2 = sdftData[peaks[p].bin + 1] * 1.25;
349349

350-
// Estimate true peak position aka. meanBin (fit parabola y(x) over y0, y1 and y2, solve dy/dx=0 for x)
351-
const float denom = 2.0f * (y0 - 2 * y1 + y2);
350+
// Estimate true peak position
351+
const float denom = y0 + y1 + y2;
352352
if (denom != 0.0f) {
353-
meanBin += (y0 - y2) / denom;
353+
float upper_ratio = y2 / denom;
354+
float lower_ratio = y0 / denom;
355+
meanBin += upper_ratio - lower_ratio;
354356
}
355357

356358
// Convert bin to frequency: freq = bin * binResoultion (bin 0 is 0Hz)

0 commit comments

Comments
 (0)