FastLED 3.10.8
Loading...
Searching...
No Matches

◆ calculateAutocorrelation() [2/2]

float fl::audio::detector::Pitch::calculateAutocorrelation ( const i16 * pcm,
size numSamples )
private

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

108 {
109 // Clear and resize autocorrelation buffer
110 mAutocorrelation.clear();
111 mAutocorrelation.resize(static_cast<size>(mMaxPeriod + 1), 0.0f);
112
113 // Normalize input to float range [-1, 1]
114 const float normFactor = 1.0f / 32768.0f;
115
116 // Calculate autocorrelation for all lags in the period range
117 // ACF[k] = sum(signal[n] * signal[n + k]) for all valid n
118 for (int lag = mMinPeriod; lag <= mMaxPeriod; lag++) {
119 float sum = 0.0f;
120 int validSamples = 0;
121
122 for (size i = 0; i + lag < numSamples; i++) {
123 float s1 = static_cast<float>(pcm[i]) * normFactor;
124 float s2 = static_cast<float>(pcm[i + lag]) * normFactor;
125 sum += s1 * s2;
126 validSamples++;
127 }
128
129 // Normalize by number of samples
130 if (validSamples > 0) {
131 mAutocorrelation[static_cast<size>(lag)] = sum / static_cast<float>(validSamples);
132 }
133 }
134
135 // Find the lag with maximum autocorrelation (best period match)
136 int bestLag = findBestPeakLag(mAutocorrelation);
137
138 // Calculate confidence based on autocorrelation peak
139 if (bestLag > 0) {
141
142 // Convert period (lag) to frequency
143 return periodToFrequency(bestLag);
144 }
145
146 // No reliable pitch found
147 mConfidence = 0.0f;
148 return 0.0f;
149}
vector< float > mAutocorrelation
Definition pitch.h:91
float calculateConfidence(const vector< float > &autocorr, int peakLag) const FL_NO_EXCEPT
float periodToFrequency(int period) const FL_NO_EXCEPT
int findBestPeakLag(const vector< float > &autocorr) const FL_NO_EXCEPT

References calculateConfidence(), findBestPeakLag(), fl::FL_NO_EXCEPT, mAutocorrelation, mConfidence, mMaxPeriod, mMinPeriod, and periodToFrequency().

Referenced by update().

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