How can I calculate the entropy of a random signal using its histogram?

20 views (last 30 days)
I have a random signal X and I want to calculate the entropy. I don't know how to calculate the probability for each bin. I assigned below the code I did in order to generate the signal and also to calculate the numbers of bins.
X=rand(50,1)
k=1+log2(50) %Computing the number of bins
bins=round(k)
n=hist(X,bins)

Answers (2)

Image Analyst
Image Analyst on 29 Oct 2013
Don't you just add on this code:
p = n / sum(n)
entropy = -sum(p .*log(p))
or am I overlooking something???

Marcin
Marcin on 30 May 2017
In Matlab R2016b (and maybe earlier) you can use entropy function. The only thing you should remember is that it is designed for image processing, therefore you have to normalize a signal before calling entropy:
x_normalized = x/max(abs(x));
e = entropy(x_normalized)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!