diff --git a/simulations/lib/util.py b/simulations/lib/util.py index ef69931..59f1a1b 100644 --- a/simulations/lib/util.py +++ b/simulations/lib/util.py @@ -18,3 +18,21 @@ def rot_vector(phi1=0.12345): ]) return np.cos(unit) + +def detect_edges(threshold, data, rising=True, falling=False): + """ + Detect rising/falling edges in data, returning the indices + of the detected edges. + + https://stackoverflow.com/a/50365462 + """ + + mask = np.full(len(data), False) + + if rising: + mask |= (data[:-1] < threshold) & (data[1:] > threshold) + + if falling: + mask |= (data[:-1] > threshold) & (data[1:] < threshold) + + return np.flatnonzero(mask)+1