derive a set of trial time ([from to]) from trial_spec [usage] [trial_times, pretrigger, posttrigger] = vb_read_trial_spec( ... info, trial_spec) [input] info : <required> <<struct>> (EEGinfo or MEGinfo) : information of target measurement_data trial_spec : <required> <<struct>> : specification of this trial. The details are below. [output] trial_times : array of combinations of trigger_time, time_from and time_to : [Ntrial x 3] pretrigger : number of sample before trigger posttrigger : number of sample after trigger [note] <struct> trial_spec trigger_list : sample number of triggers pre_trigger : time before trigger point [ms] - (A) post_trigger : time after trigger point [ms] - (B) segmented_ti : segmented time interval [ms] - (C) segment_rule : priority of segment rule (1 or 2 or 3) : Necessary parameters are two to segment. : If three parameters above are given, : this rule would be applied. : +-----+-----+-----+-----+ : | | (A) | (B) | (C) | : +-----+-----+-----+-----+ : | (A) | --- | (1) | (2) | : +-----+-----+-----+-----+ : | (B) | --- | --- | (3) | : +-----+-----+-----+-----+ : | (C) | --- | --- | --- | : +-----+-----+-----+-----+ [history] 2006-12-21 (Sako) initial version Copyright (C) 2011, ATR All Rights Reserved. License : New BSD License(see VBMEG_LICENSE.txt)
0001 function [trial_times, pretrigger, posttrigger] = vb_read_trial_spec( ... 0002 info, trial_spec) 0003 % derive a set of trial time ([from to]) from trial_spec 0004 % [usage] 0005 % [trial_times, pretrigger, posttrigger] = vb_read_trial_spec( ... 0006 % info, trial_spec) 0007 % [input] 0008 % info : <required> <<struct>> (EEGinfo or MEGinfo) 0009 % : information of target measurement_data 0010 % trial_spec : <required> <<struct>> 0011 % : specification of this trial. The details are below. 0012 % [output] 0013 % trial_times : array of combinations of trigger_time, time_from and time_to 0014 % : [Ntrial x 3] 0015 % pretrigger : number of sample before trigger 0016 % posttrigger : number of sample after trigger 0017 % [note] 0018 % <struct> trial_spec 0019 % trigger_list : sample number of triggers 0020 % pre_trigger : time before trigger point [ms] - (A) 0021 % post_trigger : time after trigger point [ms] - (B) 0022 % segmented_ti : segmented time interval [ms] - (C) 0023 % segment_rule : priority of segment rule (1 or 2 or 3) 0024 % : Necessary parameters are two to segment. 0025 % : If three parameters above are given, 0026 % : this rule would be applied. 0027 % : +-----+-----+-----+-----+ 0028 % : | | (A) | (B) | (C) | 0029 % : +-----+-----+-----+-----+ 0030 % : | (A) | --- | (1) | (2) | 0031 % : +-----+-----+-----+-----+ 0032 % : | (B) | --- | --- | (3) | 0033 % : +-----+-----+-----+-----+ 0034 % : | (C) | --- | --- | --- | 0035 % : +-----+-----+-----+-----+ 0036 % [history] 0037 % 2006-12-21 (Sako) initial version 0038 % 0039 % Copyright (C) 2011, ATR All Rights Reserved. 0040 % License : New BSD License(see VBMEG_LICENSE.txt) 0041 0042 % --- CHECK ARGUMENTS --- % 0043 if ~exist('info', 'var') info = []; end; 0044 if ~exist('trial_spec', 'var') trial_spec = []; end; 0045 0046 [info,trial_spec] = inner_check_arguments(info,trial_spec); 0047 0048 % --- MAIN PROCEDURE --------------------------------------------------------- % 0049 % 0050 % if trial_spec is essentially empty, return empty array 0051 if inner_trial_spec_is_empty(trial_spec) 0052 trial_times = []; 0053 pretrigger = 0; 0054 posttrigger = 0; 0055 return; 0056 end 0057 0058 % solve segment rule 0059 if ~isfield(trial_spec, 'segment_rule') 0060 trial_spec.segment_rule = 1; 0061 end 0062 0063 state = inner_trial_spec_get_parameter_state(trial_spec); 0064 0065 sampling_frequency = vb_info_get_sampling_frequency(info); 0066 0067 [trial_times, pretrigger, posttrigger] = inner_calc_timeset_by_state( ... 0068 state, trial_spec, sampling_frequency); 0069 % 0070 % --- END OF MAIN PROCEDURE -------------------------------------------------- % 0071 0072 0073 % --- INNER FUNCTIONS -------------------------------------------------------- % 0074 % 0075 % --- inner_trial_spec_is_empty() 0076 % inner function to judge whether trial_spec would be empty or not 0077 % [usage] 0078 % result = inner_trial_spec_is_empty(trial_spec) 0079 % [input] 0080 % trial_spec : trial_spec struct 0081 % [output] 0082 % result : true or false 0083 % [note] 0084 % If a structure of trial_spec change, this function must be modified. 0085 % [history] 0086 % 2006-12-15 (Sako) initial version 0087 function result = inner_trial_spec_is_empty(trial_spec) 0088 0089 result = true; 0090 if ~exist('trial_spec', 'var') ... 0091 || isempty(trial_spec) 0092 return; 0093 0094 elseif ~isfield(trial_spec, 'trigger_list') ... 0095 || isempty(trial_spec.trigger_list) 0096 return; 0097 0098 elseif (~isfield(trial_spec, 'pre_trigger') ... 0099 || isempty(trial_spec.pre_trigger)) ... 0100 && (~isfield(trial_spec, 'post_trigger') ... 0101 || isempty(trial_spec.post_trigger)) 0102 return; 0103 0104 elseif (~isfield(trial_spec, 'post_trigger') ... 0105 || isempty(trial_spec.post_trigger)) ... 0106 && (~isfield(trial_spec, 'segmented_ti') ... 0107 || isempty(trial_spec.segmented_ti)) 0108 return; 0109 0110 elseif (~isfield(trial_spec, 'segmented_ti') ... 0111 || isempty(trial_spec.segmented_ti)) ... 0112 && (~isfield(trial_spec, 'pre_trigger') ... 0113 || isempty(trial_spec.pre_trigger)) 0114 return; 0115 0116 else 0117 result = false; 0118 return; 0119 end 0120 % 0121 % --- end of inner_trial_spec_is_empty() 0122 0123 % --- inner_trial_spec_get_parameter_state() 0124 % inner function to check parameter state 0125 % [usage] 0126 % state = inner_trial_spec_get_parameter_state(trial_spec) 0127 % [input] 0128 % trial_spec : trial_spec struct 0129 % [output] 0130 % state : sum of state as follows 0131 % : 1) pre_trigger 0132 % : 2) post_trigger 0133 % : 4) segmented_ti 0134 % [note] 0135 % Prior condition is that trial_spec is not 'EMPTY'. 0136 % [history] 0137 % 2006-12-15 (Sako) initial version 0138 function state = inner_trial_spec_get_parameter_state(trial_spec) 0139 state = 0; 0140 if isfield(trial_spec, 'pre_trigger') && ~isempty(trial_spec.pre_trigger) 0141 state = state + 1; 0142 end; 0143 0144 if isfield(trial_spec, 'post_trigger') && ~isempty(trial_spec.post_trigger) 0145 state = state + 2; 0146 end; 0147 0148 if isfield(trial_spec, 'segmented_ti') && ~isempty(trial_spec.segmented_ti) 0149 state = state + 4; 0150 end; 0151 % 0152 % --- end of inner_trial_spec_get_parameter_state() 0153 0154 0155 % --- inner_calc_timeset_by_state() 0156 % inner function to calculate trial time set by state 0157 % [usage] 0158 % [input] 0159 % [output] 0160 % [note] 0161 % [history] 0162 % 2006-12-15 (Sako) initial version 0163 function [trial_times, pre, post] = inner_calc_timeset_by_state( ... 0164 state, trial_spec, s_freq) 0165 0166 trigger_list = trial_spec.trigger_list; 0167 0168 switch state 0169 case 7 % all paramegers 0170 switch trial_spec.segment_rule 0171 case 1 % pre_trigger & post_trigger 0172 real_state = 3; 0173 case 2 % pre_trigger & segmented_ti 0174 real_state = 5; 0175 case 3 % post_trigger & segmented_ti 0176 real_state = 6; 0177 otherwise 0178 warning('undefined segment_rule : %d\n', trial_spec.segment_rule); 0179 state = 3; 0180 end 0181 [trial_times, pre, post] = inner_calc_timeset_by_state( ... 0182 real_state, trial_spec, s_freq ); 0183 0184 case 6 % post_trigger & segmented_ti 0185 post = trial_spec.post_trigger; 0186 pre = trial_spec.segmented_ti - post; 0187 trial_times = inner_clip_timeset(pre, post, trigger_list, s_freq); 0188 0189 case 5 % segmented_ti & pre_trigger 0190 pre = trial_spec.pre_trigger; 0191 post = trial_spec.segmented_ti - pre; 0192 trial_times = inner_clip_timeset(pre, post, trigger_list, s_freq); 0193 0194 case 3 % pre_trigger & post_trigger 0195 pre = trial_spec.pre_trigger; 0196 post = trial_spec.post_trigger; 0197 trial_times = inner_clip_timeset(pre, post, trigger_list, s_freq); 0198 0199 otherwise 0200 error('unknown state number : %d', state); 0201 end 0202 % 0203 % --- end of inner_calc_timeset_by_state() 0204 0205 0206 % --- inner_clip_timeset() 0207 % inner function to clip trial time set 0208 % [usage] 0209 % trial_times = inner_clip_timeset(pre, post, trigger_list, EEGinfo) 0210 % [input] 0211 % pre : pre-trigger 0212 % post : post-trigger 0213 % trigger_list : list of trigger samples 0214 % s_freq : sampling frequency 0215 % [output] 0216 % trial_times : array of trial time set (trigger, from&to) [Ntrial x 3] 0217 % [note] 0218 % [history] 0219 % 2006-12-15 (Sako) initial version 0220 function trial_times = inner_clip_timeset(pre, post, trigger_list, s_freq) 0221 trial_times = []; 0222 fprintf('inner_clip_timeset(%d,%d, ,%d)\n', pre, post, s_freq); 0223 0224 [pre_time, sample] = vb_util_sample2msectime2sample(pre, [], s_freq); 0225 [post_time, sample] = vb_util_sample2msectime2sample(post, [], s_freq); 0226 0227 trigger_list = sort(trigger_list); 0228 trigger_num = size(trigger_list,1); 0229 for n_trial = 1:trigger_num 0230 [trigger_time, sample_list] = vb_util_sample2msectime2sample( ... 0231 trigger_list(n_trial), [], s_freq); 0232 from_t = trigger_time - pre_time; 0233 to_t = trigger_time + post_time; 0234 trial_times = [trial_times; [trigger_time, from_t, to_t]]; 0235 end 0236 % 0237 % --- end of inner_clip_timeset() 0238 0239 % --- inner_check_arguments() 0240 % check argument 0241 % 0242 function [info, trial_spec] = inner_check_arguments(info, trial_spec) 0243 if isempty(info) 0244 error('info is a required parameter'); 0245 end 0246 0247 if isempty('trial_spec') 0248 error('trial_spec is a required parameter'); 0249 end 0250 % 0251 % --- end of inner_check_arguments() 0252 % 0253 % --- END OF INNER FUNCTIONS ------------------------------------------------- % 0254 0255 %%% END OF FILE %%%