zwy
zwy

Reputation: 11

how to make the output of tf.signal.stft same as librosa.stft?

I want to pass gradient back through STFT in tensorflow. So I use tf.signal.stft instead of librosa.stft. However, their ouput are different. I want to fix certer=True for librosa.stft. I have refrenced to this. I find it didn't work for me.

Upvotes: 1

Views: 1091

Answers (1)

HaiderZhong
HaiderZhong

Reputation: 11

I did some experiments and compared the code between the two as follows,

audio=... # shape of [n_sample]
hop_length=...
n_fft=...
hop_length=...
win_length=...

s_tf = tf.signal.stft(
    signals=audio[tf.newaxis,...], # adds batch dimension
    frame_length=win_length,
    frame_step=hop_length,
    fft_length=n_fft,
    pad_end=True)

s_librosa = librosa.stft(y=audio, n_fft=n_fft, hop_length=hop_length, win_length=win_length, center=True)

# shape of s_tf: [batch_size, math.ceil(n_sample/hop_length), n_fft//2+1]
# shape of s_librosa: [n_fft//2+1, n_sample//hop_length+1]
# s_tf[0] == s_librosa.T[1:-1,:]

The point is the last line: s_tf[0] == s_librosa.T[1:,:], which means that the first frame of stft calculated by tensorflow is equal to the second frame of librosa's stft. Additionally,s_librosa's last frame is also redundant.

Upvotes: 1

Related Questions