import torch


def gaussian_discrete_erf(window_size, sigma):
    """
        Discrete Gaussian by interpolating the error function. Adapted from:
        https://github.com/Project-MONAI/MONAI/blob/master/monai/networks/layers/convutils.py
    """

    device = sigma.device if isinstance(sigma, torch.Tensor) else None

    sigma = torch.as_tensor(sigma, dtype=torch.float, device=device)

    x = torch.arange(window_size).float() - window_size // 2

    t = 0.70710678 / torch.abs(sigma)

    gauss = 0.5 * ((t * (x + 0.5)).erf() - (t * (x - 0.5)).erf())

    gauss = gauss.clamp(min=0)

    return gauss / gauss.sum()