Episode 3: Auditory Evoked Potentials

Play one tone a hundred times with a paradigm, average the EEG around each with a Trigger and a TriggerScope, and record the raw signal with the codes beside it.

A short tone leaves a small, repeatable trace in the EEG: the auditory evoked potential, here its late cortical part, N1 and P2 at about 100 and 200 ms (the brainstem response needs kilohertz rates). This episode plays the same tone a hundred times and averages the EEG around each one until the response stands out. It needs Paradigm Presenter, and so runs on Windows only, see Episode 1. The pipeline built here is the scaffold the next two episodes change in one place each.

The paradigm

AEPSingleStim.xml shows a rest picture for 20 s, then plays a 50 ms tone at 1000 Hz a hundred times, sending code 1 with each one. Each tone’s task lasts 0.2 s, then a rest of 1.0, 1.25, 1.5 or 1.75 s, picked at random, so the next tone cannot be anticipated. A run takes about three minutes.

12PARADIGM = os.path.join(os.path.dirname(os.path.abspath(__file__)),
13                        "paradigms", "AEPSingleStim.xml")
14CHANNELS = 8
A source, and the filters for display

The generator stands in for an amplifier, so the script runs without hardware. To record a person, uncomment the BCICore or GNautilus line and comment out the generator. The signal is filtered for the display and the live average: 1 to 30 Hz, and a notch at the mains frequency, 50 or 60 Hz; the script carries both so it runs anywhere. A causal filter shifts every latency by its delay and a 1 Hz high-pass trims the slow components, so the live average is for watching the run, and the raw signal in the file is what to analyse offline, zero-phase.

21    # amp = gp.BCICore(channel_count=CHANNELS)
22    # amp = gp.GNautilus(sampling_rate=250, channel_count=CHANNELS)
23    amp = gp.Generator(sampling_rate=250, channel_count=CHANNELS,
24                       signal_frequency=10, signal_amplitude=15,
25                       noise_amplitude=10)
26
27    # Filtered for display and epoching: 1 to 30 Hz, both mains notches
28    bandpass = gp.Bandpass(f_lo=1, f_hi=30)
29    notch50 = gp.Bandstop(f_lo=48, f_hi=52)
30    notch60 = gp.Bandstop(f_lo=58, f_hi=62)
31    p.connect(amp, bandpass)
32    p.connect(bandpass, notch50)
33    p.connect(notch50, notch60)
The codes, and a key

The paradigm’s codes arrive at the ParadigmPresenter itself, which is a UDPReceiver listening at the default port, so no second receiver is needed. A Keyboard is there for one key: pressing M (code 77) puts a mark on the scope and in the file, a way to note a moment by hand, such as the participant moving.

35    # The presenter receives the codes; the M key (77) marks by hand
36    presenter = gp.ParadigmPresenter(PARADIGM)
37    keyboard = gp.Keyboard()
What the scope shows

The filtered signal, the codes and the key presses, merged by a Router and marked. The two code channels are hidden, so a 77 does not overrun the lanes.

39    # Filtered signal, codes and key presses side by side on one scope
40    mk = gp.TimeSeriesScope.Markers
41    markers = [mk(color="r", label="stim", channel=CHANNELS, value=1),
42               mk(color="c", label="M key", channel=CHANNELS + 1, value=77)]
43    scope = gp.TimeSeriesScope(amplitude_limit=50, time_window=10,
44                               markers=markers,
45                               hidden_channels=[CHANNELS, CHANNELS + 1])
46    shown = gp.Router(input_channels=[gp.Router.ALL] * 3)
47    p.connect(notch60, shown["in1"])
48    p.connect(presenter, shown["in2"])
49    p.connect(keyboard, shown["in3"])
50    p.connect(shown, scope)
What the file keeps

The CsvWriter gets the unfiltered signal, with the same two channels beside it, in a file named after the paradigm. A filter can always be applied again later, but a filtered recording cannot be turned back into the raw one.

52    # The recording keeps the unfiltered signal, with the same two beside
53    # it, in a file named after the paradigm
54    kept = gp.Router(input_channels=[gp.Router.ALL] * 3)
55    p.connect(amp, kept["in1"])
56    p.connect(presenter, kept["in2"])
57    p.connect(keyboard, kept["in3"])
58    p.connect(kept, gp.CsvWriter(file_name="AEPSingleStim.csv"))
Average around every tone

A Trigger cuts 0.2 s before to 0.7 s after every code 1, and the TriggerScope shows the average of all of them so far. It is scaled to plus or minus 5 µV for the generator-sized response; on a person a vertex N1 reaches 5 to 10 µV and a P300 more, so widen it.

60    # An epoch around every code 1, averaged as they arrive
61    trigger = gp.Trigger(time_pre=0.2, time_post=0.7, target=1)
62    average = gp.TriggerScope(amplitude_limit=5)
63    p.connect(notch60, trigger)
64    p.connect(presenter, trigger[gp.Trigger.PORT_TRIGGER])
65    p.connect(trigger, average)
Three widgets

The presenter, the scope and the average, in one window.

67    app.add_widget(presenter)
68    app.add_widget(scope)
69    app.add_widget(average)
70
71    p.start()
72    app.run()
73    p.stop()
74    p.close()
Result

Run the script and press Start. With the generator there is no evoked response in the signal, so the average only shrinks towards zero as the tones accumulate, which Episode 6 measures. With an amplifier and a person, the response emerges from the average as the run goes on; let it finish.

S5E3 example screenshot

Figure 37: Two minutes into a run on a person wearing a BCI Core-8: the signal, and the average around the tones so far.

Summary

In this episode, you learned how to:

  • read a paradigm’s schedule, and what it sends when

  • build the scaffold: filters for display, raw signal for the file, codes beside both

  • average the EEG around every tone with a Trigger and a TriggerScope

Proceed to Episode 4, where a second tone, rare among the frequent ones, needs two triggers and a difference wave.

Nodes in this episode: Generator, Bandpass, Bandstop, Keyboard, Router, TimeSeriesScope, CsvWriter, Trigger, TriggerScope, ParadigmPresenter

File s5e3_auditory_evoked_potentials.py – View file on GitHub

 1"""Season 5, Episode 3: one tone a hundred times, and its average response.
 2
 3Windows only: it needs Paradigm Presenter, see Episode 1. The generator
 4stands in for an amplifier; to record a person, uncomment one amplifier
 5line and comment out the generator.
 6"""
 7
 8import os
 9
10import gpype as gp
11
12PARADIGM = os.path.join(os.path.dirname(os.path.abspath(__file__)),
13                        "paradigms", "AEPSingleStim.xml")
14CHANNELS = 8
15
16if __name__ == "__main__":
17
18    app = gp.MainApp()
19    p = gp.Pipeline()
20
21    # amp = gp.BCICore(channel_count=CHANNELS)
22    # amp = gp.GNautilus(sampling_rate=250, channel_count=CHANNELS)
23    amp = gp.Generator(sampling_rate=250, channel_count=CHANNELS,
24                       signal_frequency=10, signal_amplitude=15,
25                       noise_amplitude=10)
26
27    # Filtered for display and epoching: 1 to 30 Hz, both mains notches
28    bandpass = gp.Bandpass(f_lo=1, f_hi=30)
29    notch50 = gp.Bandstop(f_lo=48, f_hi=52)
30    notch60 = gp.Bandstop(f_lo=58, f_hi=62)
31    p.connect(amp, bandpass)
32    p.connect(bandpass, notch50)
33    p.connect(notch50, notch60)
34
35    # The presenter receives the codes; the M key (77) marks by hand
36    presenter = gp.ParadigmPresenter(PARADIGM)
37    keyboard = gp.Keyboard()
38
39    # Filtered signal, codes and key presses side by side on one scope
40    mk = gp.TimeSeriesScope.Markers
41    markers = [mk(color="r", label="stim", channel=CHANNELS, value=1),
42               mk(color="c", label="M key", channel=CHANNELS + 1, value=77)]
43    scope = gp.TimeSeriesScope(amplitude_limit=50, time_window=10,
44                               markers=markers,
45                               hidden_channels=[CHANNELS, CHANNELS + 1])
46    shown = gp.Router(input_channels=[gp.Router.ALL] * 3)
47    p.connect(notch60, shown["in1"])
48    p.connect(presenter, shown["in2"])
49    p.connect(keyboard, shown["in3"])
50    p.connect(shown, scope)
51
52    # The recording keeps the unfiltered signal, with the same two beside
53    # it, in a file named after the paradigm
54    kept = gp.Router(input_channels=[gp.Router.ALL] * 3)
55    p.connect(amp, kept["in1"])
56    p.connect(presenter, kept["in2"])
57    p.connect(keyboard, kept["in3"])
58    p.connect(kept, gp.CsvWriter(file_name="AEPSingleStim.csv"))
59
60    # An epoch around every code 1, averaged as they arrive
61    trigger = gp.Trigger(time_pre=0.2, time_post=0.7, target=1)
62    average = gp.TriggerScope(amplitude_limit=5)
63    p.connect(notch60, trigger)
64    p.connect(presenter, trigger[gp.Trigger.PORT_TRIGGER])
65    p.connect(trigger, average)
66
67    app.add_widget(presenter)
68    app.add_widget(scope)
69    app.add_widget(average)
70
71    p.start()
72    app.run()
73    p.stop()
74    p.close()