137 lines
3.8 KiB
Matlab
137 lines
3.8 KiB
Matlab
function[pre_mat post_mat rel_time trial_ind] = getTimeLockedTrialMatrix(objs, varargin)
|
|
%% getTimeLockedTrialMatrix returns each trial trace shifted for a marker
|
|
% or event timestamp
|
|
%
|
|
% Usage
|
|
% [mat trial_ind] = PDSession.getTimeLockedTrialMatrix(event_code [,selection, eye, signal_type, corrected])
|
|
%
|
|
% - mat
|
|
% Matrix of n x l, n = number of trials, l = maximum length of all traces
|
|
% - trial_ind
|
|
% Vector containing the selected trials after quality
|
|
% thresholding
|
|
% - event_code
|
|
% Event code (or Label string that is matched.)
|
|
% - selection
|
|
% Vector of selected / requested trials%
|
|
% - eye
|
|
% 'left', 'right', or 'both' (default = 'left')
|
|
% - signal_type
|
|
% 'raw', 'interpolated', 'filtered' (default = 'filtered');
|
|
% - corrected
|
|
% 0 /1, use baseline corrected signals (default : 1)
|
|
%
|
|
|
|
% Examples:
|
|
%
|
|
% *) with one parameter : session.getTrialMatrix('stimulus start');
|
|
% *) with two parameters : session.getTrialMatrix('all', 'left');
|
|
% *) with three parameters : session.getTrialMatrix('all', 'left', 'interpolated');
|
|
% *) with four parameters : session.getTrialMatrix(1:10, 'right', 'interpolated', 1);
|
|
%
|
|
% Usage :
|
|
% function[mat trial_ind] = getTimeLockedTrialMatrix(obj, varargin)
|
|
|
|
%% determine input (this can be optimized...)
|
|
if nargin == 1
|
|
error('No event code provided')
|
|
end
|
|
|
|
if nargin == 2
|
|
event_code = varargin{1};
|
|
selection = [];
|
|
eye = 'left';
|
|
signal_type = 'filtered';
|
|
corrected = 1;
|
|
end
|
|
|
|
if nargin == 3
|
|
event_code = varargin{1};
|
|
selection = varargin{2};
|
|
eye = 'left';
|
|
signal_type = 'filtered';
|
|
corrected = 1;
|
|
end
|
|
|
|
if nargin == 4
|
|
event_code = varargin{1};
|
|
selection = varargin{2};
|
|
eye = varargin{3};
|
|
signal_type = 'filtered';
|
|
corrected = 1;
|
|
|
|
end
|
|
|
|
if nargin == 5
|
|
event_code = varargin{1};
|
|
selection = varargin{2};
|
|
eye = varargin{3};
|
|
signal_type = varargin{4};
|
|
corrected = 1;
|
|
end
|
|
|
|
if nargin == 6
|
|
event_code = varargin{1};
|
|
selection = varargin{2};
|
|
eye = varargin{3};
|
|
signal_type = varargin{4};
|
|
corrected = varargin{5};
|
|
end
|
|
|
|
if corrected == 1
|
|
data_field = 'baseline_corrected';
|
|
else
|
|
data_field = 'raw';
|
|
end
|
|
|
|
%%
|
|
for s = 1:length(objs)
|
|
obj = objs(s);
|
|
|
|
if isempty(selection)
|
|
selection = 1:length(obj.trials);
|
|
end
|
|
|
|
%% incorporate trial quality
|
|
q_objs = [obj.trials(selection).quality];
|
|
q_vals = cat(1, q_objs.(eye));
|
|
valid_trials = q_vals >= obj.settings.QualityThreshold;
|
|
trial_ind = find(valid_trials);
|
|
|
|
trials = [obj.trials(valid_trials).data];
|
|
t_objs = [trials.(eye)]; data_objs = [t_objs.(data_field)];
|
|
max_trial_length = 0;
|
|
|
|
indices = obj.trials.getIndexForEvent(event_code);
|
|
indices = indices(trial_ind);
|
|
|
|
%% determine the maximum length of pre and post trial
|
|
for i = 1:length(data_objs)
|
|
if max_trial_length < length(data_objs(i).(signal_type)(1:indices(i)));
|
|
max_trial_length_pre = length(data_objs(i).(signal_type)(1:indices(i)-1));
|
|
max_trial_length_post = length(data_objs(i).(signal_type)(indices(i):end));
|
|
end
|
|
end
|
|
|
|
%% predefine matrices (selected trials x max length)
|
|
pre_mat = nan(length(trial_ind), max_trial_length_pre);
|
|
post_mat = nan(length(trial_ind), max_trial_length_post);
|
|
|
|
%% cut the trials in pre and post marker
|
|
for t = 1:length(trials)
|
|
|
|
pre_trace = trials(t).(eye).(data_field).(signal_type)(1:indices(i)-1);
|
|
pre_mat(t,end-length(pre_trace)+1:end) = pre_trace;
|
|
|
|
post_trace = trials(t).(eye).(data_field).(signal_type)(indices(i):end);
|
|
post_mat(t,1:length(post_trace)) = post_trace;
|
|
|
|
end
|
|
|
|
rel_time = repmat(-length(pre_mat):length(post_mat)-1,length(trials),1);
|
|
end
|
|
|
|
|
|
|
|
|