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

◆ compute_entropy_statistic()

std::vector< double > MayaFlux::Yantra::compute_entropy_statistic ( std::span< const double >  data,
const size_t  num_windows,
const uint32_t  hop_size,
const uint32_t  window_size,
size_t  num_bins = 0 
)

Compute entropy statistic using zero-copy processing.

This function computes the Shannon entropy for a given data span. It calculates information content based on value distribution in each window.

Parameters
dataInput data span
num_windowsNumber of windows to process
hop_sizeHop size for windowing
window_sizeSize of each window
num_binsNumber of histogram bins (0 = auto-detect)
Returns
Vector of entropy values

Definition at line 587 of file AnalysisHelper.cpp.

588{
589 std::vector<double> entropy_values(num_windows);
590
591 std::vector<size_t> indices(num_windows);
592 std::iota(indices.begin(), indices.end(), 0);
593
594 std::for_each(std::execution::par_unseq, indices.begin(), indices.end(),
595 [&](size_t i) {
596 const size_t start_idx = i * hop_size;
597 const size_t end_idx = std::min(start_idx + window_size, data.size());
598 auto window = data.subspan(start_idx, end_idx - start_idx);
599
600 if (window.empty()) {
601 entropy_values[i] = 0.0;
602 return;
603 }
604
605 size_t bins = num_bins;
606 if (bins == 0) {
607 bins = static_cast<size_t>(std::ceil(std::log2(window.size()) + 1));
608 bins = std::max(bins, static_cast<size_t>(1));
609 bins = std::min(bins, window.size());
610 }
611
612 auto [min_it, max_it] = std::ranges::minmax_element(window);
613 double min_val = *min_it;
614 double max_val = *max_it;
615
616 if (max_val <= min_val) {
617 entropy_values[i] = 0.0;
618 return;
619 }
620
621 double bin_width = (max_val - min_val) / static_cast<double>(bins);
622
623 std::vector<size_t> bin_counts(bins, 0);
624 for (double value : window) {
625 auto bin_idx = static_cast<size_t>((value - min_val) / bin_width);
626 if (bin_idx >= bins)
627 bin_idx = bins - 1;
628 bin_counts[bin_idx]++;
629 }
630
631 double entropy = 0.0;
632 size_t total_count = window.size();
633
634 for (size_t count : bin_counts) {
635 if (count > 0) {
636 double probability = static_cast<double>(count) / static_cast<double>(total_count);
637 entropy -= probability * std::log2(probability);
638 }
639 }
640
641 entropy_values[i] = entropy;
642 });
643
644 return entropy_values;
645}

References compute_entropy_statistic().

Referenced by compute_entropy_statistic(), and MayaFlux::Yantra::StatisticalAnalyzer< InputType, OutputType >::compute_statistical_values().

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