-
Notifications
You must be signed in to change notification settings - Fork 0
/
entropy_estimation.hpp
142 lines (132 loc) · 3.09 KB
/
entropy_estimation.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#ifndef ENTROPY_ESTIMATION_HEADER
#define ENTROPY_ESTIMATION_HEADER
#include <cmath>
#include <math.h>
#include "symbolstats.hpp"
#include "table_encode.hpp"
double estimateEntropy(uint8_t* in_bytes, size_t size){
uint8_t frequencies[size];
for(size_t i=0;i<256;i++){
frequencies[i] = 0;
}
for(size_t i=0;i<size;i++){
frequencies[in_bytes[i]]++;
}
double sum = 0;
for(size_t i=0;i<256;i++){
if(frequencies[i]){
sum += -std::log2((double)frequencies[i]/(double)size) * (double)frequencies[i];
}
}
return sum;
}
double estimateEntropy_freq(SymbolStats frequencies, size_t size){
double sum = 0;
for(size_t i=0;i<256;i++){
if(frequencies.freqs[i]){
sum += -std::log2((double)frequencies.freqs[i]/(double)size) * (double)frequencies.freqs[i];
}
}
return sum;
}
double* entropyLookup(SymbolStats stats,size_t total){
double* table = new double[256];
for(size_t i=0;i<256;i++){
if(stats.freqs[i] == 0){
table[i] = -std::log2(1/(double)total);
}
else{
table[i] = -std::log2(stats.freqs[i]/(double)total);
}
}
return table;
}
double estimateEntropy_overhead(SymbolStats stats1, uint32_t range){
size_t overhead1;
SymbolStats codedTable1 = encode_freqTable_dry(stats1, overhead1, range);
double* cost1 = entropyLookup(codedTable1,1 << 16);
double collected = overhead1;
for(size_t i=0;i<range;i++){
if(stats1.freqs[i]){
collected += stats1.freqs[i] * cost1[i];
}
}
delete[] cost1;
return collected;
}
double* entropyLookup(SymbolStats stats){
double* table = new double[256];
size_t total = 0;
for(size_t i=0;i<256;i++){
total += stats.freqs[i];
}
if(total == 0){
for(size_t i=0;i<256;i++){
table[i] = 0;
}
return table;
}
for(size_t i=0;i<256;i++){
if(stats.freqs[i] == 0){
table[i] = -std::log2(1/(double)total);
}
else{
table[i] = -std::log2(stats.freqs[i]/(double)total);
}
}
return table;
}
double regionalEntropy(
uint8_t* in_bytes,
double* entropyTable,
size_t tileIndex,
uint32_t width,
uint32_t height,
uint32_t b_width_block,
uint32_t b_height_block
){
uint32_t b_width = (width + b_width_block - 1)/b_width_block;
double sum = 0;
for(size_t y=0;y<b_height_block;y++){
uint32_t y_pos = (tileIndex / b_width)*b_height_block + y;
if(y >= height){
continue;
}
for(size_t x=0;x<b_width_block;x++){
uint32_t x_pos = (tileIndex % b_width)*b_width_block + x;
if(x >= width){
continue;
}
sum += entropyTable[in_bytes[y_pos * width + x_pos]];
}
}
return sum;
}
double regionalEntropy(
uint8_t* in_bytes,
double* entropyTable,
size_t tileIndex,
uint32_t width,
uint32_t height,
uint32_t b_width_block,
uint32_t b_height_block,
uint8_t offset
){
uint32_t b_width = (width + b_width_block - 1)/b_width_block;
double sum = 0;
for(size_t y=0;y<b_height_block;y++){
uint32_t y_pos = (tileIndex / b_width)*b_height_block + y;
if(y >= height){
continue;
}
for(size_t x=0;x<b_width_block;x++){
uint32_t x_pos = (tileIndex % b_width)*b_width_block + x;
if(x >= width){
continue;
}
sum += entropyTable[in_bytes[(y_pos * width + x_pos)*3 + offset]];
}
}
return sum;
}
#endif //ENTROPY_ESTIMATION