2022-03-11 16:14:48 +01:00
|
|
|
"""
|
|
|
|
Various useful utilities (duh)
|
|
|
|
"""
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
def sampled_time(sample_rate=1, start=0, end=1, offset=0):
|
|
|
|
return offset + np.arange(start, end, 1/sample_rate)
|
|
|
|
|
|
|
|
def rot_vector(phi1=0.12345):
|
|
|
|
"""
|
|
|
|
Return a unit vector rotated by phi radians.
|
|
|
|
"""
|
|
|
|
|
|
|
|
unit = np.array([
|
|
|
|
phi1,
|
|
|
|
phi1 - np.pi/2
|
|
|
|
])
|
|
|
|
|
|
|
|
return np.cos(unit)
|
2022-03-24 17:29:04 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
2022-04-08 16:06:41 +02:00
|
|
|
mask = np.full(len(data)-1, False)
|
2022-03-24 17:29:04 +01:00
|
|
|
|
|
|
|
if rising:
|
|
|
|
mask |= (data[:-1] < threshold) & (data[1:] > threshold)
|
|
|
|
|
|
|
|
if falling:
|
|
|
|
mask |= (data[:-1] > threshold) & (data[1:] < threshold)
|
|
|
|
|
|
|
|
return np.flatnonzero(mask)+1
|