What's the difference between librosa.core.stft and scipy.signal.stft?

1,689 views
Skip to first unread message

Hongbo Chen

unread,
Apr 19, 2018, 8:23:48 AM4/19/18
to librosa
I tried  scipy.signal.stft and librosa.core.stft on the same audio slice, but the result is different.

here is my code:

using librosa:

d = librosa.core.stft(sections[1], n_fft = 2048, window='hamming')
librosa.display.specshow(librosa.amplitude_to_db(d,ref=np.max),y_axis='log', x_axis='time')
d.shape
 (1025, 5)

 using scipy.singal:

fs = 44100
f, t, Zxx = scipy.signal.stft(sections[1],fs ,window='hamming', nfft=2048, nperseg=2048, noverlap=1536)
librosa.display.specshow(librosa.amplitude_to_db(Zxx[:, :5],ref=np.max),y_axis='log', x_axis='time')
Zxx.shape
(1025, 6)


the length of FFT is set to 2048, I think the first column should be just the same, but it's not.

and the  noverlap parameter is not in librosa, I think in librosa noverlap can be calculated by hop_length with the equation hop_length + noverlap = nfft

Besides, I think both algorithms pads zeros by default and I didn't change it.


By the way, len(sections[1]) is 2050


I would be very appreciate if anyone can answer my question!

Brian McFee

unread,
Apr 19, 2018, 9:36:50 AM4/19/18
to librosa
TLDR is that there are many small differences.

I coded up a quick notebook trying to get them to match, you can check it out here.

The first difference that you're likely to hit is that scipy's stft defaults to zero-padding at the boundaries, and librosa's defaults to reflection-padding.  For the comparison, I disabled padding and centering in both stfts.

It looks like scipy's stft is also doing some length normalization that librosa's doesn't.  If I multiply the spectrum by n_fft//2 + 1, they come out pretty close (DC component ratios near 1).

Otherwise, there are occasionally some discontinuities between the two that I can't fully explain, but may be due to some numerical precision issues:

They're in the 5th decimal place though, so I'm not particularly worried about it.
Auto Generated Inline Image 1

Hongbo Chen

unread,
Apr 19, 2018, 10:14:03 PM4/19/18
to librosa
Thanks a lot!
Although there is a little difference, I think it's not important as you say!
Reply all
Reply to author
Forward
0 new messages