Skip to content

Commit

Permalink
Improve the peak estimate
Browse files Browse the repository at this point in the history
Fixes bugs with peak estimate that can occur when denom is low. This is still highly accurate :)
  • Loading branch information
Quick-Flash authored and bkleiner committed Nov 5, 2023
1 parent d8e6905 commit 2713138
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/flight/sdft.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,17 @@ bool sdft_update(sdft_t *sdft) {
continue;
}

const float y0 = sdft->magnitude[sdft->peak_indicies[peak] - 1];
const float y1 = 1.75f * sdft->magnitude[sdft->peak_indicies[peak]];
const float y0 = 0.95 * sdft->magnitude[sdft->peak_indicies[peak] - 1];
const float y1 = sdft->magnitude[sdft->peak_indicies[peak]];
const float y2 = 1.25f * sdft->magnitude[sdft->peak_indicies[peak] + 1];

// Estimate true peak position aka. meanBin (fit parabola y(x) over y0, y1 and y2, solve dy/dx=0 for x)
float meanBin = sdft->peak_indicies[peak];
const float denom = 2.0f * (y0 - y1 + y2);
const float denom = y0 + y1 + y2;
if (denom != 0.0f) {
meanBin += (y0 - y2) / denom;
float lower_ratio = y0 / denom;
float upper_ratio = y2 / denom;
meanBin += upper_ratio - lower_ratio;
}

const float f_hz = meanBin * (float)resolution_hz;
Expand Down Expand Up @@ -224,4 +226,4 @@ bool sdft_update(sdft_t *sdft) {
}

return filters_updated;
}
}

0 comments on commit 2713138

Please sign in to comment.