-
Notifications
You must be signed in to change notification settings - Fork 1
/
val.c
34 lines (25 loc) · 772 Bytes
/
val.c
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
#include <string.h>
#include <math.h>
#define min(x, y) ((x)<(y)?(x):(y))
double calc_nbits_in_data(unsigned char *data, int nbytestoprocess)
{
int cnts[256], loop;
double ent=0.0;
memset(cnts, 0x00, sizeof(cnts));
for(loop=0; loop<nbytestoprocess; loop++)
{
cnts[data[loop]]++;
}
for(loop=0; loop<256;loop++)
{
double prob = (double)cnts[loop] / (double)nbytestoprocess;
if (prob > 0.0)
{
ent += prob * (log(1.0/prob)/log(2.0));
}
}
ent *= (double)nbytestoprocess;
if (ent < 0.0) ent=0.0;
ent = min((double)(nbytestoprocess*8), ent);
return ent;
}