source: fact/tools/pyscripts/sandbox/dneise/fact_compress/c++/entropy.cpp@ 17633

Last change on this file since 17633 was 14268, checked in by neise, 12 years ago
evolving
File size: 1.6 KB
Line 
1#include <iostream>
2#include <fstream>
3#include <math.h>
4using namespace std;
5
6
7
8int main (int argc, char *argv[]) {
9unsigned char symbol;
10unsigned long *multiples = NULL;
11double *probs = NULL;
12double entropy = 0;
13ofstream outfile;
14ifstream in1;
15long begin,end, size;
16
17 int bits = sizeof(symbol)*8;
18 long symbols = 1<<bits;
19 multiples = new unsigned long [symbols];
20 probs = new double [symbols];
21
22 if (argc > 1)
23 {
24 in1.open(argv[1], ios::in|ios::binary);
25 }
26 else
27 {
28 return 1;
29 }
30
31 outfile.open("entropy.txt");
32
33 if (in1.is_open())
34 {
35 begin = in1.tellg();
36 in1.seekg (0, ios::end);
37 end = in1.tellg();
38 in1.seekg (0, ios::beg);
39 size = end-begin;
40 outfile << argv[1] << " size:" << size << endl;
41 outfile << "symbols:" << symbols << endl;
42
43 while ( in1.good())
44 {
45 symbol = in1.get();
46 multiples[symbol]++;
47 }
48
49 outfile << "multiples:" << endl;
50 for (long i=0; i<symbols; i++)
51 {
52 outfile << multiples[i] << "\t";
53 probs[i] = double(multiples[i])/size;
54 }
55 outfile << endl;
56
57 outfile << "probs:" << endl;
58 for (long i=0; i<symbols; i++)
59 {
60 outfile << probs[i] << "\t";
61 if (probs[i] > 0.)
62 {
63 entropy += probs[i] * log(probs[i]);
64 }
65 }
66 entropy /= log(2);
67 outfile << endl;
68
69 outfile << "entropy:" << entropy << endl;
70
71
72
73
74 outfile.close();
75 in1.close();
76 } //end of if file open
77
78return 0;
79} //end of main
Note: See TracBrowser for help on using the repository browser.