Pythorch的STFT/ISTFT
torch-stft的Python项目详细描述
pytorch中的stft/istft
用一维卷积用pytorch写成的stft/istft。要求是最新版本的pytorch、numpy和librosa(用于在test_stft.py中加载音频)。感谢shrikant venkataramani共享代码,这是基于和rafael valle捕获错误和添加适当的窗口逻辑。使用Python3。
安装
使用pip轻松安装:
pip install torch_stft
用法
import torch
from torch_stft import STFT
import numpy as np
import librosa
import matplotlib.pyplot as plt
audio = librosa.load(librosa.util.example_audio_file(), duration=10.0, offset=30)[0]
device = 'cpu'
filter_length = 1024
hop_length = 256
win_length = 1024 # doesn't need to be specified. if not specified, it's the same as filter_length
window = 'hann'
audio = torch.FloatTensor(audio)
audio = audio.unsqueeze(0)
audio = audio.to(device)
stft = STFT(
filter_length=filter_length,
hop_length=hop_length,
win_length=win_length,
window=window
).to(device)
magnitude, phase = stft.transform(audio)
output = stft.inverse(magnitude, phase)
output = output.cpu().data.numpy()[..., :]
audio = audio.cpu().data.numpy()[..., :]
print(np.mean((output - audio) ** 2)) # on order of 1e-16
输出^{
测试
只需克隆此repo并运行
pip install -r requirements.txt
python -m pytest .
不幸的是,由于它是用一维卷积实现的,所以有些滤波器的长度/跳长 当在足够大的输入上运行时,组合可能会导致GPU内存不足错误。
贡献
欢迎拉取请求。