Skip to content

Commit

Permalink
refactors and fixes sample entropy (last M values must be ignored)
Browse files Browse the repository at this point in the history
reason for fix: last M values in time series cannot be followed for M steps
reference: Richman and Moorman (2000), page H2042

reason for refactoring: code was unnecessarily complicated with a for loop
disguised as a while loop and an unnecessary separation of the first
iteration
  • Loading branch information
CSchoel committed Dec 28, 2019
1 parent ae2bf71 commit adeb126
Showing 1 changed file with 4 additions and 19 deletions.
23 changes: 4 additions & 19 deletions pyentrp/entropy.py
Expand Up @@ -154,26 +154,11 @@ def sample_entropy(time_series, sample_length, tolerance = None):
template = time_series[i:(i+M+1)];#We have 'M+1' elements in the template
rem_time_series = time_series[i+1:]

searchlist = np.nonzero(np.abs(rem_time_series - template[0]) < tolerance)[0]

go = len(searchlist) > 0;

length = 1;

Ntemp[length] += len(searchlist)

while go:
length += 1
nextindxlist = searchlist + 1;
nextindxlist = nextindxlist[nextindxlist < n - 1 - i]#Remove candidates too close to the end
nextcandidates = rem_time_series[nextindxlist]
hitlist = np.abs(nextcandidates - template[length-1]) < tolerance
searchlist = nextindxlist[hitlist]

searchlist = np.arange(len(rem_time_series) - M, dtype=np.int32)
for length in range(1, len(template)+1):
hitlist = np.abs(rem_time_series[searchlist] - template[length-1]) < tolerance
Ntemp[length] += np.sum(hitlist)

go = any(hitlist) and length < M + 1

searchlist = searchlist[hitlist] + 1

sampen = - np.log(Ntemp[1:] / Ntemp[:-1])
return sampen
Expand Down

0 comments on commit adeb126

Please sign in to comment.