version 1.0.0.0 (1.73 KB) by
Kijoon Lee

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

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.

Kijoon Lee (2021). Sample Entropy (https://www.mathworks.com/matlabcentral/fileexchange/35784-sample-entropy), MATLAB Central File Exchange. Retrieved .

Created with
R2011b

Compatible with any release

**Inspired by:**
Fast Approximate Entropy

**Inspired:**
Fast sample entropy, Sample Entropy

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

Start Hunting!Create scripts with code, output, and formatted text in a single executable document.

xiaoxiao wangIn line 46 "count(i) = sum(D)/(N-dim);", we can omit the denominator, even though it should be (N-dim-1). In addition, in line 49 "correl(m-dim+1) = sum(count)/(N-dim);", we can also omit the denominator. And then the "sum(count)" basen on m is the total number of template matches of length m. In this way, we can obtain A (number of template matches of length m+1) and B (number of template matches of length m). SampEn =-log(A/B).

References: Richman, JS; Moorman, JR (2000). "Physiological time-series analysis using approximate entropy and sample entropy". American Journal of Physiology. Heart and Circulatory Physiology. 278 (6): H2042.

Víctor Martínez-CagigalAs I previously indicated several months ago, this script returns an incorrect value of SampEn because it misses some comparisons between templates.

Víctor Martínez-CagigalHi, the vectorized version of the algorithm is "almost" awesome. This code is missing matches between templates. The creation of the 'dataMat' matrix is incomplete. Send me an e-mail if you want a clearer explanison. Thanks!

Muhammad Tariq Sadiqhello every one, please tell me what value of embedded dimension (dim) should be chosen ?

RABNAWAZ KhanHi, I am using this function to compute the complexity of the EEG signals. My EEG signal has 14 channels (electrodes) and approximately 15000 points at each channel. I run this code separately for each channel. but I am wondered about the stationarity criteria of the entropy measurements. Because for the measurement of entropy the data should be stationary. when we use this code for EEG signal, how we meet the stationarity requirement for entropy measurement? any help in this regard will be appreciated. Thank you

Anna FrenchThis method is very fast compared to code written by one of my collaborator (could probably be sped up by using bsxfun instead of repmat, but that is beside the point).

This code is correct, at least by the method defined in pubmed article #10843903. The commenters that say it is incorrect either did not understand the code, are confusing this method with other sample entropy methods (ie approximated methods), or are mistaken. They are not just "computing it with the last part of the data", they are just selecting the columns so as to not duplicate calculations that have already been done.

Also the N-m-1 issue brought up is not important to the overall calculation, since those division values cancel anyhow (the writer actually didn't need to include them at all in their code).

Omer Çelsukumar nagineniVictorThis function doesn't compute correctly the sample entropy.

In line 39 ( dist = max(abs(tempMat(:,i+1:N-dim) - repmat(tempMat(:,i),1,N-dim-i))); ), distance is compute only with the last part of the data, instead of whole data less self-matching). Also, the denominator in " count(i) = sum(D)/(N-dim); " (line 45), is N - dim - 1

Another user has also implemented sample entropy: https://es.mathworks.com/matlabcentral/fileexchange/50289-a-set-of-entropy-measures-for-temporal-series--1d-signals-

Its code has a small bug but only a small change is required. See my comment in there.

Zachery HernandezI found this site that explains more about it in case anyone was interested: https://www.physionet.org/physiotools/sampen/

Ares ChristKainan WANGAlbert Cid RoyoHi! Can you comment from which article/book/whatever you extracted this code?

Regards

mejaliHow 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.

Thanks

shishirDear sir,

How can this be applied on the Image.

Thank You

JonneWhen 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)));

else

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

end

Hesham MahrousTo 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)));

to

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

MoI 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.

SYong-Ping WangYong-Ping WangHesamI wrote .c file which is more than two times faster than .m file.

http://www.mathworks.com/matlabcentral/fileexchange/48371-sample-entropy

Awadhesh Ranjanzhou weiLiang ZOUmiss khatimahAlexanderRegard my previous one star rating as a mistake. Apparently mathworks immediately submitted the rating when I clicked on one of the stars.

AlexanderAlexanderKavithaMay 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'?

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

HughI 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?