Skip to content

torchaudio.transforms.Resample outputs repeating blocks of kernel values instead of resampled audio (Dimension/Convolution bug) #1574

Description

@ralfdoehmer-ops

Description:
There is a severe bug in the C# translation of torchaudio.transforms.Resample / functional._apply_sinc_resample_kernel when converting audio (e.g., from 22050 Hz to 16000 Hz).
Instead of a cleanly resampled waveform, the output contains repeating blocks of identical values matching the upsampling factor of the kernel (exactly 320 repeating float values).
Root Cause:
The C# implementation of _apply_sinc_resample_kernel translates the original Python code but mishandles tensor dimensions when invoking torch.nn.functional.conv1d.
When downsampling from 22050 to 16000, the calculated kernel has the shape [320, 1, 459].
Because the C# wrapper applies conv1d with a 1D input tensor improperly shaped or matched, the convolution is applied across channels instead of across time.
This results in an output shape of [1, 320, NewSamples].
When reading the tensor back to C# via .data().ToArray(), the data layout is flattened linearly, causing the user to read the 320 kernel-channel values for each time step sequentially. This looks like a static block of 320 repeating values. The required reshaping/transposing to fold the 320 channels back into the time axis is either missing or broken in the C# port.
To Reproduce:csharp
// Input: Float array of 22050Hz audio
float[] signal = ...
using var nativeTensor = torch.from_array(signal).to(torch.ScalarType.Float32);
using var inputTensor = nativeTensor.unsqueeze(0); // [1, Samples]

using var resampler = torchaudio.transforms.Resample(orig_freq: 22050, new_freq: 16000);
using var output = resampler.forward(inputTensor);

// Look at the raw float/byte array: It contains blocks of 320 repeating values
float[] result = output.squeeze(0).ToArray();

Behavior:
The output should match the bit-accurate windowed sinc interpolation of Python's torchaudio.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions