Timebird
Timebird

Reputation: 199

How extract numpy array features from spectrogram?

I created a spectrogram with this code:

D = librosa.amplitude_to_db(librosa.stft(y), ref=np.max)
librosa.display.specshow(D, y_axis='linear', x_axis='time')
plt.colorbar(format='%+2.0f dB')
plt.title('Log-frequency power spectrogram')
plt.savefig('sp.png')
plt.show()

What should I do to extract times array and db array from this graph? (It's needed for .csv output)

Upvotes: 2

Views: 3034

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339180

You may use the same functions that are used by librosa to plot the spectrogram to obtain the arrays along the axes. D already is the "db array".

import librosa
import librosa.display
import matplotlib.pyplot as plt
import numpy as np

y, sr = librosa.load(librosa.util.example_audio_file())
#sr = 22050 #default sampling rate
D = librosa.amplitude_to_db(librosa.stft(y), ref=np.max)

# yaxis
n = D.shape[0]
yout = librosa.fft_frequencies(sr=sr, n_fft=1+(2 * (n - 1)) )
print yout, yout.min(), yout.max()

#xaxis
m = D.shape[1]
hop_length=512

xout = librosa.frames_to_time(np.arange(m+1), sr=sr, hop_length=hop_length)
print xout, xout.min(), xout.max()


librosa.display.specshow(D, sr=sr, hop_length=hop_length, y_axis='linear', x_axis='time')
plt.colorbar(format='%+2.0f dB')
plt.title('Log-frequency power spectrogram')
plt.savefig('sp.png')
plt.show()

Upvotes: 2

Related Questions