Algorithms | latest | spectral-skewness
Spectral skewness
Spectral skewness is a measure of the asymmetry of the power spectrum around its mean, the spectral centroid
- Positive skewness: Indicates a spectrum with a tail extending towards higher frequencies.
- Negative skewness: Indicates a spectrum with a tail extending towards lower frequencies.
- Zero skewness: Indicates a symmetric spectrum around the centroid.
It is computed from the power spectrum
References
- https://www.mathworks.com/help/signal/ref/spectralskewness.html
- Peeters, G. (2004). A large set of audio features for sound description (similarity and classification) in the CUIDADO project. In CUIDADO IST Project Report (Vol. 54).
Code
INFO
The following snippet is written in a generic and unoptimized manner. The code aims to be comprehensible to programmers familiar with various programming languages and may not represent the most efficient or idiomatic Python practices. Please refer to implementations for optimized implementations in different programming languages.
py
import numpy as np
def _spectral_centroid(spectrum: np.ndarray, samplerate: float):
ps = np.abs(spectrum) ** 2
ps_sum = 0.0
ps_sum_weighted = 0.0
for i, magnitude in enumerate(ps):
ps_sum += magnitude
ps_sum_weighted += magnitude * i
return 0.5 * samplerate / (len(ps) - 1) * (ps_sum_weighted / ps_sum)
def spectral_skewness(spectrum: np.ndarray, samplerate: float):
f_centroid = _spectral_centroid(spectrum, samplerate)
ps = np.abs(spectrum) ** 2
ps_sum = 0.0
ps_sum_weighted_2 = 0.0
ps_sum_weighted_3 = 0.0
for i, magnitude in enumerate(ps):
f = 0.5 * samplerate / (len(ps) - 1) * i
ps_sum += magnitude
ps_sum_weighted_2 += magnitude * (f - f_centroid) ** 2
ps_sum_weighted_3 += magnitude * (f - f_centroid) ** 3
return (ps_sum_weighted_3 / ps_sum) / np.sqrt(ps_sum_weighted_2 / ps_sum) ** 3