antropy.spectral_entropy¶
- antropy.spectral_entropy(x, sf, method='fft', nperseg=None, normalize=False, axis=-1)[source]¶
Spectral Entropy.
- Parameters:
- xlist or np.array
1D or N-D data.
- sffloat
Sampling frequency, in Hz.
- methodstr
Spectral estimation method:
'fft'
: Fourier Transform (scipy.signal.periodogram()
)'welch'
: Welch periodogram (scipy.signal.welch()
)
- npersegint or None
Length of each FFT segment for Welch method. If None (default), uses scipy default of 256 samples.
- normalizebool
If True, divide by log2(psd.size) to normalize the spectral entropy between 0 and 1. Otherwise, return the spectral entropy in bit.
- axisint
The axis along which the entropy is calculated. Default is -1 (last).
- Returns:
- sefloat
Spectral Entropy
Notes
Spectral Entropy is defined to be the Shannon entropy of the power spectral density (PSD) of the data:
\[H(x, sf) = -\sum_{f=0}^{f_s/2} P(f) \log_2[P(f)]\]Where \(P\) is the normalised PSD, and \(f_s\) is the sampling frequency.
References
Inouye, T. et al. (1991). Quantification of EEG irregularity by use of the entropy of the power spectrum. Electroencephalography and clinical neurophysiology, 79(3), 204-210.
Examples
Spectral entropy of a pure sine using FFT
>>> import numpy as np >>> import antropy as ant >>> sf, f, dur = 100, 1, 4 >>> N = sf * dur # Total number of discrete samples >>> t = np.arange(N) / sf # Time vector >>> x = np.sin(2 * np.pi * f * t) >>> np.round(ant.spectral_entropy(x, sf, method='fft'), 2) 0.0
Spectral entropy of a random signal using Welch’s method
>>> np.random.seed(42) >>> x = np.random.rand(3000) >>> ant.spectral_entropy(x, sf=100, method='welch') 6.98004566237139
Normalized spectral entropy
>>> ant.spectral_entropy(x, sf=100, method='welch', normalize=True) 0.9955526198316073
Normalized spectral entropy of 2D data
>>> np.random.seed(42) >>> x = np.random.normal(size=(4, 3000)) >>> np.round(ant.spectral_entropy(x, sf=100, normalize=True), 4) array([0.9464, 0.9428, 0.9431, 0.9417])
Fractional Gaussian noise with H = 0.5
>>> import stochastic.processes.noise as sn >>> rng = np.random.default_rng(seed=42) >>> x = sn.FractionalGaussianNoise(hurst=0.5, rng=rng).sample(10000) >>> print(f"{ant.spectral_entropy(x, sf=100, normalize=True):.4f}") 0.9505
Fractional Gaussian noise with H = 0.9
>>> rng = np.random.default_rng(seed=42) >>> x = sn.FractionalGaussianNoise(hurst=0.9, rng=rng).sample(10000) >>> print(f"{ant.spectral_entropy(x, sf=100, normalize=True):.4f}") 0.8477
Fractional Gaussian noise with H = 0.1
>>> rng = np.random.default_rng(seed=42) >>> x = sn.FractionalGaussianNoise(hurst=0.1, rng=rng).sample(10000) >>> print(f"{ant.spectral_entropy(x, sf=100, normalize=True):.4f}") 0.9248