def freq_window(freq, factor, axis):
    """
    Compute a window centered at freq within an axis.

    freq_window() returns a tuple with the lower and upper indices in the axis for a
    frequency window of freq +- factor. The size is at most 2*factor, but
    is limited by the axis borders. It returns (0,0) or (end,end) if the window is
    out of the axis by the left or right respectively (i.e., end = len(axis)).
    Equispaced axes are assumed.
    @rtype : tuple
    @param freq: center frequency
    @param factor: the window factor (half window size)
    @param axis: a equispaced array of elements
    """
    dlta = axis[1] - axis[0]
    ini = int(round((freq - factor - axis[0]) / dlta))
    end = int(round((freq + factor - axis[0]) / dlta))
    if ini < 0:
        ini = 0
    if end > len(axis):
        end = len(axis)
    return ini, end
