Code covered by the BSD License  

Highlights from
Sample Entropy

4.4 | 12 ratings Rate this file 126 Downloads (last 30 days) File Size: 1.73 KB File ID: #35784 Version: 1.0
image thumbnail

Sample Entropy



This short code calculates the sample entropy (SampEn) of a given time series data.

| Watch this File

File Information

SampEn is a measure of complexity that can be easily applied to any type of time series data, including physiological data such as heart rate variability and EEG data.

SampEn is conceptually similar to approximate entropy (ApEn), but has following differences:

1) SampEn does not count self-matching. The possible trouble of having log(0) is avoided by taking logarithm at the latest step.
2) SampEn does not depend on the datasize as much as ApEn does. The comparison is shown in the graph above. This property makes it amenable to applications with relatively short data size.

This code uses the same vectorisation technique as in Fast Approximate Entropy, another submission from the same author.


Fast Approximate Entropy inspired this file.

This file inspired Sample Entropy and Fast Sample Entropy.

MATLAB release MATLAB 7.13 (R2011b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (20)
23 Nov 2015 Albert Cid Royo

Hi! Can you comment from which article/book/whatever you extracted this code?


Comment only
14 Oct 2015 mejali

mejali (view profile)

How would I best modify this function to calculate SampEn as a sliding window? I.e. I have a 1000 data points, but I would like it to calculate SampEn for the first 200 beat intervals, then the next 200 intervals starting from 2:201, then 3:202..and so on. I tried some ways but failed each time. Appreciate your help.

Comment only
06 Jul 2015 shishir

Dear sir,
How can this be applied on the Image.

Thank You

Comment only
07 May 2015 Jonne

Jonne (view profile)

When an embedded dimension of 1 is used, the code doesn't work. This is due to the 'max' function called in line 39. With an embedded dimension of 1, there is no need to look for the maximum absolute differences. Use the following code instead of line 39:

if m==1;
dist = abs(bsxfun(@minus,tempMat(:,i+1:N- dim),tempMat(:,i)));
dist = max(abs(bsxfun(@minus,tempMat(:,i+1:N-dim),tempMat(:,i))));

05 May 2015 Hesham Mahrous

To make the function a little faster, I suggest to replace line 39 from:
dist = max(abs(tempMat(:,i+1:N-dim) - repmat(tempMat(:,i),1,N-dim-i)));


dist = max(abs(bsxfun(@minus,tempMat(:,i+1:N-dim),tempMat(:,i))));

Comment only
01 May 2015 Mo

Mo (view profile)

I am new to this field and I am struggling to understand your code according to function for Sample Entropy:
saen = SampEn( dim, r, data, tau )

Could you please explain in following codes what is the meaning of different variables (e.g. ‘nargin<4’; ‘datamat’; ‘zeros’; ‘correl’)?
Where is ‘r’ value in this code?
How we can change ‘dim’ value in this code?
What does mean correl =zeros(1,2)?

I am trying to use this matlab code for step variability in gait and my ‘m’ would be 2 and ‘r’ would be between 0.1-0.25 std. How can I amend this code for my analysis?

I really appreciate for any helps.

Comment only
30 Apr 2015 S

S (view profile)

16 Feb 2015 Yong-Ping Wang  
16 Feb 2015 Yong-Ping Wang  
06 Nov 2014 Hesam

Hesam (view profile)

I wrote .c file which is more than two times faster than .m file.

09 Jul 2014 Awadhesh Ranjan  
18 Feb 2014 zhou wei  
08 Nov 2013 Liang ZOU  
17 Apr 2013 miss khatimah  
19 Mar 2013 Alexander

Regard my previous one star rating as a mistake. Apparently mathworks immediately submitted the rating when I clicked on one of the stars.

Comment only
19 Mar 2013 Alexander  
19 Mar 2013 Alexander  
20 Jul 2012 Kavitha

May I know what is the first argument dim in the function? I want to pass on-channel EEG signal 500 samples (with sampling frequency 128 Hz) to the program to calculate entropy? Also please advice me how to use 'r'?

Comment only
28 Mar 2012 Hugh

Hugh (view profile)

oops... I found that function rand is uniform distribution, not white noise. Your code is right!

28 Mar 2012 Hugh

Hugh (view profile)

I have a question.
It is known that if the input data is white noise, SampEn will decrease as tau increases. However, I tried this code with input rand(1,2000) and different tau, and it seems that SampEn will have similar values no matter what tau. What's wrong with this?

Comment only

Contact us