Creating function for baseline correction

Hi!
I am trying to do a baseline correction of my epoched EEG data (64(channels)x250(sampling rate) x 586(trials) matrix). The baseline correction should compute the mean across the columns (so the mean for every row) between some startT and endT (should be specified in ms) before stimulus presentation (which is always at 0, or from columns nr 51 in the matrix). I have another vector containing the sampling times (times= [-200:4:796]). I need to somehow create a function that takes startT and endT as inputs, and computes the the mean value for each of the rows from startT(ms) to endT(ms), and then subtracts this value from each corresponding element in the matrix.
for i = 1:size(epochs_CG)
baselineCG = mean(epochs_CG(:,1:50,:), 2)
end
epochs_CG = epochsCG - baselineCG;
So this works when I run it, but I can't figure out how to write it as a function. I'm sorry, I'm new to matlab, would appreciate any help!

 Accepted Answer

If you want to understand how to write MATLAB functions, see: Function Basics (link).

4 Comments

Hiya, thanks for the link!
I'll try to be more specific; So I have this function;
function epochs = baselineCorr(epochs, startT, endT)
for i = 1:length(epochs)
baseline= mean(epochs(:,startT:endT,:),2);
epochs = epochs-baseline;
end
This works fine as long as i use column numbers as startT and endT (for example 1:50 for a baseline from -200ms before stimulus presentation). What I can't figure out is how to make it so that the input is specified in ms, not columns. I'm thinking I can use the sTimes variable somehow, which contains the sampling times. Thanks again!
My pleasure.
You would have to include the sampling frequency (that I believe is 250 Hz in your example) to the function arguments, as well as your desired starting time offset. The calculation then goes something like this:
Fs = 250; % Sampling Frequency (Hz)
Ts = 1/Fs; % Sampling Interval (s)
startT = -200; % Start Time (ms)
startIdx = startT / (Ts * 1E+3); % Start Index
producing:
startIdx =
-50
so you would add the ‘startIdx’ value to whatever you are now using to indicate the start times of your EEG data.
You might want to include a check to be certain that the offset is an integer, and that it always includes your desired starting time:
startIdx = floor(startT / (Ts * 1E+3)); % Start Index
The floor function rounds to integers, and towards -Inf.
Thank you so much!
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!