home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / compress / 2800 < prev    next >
Encoding:
Text File  |  1992-07-25  |  1.4 KB  |  43 lines

  1. Newsgroups: comp.compression
  2. Path: sparky!uunet!noc.near.net!gateway!pictel!garys
  3. From: garys@pictel.com (Gary Sullivan)
  4. Subject: Re: more entropy
  5. Message-ID: <1992Jul24.225603.3832@pictel.com>
  6. Organization: PictureTel Corporation
  7. References: <1992Jul23.174740.14559@usenet.ins.cwru.edu>
  8. Date: Fri, 24 Jul 1992 22:56:03 GMT
  9. Lines: 32
  10.  
  11. In article <1992Jul23.174740.14559@usenet.ins.cwru.edu> daf10@po.CWRU.Edu (David A. Ferrance) writes:
  12. >
  13. >If I have an unsigned int count[256][256], what is wrong with
  14. >calculating entropy like this:
  15. >
  16. >for (i=0;i<256;i++) for (j=0;j<256;j++)  {
  17. >    freq = count[i][j] / total;
  18. >    ent += freq * log10(1/freq) / 0.30103;
  19. >    }
  20. >Where total and ent are doubles, total is the # of bytes total, ent
  21. >starts off as 0, and the values of the array are the # of occurances of
  22. >each 2 letter combination?
  23. >
  24. >I get values > 8.
  25.  
  26. "total" shouldn't be the total number of "bytes", it should be the total
  27. of the counts.  I assume that's what you meant.
  28. The only other thing I can see that is wrong is that it's inefficient.
  29. I'd do:
  30.  
  31. for(i=0, tmp=0.0; i<256; i++)
  32.   for(j=0; j<256; j++) {
  33.     freq = (double)count[i][j];
  34.     tmp += freq * log10(freq);
  35.   }
  36. entropy = (log10((double)total) - tmp / total) / 0.30103;
  37.  
  38. As someone else replied, you'd need to worry if this came out above 16,
  39. but 8 or so is OK.
  40.  
  41. ---------------------------------
  42. Gary Sullivan  (garys@pictel.com)
  43.