by Dr. Alex Kreilinger
Strategic Product Manager (Brain Products)
Before starting a study that involves EEG recording, it is always a good idea to make sure the setup, including software and hardware, works as planned to avoid any unwanted surprises once it’s time to analyze the recorded data. Unfortunately, it is not always obvious if the timing is off. For example, if a hardware trigger does not match the precise time of the corresponding event.
Two undesirable things can happen in this case
First, there can be a constant delay between event trigger and actual event, called latency. Second, the time delay between event trigger and event can vary, which is called jitter. If there is a stable latency, the only effect is that your temporal components may appear sooner or later. For example, a P300 appears some milliseconds later than expected. The effect of a large jitter is worse, though, as averaging waveforms will smoothen the shape of the ERP. This will change the appearance of the ERP and filter out high frequency components.
In a best-case scenario, you want to have no latency and no jitter at all. Since this is virtually impossible, minimizing the two effects should be your next best option. Moreover, if the latency is stable, it is possible to account for it in the analysis if you know the exact value.
In this article
… we want to demonstrate a specific method to measure delays between the time a hardware trigger is acquired and the actual event it corresponds to. We also want to demonstrate the difference between such hardware triggers and software markers sent via LabStreamingLayer (LSL). To provide an example that everyone can use, we are using PsychoPy, an open-source software based on Python which can be used to run experiments. Using just a bare minimum of code, we demonstrate how to run an experiment specifically designed to point out potential timing issues. In the code, we create flashes on the screen and, at the same time, send a hardware trigger via our TriggerBox that can be recorded with any Brain Products amplifier. At the same time, we also send an LSL marker that can be used as an alternative to these hardware triggers. Please see Figure 1 for a basic scheme showing these three different events and where delays can be expected.
Comparing only hardware triggers and screen flashes
The main purpose of the script is to measure the delay between the time of the hardware trigger and the actual event, which is the flashing of the screen. You can easily do this by connecting a TriggerBox to the computer that is running PsychoPy and connecting the TriggerBox output to your Brain Products amplifier. The TriggerBox connects directly with actiCHamp (Plus) and BrainAmp (via the BrainAmp USB2 Adapter, BUA), or via the Sensor & Trigger Extension for LiveAmp. Now, connect a Photo Sensor to an AUX input and put the sensor on the screen where the flashes appear (per default in the upper left corner). You can also use a StimTrak that generates triggers out of the Photo Sensor signal and connect it to the TriggerBox, but we will assume that the analog signal from the Photo Sensor will be used to generate triggers in the analysis part later.
With this setup, you can record the data any way you want: You can use BrainVision Recorder, or you can use the amplifier-specific LSL connector (see an example for LiveAmp in Figure 2) and LabRecorder. It does not matter, as all the relevant information is only stored in the EEG stream. It does, however, make a difference regarding the software you want to use for analyzing the data, as you will create an XDF file with LabRecorder and BrainVision data format with BrainVision Recorder.
Either way, make sure to specify one AUX channel for the Photo Sensor. You can add EEG channels, but there is no need for them. It is also recommended to select “EEG Channel” for the “LSL Trigger Output Style”.
Adding LSL markers to the comparison
If you want to add LSL markers to your analysis, you also need to record the LSL marker stream that is created in PsychoPy. In the previous example, we only focused on hardware triggers. If you want to record the LSL marker stream you must use LabRecorder and select both the LSL marker stream and the LSL EEG stream coming from the amplifier (see Figure 3).
The ideal outcome
What do we expect when we design an experiment in a way that events should happen at the exact same time?
1. The hardware trigger appears at the same time as the Photo Sensor signal starts to rise.
2. The time stamp of the LSL marker in the recorded XDF file is the same as the time stamp of the hardware trigger.
The realistic outcome
Unfortunately, we may not achieve our desired outcome due to several factors:
The screen does not update immediately. There can be several reasons for this. The graphics card settings may not be ideal, or some extra frames are added due to triple buffering the frames. One example would be to set the scale in the Windows® display setting to a value other than 100 %. There are also many ways in which a small mistake in the code itself can cause extra delays or jitter.
Differences in the time stamps between LSL markers and hardware triggers from an LSL EEG stream can arise from the time that passes between acquisition and sending the EEG as an LSL stream. For example, when using LiveAmp with default settings of 32 channels and 500 Hz, sending the data via Bluetooth®, including some buffering, the delay is around 13 ms. For actiCHamp (Plus), which is connected to the computer via a USB cable, the delay is only a few ms with the same settings. Sending an LSL marker from PsychoPy, however, does not cost any additional time and the markers are saved in LabRecorder immediately.
You may find other problems with this test. For example, you find out that your computers are not performing well enough, or you can see that the network is overloaded.
How this tool should be used
Please keep in mind that this script is very generic and only intended to give you an idea how to verify and test your setup. You may use parts of the code and include it in your own scripts, or you may just get an idea of possible sources for timing inconsistencies and use this information for some further brainstorming. You can also quickly run this script when you want to test some changes in your equipment or settings. We always recommend checking your setup to ensure everything is running smoothly before you start collecting actual data for your study and we hope the exercise provided in this article will find its way into your usual routines.
Analyzing the data
In the provided script, we use MNE-Python to load the XDF file and compare time stamps of the hardware triggers, LSL markers, and the Photo Sensor signal. Please download the analysis script and the XDF files (see box below) that were created with the previously explained setup. There is one file for LiveAmp (“TimingTest_LA.xdf”) and one for actiCHamp/actiCHamp Plus (“TimingTest_aC.xdf”), the main difference being the different signal delays, which you will notice after running the analysis.
Before you can run the script, you need to make sure to install the following packages, for example with a command such as pip install mnelab pyxdf PySide2 rich. Make sure you install at least MNELAB version 0.8.
Executing the script (for example in Spyder) will result in a table with some statistics and a figure (Figure 4), demonstrating the nature of the delays for a LiveAmp (left) and an actiCHamp/actiCHamp Plus (right) example. We analyze the delays between photo signal and hardware triggers, between photo signal and LSL markers, and between hardware triggers and LSL markers. The delay between photo signal and hardware trigger is almost the same for both amplifiers, which is to be expected because it is independent from the acquisition device. The delay of ~18 ms indicates that the square flashes only after a full screen update cycle at 60 Hz. The delay between hardware triggers and LSL markers is the main difference between the two recordings. It shows that the LSL stream from LiveAmp has a higher latency (~13 ms) than the one from actiCHamp/actiCHamp Plus (~3 ms). This difference also results in the longer delay between time stamps of the photo signal versus LSL markers, on average more than 30 ms for LiveAmp. These additional 30 ms should be considered when LSL markers are used as starting points for epochs containing events related to the actual flashing of the screen. In chronological order, the LSL markers appear first, followed by the hardware triggers, and then the screen updates come last.
Please feel free to adjust the script to your needs and test with other files you recorded yourself.
As always, please don’t hesitate to reach out to us if you have any questions on timing verification in general or require assistance related to your specific setup.
Further reading:Do you trust your triggers? Novel product presentation: StimTrak
How to reprocess EEG data to correct offsets between EEG and coregistered markers