In [1]:
import numpy, scipy, matplotlib.pyplot as plt, IPython.display, sklearn
import essentia, essentia.standard as ess
plt.rcParams['figure.figsize'] = (14,5)

Spectral Features in Essentia

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.

essentia.standard.Centroid

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


0.498104333878

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]:
0.49810430034108322

essentia.standard.CentralMoments

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)


[  1.00000000e+00   0.00000000e+00   8.12825486e-02  -3.57219629e-04
   1.20308148e-02]

essentia.standard.DistributionShape

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


(0.08128254860639572, -0.01541485171765089, -1.1790399551391602)