import math


def _get_alpha_bar_from_time(t):
    """
    Noise scheduling method proposed by Nichol et. al to avoid too noisy image especially for smaller resolution.

    This strategy creates beta as follows:
        alpha_bar(t) = f(t) / f(0)
        f(t) = cos((t / T + s) / (1 + s) * PI / 2) ** 2
        beta(t) = 1 - alpha_bar(t) / alpha_bar(t-1)

    where s = 0.008 (~= 1 / 127.5), which ensures sqrt(beta_0) is slightly smaller than the pixel bin size.
    """

    assert 0 <= t <= 1, "t must be normalized by max diffusion timestep (T)."

    return math.cos((t + 0.008) / 1.008 * math.pi / 2) ** 2