0
$\begingroup$

I am using librosa.stft to apply an STFT to an audio sample and display a spectogram of the results when using different sampling windows (n_fft = 1000 and n_fft = 3000). I am curious why the different sampling windows results in different range of values used on the x axis? The original audio sample is 4 seconds long.

# read the .wav file, print sample rate for reference
audio, sample_rate = librosa.load("voice.wav")
print('sampling rate:', sample_rate)

# perform stft using librosa module for given N values
# the parameter n_fft is where we specify value for N
audio_stft_1000 = np.abs(librosa.stft(audio, n_fft=1000))
audio_stft_3000 = np.abs(librosa.stft(audio, n_fft=3000))

# create plots according to Librosa documentation

# N = 1000
fig, ax = plt.subplots()
img = librosa.display.specshow(librosa.amplitude_to_db(audio_stft_1000,
                                                       ref=np.max),
                               y_axis='log', x_axis='s', ax=ax,
                               sr=sample_rate, n_fft=1000)
fig.colorbar(img, ax=ax, format="%+2.0f dB")
ax.set_title('Power spectrogram N = 1000')

# N = 3000
fig, ax = plt.subplots()
img = librosa.display.specshow(librosa.amplitude_to_db(audio_stft_3000,
                                                       ref=np.max),
                               y_axis='log', x_axis='s', ax=ax,
                               sr=sample_rate, n_fft=3000)
fig.colorbar(img, ax=ax, format="%+2.0f dB")
ax.set_title('Power spectrogram N = 3000')

# Show the plots
plt.show()

enter image description here

$\endgroup$

1 Answer 1

3
$\begingroup$

Per the Librosa Specshow documentation, you need to specify the hop_length in your call to librosa.specshow() to get the correct time axis.

Since you didn't specify hop_length in your librosa.stft() call, it defaulted to winlength//4, which in your case is nfft//4 (see Librosa stft documentation).

Try:

Nfft = 1000 # or 3000
img = librosa.display.specshow(librosa.amplitude_to_db(audio_stft_1000,
                                                   ref=np.max),
                           y_axis='log', x_axis='s', ax=ax,
                           sr=sample_rate, n_fft=Nfft , hop_length=Nfft//4)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.