Show the code
import numpy as np
import librosa
import librosa.display
from matplotlib import pyplot as plt
The instrument samples are taken from McGill University Master Samples (MUMS, Opolko & Wapnick, 2006), polished by Eerola and Ferrer (2008) and used in subsequent experiments (Eerola et al., 2012).
import numpy as np
import librosa
import librosa.display
from matplotlib import pyplot as plt
= librosa.load('data/63.wav')
x, sr = np.abs(librosa.stft(x))
stft = librosa.fft_frequencies(sr=sr)
freqs
= librosa.pyin(x, fmin=librosa.note_to_hz('C2'),
f0, voiced_flag, voiced_probs =librosa.note_to_hz('C7'))
fmax= np.nanmedian(f0) # Get the Hz of the F0 for nice labels
f = librosa.hz_to_note(f) # Convert Hz to note name
n print(n)
= np.arange(f, f*10, f)
X
= plt.subplots(nrows=2, ncols=1, figsize=(8.0, 5.0))
fig, ax
# 1. Spectrum of a tone
# collapse across time and plot a spectrum
= stft.mean(axis=1)/max(stft.mean(axis=1))
Dmean 0].plot(freqs, Dmean, color='blue')
ax[0].set_title("Violin Spectrum")
ax[0].set(xlim=[130, X.max()])
ax[0].set_ylabel("Norm. Ampl.")
ax[0].set_xlabel("Frequency (Hz)")
ax[0].grid()
ax[0].set_xticks(X)
ax[
# calculate spectral centroid and plot it
= librosa.feature.spectral_centroid(y=x, sr=sr)
centroid = centroid.mean()
centroidM print(centroidM.round(0))
= "Centroid " + str(int(centroidM.round(0)))+" Hz"
centroidM_label 0].annotate("", xy=(130, 0.75), xycoords='data', xytext=(centroidM, 0.75),
ax[=dict(arrowstyle="<|-", connectionstyle="arc3",
arrowprops="0.3"), size=4)
color0].annotate("", xy=(centroidM, 0.75), xycoords='data',
ax[=(X.max(), 0.75),
xytext=dict(arrowstyle="-|>", connectionstyle="arc3",
arrowprops="0.3"), size=4)
color0].text(centroidM-120, 0.83, centroidM_label, size=10, color='0.2')
ax[
# Envelope
= librosa.feature.rms(y=x, frame_length=2048, hop_length=512)
rms = librosa.times_like(rms)
times 1].plot(times, rms[0], color='red')
ax[
=sr, ax=ax[1], color='0.75', max_points=3000)
librosa.display.waveshow(x, sr1].grid()
ax[1].set(ylim=[-0.25, 0.25])
ax[1].text(0.25, 0.17, "A", size=12, color='0.2')
ax[1].text(1.20, 0.17, "S", size=12, color='0.2')
ax[1].text(1.85, 0.17, "D", size=12, color='0.2')
ax[1].annotate("", xy=(0.00, 0.15), xycoords='data', xytext=(0.50, 0.15),
ax[=dict(arrowstyle="|-|", connectionstyle="arc3",
arrowprops='0.2'), size=4)
color1].annotate("", xy=(0.50, 0.15), xycoords='data', xytext=(1.79, 0.15),
ax[=dict(arrowstyle="|-|", connectionstyle="arc3",
arrowprops='0.2'), size=4)
color1].annotate("", xy=(1.79, 0.15), xycoords='data', xytext=(2.0, 0.15),
ax[=dict(arrowstyle="|-|", connectionstyle="arc3",
arrowprops='0.2'), size=4)
color1].set_ylabel("Amplitude")
ax[1].set_title("Violin Envelope")
ax[1].set_xlabel("Time (s)")
ax[
fig.tight_layout() plt.show()
D♯4
1623.0
= librosa.load('data/24.wav')
x, sr = np.abs(librosa.stft(x))
stft = librosa.fft_frequencies(sr=sr)
freqs
= librosa.pyin(x, fmin=librosa.note_to_hz('C2'), fmax=librosa.note_to_hz('C7'))
f0, voiced_flag, voiced_probs =np.nanmedian(f0) # Get the Hz of the fundamental frequency for nice labels
f=librosa.hz_to_note(f) # Convert Hz to note name
n
=np.arange(f,f*10,f)
X
= plt.subplots(nrows=2, ncols=1, figsize=(8.0, 5.0))
fig, ax
# collapse across time and plot a spectrum representation (energy across frequencies)
=stft.mean(axis=1)/max(stft.mean(axis=1))
Dmean0].plot(freqs,Dmean,color='blue')
ax[#ax[0].label_outer()
#ax[0].set_title("Spectrum")
0].set(xlim=[130, X.max()])
ax[0].set_ylabel("Norm. Ampl.")
ax[0].set_xlabel("Frequency (Hz)")
ax[0].grid()
ax[0].set_xticks(X)
ax[0].set_title("Clarinet Spectrum")
ax[
# calculate spectral centroid and plot it
= librosa.feature.spectral_centroid(y=x, sr=sr)
centroid = centroid.mean()
centroidM print(centroidM.round(0))
= "Centroid " + str(int(centroidM.round(0)))+" Hz"
centroidM_label 0].annotate("",xy=(130, 0.75), xycoords='data',xytext=(centroidM, 0.75), arrowprops=dict(arrowstyle="<|-",connectionstyle="arc3",color="0.3"),size=4)
ax[0].annotate("",xy=(centroidM, 0.75), xycoords='data',xytext=(X.max(), 0.75), arrowprops=dict(arrowstyle="-|>",connectionstyle="arc3",color="0.3"),size=4)
ax[0].text(centroidM-120,0.83,centroidM_label,size=10,color='0.2')
ax[
=librosa.feature.rms(y=x,frame_length=2048,hop_length=512) # Extra dynamics (RMS)
rms= librosa.times_like(rms)
times 1].plot(times, rms[0],color='red')
ax[
=sr, ax=ax[1],color='0.75',max_points=3000)
librosa.display.waveshow(x, sr1].grid()
ax[1].set(ylim=[-0.25, 0.25])
ax[
1].text(0.00,0.17,"A",size=12,color='0.2')
ax[#ax[0,1].text(0.50,0.99,"D",size=15)
1].text(0.90,0.17,"S",size=12,color='0.2')
ax[1].text(1.85,0.17,"D",size=12,color='0.2')
ax[1].annotate("",xy=(0.00, 0.15), xycoords='data',xytext=(0.07, 0.15),arrowprops=dict(arrowstyle="|-|",connectionstyle="arc3",color='0.2'),size=4)
ax[#ax[0,1].annotate("",xy=(0.05, 0.14), xycoords='data',xytext=(0.20, 0.14),arrowprops=dict(arrowstyle="<->",connectionstyle="arc3"),size=15)
1].annotate("",xy=(0.07, 0.15), xycoords='data',xytext=(1.77, 0.15),arrowprops=dict(arrowstyle="|-|",connectionstyle="arc3",color='0.2'),size=4)
ax[1].annotate("",xy=(1.77, 0.15), xycoords='data',xytext=(2.0, 0.15),arrowprops=dict(arrowstyle="|-|",connectionstyle="arc3",color='0.2'),size=4)
ax[1].set_ylabel("Amplitude")
ax[1].set_xlabel("Time (s)")
ax[1].set_title("Clarinet Envelope")
ax[
fig.tight_layout() plt.show()
1701.0
= librosa.load('data/90.wav')
x, sr
= np.abs(librosa.stft(x))
stft = librosa.fft_frequencies(sr=sr)
freqs
= librosa.pyin(x, fmin=librosa.note_to_hz('C2'), fmax=librosa.note_to_hz('C7'))
f0, voiced_flag, voiced_probs =np.nanmedian(f0) # Get the Hz of the fundamental frequency for nice labels
f=librosa.hz_to_note(f) # Convert Hz to note name
nprint(n)
=np.arange(f,f*10,f)
X
= plt.subplots(nrows=2, ncols=1, figsize=(8.0, 5.0))
fig, ax
# collapse across time and plot a spectrum representation (energy across frequencies)
=stft.mean(axis=1)/max(stft.mean(axis=1))
Dmean0].plot(freqs,Dmean,color='blue')
ax[0].set_title("Marimba Spectrum")
ax[0].set(xlim=[130, X.max()])
ax[0].set_ylabel("Norm. Ampl.")
ax[0].set_xlabel("Frequency (Hz)")
ax[0].grid()
ax[0].set_xticks(X)
ax[
# calculate spectral centroid and plot it
= librosa.feature.spectral_centroid(y=x, sr=sr)
centroid = centroid.mean()
centroidM print(centroidM.round(0))
= "Centroid " + str(int(centroidM.round(0)))+" Hz"
centroidM_label 0].annotate("",xy=(130, 0.75), xycoords='data',xytext=(centroidM, 0.75), arrowprops=dict(arrowstyle="<|-",connectionstyle="arc3",color="0.3"),size=4)
ax[0].annotate("",xy=(centroidM, 0.75), xycoords='data',xytext=(X.max(), 0.75), arrowprops=dict(arrowstyle="-|>",connectionstyle="arc3",color="0.3"),size=4)
ax[0].text(centroidM-120,0.83,centroidM_label,size=10,color='0.2')
ax[
=librosa.feature.rms(y=x,frame_length=2048,hop_length=512)
rms= librosa.times_like(rms)
times 1].plot(times, rms[0],color='red')
ax[=sr, ax=ax[1],color='0.75',max_points=3000)
librosa.display.waveshow(x, sr1].grid()
ax[1].set(ylim=[-0.25, 0.25])
ax[1].set(xlim=[0, 0.70])
ax[
1].text(0.00,0.17,"A",size=12,color='0.2')
ax[1].text(0.09,0.17,"S",size=12,color='0.2')
ax[1].text(0.40,0.17,"D",size=12,color='0.2')
ax[1].annotate("",xy=(0.00, 0.15), xycoords='data',xytext=(0.01, 0.15),arrowprops=dict(arrowstyle="|-|",connectionstyle="arc3",color='0.2'),size=4)
ax[1].annotate("",xy=(0.01, 0.15), xycoords='data',xytext=(0.18, 0.15),arrowprops=dict(arrowstyle="|-|",connectionstyle="arc3",color='0.2'),size=4)
ax[1].annotate("",xy=(0.18, 0.15), xycoords='data',xytext=(0.70, 0.15),arrowprops=dict(arrowstyle="|-|",connectionstyle="arc3",color='0.2'),size=4)
ax[1].set_ylabel("Amplitude")
ax[1].set_title("Marimba Envelope")
ax[1].set_xlabel("Time (s)")
ax[
fig.tight_layout() plt.show()
D♯4
1219.0