def freq_by_zeros(xdata, sample_rate):
    """ Calculate frequency of oscillating signal by zeros. Signal
    must be detrended before. """
    T = len(xdata) / sample_rate
    n = 0
    for x_p, x_c in zip(xdata[:-1], xdata[1:]):
        if x_p * x_c < 0:
            n += 1
    return n / 2 / T
