STFT
ISTFT
画时频图
torch.STFT, istft
示例代码
istft
import librosa
import numpy as np
import torch
import soundfile as sf
### 选用torch.STFT
# sigal, fs = librosa.load(r'/home/albert_z/deepLearning/experiment/data/train_noisy/p226_001_0.wav',sr=16000)
# mag,pha = librosa.magphase(librosa.stft(sigal,n_fft=512,win_length=320,hop_length=160,window="hann"))
# print(mag.shape)
# print(pha.shape)
# print(mag.transpose(0,1))
# print(pha.transpose(0,1))
# # enhanced = librosa.istft(noisy_mag * noisy_phase, hop_length=hop_length, win_length=win_length, length=len(noisy))
# enhanced = librosa.istft(mag*pha,hop_length=160,win_length=320,length=fs)
# sf.write("./test.wav",enhanced,samplerate=16000)
'''
def test_torch(inputs):
fft_len=512
win_len=320
len_hop=160
inputs = torch.from_numpy(inputs.reshape(1,-1).astype(np.float32))
window = torch.hann_window(win_len)
spec = torch.stft(inputs, fft_len, len_hop, win_len, window, center=True, return_complex=False)
print("stft out", spec.shape)
out = torch.istft(spec, fft_len, len_hop, win_len, window, True, return_complex=False)
return out
'''
sigal, fs = librosa.load(r'./data/train_noisy/p226_001_0.wav',sr=16000)
sigal = torch.from_numpy(sigal).unsqueeze(0)
fft_len=512
win_len=320
len_hop=160
window = torch.hann_window(win_len)
spec = torch.stft(sigal, fft_len, len_hop, win_len, window, center=True, return_complex=False).permute(0,3,2,1) # B,2,T,F
print("stft out", spec.shape)
spec = spec.permute(0,3,2,1)
out = torch.istft(spec, fft_len, len_hop, win_len, window, True, return_complex=False)
print("istft",out.shape)
sf.write("./test.wav",out.squeeze(0),samplerate=16000)
'''
real = spec[:, :, :, 0] # 实部
imag = spec[:, :, :, 1] # 虚部
mags = torch.abs(torch.sqrt(torch.pow(rea, 2) + torch.pow(imag, 2)))
phase = torch.atan2(imag.data, real.data)
'''
stft
import soundfile as sf
import torch
tt, fs = sf.read(r'./data/train_noisy/p226_001_0.wav')
# 第三种, 选择第三种
tt = torch.from_numpy(tt).unsqueeze(0)
# print(tt.shape)
feat_x = torch.stft(
tt,
n_fft=512,
hop_length=160,
win_length=320,
window=torch.hann_window(320),
).permute(0, 3, 2, 1)
print(feat_x.shape)
feat_x_phase = torch.atan2(feat_x[:, -1, :, :], feat_x[:, 0, :, :])
feat_x_mag = (torch.norm(feat_x, dim=1)) ** 0.5
print(feat_x_mag)
print(feat_x_phase)
print(feat_x_mag.shape)
print(feat_x_phase.shape)