ET_PDToolkit/PDToolkit/@PDTrial/getBaseline.m

68 lines
1.9 KiB
Mathematica
Raw Normal View History

2018-06-12 14:49:55 +02:00
function[baseline] = getBaseline(trial, settings)
%% Based on baseline interval samples, this routine
% determines a baseline value that can be subsequently
% used to correct the raw signal
%
% In this routine, currently 4 methods are available:
% 1) no correction
% 2) mean value across baseline samples
% 3) minimum value of all baseline samples
% 4) mean value for the lowest n percent of baseline samples
% 5) offset value for a fitted line of all baseline samples
baseline=NaN;
if isempty(trial.baseline_onset)
trial.baseline_onset = trial.trial_start;
end
if isempty(trial.baseline_offset)
if isempty(trial.stimulus_offset)
warning('Stimulus onset has not been set! Bailing out...');
return;
end
trial.baseline_offset = trial.stimulus_onset;
end
eyes = fieldnames(trial.data);
for e = 1:length(eyes)
eye = eyes{e};
signal = trial.data.(eye).uncorrected.interpolated;
if isempty(signal)
baseline_signal = NaN;
else
baseline_signal = trial.data.(eye).uncorrected.interpolated(intersect(find(trial.time >= trial.baseline_onset),...
find(trial.time < trial.baseline_offset)));
end
end
switch settings.BaselineCorrection
case 1
baseline = NaN; % No correction
case 2 % Mean value
baseline = nanmean(baseline_signal);
case 3 % Min-value between baseline
baseline = min(baseline_signal);
case 4 % Percentile
percentage = settings.BaselineCorrectionPercentile;
perc_val = prctile(baseline_signal, percentage);
baseline = perc_val; %mean(baseline_signal(find(baseline_signal <= perc_val)));
case 5 % Offset (fitted line)
P = polyfit(1:length(baseline_signal), baseline_signal, 0);
baseline = P(1);
case 6 % Fixed point
baseline = baseline_signam
end
end