entropy2.m 693 B

12345678910111213141516171819202122232425262728293031
  1. % E = ENTROPY2(MTX,BINSIZE)
  2. %
  3. % Compute the first-order sample entropy of MTX. Samples of VEC are
  4. % first discretized. Optional argument BINSIZE controls the
  5. % discretization, and defaults to 256/(max(VEC)-min(VEC)).
  6. %
  7. % NOTE: This is a heavily biased estimate of entropy (it is too
  8. % small) when you don't have much data!
  9. % Eero Simoncelli, 6/96.
  10. function res = entropy2(mtx,binsize)
  11. %% Ensure it's a vector, not a matrix.
  12. vec = mtx(:);
  13. [mn,mx] = range2(vec);
  14. if (exist('binsize') == 1)
  15. nbins = max((mx-mn)/binsize, 1);
  16. else
  17. nbins = 256;
  18. end
  19. [bincount,bins] = histo(vec,nbins);
  20. %% Collect non-zero bins:
  21. H = bincount(find(bincount));
  22. H = H/sum(H);
  23. res = -sum(H .* log2(H));