generated from gdt050579/appcui-template
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EntropyVisualizer] + add shannon entropy as example
Signed-off-by: Gheorghita Mutu <[email protected]>
- Loading branch information
1 parent
2c96456
commit 5b8d6a1
Showing
6 changed files
with
285 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
target_sources(GViewCore PRIVATE | ||
Entropy.cpp | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "Internal.hpp" | ||
|
||
namespace GView::Entropy | ||
{ | ||
double ShannonEntropy(const BufferView& buffer) | ||
{ | ||
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 entropy = 0.0; | ||
for (const auto& value : frequency) { | ||
if (value == 0) { | ||
continue; | ||
} | ||
double probability = static_cast<double>(value) / buffer.GetLength(); | ||
entropy -= probability * std::log2(probability); | ||
} | ||
|
||
return entropy; // max log2(n) = 8 | ||
} | ||
} // namespace GView::Entropy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters