0001 function trl_raw = bml_definetrial(cfg,raw)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 if istable(cfg)
0030 cfg = struct('annot',cfg);
0031 end
0032 annot = bml_getopt(cfg,'annot');
0033 fixsamples = bml_getopt(cfg,'fixsamples','auto');
0034 trial_t0 = bml_getopt(cfg,'trial_t0',false);
0035 equalize_times = bml_getopt(cfg,'equalize_times');
0036 timetol = bml_getopt(cfg,'timetol',1e-6);
0037
0038 assert(~isempty(annot),'annot required');
0039 assert(~isempty(raw),'raw required');
0040 assert(isstruct(raw),'ivalid raw');
0041 assert(all(ismember({'label','time','trial'},fields(raw))),'ivalid raw');
0042 assert(numel(raw.trial)==1,'raw should have single trial (continuous)');
0043
0044 if islogical(trial_t0)
0045 if trial_t0
0046 trial_t0 = annot.starts;
0047 else
0048 trial_t0 = zeros(height(annot),1);
0049 end
0050 elseif ischar(trial_t0) || isstring(trial_t0) || iscellstr(trial_t0)
0051 trial_t0 = cellstr(trial_t0);
0052 if ismember(trial_t0,annot.Properties.VariableNames)
0053 trial_t0 = annot.(trial_t0{1});
0054 else
0055 error('specified trial_t0 doesn''t coincide with annot''s variable names');
0056 end
0057 elseif istable(trial_t0)
0058 if width(trial_t0)==1 && height(trial_t0)==height(annot)
0059 trial_t0 = trial_t0{:,1};
0060 else
0061 error('invalid trial_t0');
0062 end
0063 elseif isnumeric(trial_t0)
0064 if size(trial_t0,1) < size(trial_t0,2)
0065 trial_t0 = trial_t0';
0066 end
0067 assert(size(trial_t0,1) == height(annot),'invalid trial_t0');
0068 else
0069 error('invalid trial_t0');
0070 end
0071
0072 coord = bml_raw2coord(raw);
0073 trl = [bml_time2idx(coord,annot.starts), bml_time2idx(coord,annot.ends)];
0074
0075 if isnumeric(fixsamples)
0076 trl(:,2) = trl(:,1) + fixsamples;
0077 elseif islogical(fixsamples) && ~fixsamples
0078
0079 elseif strcmp(fixsamples,'auto')
0080 if length(unique(annot.duration))==1
0081 trl(:,2) = trl(:,1) + min(trl(:,2)-trl(:,1));
0082 end
0083 end
0084
0085 trl_raw=raw;
0086 trl_raw.trial = cell(1,size(trl,1));
0087 trl_raw.time = cell(1,size(trl,1));
0088 trl_raw.sampleinfo = trl;
0089 for i=1:size(trl,1)
0090 trl_raw.trial{i}=raw.trial{1}(:,trl(i,1):trl(i,2));
0091 trl_raw.time{i}=raw.time{1}(:,trl(i,1):trl(i,2)) - trial_t0(i);
0092 end
0093
0094 if isempty(equalize_times)
0095 equalize_times = ~all(trial_t0==0) && length(unique(trl(i,2)-trl(i,1)))==1;
0096 end
0097 if equalize_times
0098 trial_len = unique(trl(i,2)-trl(i,1)+1);
0099 assert(~all(trial_t0==0) && length(trial_len)==1,...
0100 'can''t equalize times');
0101 period = cellfun(@(x) mean(diff(x)),trl_raw.time);
0102 period = uniquetol(period,timetol);
0103 assert(length(period)==1,'inconsistent sampling frequencies');
0104 t0 = mean(cellfun(@(x)x(1),trl_raw.time));
0105 t0 = round(t0,max([-ceil(log10(timetol)),0]));
0106 time_eq = t0 + (0:(trial_len-1))*period;
0107 for i=1:numel(trl_raw.time)
0108 trl_raw.time{i} = time_eq;
0109 end
0110 end
0111
0112 trl_raw = ft_checkdata(trl_raw);
0113
0114
0115
0116