FastLED 3.10.6
Loading...
Searching...
No Matches

◆ calculateBandEnergy()

float fl::audio::detector::FrequencyBands::calculateBandEnergy ( const fft::Bins & fft,
float minFreq,
float maxFreq,
float fftMinFreq,
float fftMaxFreq )
private

Definition at line 113 of file frequency_bands.cpp.hpp.

114 {
115 const int numBins = static_cast<int>(fft.raw().size());
116 if (numBins <= 1) {
117 return 0.0f;
118 }
119
120 float totalEnergy = 0.0f;
121 float totalWeight = 0.0f;
122
123 for (int i = 0; i < numBins; i++) {
124 // Compute the frequency range for this CQ bin using log-spaced
125 // boundaries that match the CQ kernel's actual frequency layout.
126 float binLow;
127 float binHigh;
128 if (i == 0) {
129 binLow = fftMinFreq;
130 } else {
131 binLow = fft.binBoundary(i - 1);
132 }
133 if (i == numBins - 1) {
134 binHigh = fftMaxFreq;
135 } else {
136 binHigh = fft.binBoundary(i);
137 }
138
139 // Calculate fractional overlap between this bin and the target band
140 float overlapMin = fl::max(binLow, minFreq);
141 float overlapMax = fl::min(binHigh, maxFreq);
142
143 if (overlapMax <= overlapMin) {
144 continue; // No overlap
145 }
146
147 float binWidth = binHigh - binLow;
148 float overlapFraction = (overlapMax - overlapMin) / binWidth;
149
150 totalEnergy += fft.raw()[i] * overlapFraction;
151 totalWeight += overlapFraction;
152 }
153
154 // Normalize by total fractional weight to make bands comparable
155 return (totalWeight > 0.0f) ? totalEnergy / totalWeight : 0.0f;
156}
AudioAnalyzeFFT1024 fft
constexpr common_type_t< T, U > max(T a, U b) FL_NO_EXCEPT
Definition math.h:75
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NO_EXCEPT
Definition math.h:71

References fl::max(), and fl::min().

Referenced by update().

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