Calculating Entropy of a file and coding it using Hamming

Hello there!

I am trying to solve a problem for a project i took and i am in the final

part of it...(view below for my until now code)

Well the aim is to calculate the entropy of a file and then to use

Hamming to code it. I managed to make both parts based on what I learned

from the class and read in the book. (Please if you are familiar with the

object tell me if it is correct :) )

So, my problem is that I am not able to merge those two parts below (you

dont need to read the theory for it).. like finding the entropy and then

continue for the coding of that file.

Any help, advice, anything is appreciate.
Thank you!

Entropy Calculation

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
#include "BufferedNode.h"
#include "Buffer.h"
#include "Vector.h"
#include <strstream>
#include <math.h>

#ifdef HAVE_VALUES_H
#include <values.h>
#endif

#ifdef HAVE_FLOAT_H
#include <float.h>
#endif

class Entropy;

DECLARE_NODE(Entropy)
/*Node
 *
 * @name Entropy
 * @category DSP:Misc
 * @description Calculates the entropy of a vector
 *
 * @input_name INPUT
 * @input_type Vector<float>
 * @input_description Input vector
 *
 * @output_name OUTPUT
 * @output_type Vector<float>
 * @output_description Entropy value (vector of 1)
 *
END*/


class Entropy : public BufferedNode {
   
   int inputID;
   int outputID;

public:
   Entropy(string nodeName, ParameterSet params)
      : BufferedNode(nodeName, params)

   {
      inputID = addInput("INPUT");
      outputID = addOutput("OUTPUT");
   }

   void calculate(int output_id, int count, Buffer &out)
   {
      ObjectRef inputValue = getInput(inputID, count);

      const Vector<float> &in = object_cast<Vector<float> > (inputValue);
      int inputLength = in.size();

      Vector<float> &output = *Vector<float>::alloc(1);
      out[count] = &output;

      float s2=0;
      float entr=0;
      for (int i=0;i<inputLength;i++)
      {
         s2+=in[i]*in[i];
      }
      s2 = 1/s2;

      for (int i=0;i<inputLength;i++)
      {
	 if (in[i] != 0)
	    entr -= s2*in[i]*in[i] * log(s2*in[i]*in[i]);
      }
      //cout << entr << endl;
      output[0] = entr;
   }

};


Hamming Coding
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
#include<iostream.h>   
#include<math.h>   
void hanming()   
{   
int i,n,k=2;   
int h[20];   
for(i=0;i<20;i++)h[i]=0;   
cout<<"bla bla"<<endl; cin="">>n;   
while(pow(2,k)<n+k+1)k++; cout=""><<"bla bla"<<endl; for(i="1;i<=n+k;i++){" if(i!="1&&i!=2&&i!=4&&i!=8)cin">>h[i];   
}   
h[1]=(h[3]+h[5]+h[7]+h[9]+h[11]+h[13]+h[15])%2;   
h[2]=(h[3]+h[6]+h[7]+h[10]+h[11]+h[14]+h[15])%2;   
h[4]=(h[5]+h[6]+h[7]+h[12]+h[13]+h[14]+h[15])%2;   
h[8]=(h[9]+h[10]+h[11]+h[12]+h[13]+h[14]+h[15])%2;   
for(i=1;i<=n+k;i++)cout<<h[i]; jiaoyan(int="" a[],int="" n)="" {="" i,p1,p2,p4,p8,m;="" int="" h[20];="" for(i="0;i<n;i++)h

[i+1]=a[i];" k="2;" p1="(h[1]+h[3])%2;" p2="(h[2]+h[3])%2;" m="2*p2+p1;" return="" m;="" }="" if(n="=3){">=5&&n<=7){   
// k=3;   
p1=(h[1]+h[3]+h[5]+h[7])%2;   
p2=(h[2]+h[3]+h[6]+h[7])%2;   
p4=(h[4]+h[5]+h[6]+h[7])%2;   
m=4*p4+2*p2+p1;   
return m;   
}   
if(n>=9&&n<=15){   
//k=4;   
p1=(h[1]+h[3]+h[5]+h[7]+h[9]+h[11]+h[13]+h[15])%2;   
p2=(h[2]+h[3]+h[6]+h[7]+h[10]+h[11]+h[14]+h[15])%2;   
p4=(h[4]+h[5]+h[6]+h[7]+h[12]+h[13]+h[14]+h[15])%2;   
p8=(h[8]+h[9]+h[10]+h[11]+h[12]+h[13]+h[14]+h[15])%2;   
m=8*p8+4*p4+2*p2+p1;   
return m;   
}   
else{   
cout<<"bla bla"<<endl; return="" -1;="" }="" ����="" void="" main()="" {="" hanming();="" coco;="" int="" i,n,m,h[20];="" 

cout=""><<endl; cout=""><<"bla bla"<<endl; cin="">>n;   
cout<<"bla bla"<<endl; for(i="0;i<n;i++)cin">>h[i];   
m=jiaoyan(h,n);   
if(m==0)cout<<"bla bla"<<endl; if(m!="0)cout<<"bla bla"<<m;" cout=""><<endl; cin="">>coco;   
}   

I do not think that you are calculating the entropy correctly... Look here to see how to do it
http://stackoverflow.com/questions/990477/how-to-calculate-the-entropy-of-a-file#990646
The stackoverflow.com snippet calculates the entropy in bytes, not bits, but it also explains the difference, so you can change that if you want.

That Hamming code section is nonsense. You need to throw it away. (Whoever gave it to you was not your friend.)

The responses over at DaniWeb were correct also, in that it is difficult to analyze code that is using some unknown and completely non-standard object system.


In both cases, however, the returned value is a single number.

For the file's entropy, your prototype should be something like:

1 double entropy( const std::string& filename );

Remember, a file is an array of char, not float. The return value is a floating point value in the range [0.0, 1.0].

For the Hamming encoding, I think you want to create a new file that has Hamming-encoded the original file? In that case, for each input byte (char) in the original file, Hamming Code it and then pack the results into the output file. You will have to choose how you want to do it, but I recommend Hamming(7,4) or Hamming(8,4). Keep in mind that this will double the size of your data. Your prototypes should be:

1
2
unsigned char hamming84( unsigned char fourbits );
void hamming84file( const std::string& inputfilename, const std::string& outputfilename );

If what you are looking for is just to Hamming encode the file's entropy, then I recommend that you first convert the entropy to a string (like "0.47923") and Hamming-encode that, instead of encoding the floating point value directly. This is because different processors use different formats and endiannesses to represent FP numbers -- but everyone everywhere can convert to and from the string representation of a FP number.

Whew. Hope this helps.
hey there, do you accept payments to make me the code?
NO

It isn't hard. You can do it yourself.
shut up bitch
HAHAHAHAHAHAHAHA!

I'm pretty sure you've everything that can be done to have people not help you. Are you going to spam the forums and get banned, next?
ROFLMAO...

話にならんぜ~!
closed account (z05DSL3A)
kordellas,
Try RentACoder if you want to pay someone to do the work:
http://www.rentacoder.com/RentACoder/DotNet/default.aspx
Topic archived. No new replies allowed.