Hey, I'm back! Let's see how far I can get with music and data science. Since I know my way around Django, I'm a little confident that I can build something with these two altogether. I'm still using librosa here, pandas, matplotlib and numpy.
You can check out the google colab notebook if you wish to.
Let's install and import the dependencies:
pip install librosa matplotlib pandas
import matplotlib.pyplot as plt
import librosa
from librosa import display, load, amplitude_to_db, stft
import numpy as np
import pandas as pd
We import a couple of librosa's functions for using on our data as well as numpy, pandas and matplotlib.
Load the data and plot the waveform
y, sr = load('We Are Monsters.wav')
pd.Series(y).plot(figsize=(10, 5), lw=1, title="We Are Monsters")
We load the waveform and sample rate using the y
and sr
variable respectively and plot the data as shown in the figure below:
<matplotlib.axes._subplots.AxesSubplot at 0x7f885cf29100>
We now create a spectrogram from our data
D = stft(y)
sound_db = amplitude_to_db(np.abs(D), ref=np.max)
sound_db.shape # check the number of rows and columns in the dataset
fig, ax = plt.subplots(figsize=(10, 5))
img = display.specshow(sound_db, x_axis='time', y_axis='log', ax=ax)
(1025, 11179)
We transform the waveform(y) and get the absolute frequency of the data to produce a spectrogram and plot the data as shown in the figure below:
Lastly we create a melspectrogram
S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128)
s_db_mel = amplitude_to_db(S, ref=np.max)
fig, ax = plt.subplots(figsize=(10, 5))
img = display.specshow(s_db_mel, x_axis='time', y_axis='log', ax=ax)
Now we transform the data using the melspectrogram feature of librosa and adding 128 n_mels
then plot the data again as shown in the image below:
Conclusion
This is the continuation of my learning of python and music from day 1, which is yesterday. I may have missed some explanation on code snippets above. I would love to hear how you explain them.
Top comments (0)