Signal convolution in torch

I am convolving two 1D signals with scipy.signal.fftconvolve: c = fftconvolve(b, a, "full"). I would like to replace the fftconvolve function with a torch function. I tried to use torch.nn.functional.conv1d, but it doesn’t return the result I expected. My signals have the same length (and not start/end with 0).

For the sake of completeness, I tested the following code:

from scipy import signal
from torch.nn.functional import conv1d
a = np.repeat([0., 1., 0.], 100)
b = np.repeat([0., 1., 0.], 100)
c = signal.convolve(a, b, mode='full')
c_fft = signal.fftconvolve(a, b, mode='full')

torch_a = torch.Tensor(a)
torch_a = torch.unsqueeze(torch_a, 0)
torch_a = torch.unsqueeze(torch_a, 0)
torch_b = torch.Tensor(b)
torch_b = torch.unsqueeze(torch_b, 0)
torch_b = torch.unsqueeze(torch_b, 0)
torch_c = conv1d(torch_a, torch_b, padding=len(a))

Here conv1d behaves as expected, but if I set a = b = np.repeat([1., 1., 0.], 100) the result is no longer the same (I assume because the signal doesn’t start/end with 0).

Any help is welcome!


try reducing the padding

torch_c = conv1d(torch_a, torch_b, padding=len(a)-1)

No improvement, unfortunately :sweat_smile: