MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ compute_harmonic_energy()

std::vector< double > MayaFlux::Yantra::compute_harmonic_energy ( std::span< const double >  data,
const size_t  num_windows,
const uint32_t  hop_size,
const uint32_t  window_size 
)

Compute harmonic energy using low-frequency FFT analysis.

This function computes the harmonic energy for a given data span. It focuses on the lower portion of the frequency spectrum (harmonics).

Parameters
dataInput data span
num_windowsNumber of windows to process
hop_sizeHop size for windowing
window_sizeSize of each window
Returns
Vector of harmonic energy values

Definition at line 175 of file AnalysisHelper.cpp.

176{
177 std::vector<double> harmonic_energy(num_windows);
178
179 std::vector<size_t> indices(num_windows);
180 std::iota(indices.begin(), indices.end(), 0);
181
182 Eigen::VectorXd hanning_window(window_size);
183 for (uint32_t i = 0; i < window_size; ++i) {
184 hanning_window(i) = 0.5 * (1.0 - std::cos(2.0 * M_PI * i / (window_size - 1)));
185 }
186
187 std::for_each(std::execution::par_unseq, indices.begin(), indices.end(),
188 [&](size_t i) {
189 const size_t start_idx = i * hop_size;
190 const size_t end_idx = std::min(start_idx + window_size, data.size());
191 auto window = data.subspan(start_idx, end_idx - start_idx);
192
193 Eigen::VectorXd windowed_data = Eigen::VectorXd::Zero(window_size);
194 const size_t actual_size = window.size();
195
196 for (int j = 0; j < actual_size; ++j) {
197 windowed_data(j) = window[j] * hanning_window(j);
198 }
199
200 Eigen::FFT<double> fft;
201 Eigen::VectorXcd fft_result;
202 fft.fwd(fft_result, windowed_data);
203
204 const int harmonic_bins = std::max(1, static_cast<int>(fft_result.size() / 8));
205 double energy = 0.0;
206
207 for (int j = 1; j < harmonic_bins; ++j) {
208 energy += std::norm(fft_result(j));
209 }
210
211 harmonic_energy[i] = energy / static_cast<double>(harmonic_bins);
212 });
213
214 return harmonic_energy;
215}

References compute_harmonic_energy().

Referenced by MayaFlux::Yantra::EnergyAnalyzer< InputType, OutputType >::compute_energy_values(), and compute_harmonic_energy().

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