def _time_above_threshold(data, thresh, sampling_rate):
    """Return the number of ms that a given time series is above a threshold."""
    max_count = 0
    curr_count = 0
    above = False
    last_sample = 0.0
    for sample in data:
        if above is False and abs(sample) >= thresh:
            above = True
            curr_count = 0
        elif above:
            curr_count += 1
            if abs(sample) < thresh or last_sample * sample < 0.0:
                above = False
                if curr_count > max_count:
                    max_count = curr_count
        last_sample = sample
    if above and curr_count > max_count:
        max_count = curr_count
    return max_count / sampling_rate * 1000.0
