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

◆ compute_spectral_energy()

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

Compute spectral energy using FFT-based analysis.

This function computes the spectral energy for a given data span using FFT. It calculates the magnitude spectrum and sums the energy across frequency bins.

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

Definition at line 135 of file AnalysisHelper.cpp.

136{
137 std::vector<double> spectral_energy(num_windows);
138
139 std::vector<size_t> indices(num_windows);
140 std::iota(indices.begin(), indices.end(), 0);
141
142 Eigen::VectorXd hanning_window(window_size);
143 for (uint32_t i = 0; i < window_size; ++i) {
144 hanning_window(i) = 0.5 * (1.0 - std::cos(2.0 * M_PI * i / (window_size - 1)));
145 }
146
147 std::for_each(std::execution::par_unseq, indices.begin(), indices.end(),
148 [&](size_t i) {
149 const size_t start_idx = i * hop_size;
150 const size_t end_idx = std::min(start_idx + window_size, data.size());
151 auto window = data.subspan(start_idx, end_idx - start_idx);
152
153 Eigen::VectorXd windowed_data = Eigen::VectorXd::Zero(window_size);
154 const size_t actual_size = window.size();
155
156 for (int j = 0; j < actual_size; ++j) {
157 windowed_data(j) = window[j] * hanning_window(j);
158 }
159
160 Eigen::FFT<double> fft;
161 Eigen::VectorXcd fft_result;
162 fft.fwd(fft_result, windowed_data);
163
164 double energy = 0.0;
165 for (int j = 0; j < fft_result.size(); ++j) {
166 energy += std::norm(fft_result(j));
167 }
168
169 spectral_energy[i] = energy / static_cast<double>(window_size);
170 });
171
172 return spectral_energy;
173}

References compute_spectral_energy().

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

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