def find_threshold_crossings(arr, _threshold):
    """
    Find all indices at which a threshold is crossed from above and from below
    in an array. Used for finding indices to compute ap widths and half widths.
    """
    #print("threshold = {}".format(_threshold))
    ups = []
    downs = []
    for i, _ in enumerate(arr[:-1]): # Don't iterate on last element
        # Get crossings of threshold from below
        if arr[i] < _threshold:
            if arr[i+1] >= _threshold:
                ups.append(i)
        # Get crossings of threshold from above
        if arr[i] > _threshold:
            if arr[i+1] <= _threshold:
                downs.append(i)
    return ups, downs