Home › Forums › C Programming › Calculating Entropy of a file and coding it using Hamming
- This topic has 0 replies, 1 voice, and was last updated 15 years, 6 months ago by RolandoGwendole.
Viewing 0 reply threads
- AuthorPosts
- June 19, 2009 at 9:59 am #2206RolandoGwendoleParticipant
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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576#include "BufferedNode.h"<br />#include "Buffer.h"<br />#include "Vector.h"<br />#include <strstream><br />#include <math.h><br /><br />#ifdef HAVE_VALUES_H<br />#include <values.h><br />#endif<br /><br />#ifdef HAVE_FLOAT_H<br />#include <float.h><br />#endif<br /><br />class Entropy;<br /><br />DECLARE_NODE(Entropy)<br />/*Node<br />*<br />* @name Entropy<br />* @category DSP:Misc<br />* @description Calculates the entropy of a vector<br />*<br />* @input_name INPUT<br />* @input_type Vector<float><br />* @input_description Input vector<br />*<br />* @output_name OUTPUT<br />* @output_type Vector</float><float><br />* @output_description Entropy value (vector of 1)<br />*<br />END*/<br /><br /><br />class Entropy : public BufferedNode {<br /><br />int inputID;<br />int outputID;<br /><br />public:<br />Entropy(string nodeName, ParameterSet params)<br />: BufferedNode(nodeName, params)<br /><br />{<br />inputID = addInput("INPUT");<br />outputID = addOutput("OUTPUT");<br />}<br /><br />void calculate(int output_id, int count, Buffer &out)<br />{<br />ObjectRef inputValue = getInput(inputID, count);<br /><br />const Vector</float><float> &in = object_cast<Vector</float><float> > (inputValue);<br />int inputLength = in.size();<br /><br />Vector</float><float> &output = *Vector</float><float>::alloc(1);<br />out[count] = &output;<br /><br />float s2=0;<br />float entr=0;<br />for (int i=0;i<inputLength;i++)<br />{<br />s2+=in<i>*in</i><i>;<br />}<br />s2 = 1/s2;<br /><br />for (int i=0;i<inputLength;i++)<br />{<br />if (in</i><i> != 0)<br />entr -= s2*in</i><i>*in</i><i> * log(s2*in</i><i>*in</i><i>);<br />}<br />//cout << entr << endl;<br />output[0] = entr;<br />}<br /><br />};</i></float></strstream>
Hamming coding :1234567891011121314151617181920212223242526272829303132333435363738394041#include<iostream.h><br />#include<math.h><br />void hanming()<br />{<br />int i,n,k=2;<br />int h[20];<br />for(i=0;i<20;i++)h<i>=0;<br />cout<<"bla bla"<<endl; cin="">>n;<br />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><i>;<br />}<br />h[1]=(h[3]+h[5]+h[7]+h[9]+h[11]+h[13]+h[15])%2;<br />h[2]=(h[3]+h[6]+h[7]+h[10]+h[11]+h[14]+h[15])%2;<br />h[4]=(h[5]+h[6]+h[7]+h[12]+h[13]+h[14]+h[15])%2;<br />h[8]=(h[9]+h[10]+h[11]+h[12]+h[13]+h[14]+h[15])%2;<br />for(i=1;i<=n+k;i++)cout<<h</i><i>; jiaoyan(int="" a[],int="" n)="" {="" i,p1,p2,p4,p8,m;="" int="" h[20];="" for(i="0;i<n;i++)h<br /><br />[i+1]=a</i><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){<br />// k=3;<br />p1=(h[1]+h[3]+h[5]+h[7])%2;<br />p2=(h[2]+h[3]+h[6]+h[7])%2;<br />p4=(h[4]+h[5]+h[6]+h[7])%2;<br />m=4*p4+2*p2+p1;<br />return m;<br />}<br />if(n>=9&&n<=15){<br />//k=4;<br />p1=(h[1]+h[3]+h[5]+h[7]+h[9]+h[11]+h[13]+h[15])%2;<br />p2=(h[2]+h[3]+h[6]+h[7]+h[10]+h[11]+h[14]+h[15])%2;<br />p4=(h[4]+h[5]+h[6]+h[7]+h[12]+h[13]+h[14]+h[15])%2;<br />p8=(h[8]+h[9]+h[10]+h[11]+h[12]+h[13]+h[14]+h[15])%2;<br />m=8*p8+4*p4+2*p2+p1;<br />return m;<br />}<br />else{<br />cout<<"bla bla"<<endl; return="" -1;="" }="" ����="" void="" main()="" {="" hanming();="" coco;="" int="" i,n,m,h[20];=""<br /><br />cout=""><<endl; cout=""><<"bla bla"<<endl; cin="">>n;<br />cout<<"bla bla"<<endl; for(i="0;i<n;i++)cin">>h</i><i>;<br />m=jiaoyan(h,n);<br />if(m==0)cout<<"bla bla"<<endl; if(m!="0)cout<<"bla bla"<<m;" cout=""><<endl; cin="">>coco;<br />} </i>
- AuthorPosts
Viewing 0 reply threads
- The forum ‘C Programming’ is closed to new topics and replies.