In [1]:

    import numpy, scipy, matplotlib.pyplot as plt, sklearn, stanford_mir
    %matplotlib inline

← Back to Index

# Principal Component Analysis¶

Load a file:

In [2]:

    x, fs = librosa.load('prelude_cmaj_10s.wav')

Compute its chromagram:

In [3]:

    X = librosa.feature.chromagram(x, sr=fs)

In [4]:

    print X.shape

    (12, 431)

Display the chromagram:

In [5]:

    librosa.display.specshow(X, sr=fs, y_axis='chroma')

Out[5]:

    <matplotlib.image.AxesImage at 0x111078790>

Display a single chroma vector:

In [6]:

    plt.stem(X[:,0])

Out[6]:

    <Container object of 3 artists>

Create a PCA model object.

In [7]:

    model = sklearn.decomposition.PCA(n_components=11, whiten=True)

Apply PCA to the chromagram:

In [8]:

    Y = model.fit_transform(X)

In [9]:

    print Y.shape

    (12, 11)

Display all of the principal components:

In [10]:

    plt.imshow(Y, interpolation='nearest', origin='lower')

Out[10]:

    <matplotlib.image.AxesImage at 0x111dda2d0>

Plot one of the principal components:

In [11]:

    plt.stem(Y[:,5])

Out[11]:

    <Container object of 3 artists>

← Back to Index