FastLED 3.9.15
Loading...
Searching...
No Matches

◆ calculateConfidence() [2/2]

float fl::audio::detector::Pitch::calculateConfidence ( const vector< float > & autocorr,
int peakLag ) const
private

Definition at line 171 of file pitch.cpp.hpp.

171 {
172 // Confidence based on:
173 // 1. Strength of the autocorrelation peak
174 // 2. Ratio of peak to nearby values (peak clarity)
175
176 if (peakLag <= 0 || peakLag >= static_cast<int>(autocorr.size())) {
177 return 0.0f;
178 }
179
180 float peakValue = autocorr[static_cast<size>(peakLag)];
181
182 // Confidence is primarily based on peak strength
183 // Autocorrelation ranges from -1 to 1, but we only care about positive peaks
184 float confidence = fl::max(0.0f, fl::min(1.0f, peakValue));
185
186 // Calculate clarity by comparing peak to surrounding values
187 // Look at ±10% of the period
188 int windowSize = fl::max(2, peakLag / 10);
189 float neighborSum = 0.0f;
190 int neighborCount = 0;
191
192 for (int offset = -windowSize; offset <= windowSize; offset++) {
193 if (offset == 0) continue; // Skip the peak itself
194
195 int lag = peakLag + offset;
196 if (lag >= mMinPeriod && lag <= mMaxPeriod && lag < static_cast<int>(autocorr.size())) {
197 neighborSum += fl::max(0.0f, autocorr[static_cast<size>(lag)]);
198 neighborCount++;
199 }
200 }
201
202 if (neighborCount > 0) {
203 float neighborAvg = neighborSum / static_cast<float>(neighborCount);
204
205 // Peak should be significantly higher than neighbors
206 // If peak is 2x higher, that's good; if it's similar, reduce confidence
207 if (neighborAvg > 1e-6f) {
208 float clarity = fl::min(1.0f, (peakValue - neighborAvg) / neighborAvg);
209 confidence *= (0.7f + 0.3f * clarity); // Weight clarity at 30%
210 }
211 }
212
213 return confidence;
214}
fl::UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NOEXCEPT
Definition math.h:71
constexpr common_type_t< T, U > max(T a, U b) FL_NOEXCEPT
Definition math.h:75

References fl::max(), fl::min(), mMaxPeriod, mMinPeriod, offset(), and fl::vector_basic::size().

Referenced by calculateAutocorrelation().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: