1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
fig, ax = plt.subplots(nrows=3, figsize=(14, 12))
tempogram = librosa.feature.tempogram(onset_envelope=spectral_flux, sr=sr, hop_length=hop_length)
librosa.display.specshow(tempogram, sr=sr, hop_length=hop_length, x_axis='time', y_axis='tempo', cmap='magma', ax=ax[0])
tempo = librosa.beat.tempo(onset_envelope=spectral_flux, sr=sr, hop_length=hop_length)[0]
ax[0].axhline(tempo, color='w', linestyle='--', alpha=1, label='Estimated tempo={:g}'.format(tempo))
ax[0].legend(loc='upper right') ax[0].set_title('Fig.2: Tempogram',fontsize=15)
ac_global = librosa.autocorrelate(spectral_flux, max_size=tempogram.shape[0])
ac_global = librosa.util.normalize(ac_global)
x_scale = np.linspace(start=0, stop=tempogram.shape[0] * float(hop_length) / sr, num=tempogram.shape[0])
ax[1].plot(x_scale, np.mean(tempogram, axis=1), label='Mean local autocorrelation') ax[1].plot(x_scale, ac_global, '--', label='Global autocorrelation') ax[1].legend(loc='upper right') ax[1].set(xlabel='Lag (seconds)')
freqs = librosa.tempo_frequencies(n_bins = tempogram.shape[0], hop_length=hop_length, sr=sr)
ax[2].semilogx(freqs[1:], np.mean(tempogram[1:], axis=1), label='Mean local autocorrelation', basex=2) ax[2].semilogx(freqs[1:], ac_global[1:], linestyle='--', label='Global autocorrelation', basex=2) ax[2].axvline(tempo, color='black', linestyle='--', label='Estimated tempo={:g}'.format(tempo))
ax[2].legend(loc='upper right') ax[2].set(xlabel='BPM')
plt.show()
|