Simu: add edge detection function

This commit is contained in:
Eric Teunis de Boone 2022-03-24 17:29:04 +01:00
parent ab20db4ffe
commit 5e4cb0ea83

View file

@ -18,3 +18,21 @@ def rot_vector(phi1=0.12345):
]) ])
return np.cos(unit) 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