pytorch / audio

Data manipulation and transformation for audio signal processing, powered by PyTorch
https://pytorch.org/audio
BSD 2-Clause "Simplified" License
2.54k stars 654 forks source link

Resampling from 8KHz to 16KHz generate nonexisting spectral components #3603

Open pengyizhou opened 1 year ago

pengyizhou commented 1 year ago

🐛 Describe the bug

Hi, I have found a abnormal situation when I try to use torchaudio to resample 8KHz speech data to 16KHz. The code I am using is as follow.

import torchaudio
import torchaudio.backend.sox_io_backend as sox
wav = "float32_wav_file" # given a float32, 8KHz wav file
audio, samplerate = sox.load(wav, normalize=False)
audio = torchaudio.transforms.Resample(8000, 16000)(audio)
torchaudio.save(filepath="new_wav_path", src=audio, sample_rate=16000)
sox $original_wav -r 16000 $new_wav
CleanShot 2023-09-12 at 20 35 41@2x

Here is another more obvious example: CleanShot 2023-09-12 at 20 40 12

When using torchaudio to upsample an 8KHz audio to 16KHz, it will somehow generate some spectral components that should not exist.

Versions

PyTorch version: 2.0.1+cu118 Is debug build: False CUDA used to build PyTorch: 11.8 ROCM used to build PyTorch: N/A

OS: Rocky Linux 8.4 (Green Obsidian) (x86_64) GCC version: (GCC) 9.3.0 Clang version: 14.0.6 (Red Hat 14.0.6-1.module+el8.7.0+1080+d88dc670) CMake version: version 3.19.4 Libc version: glibc-2.28

Python version: 3.8.17 (default, Jul 5 2023, 21:04:15) [GCC 11.2.0] (64-bit runtime) Python platform: Linux-4.18.0-305.25.1.el8_4.x86_64-x86_64-with-glibc2.17 Is CUDA available: False CUDA runtime version: 11.6.55 CUDA_MODULE_LOADING set to: N/A GPU models and configuration: Could not collect Nvidia driver version: Could not collect cuDNN version: Could not collect HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: True

CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 48 On-line CPU(s) list: 0-47 Thread(s) per core: 2 Core(s) per socket: 12 Socket(s): 2 NUMA node(s): 2 Vendor ID: GenuineIntel CPU family: 6 Model: 85 Model name: Intel(R) Xeon(R) Silver 4214R CPU @ 2.40GHz Stepping: 7 CPU MHz: 2999.754 CPU max MHz: 3500.0000 CPU min MHz: 1000.0000 BogoMIPS: 4800.00 Virtualization: VT-x L1d cache: 32K L1i cache: 32K L2 cache: 1024K L3 cache: 16896K NUMA node0 CPU(s): 0-11,24-35 NUMA node1 CPU(s): 12-23,36-47 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req pku ospke avx512_vnni md_clear flush_l1d arch_capabilities

Versions of relevant libraries: [pip3] flake8-bugbear==23.1.20 [pip3] flake8-comprehensions==3.10.1 [pip3] flake8-executable==2.1.3 [pip3] flake8-pyi==20.5.0 [pip3] numpy==1.22.0 [pip3] pytorch-ranger==0.1.1 [pip3] pytorch-wpe==0.0.1 [pip3] torch==2.0.1+cu118 [pip3] torch-complex==0.4.3 [pip3] torch-optimizer==0.3.0 [pip3] torchaudio==2.0.2+cu118 [pip3] triton==2.0.0 [conda] numpy 1.22.0 pypi_0 pypi [conda] torch 2.0.1+cu118 pypi_0 pypi [conda] torchaudio 2.0.2+cu118 pypi_0 pypi [conda] triton 2.0.0 pypi_0 pypi

mthrok commented 1 year ago

Hi

What would happen if you set rolloff to something close to 0.5 = (8000/16000), or other parameters listed https://pytorch.org/audio/main/generated/torchaudio.functional.resample.html#torchaudio.functional.resample? If that works, perhaps we could set parameter automatically when performing upsampling.

pengyizhou commented 1 year ago

Hi

What would happen if you set rolloff to something close to 0.5 = (8000/16000), or other parameters listed https://pytorch.org/audio/main/generated/torchaudio.functional.resample.html#torchaudio.functional.resample? If that works, perhaps we could set parameter automatically when performing upsampling.

Hi, thanks for your reply!

I have tried some of these parameters, the results are as follows.

mthrok commented 1 year ago

When set the lowpass_filter_width to a higher value, e.g., 12,24,32, and keep rolloff default(=0.99). When lowpass_filter_width becomes larger, the spectrum would be closer to the correct one, but not exactly correct.

This one looks close enough to me. What do you mean "exactly correct"? They are two different implementation and they are not guaranteed to match exactly.

One way to inspect the nature of sampling is to resample the sine sweep like it's done here for downsampling. https://pytorch.org/audio/main/tutorials/audio_resampling_tutorial.html#resampling-overview

You can plot the spectrogram of upsampled sine sweep and see what kind of artifacts present.

felipeespic commented 1 year ago

Use values like lowpass_filter_width=100 or lowpass_filter_width=200, if speed is not critical for you. The default lowpass_filter_width=6 is too low.