Skip to content

Sample Entropy Function

The sample entropy function computes the sample entropy of a window. When combined with the SlidingWindow abstraction, the sample entropy function can be used to compute the sample entropy feature of a time series. Sample entropy is a measure of the complexity of the signal [1]. It is a modification of the approximate entropy (ApEn) algorithm which can be found here. It is defined as:

\[ \text{Sample Entropy} = -\log\left(\frac{A}{B}\right) \]

where \(A\) is the number of matches for template vectors of length \(m\) and \(B\) is the number of matches for template vectors of length \(m + 1\). A match is defined as a template vector \(x_{m_i}\) that is close to another template vector \(x_{m_j}\) in the sense that the maximum absolute difference between their corresponding scalar elements is less than or equal to a threshold \(r\).

Compute the sample entropy of the values in x where where is True. This is a measure of the complexity of a signal.

Parameters:

Name Type Description Default
x ndarray

The signal to find the sample entropy of.

required
m Union[int, int_]

The length of the template vector.

required
r Union[int, int_]

The tolerance.

required
where Callable[[Union[int, float, int_, float_]], Union[bool, bool_]]

A function that takes a value and returns True or False. Default is lambda x: not np.isnan(x) i.e. a measurement is valid if it is not a NaN value.

lambda : not numpy.isnan(x)

Returns:

Type Description
Union[float, float_]

The sample entropy of the values in x where where is True.

References

Sample Entropy -https://en.wikipedia.org/wiki/Sample_entropy

Examples

import numpy as np
import autonfeat as aft
import autonfeat.functional as F

# Random data
n_samples = 100
x = np.random.rand(n_samples)

# Create sliding window
ws = 10
ss = 10
window = aft.SlidingWindow(window_size=ws, step_size=ss)

# Get featurizer
featurizer = window.use(F.sample_entropy_tf)

# Get features
features = featurizer(x, m=2, r=0.2)

# Print features
print(features)

References

[1] https://en.wikipedia.org/wiki/Sample_entropy

If you enjoy using AutonFeat, please consider starring the repository ⭐️.