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

◆ calculateAutocorrelationIrregularity()

float fl::audio::detector::Vocal::calculateAutocorrelationIrregularity ( span< const i16 > pcm)
private

Definition at line 407 of file vocal.cpp.hpp.

407 {
408 const int n = static_cast<int>(pcm.size());
409
410 // Vocal fundamental lag range at mSampleRate
411 const int minLag = fl::max(2, mSampleRate / 500); // 500 Hz
412 const int maxLag = fl::min(n / 2, mSampleRate / 172); // 172 Hz
413
414 if (minLag >= maxLag || maxLag >= n) return 0.0f;
415
416 // Subsample by 4: safe since step(4) << minLag(88).
417 // normFactor² cancels in the ratio sum/acf0, so work in raw i16 space.
418 const int step = 4;
419
420 // ACF[0] = energy (subsampled)
421 float acf0 = 0.0f;
422 for (int i = 0; i < n; i += step) {
423 float s = static_cast<float>(pcm[i]);
424 acf0 += s * s;
425 }
426 if (acf0 < 1.0f) return 0.0f;
427
428 // Probe 16 evenly-spaced lags, each with subsampled inner loop.
429 // Total: ~16 × (n/4)/lag ≈ 1700 MACs (vs 34K without subsampling).
430 const int lagRange = maxLag - minLag;
431 const int maxProbes = 16;
432 const int lagStep = fl::max(1, lagRange / maxProbes);
433 float bestPeak = 0.0f;
434 for (int lag = minLag; lag <= maxLag; lag += lagStep) {
435 float sum = 0.0f;
436 for (int i = 0; i + lag < n; i += step) {
437 sum += static_cast<float>(pcm[i]) * static_cast<float>(pcm[i + lag]);
438 }
439 float normalized = sum / acf0;
440 bestPeak = fl::max(bestPeak, normalized);
441 }
442
443 return 1.0f - bestPeak; // 0 = perfectly periodic, 1 = no periodicity
444}
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
constexpr enable_if< is_fixed_point< T >::value, T >::type step(T edge, T x) FL_NOEXCEPT

References fl::max(), fl::min(), mSampleRate, fl::span< T, Extent >::size(), and fl::step().

Referenced by update().

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