1
$\begingroup$

I read the source code of librosa.stft and sicpy.signal.stft, and notice that the calculation results of STFT(short-time fourier transform) in these two libraries are quite different: In scipy.signal.stft, the stft result is scaled by 1.0/win.sum(), while in librosa.stft no scaling or normalization procedure is done. So why scipy.signal.stft do the additional scaling procedure? And is there any other difference in the calculation of STFT in these two libraries?

$\endgroup$
1
$\begingroup$

Fft scaling (normalization) isn't strictly a part of an fft algorithm although some implementations (like the one you mention) include it. Since scaling depends on the actual fft algorithm, it makes sense, in my opinion, to include it in the implementation because it may not be obvious for programmers to decide what scaling factor(s) to use (the usual scaling factors are $1/N$, $2/N$ (an efficient fft algorithm that is using only half the spectrum so we need to multiply by 2 to compensate for the missing energy), $1/sum(wnd)$, $2/sum(wnd)$

As for your second question, I haven't gone over the code (you haven't provided any links to the source), but some (usually minor) calculation differences might exist given that an fft algorithm can be implemented in a number of ways ( e.g real fft vs complex fft, decimation in frequency vs decimation in time, radix 2 vs mixed radix etc and combinations thereof)

$\endgroup$
1
$\begingroup$

scipy.signal.stft uses scale factor for the result stft source code
To get the same values as for librosa.stft you need:

_, _, stft_res = scipy.signal.stft(inputAudio, window='hamming', nperseg=640, noverlap=480, boundary=None, padded=False)
hamm_win = scipy.signal.get_window('hamming', 640)
scale = np.sqrt(1.0 / hamm_win.sum()**2)
stft_res = stft_res / scale

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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