Skip to content

Commit

Permalink
spacer detection method
Browse files Browse the repository at this point in the history
  • Loading branch information
oliche committed Nov 8, 2022
1 parent 9dd39ec commit c72e1df
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
20 changes: 20 additions & 0 deletions iblrig/spacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,23 @@ def add_spacer_states(self, sma=None, next_state="exit"):
state_change_conditions={"Tup": next_loop},
output_actions=[],
)

def find_spacers(self, signal, threshold=0.9, fs=1000):
"""
Find spacers in a voltage time serie. Assumes that the signal is a digital signal between 0 and 1
:param signal:
:param threshold:
:param fs:
:return:
"""
template = self.generate_template(fs=fs)
xcor = np.correlate(signal, template, mode="full") / np.sum(template)
idetect = np.where(xcor > threshold)[0]
iidetect = np.cumsum(np.diff(idetect, prepend=0) > 1)
nspacers = iidetect[-1]
tspacer = np.zeros(nspacers)
for i in range(nspacers):
ispacer = idetect[iidetect == i + 1]
imax = np.argmax(xcor[ispacer])
tspacer[i] = (ispacer[imax] - template.size + 1) / fs
return tspacer
15 changes: 15 additions & 0 deletions test_iblrig/test_spacers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,18 @@ def test_spacer(self):
ac[sig.size-100: sig.size + 100] = 0 # remove the main peak
# the autocorrelation side lobes should be less than 30%
assert np.max(ac) < .3

def test_find_spacers(self):
"""
Generates a fake signal with 2 spacers and finds them
:return:
"""
fs = 1000
spacer = Spacer(dt_start=.02, dt_end=.4, n_pulses=8, tup=.05)
start_times = [4.38, 96.58]
template = spacer.generate_template(fs)
signal = np.zeros(int(start_times[-1] * fs + template.size * 2))
for start_time in start_times:
signal[int(start_time * fs): int(start_time * fs) + template.size] = template
spacer_times = spacer.find_spacers(signal, fs=fs)
np.testing.assert_allclose(spacer_times, start_times)

0 comments on commit c72e1df

Please sign in to comment.