Skip to content

Commit

Permalink
[Entropy] + add Renyi entropy (untested) (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
gheorghitamutu committed Aug 31, 2024
1 parent 6cd71aa commit 57c5739
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions GViewCore/src/Entropy/Entropy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,58 @@

#include <math.h>

constexpr uint32 MAX_NUMBER_OF_BYTES = 256;

namespace GView::Entropy
{
double ShannonEntropy(const BufferView& buffer)
void SetFrequencies(const BufferView& buffer, std::array<char, MAX_NUMBER_OF_BYTES>& frequency)
{
char frequency[256]{};

// Count frequency of each byte in the buffer
for (uint32 i = 0; i < buffer.GetLength(); i++) {
const auto c = buffer[i];
frequency[c]++;
}
}

// Calculate entropy
double ShannonEntropy_private(const BufferView& buffer, std::array<char, MAX_NUMBER_OF_BYTES>& frequency)
{
double entropy = 0.0;
for (const auto& value : frequency) {
if (value == 0) {
for (auto f : frequency) {
if (f == 0) {
continue;
}
double probability = static_cast<double>(value) / buffer.GetLength();
double probability = static_cast<double>(f) / buffer.GetLength();
entropy -= probability * log2(probability);
}

return entropy; // max log2(n) = 8
}

double ShannonEntropy(const BufferView& buffer)
{
std::array<char, MAX_NUMBER_OF_BYTES> frequency{};
SetFrequencies(buffer, frequency);
return ShannonEntropy_private(buffer, frequency);
}

double RenyiEntropy(const BufferView& buffer, double alpha)
{
std::array<char, MAX_NUMBER_OF_BYTES> frequency{};
SetFrequencies(buffer, frequency);

if (alpha == 1.0) {
return ShannonEntropy_private(buffer, frequency);
}

double sum = 0.0;
for (auto f : frequency) {
double probability = static_cast<double>(f) / buffer.GetLength();
if (probability > 0) {
sum += pow(probability, alpha);
}
}

// Convert to bits if using log base e
return ((1.0 / (1.0 - alpha)) * log(sum)) / log(2);
}
} // namespace GView::Entropy

0 comments on commit 57c5739

Please sign in to comment.