Audio triggers for EEG system

19 views (last 30 days)
Nathan
Nathan on 17 May 2024
Answered: Ayush on 3 Sep 2024
I have 3 scripts that generate 3 different audio files. Each 3 seconds long, with 1 containing white noise in the background and a pulse stimulus, 1 containing white noise in the background and a short pause/gap in the noise, and then the 3rd contains both the gap and the pulse.
I then have a 4th script that combines these 3 audio files and randomly repeats them as a continuous 8 minute sound file.
I want to add triggers to this so that if I play this audio file while connected to an EEG system triggers will appear to identify the timepoints that each of these individual files occur within the full 8 minute file. The triggers will be sent via a parallel port.
Does anyone know the best way to do this?
Can matlab distinguish the changes in db? I was going to time-lock a trigger to each file but there is a jitter in the 8-minute file so I don’t think this would work.
  2 Comments
Taylor
Taylor on 17 May 2024
MATLAB can most likely distinguish changes in the audio signal (depending on how large of a spike in amplitude the pulse is). Does your amplifier have and analog or audio in? It might just be easier to split your audio signal into this channel so it is time-synced with the EEG data by your recording system. Then you can process it in post to determine the exact timestamps of the pulses/gaps.
Nathan
Nathan on 21 May 2024
Hi there,
Just following up on this. I was able to write a script that detects the time there was a change in db levels. I've now saved these time points in a workspace variable (column 1 = time in seconds, column 2 = label of sound stimuli either 1,2 or 3) there are 150 time points. I am now trying to write a script that synchronises playing the audio file and sending the triggers from this variable to an EEG parallel port at the exact same time (i.e. 1.044 seconds in there is stimulus 1 so I want to play the audio and exactly 1.044 seconds later the trigger is sent to the EEG system).
I have a snippet of code that I can use to send triggers but I seem to have really confused myself on how to synch up the audio and variable containing the time points.
Hope this makes some sort of sense.
if par.recordEEG
if par.booth == 1
port = hex2dec('2010');
lptwrite(port,0);
ioObj = 0;
elseif par.booth == 3
addpath('C:\Windows\SysWOW64');
ioObj = io64;
status = io64(ioObj); % Status = 0, is ready to send triggers out
port = hex2dec('3FB8');% windows 10
elseif par.booth == 2
port = BioSemiSerialPort();
ioObj = [];
end
if par.recordEEG==2% just print triggers for now - dummy mode
sendtrigger = @(trg,port,SITE,ioObj) fprintf('TRIGGER: %d\n', trg);
port = 0;
ioObj = 0;
end
end

Sign in to comment.

Answers (1)

Ayush
Ayush on 3 Sep 2024
Hi Nathan,
I understand that you can successfully transmit trigger signals to the EEG system but are encountering challenges with synchronizing these signals with the audio player.
A potential solution is the "two-pointer" algorithm. In this method, one pointer tracks the audio timestamps while the other tracks the stimulus timestamps. This can help ensure that both processes are aligned effectively. This can be achieved by using “tic-toc” functions.
For further clarification, please refer to the code provided below.
% you can replace it with actual data
data = [time_points, stimulus_labels];
[audioData, fs] = audioread('your_audio_file.wav');
audioPlayer = audioplayer(audioData, fs);
play(audioPlayer);
% store the start time of the audio
startTime = tic;
% you can replace it with your actual trigger sending function
sendtrigger = @(trg, port) fprintf('Sending trigger %d to port %d\n', trg, port);
port = BioSemiSerialPort();
ioObj=[]
% if no hardware device is present, alternatively, you can define the port
% and ioObj in a similar way as you did in your code
port = hex2dec('3FB8'); % windows 10
ioObj = io64; %64 bit OS
% Iterate through the time points and stimuli
for i = 1:size(data, 1)
% Get the time point and stimulus
timePoint = data(i, 1);
stimulus = data(i, 2);
% Calculate the delay
delay = timePoint - toc(startTime); % Time to wait until the trigger should be sent
% Wait until it's time to send the trigger
pause(max(0, delay));
% Send the trigger
sendtrigger(stimulus, port);
end
% Ensure that the audio playback is completed
while isplaying(audioPlayer)
% can do additional checks or operations concurrently using similar
% approach as used above
end
stop(audioPlayer);
For further information, you can refer to the following documentation of “tic” and “toc” functions: https://www.mathworks.com/help/matlab/ref/tic.html
Hope it helps!

Categories

Find more on EEG/MEG/ECoG in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!