xdf-modules / pyxdf

Python package for working with XDF files
BSD 2-Clause "Simplified" License
37 stars 17 forks source link

Apply dejittering / sychronization to several streams at once #57

Open tstenner opened 4 years ago

tstenner commented 4 years ago

Some devices (case in point: the BrainAmps) record a digital trigger alongside the EEG data and offer three options to send the markers:

  1. as an additional channel in the EEG stream converted to whatever data type the EEG data has
  2. as a continouous int8 / int16 / string stream with the same sampling rate as the EEG stream
  3. as a separate, irregular int8 / int16 / string stream

The procedure is as follows (pseudocode, but probably also valid python):

data, triggers, first_timestamp = device.get_chunk()
trigger_indices = triggers.find()
data_outlet.push(data, first_timestamp)
trigger1_outlet.push([data; triggers], first_timestamp)
trigger2_outlet.push(triggers, first_timestamp)
trigger3_outlet.push(triggers[trigger_indices], first_timestamp + trigger_indices / sampling_rate)

Both for resource usage and usability, 3 is the best option but irregularly sampled stream can't be dejittered so the markers aren't as reliable as they should be.

But, as seen in the example above, the timestamps have the same jitter and time offsets so if it were possible to copy dejitter / synchronization parameters from one stream to another the third option would be feasable for offline analysis.

agricolab commented 4 years ago

Are you certain that the markerstream under option 3 has the same jitter as the datastream? Would the following approach to align a marker of type 3 with the data work?

for marker, tstamp in zip(marker["time_series"], marker["time_stamps"]:
    idx = np.argmin(np.abs(tstamp-data["time_stamps"]))

even if the data stream is dejittered and synchronized, the sample-index should not be affected as long as data arrived in order.

I do it similarly here: https://github.com/agricolab/xdf-Python/blob/sync_timestamps/pyxdf/pyxdf.py#L864 or