Torchaudio resampling could be faster and simpler
See original GitHub issue🐛 Bug
I have been implementing a few DSP algorithms lately, including sinc based resampling. The following implementation could simplify the code in torchaudio and is also faster (single call to conv1d, instead of multiple calls to conv1d and conv_transpose1d). I’ll be happy to open a pull requests if you are interested.
https://github.com/adefossez/julius/blob/main/julius/resample.py
To Reproduce
Steps to reproduce the behavior:
- Install modules
pip install -U torch torchaudio julius
- Run the benchmark code hereafter
from torchaudio.compliance import kaldi
import torch
import julius
x = th.randn(1, 44100 * 10)
rolloff = 0.99 # lowpass filter freq used by torchaudio
zeros = 6 # use the same number of zero crossing as torchaudio
for from_sr, to_sr in [(5, 7), (7, 5)]:
print("comparing for", from_sr, to_sr)
%timeit kaldi.resample_waveform(x, from_sr, to_sr)
%timeit julius.resample_frac(x, from_sr, to_sr, rolloff=rolloff, zeros=zeros)
yt = kaldi.resample_waveform(x, from_sr, to_sr)
yj = julius.resample_frac(x, from_sr, to_sr, rolloff=rolloff, zeros=zeros)
print(torch.norm(yt - yj))
Expected behavior
Would expect torchaudio to be as fast as julius, results are
comparing for 5 7
32.4 ms ± 309 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
4.46 ms ± 44.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
tensor(8.7085e-05)
comparing for 7 5
12.9 ms ± 323 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
2.83 ms ± 43.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
tensor(5.1333e-05)
Environment
- What commands did you used to install torchaudio (conda/pip/build from source)? pip
- If you are building from source, which commit is it?
- What does
torchaudio.__version__
print? 0.7.0a0+ac17b64
PyTorch version: 1.7.0 Is debug build: True CUDA used to build PyTorch: None ROCM used to build PyTorch: N/A
OS: macOS 10.15.7 (x86_64) GCC version: Could not collect Clang version: 11.0.0 CMake version: version 3.18.4
Python version: 3.8 (64-bit runtime) Is CUDA available: False CUDA runtime version: No CUDA GPU models and configuration: No CUDA Nvidia driver version: No CUDA cuDNN version: No CUDA HIP runtime version: N/A MIOpen runtime version: N/A
Versions of relevant libraries: [pip3] numpy==1.19.2 [conda] blas 1.0 mkl [conda] mkl 2019.4 233 [conda] mkl-service 2.3.0 py38hfbe908c_0 [conda] mkl_fft 1.2.0 py38hc64f4ea_0 [conda] mkl_random 1.1.1 py38h959d312_0 [conda] numpy 1.19.1 py38h3b9f5b6_0 [conda] numpy-base 1.19.1 py38hcfb5961_0 [conda] pytorch 1.7.0 py3.8_0 pytorch [conda] torchaudio 0.7.0 py38 pytorch
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (7 by maintainers)
Top GitHub Comments
I’m not an expert in resampling by any means but I created a quick benchmark for speed and compared Log Spectral Distance and the Noise Ratio between slower more high quality methods in a notebook that might be useful for this thread
tldr; julius seems to be the best option
Thanks again for working on this 😃 I see #1087 as closing this issue. If this is not the case, please feel free to re-open, or if there are follow-ups, to open new issues.