<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 &larr; Back to Index
Exercise
Step1:  Load 120 seconds of an audio file
Step2:  Plot the time-domain waveform of the audio signal
Step3:  Play the audio file
Step4:  Step 2
Step5:  We transpose the result to accommodate scikit-learn which assumes that each row is one observation, and each column is one feature dimension
Step6:  Scale the features to have zero mean and unit variance
Step7:  Verify that the scaling worked
Step8:  Step 2b
Step9:  Load 120 seconds of an audio file
Step10:  Listen to the second audio file.
Step11:  Plot the time-domain waveform and spectrogram of the second audio file. In what ways does the time-domain waveform look different than the first audio file? What differences in musical attributes might this reflect? What additional insights are gained from plotting the spectrogram? Explain.
Step12:  Extract MFCCs from the second audio file. Be sure to transpose the resulting matrix such that each row is one observation, i.e. one set of MFCCs. Also be sure that the shape and size of the resulting MFCC matrix is equivalent to that for the first audio file.
Step13:  Scale the resulting MFCC features to have approximately zero mean and unit variance. Re-use the scaler from above.
Step14:  Verify that the mean of the MFCCs for the second audio file is approximately equal to zero and the variance is approximately equal to one.
Step15:  Step 3
Step16:  Construct a vector of ground-truth labels, where 0 refers to the first audio file, and 1 refers to the second audio file.
Step17:  Create a classifer model object
Step18:  Train the classifier
Step19:  Step 4
Step20:  Listen to both of the test audio excerpts
Step21:  Compute MFCCs from both of the test audio excerpts
Step22:  Scale the MFCCs using the previous scaler
Step23:  Concatenate all test features together
Step24:  Concatenate all test labels together
Step25:  Compute the predicted labels
Step26:  Finally, compute the accuracy score of the classifier on the test data
Step27:  Currently, the classifier returns one prediction for every MFCC vector in the test audio signal. Can you modify the procedure above such that the classifier returns a single prediction for a 10-second excerpt?
Step28:  Step 5
Step29:  Compute the pairwise correlation of every pair of 12 MFCCs against one another for both test audio excerpts. For each audio excerpt, which pair of MFCCs are the most correlated? least correlated?
Step30:  Display a scatter plot of any two of the MFCC dimensions (i.e. columns of the data frame) against one another. Try for multiple pairs of MFCC dimensions.
Step31:  Display a scatter plot of any two of the MFCC dimensions (i.e. columns of the data frame) against one another. Try for multiple pairs of MFCC dimensions.
Step32:  Plot a histogram of all values across a single MFCC, i.e. MFCC coefficient number. Repeat for a few different MFCC numbers
<ASSISTANT_TASK:>
Python Code:
filename_brahms = 'brahms_hungarian_dance_5.mp3'
url = "http://audio.musicinformationretrieval.com/" + filename_brahms
if not os.path.exists(filename_brahms):
    urllib.urlretrieve(url, filename=filename_brahms)
Explanation: &larr; Back to Index
Exercise: Genre Recognition
Goals
Extract features from an audio signal.
Train a genre classifier.
Use the classifier to classify the genre in a song.
Step 1: Retrieve Audio
Download an audio file onto your local machine.
End of explanation
librosa.load?
x_brahms, fs_brahms = librosa.load(filename_brahms, duration=120)
Explanation: Load 120 seconds of an audio file:
End of explanation
librosa.display.waveplot?
# Your code here:
Explanation: Plot the time-domain waveform of the audio signal:
End of explanation
IPython.display.Audio?
# Your code here:
Explanation: Play the audio file:
End of explanation
librosa.feature.mfcc?
n_mfcc = 12
mfcc_brahms = librosa.feature.mfcc(x_brahms, sr=fs_brahms, n_mfcc=n_mfcc).T
Explanation: Step 2: Extract Features
For each segment, compute the MFCCs. Experiment with n_mfcc to select a different number of coefficients, e.g. 12.
End of explanation
mfcc_brahms.shape
Explanation: We transpose the result to accommodate scikit-learn which assumes that each row is one observation, and each column is one feature dimension:
End of explanation
scaler = sklearn.preprocessing.StandardScaler()
mfcc_brahms_scaled = scaler.fit_transform(mfcc_brahms)
Explanation: Scale the features to have zero mean and unit variance:
End of explanation
mfcc_brahms_scaled.mean(axis=0)
mfcc_brahms_scaled.std(axis=0)
Explanation: Verify that the scaling worked:
End of explanation
filename_busta = 'busta_rhymes_hits_for_days.mp3'
url = "http://audio.musicinformationretrieval.com/" + filename_busta
urllib.urlretrieve?
# Your code here. Download the second audio file in the same manner as the first audio file above.
Explanation: Step 2b: Repeat steps 1 and 2 for another audio file.
End of explanation
librosa.load?
# Your code here. Load the second audio file in the same manner as the first audio file.
# x_busta, fs_busta = 
Explanation: Load 120 seconds of an audio file:
End of explanation
IPython.display.Audio?
Explanation: Listen to the second audio file.
End of explanation
plt.plot?
# See http://musicinformationretrieval.com/stft.html for more details on displaying spectrograms.
librosa.feature.melspectrogram?
librosa.amplitude_to_db?
librosa.display.specshow?
Explanation: Plot the time-domain waveform and spectrogram of the second audio file. In what ways does the time-domain waveform look different than the first audio file? What differences in musical attributes might this reflect? What additional insights are gained from plotting the spectrogram? Explain.
End of explanation
librosa.feature.mfcc?
# Your code here:
# mfcc_busta =
mfcc_busta.shape
Explanation: Extract MFCCs from the second audio file. Be sure to transpose the resulting matrix such that each row is one observation, i.e. one set of MFCCs. Also be sure that the shape and size of the resulting MFCC matrix is equivalent to that for the first audio file.
End of explanation
scaler.transform?
# Your code here:
# mfcc_busta_scaled =
Explanation: Scale the resulting MFCC features to have approximately zero mean and unit variance. Re-use the scaler from above.
End of explanation
mfcc_busta_scaled.mean?
mfcc_busta_scaled.std?
Explanation: Verify that the mean of the MFCCs for the second audio file is approximately equal to zero and the variance is approximately equal to one.
End of explanation
features = numpy.vstack((mfcc_brahms_scaled, mfcc_busta_scaled))
features.shape
Explanation: Step 3: Train a Classifier
Concatenate all of the scaled feature vectors into one feature table.
End of explanation
labels = numpy.concatenate((numpy.zeros(len(mfcc_brahms_scaled)), numpy.ones(len(mfcc_busta_scaled))))
Explanation: Construct a vector of ground-truth labels, where 0 refers to the first audio file, and 1 refers to the second audio file.
End of explanation
# Support Vector Machine
model = sklearn.svm.SVC()
Explanation: Create a classifer model object:
End of explanation
model.fit?
# Your code here
Explanation: Train the classifier:
End of explanation
x_brahms_test, fs_brahms = librosa.load(filename_brahms, duration=10, offset=120)
x_busta_test, fs_busta = librosa.load(filename_busta, duration=10, offset=120)
Explanation: Step 4: Run the Classifier
To test the classifier, we will extract an unused 10-second segment from the earlier audio fields as test excerpts:
End of explanation
IPython.display.Audio?
IPython.display.Audio?
Explanation: Listen to both of the test audio excerpts:
End of explanation
librosa.feature.mfcc?
librosa.feature.mfcc?
Explanation: Compute MFCCs from both of the test audio excerpts:
End of explanation
scaler.transform?
scaler.transform?
Explanation: Scale the MFCCs using the previous scaler:
End of explanation
numpy.vstack?
Explanation: Concatenate all test features together:
End of explanation
numpy.concatenate?
Explanation: Concatenate all test labels together:
End of explanation
model.predict?
Explanation: Compute the predicted labels:
End of explanation
score = model.score(test_features, test_labels)
score
Explanation: Finally, compute the accuracy score of the classifier on the test data:
End of explanation
# Your code here.
Explanation: Currently, the classifier returns one prediction for every MFCC vector in the test audio signal. Can you modify the procedure above such that the classifier returns a single prediction for a 10-second excerpt?
End of explanation
df_brahms = pandas.DataFrame(mfcc_brahms_test_scaled)
df_brahms.shape
df_brahms.head()
df_busta = pandas.DataFrame(mfcc_busta_test_scaled)
Explanation: Step 5: Analysis in Pandas
Read the MFCC features from the first test audio excerpt into a data frame:
End of explanation
df_brahms.corr()
df_busta.corr()
Explanation: Compute the pairwise correlation of every pair of 12 MFCCs against one another for both test audio excerpts. For each audio excerpt, which pair of MFCCs are the most correlated? least correlated?
End of explanation
df_brahms.plot.scatter?
Explanation: Display a scatter plot of any two of the MFCC dimensions (i.e. columns of the data frame) against one another. Try for multiple pairs of MFCC dimensions.
End of explanation
df_busta.plot.scatter?
Explanation: Display a scatter plot of any two of the MFCC dimensions (i.e. columns of the data frame) against one another. Try for multiple pairs of MFCC dimensions.
End of explanation
df_brahms[0].plot.hist()
df_busta[11].plot.hist()
Explanation: Plot a histogram of all values across a single MFCC, i.e. MFCC coefficient number. Repeat for a few different MFCC numbers:
End of explanation
<END_TASK>