In [1]:
import numpy, scipy, matplotlib.pyplot as plt, IPython.display, sklearn
import essentia, essentia.standard as ess
plt.rcParams['figure.figsize'] = (14,5)
For classification, we're going to be using new features in our arsenal: spectral moments (centroid, bandwidth, skewness, kurtosis) and other spectral statistics.
Moments is a term used in physics and statistics. There are raw moments and central moments.
You are probably already familiar with two examples of moments: mean and variance. The first raw moment is known as the mean. The second central moment is known as the variance.
To compute the spectral centroid in Essentia, we will use essentia.standard.Centroid
:
In [2]:
spectrum = ess.Spectrum()
centroid = ess.Centroid()
x = essentia.array(scipy.randn(1024))
X = spectrum(x)
spectral_centroid = centroid(X)
print spectral_centroid
This value is normalized between 0 and 1. If 0, then the centroid is at zero. If 1, then the centroid is all the way to the "right", i.e., equal to fs/2
, the Nyquist frequency, or the highest frequency a digital signal can possibly have.
Here is a sanity check:
In [3]:
sum((X/sum(X))*numpy.linspace(0, 1, len(X)))
Out[3]:
The first step to computing the other three spectral moments (spread, skewness, and kurtosis) is to compute the central moments of a spectrum:
In [4]:
central_moments = ess.CentralMoments()
print central_moments(X)
To compute the spectral spread, skewness, and kurtosis, we use essentia.standard.DistributionShape
:
In [5]:
distributionshape = ess.DistributionShape()
spectral_moments = distributionshape(central_moments(X))
print spectral_moments