I'm measuring how stable are the signals generated on parallel port.
We have numpy arrays imported from our CSV file generated by an oscilloscope. The following output is the stripped variant to show the problem:
import numpy as np
data = np.array([0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0])
t = np.array([0. ,0.0005, 0.001 ,0.0015,
0.002 ,0.0025, 0.003 ,0.0035,
0.004 ,0.0045, 0.005 ,0.0055,
0.006 ,0.0065, 0.007 ,0.0075,
0.008 ])
It looks something like this when plotted.
I'm looking for the cleanest way to get the lasting times of the impulse as values in a list.
After writing this, i'll try to implement the solution myself from scratch, what i'm hoping for is for the preferred way of getting the lasting times, perhaps there is a numpy function for that which i'm unaware of?
Assuming data
is an int array of 0
's and 1
's:
1.) This defines the lasting time of the pulse from the last 0
to last 1
:
import numpy as np
idx = t[np.abs(np.diff(data)) == 1]
lasting_times = idx[1::2] - idx[::2]
2.) If you rather prefer the definition of lasting time as from the first 1
to last 1
:
diff = np.diff(data)
lasting_times = t[diff == -1] - t[1:][diff == 1]
NOTE:
In any way you have to deal with the ends of your data, i.e these solutions assume data
starts and ends with 0
...