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

Spectral Features

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.

librosa.feature.spectral_centroid


In [2]:
x, fs = librosa.load('simple_loop.wav')
IPython.display.Audio(x, rate=fs)


Out[2]:

In [3]:
spectral_centroids = librosa.feature.spectral_centroid(x, sr=fs)
plt.plot(spectral_centroids[0])


Out[3]:
[<matplotlib.lines.Line2D at 0x10abbc510>]

librosa.feature.spectral_bandwidth


In [4]:
spectral_bandwidth = librosa.feature.spectral_bandwidth(x, sr=fs)
plt.plot(spectral_bandwidth[0])


Out[4]:
[<matplotlib.lines.Line2D at 0x10ad656d0>]

librosa.feature.spectral_contrast


In [5]:
spectral_contrast = librosa.feature.spectral_contrast(x, sr=fs)

# For visualization, scale each feature dimension to have zero mean and unit variance
spectral_contrast = sklearn.preprocessing.scale(spectral_contrast, axis=1)

librosa.display.specshow(spectral_contrast, x_axis='time')


Out[5]:
<matplotlib.image.AxesImage at 0x10aea9610>

librosa.feature.spectral_rolloff


In [6]:
spectral_rolloff = librosa.feature.spectral_rolloff(x, sr=fs)
plt.plot(spectral_rolloff[0])


Out[6]:
[<matplotlib.lines.Line2D at 0x10aeda1d0>]