python 常規(guī)的 stft 都是在 cpu 上進(jìn)行計(jì)算,如果網(wǎng)絡(luò)訓(xùn)練是在 GPU 上進(jìn)行,那么就涉及到數(shù)據(jù)傳輸?shù)膯栴},降低計(jì)算效率;而 torch 自帶的 stft 可以直接在 GPU 上進(jìn)行計(jì)算,因此可以節(jié)省計(jì)算時(shí)間。
import torch
import torchaudio
time_audio, sr = torchaudio.load('./audio/bed_room_record_0.wav')
frequency_audio = torch.stft(time_audio, n_fft = 512, hop_length = 160, return_complex=True, onesided=True)
time_recover = torch.istft(frequency_audio, n_fft = 512, hop_length = 160)
運(yùn)行結(jié)果如下:
?
根據(jù)結(jié)果可以發(fā)現(xiàn)輸入跟短時(shí)傅里葉逆變換的的結(jié)果大小并不一致,這是因?yàn)閟tft截?cái)嗟脑?,可以通過在輸入信號之前添加padding的操作實(shí)現(xiàn)前后大小一致的目標(biāo)。
import torch
import torchaudio
import numpy as np
def padding_audio(time_audio,hop_len):
length = time_audio.size(-1)
frame_num = int(np.ceil(length/hop_len))
padded_len = frame_num * hop_len
padding_len = padded_len - length
padded_audio = torch.cat([time_audio,time_audio[:,:padding_len]],-1)
return padded_audio
time_audio, sr = torchaudio.load('./audio/bed_room_record_0.wav')
padded_time_audio = padding_audio(time_audio,160)
frequency_audio = torch.stft(padded_time_audio, n_fft = 512, hop_length = 160, return_complex=True, onesided=True)
time_recover = torch.istft(frequency_audio, n_fft = 512, hop_length = 160)
torch.cat((padded_time_audio, time_recover), 0)
?輸出結(jié)果如下:
?文章來源:http://www.zghlxwxcb.cn/news/detail-579954.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-579954.html
到了這里,關(guān)于使用 torch.stft 進(jìn)行短時(shí)傅里葉變換的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!