Changeset View
Changeset View
Standalone View
Standalone View
extern/draco/draco/src/draco/compression/entropy/shannon_entropy.cc
- This file was moved from extern/draco/dracoenc/src/draco/compression/entropy/shannon_entropy.cc.
| Show All 21 Lines | if (symbol_frequencies[i] > 0) { | ||||
| ++num_unique_symbols; | ++num_unique_symbols; | ||||
| // Compute Shannon entropy for the symbol. | // Compute Shannon entropy for the symbol. | ||||
| // We don't want to use std::log2 here for Android build. | // We don't want to use std::log2 here for Android build. | ||||
| total_bits += | total_bits += | ||||
| symbol_frequencies[i] * | symbol_frequencies[i] * | ||||
| log2(static_cast<double>(symbol_frequencies[i]) / num_symbols_d); | log2(static_cast<double>(symbol_frequencies[i]) / num_symbols_d); | ||||
| } | } | ||||
| } | } | ||||
| if (out_num_unique_symbols) | if (out_num_unique_symbols) { | ||||
| *out_num_unique_symbols = num_unique_symbols; | *out_num_unique_symbols = num_unique_symbols; | ||||
| } | |||||
| // Entropy is always negative. | // Entropy is always negative. | ||||
| return static_cast<int64_t>(-total_bits); | return static_cast<int64_t>(-total_bits); | ||||
| } | } | ||||
| double ComputeBinaryShannonEntropy(uint32_t num_values, | double ComputeBinaryShannonEntropy(uint32_t num_values, | ||||
| uint32_t num_true_values) { | uint32_t num_true_values) { | ||||
| if (num_values == 0) | if (num_values == 0) { | ||||
| return 0; | return 0; | ||||
| } | |||||
| // We can exit early if the data set has 0 entropy. | // We can exit early if the data set has 0 entropy. | ||||
| if (num_true_values == 0 || num_values == num_true_values) | if (num_true_values == 0 || num_values == num_true_values) { | ||||
| return 0; | return 0; | ||||
| } | |||||
| const double true_freq = | const double true_freq = | ||||
| static_cast<double>(num_true_values) / static_cast<double>(num_values); | static_cast<double>(num_true_values) / static_cast<double>(num_values); | ||||
| const double false_freq = 1.0 - true_freq; | const double false_freq = 1.0 - true_freq; | ||||
| return -(true_freq * std::log2(true_freq) + | return -(true_freq * std::log2(true_freq) + | ||||
| false_freq * std::log2(false_freq)); | false_freq * std::log2(false_freq)); | ||||
| } | } | ||||
| ShannonEntropyTracker::ShannonEntropyTracker() {} | ShannonEntropyTracker::ShannonEntropyTracker() {} | ||||
| ▲ Show 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | for (int i = 0; i < num_symbols; ++i) { | ||||
| frequencies_[symbol]--; | frequencies_[symbol]--; | ||||
| } | } | ||||
| } | } | ||||
| return ret_data; | return ret_data; | ||||
| } | } | ||||
| int64_t ShannonEntropyTracker::GetNumberOfDataBits( | int64_t ShannonEntropyTracker::GetNumberOfDataBits( | ||||
| const EntropyData &entropy_data) { | const EntropyData &entropy_data) { | ||||
| if (entropy_data.num_values < 2) | if (entropy_data.num_values < 2) { | ||||
| return 0; | return 0; | ||||
| } | |||||
| // We need to compute the number of bits required to represent the stream | // We need to compute the number of bits required to represent the stream | ||||
| // using the entropy norm. Note that: | // using the entropy norm. Note that: | ||||
| // | // | ||||
| // entropy = log2(num_values) - entropy_norm / num_values | // entropy = log2(num_values) - entropy_norm / num_values | ||||
| // | // | ||||
| // and number of bits required for the entropy is: num_values * entropy | // and number of bits required for the entropy is: num_values * entropy | ||||
| // | // | ||||
| return static_cast<int64_t>( | return static_cast<int64_t>( | ||||
| Show All 11 Lines | |||||