Extract data from regions with high spectral energy.
256{
257 std::vector<std::vector<double>> result;
258 result.reserve(data.size());
259
260 for (const auto& channel : data) {
261 if (channel.empty()) {
262 result.emplace_back();
263 continue;
264 }
265
266 uint32_t effective_window_size = std::min(window_size, static_cast<uint32_t>(channel.size()));
267 uint32_t effective_hop_size = std::min(hop_size, effective_window_size / 2);
268 if (effective_hop_size == 0)
269 effective_hop_size = 1;
270
271 if (!validate_extraction_parameters(effective_window_size, effective_hop_size, channel.size())) {
272 result.emplace_back();
273 continue;
274 }
275
276 try {
277 auto energy_analyzer = std::make_shared<EnergyAnalyzer<std::vector<Kakshya::DataVariant>, Eigen::VectorXd>>(
278 effective_window_size, effective_hop_size);
279 energy_analyzer->set_parameter("method", "spectral");
280
281 std::vector<Kakshya::DataVariant> data_variant { Kakshya::DataVariant { std::vector<double>(channel.begin(), channel.end()) } };
282 EnergyAnalysis energy_result = energy_analyzer->analyze_energy(data_variant);
283
284 if (energy_result.channels.empty() || energy_result.channels[0].energy_values.empty() || energy_result.channels[0].window_positions.empty()) {
285 result.emplace_back();
286 continue;
287 }
288
289 std::vector<std::pair<size_t, size_t>> qualifying_windows;
290 const auto& ch_energy = energy_result.channels[0].energy_values;
291 const auto& ch_windows = energy_result.channels[0].window_positions;
292 for (size_t i = 0; i < ch_energy.size(); ++i) {
293 if (ch_energy[i] > spectral_threshold) {
294 auto [start_idx, end_idx] = ch_windows[i];
295 if (start_idx < channel.size() && end_idx <= channel.size() && start_idx < end_idx) {
296 qualifying_windows.emplace_back(start_idx, end_idx);
297 }
298 }
299 }
300
301 auto merged_windows = merge_overlapping_windows(qualifying_windows);
302
303 std::vector<double> extracted_data;
304 for (const auto& [start_idx, end_idx] : merged_windows) {
305 std::ranges::copy(channel.subspan(start_idx, end_idx - start_idx),
306 std::back_inserter(extracted_data));
307 }
308
309 result.push_back(std::move(extracted_data));
310 } catch (const std::exception&) {
311 result.emplace_back();
312 }
313 }
314
315 return result;
316}