qfeval_functions.functions.gaussian_blur

gaussian_blur(x, sigma, dim=-1)[source]

Apply Gaussian blur to a tensor along a specified dimension.

This function applies a one-dimensional Gaussian filter to smooth data along the specified dimension. The Gaussian blur operation computes a weighted average of neighboring values, where weights follow a Gaussian (normal) distribution centered at each point. This is commonly used for noise reduction, data smoothing, and signal processing.

Unlike typical implementations that use point-sampling (such as scipy.ndimage.gaussian_filter1d), this function uses interval averages of the Gaussian function for improved accuracy, especially for small sigma values. This approach avoids undersampling issues and provides more accurate results.

Parameters:
  • x (Tensor) – The input tensor to be blurred.

  • sigma (float) – The standard deviation of the Gaussian kernel. Larger values produce more smoothing. Must be positive.

  • dim (int) – The dimension along which to apply the Gaussian blur. Default is -1 (the last dimension).

Returns:

A tensor of the same shape as the input, containing the Gaussian-blurred values.

Return type:

Tensor

Example

>>> # Simple 1D Gaussian blur
>>> x = torch.tensor([0., 0., 0., 10., 0., 0., 0.])
>>> QF.gaussian_blur(x, sigma=1.0)
tensor([0.0864, 0.6494, 2.4324, 3.8310, 2.4324, 0.6494, 0.0864])
>>> # 2D tensor: blur along different dimensions
>>> x = torch.zeros(3, 5)
>>> x[1, 2] = 10.0
>>> QF.gaussian_blur(x, sigma=0.2, dim=0)  # blur along rows
tensor([[0.0000, 0.0000, 0.0625, 0.0000, 0.0000],
        [0.0000, 0.0000, 9.8758, 0.0000, 0.0000],
        [0.0000, 0.0000, 0.0625, 0.0000, 0.0000]])
>>> QF.gaussian_blur(x, sigma=0.2, dim=1)  # blur along columns
tensor([[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
        [0.0000, 0.0621, 9.8758, 0.0621, 0.0000],
        [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]])