dconv

dnois.fourier.dconv(f1: Tensor, f2: Tensor, dims: Sequence[int] = None, out: Literal['full', 'same', 'valid'] = 'full', padding: int | Sequence[int] | str = 'linear', real: bool = None) Tensor

Computes n-D discrete convolution for two tensors \(f_1\) and \(f_2\) utilizing FFT.

In each involved dimension, the lengths of \(f_1\), \(N\) and \(f_2\), \(M\) can be different. In that case, the shorter sequence will be padded first to match length of the longer one. Notably, it is circular convolution that is computed without additional padding (if padding is 0, circular or none). Specify padding as linear or \(\min(N,M)-1\) to compute linear convolution, with extra computational overhead. padding can also be set as a non-negative integer within this range for a balance. Note that the lengths of each dimension can be different.

See Convolution for examples.

See also

This function is similar to scipy.signal.fftconvolve when padding is linear. dconv1() and dconv2() are 1D and 2D variants, respectively, with slightly different signature. conv() provides more functionalities.

Parameters:
  • f1 (Tensor) – The first tensor \(f_1\).

  • f2 (Tensor) – The second tensor \(f_2\).

  • dims (Sequence[int]) – The dimensions to be convolved. Default: the last ndim dimensions where ndim is the fewer number of dimensions of f1 and f2

  • out (str) –

    One of the following options. Default: full. In each dimension:

    full

    Return complete result so the size of each dimension is max(N, M) + padding.

    same

    Return the middle segment of result. In other words, drop the first min(N, M) // 2 elements and return subsequent max(N, M) ones. If the length is less than max(N, M) after dropping, the elements from head will be appended to reach this length.

    valid

    Return only the segments where f1 and f2 overlap completely so the size of each dimension is \(\max(N,M)-\min(N,M)+1\).

  • padding (int, Sequence[int] or str) – Padding amount in all dimensions. See description above. Default: linear.

  • real (bool) – If both f1 and f2 are real-valued and real is True, torch.fft.rfft() can be used to improve computation and a real-valued tensor will be returned. If either f1 or f2 is complex or real=False, a complex tensor will be returned. Default: depending on the dtype of f1 and f2.

Returns:

Convolution between f1 and f2. Complex if either f1 or f2 is complex or real=False, real-valued otherwise.

Return type:

Tensor