From 4fa41ea327ac0370d373089f714ad43b15c28fd4 Mon Sep 17 00:00:00 2001 From: Yegor Alexeyev Date: Fri, 30 Oct 2026 08:49:59 +0100 Subject: [PATCH] implemented 99th percentile method for global peak amplitude calculation. This is more robust. --- fon/Sound_to_Pitch.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/fon/Sound_to_Pitch.cpp b/fon/Sound_to_Pitch.cpp index cd030a37c3..e683179fbc 100644 --- a/fon/Sound_to_Pitch.cpp +++ b/fon/Sound_to_Pitch.cpp @@ -36,6 +36,8 @@ #include "NUM2.h" #include "Sound_and_Spectrum.h" +#include + #define AC_HANNING 0 #define AC_GAUSS 1 #define FCC_NORMAL 2 @@ -362,18 +364,29 @@ autoPitch Sound_to_Pitch_any (Sound me, /* Compute the global absolute peak for determination of silence threshold. */ + std::set maximums; + size_t nmaximums = (my nx * my ny) / 100; // 99th percentile + globalPeak = 0.0; for (integer ichan = 1; ichan <= my ny; ichan ++) { const double mean = NUMmean (my z.row (ichan)); for (integer i = 1; i <= my nx; i ++) { double value = fabs (my z [ichan] [i] - mean); - if (value > globalPeak) + + if (maximums.size() == 0 || value >= *maximums.begin()) { + if (maximums.size() == nmaximums) { + maximums.erase(maximums.begin()); + } + maximums.insert(value); globalPeak = value; + } } } - if (globalPeak == 0.0) + if (maximums.size() == 0) return thee; + globalPeak = *maximums.begin(); + autoVEC window, windowR; if (method >= FCC_NORMAL) { // for cross-correlation analysis