Skip to content

Instantly share code, notes, and snippets.

@JohnAtl
Last active March 4, 2020 12:59
Show Gist options
  • Save JohnAtl/328be258db7c2ac83d2ce5227bf51303 to your computer and use it in GitHub Desktop.
Save JohnAtl/328be258db7c2ac83d2ce5227bf51303 to your computer and use it in GitHub Desktop.
%
% Insert events based on transitions on a high-level input channel.
%
% As with all code from the internet, verify that it works for your unique situation.
%
function EEG = hl1_to_events(EEG)
% Find the index of the high-level (trigger) channel.
hl1_index = eeg_chaninds(EEG, 'HL1');
hl1 = EEG.data(hl1_index,:);
% My high-level channel is in microvolts. Divide by 1M to make it volts, and easier to work with.
hl1 = hl1 / 1000000;
% Values above 2.5v are considered 'high'.
thresholded = hl1 > 2.5;
% Take the derivative to find transitions in the signal.
% High to low transitions will be -1, low to high will be +1.
d_th = diff(thresholded);
% Find the indexes of the transitions, and converts to seconds
% based on sampling rate of the EEG signal.
% For high-to-low transitions, use find(d_th < -0.5)
% For low-to-high transitions, use find(d_th > 0.5)
hl1_onset_indexes = find(d_th < -0.5);
hl1_onsets = hl1_onset_indexes / EEG.srate;
% Insert an event for each high-level onset.
% This can be slow if there are a lot of events.
for onset_ndx = 1 : length(hl1_onsets)
latency = hl1_onsets(onset_ndx);
EEG = pop_editeventvals(EEG, 'insert', { 1 'EVENT' latency 0 });
end
% Have EEGLAB check that the events are consistent.
EEG = eeg_checkset(EEG, 'eventconsistency');
EEG = eeg_checkset(EEG, 'checkur');
eeglab redraw;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment