import numpy, scipy, matplotlib.pyplot as plt, sklearn, urllib, stanford_mir, IPython.display
%matplotlib inline
plt.rcParams['figure.figsize'] = (14, 5)
Download a file:
filename = '125_bounce.wav'
url = 'http://audio.musicinformationretrieval.com/'
urllib.urlretrieve(url + filename, filename=filename)
('125_bounce.wav', <httplib.HTTPMessage instance at 0x109afb248>)
Load a file:
x, fs = librosa.load(filename)
Listen to the signal:
IPython.display.Audio(x, rate=fs)
Compute some features:
X = librosa.feature.mfcc(x, sr=fs)
print X.shape
(20, 331)
Scale the features to have zero mean and unit variance:
X = sklearn.preprocessing.scale(X)
X.mean()
-4.2933095816320253e-18
Create a PCA model object.
model = sklearn.decomposition.PCA(n_components=2, whiten=True)
Apply PCA to the scaled features:
model.fit(X.T)
PCA(copy=True, n_components=2, whiten=True)
Y = model.transform(X.T)
print Y.shape
(331, 2)
Let's see how many principal components were returned:
model.components_.shape
(2, 20)
Plot the two top principal components for each data point:
plt.scatter(Y[:,0], Y[:,1])
<matplotlib.collections.PathCollection at 0x10aebbc10>