0001 function [meg_data, ch_info] = vb_megfile_make_trial_data(meg_file, proc_spec)
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
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 if ~exist('meg_file', 'var'), meg_file = ''; end
0047 if ~exist('proc_spec', 'var'), proc_spec = []; end
0048 [meg_file, proc_spec] = inner_check_arguments(meg_file, proc_spec);
0049
0050
0051
0052 meg_data = [];
0053 ch_info = [];
0054
0055 fs0 = proc_spec.sample_freq;
0056
0057 meg_info = vb_load_measurement_info(meg_file);
0058 fs = vb_info_get_sampling_frequency(meg_info);
0059
0060
0061
0062
0063 frate = (fs/fs0);
0064
0065
0066 trigger = round( proc_spec.trigger * frate );
0067
0068 cond_id = proc_spec.cond_id;
0069
0070
0071 pretrigger = ceil(proc_spec.pretrigger_ms * (fs/1000));
0072 posttrigger = ceil(proc_spec.posttrigger_ms * (fs/1000));
0073
0074 std_sample_list = (-pretrigger:posttrigger)';
0075 n_sample = length(std_sample_list);
0076 n_trial = length(trigger);
0077
0078 trig_info.Trigger = trigger;
0079 trig_info.Pretrigger = pretrigger;
0080 trig_info.Posttrigger = posttrigger;
0081 [tied_sample] = vb_loadspec_make_trial_sample(trig_info);
0082
0083
0084 ix_trial = zeros(n_sample, n_trial);
0085 for i_trial = 1:n_trial
0086 ix_trial(:,i_trial) = tied_sample(i_trial,1):tied_sample(i_trial,2);
0087 end
0088
0089
0090
0091
0092
0093
0094 if isempty(proc_spec.new_file)
0095 new_bin_dir = '';
0096 save_externally = false;
0097 else
0098
0099 if ~isfield(proc_spec, 'new_bin_dir') || isempty(proc_spec.new_bin_dir)
0100 new_bin_dir = '';
0101 save_externally = false;
0102
0103 else
0104 save_externally = true;
0105
0106 new_bin_dir = fullfile(proc_spec.root_dir, proc_spec.new_bin_dir);
0107 if exist(new_bin_dir, 'dir') ~= 7
0108 vb_mkdir(new_bin_dir);
0109 end
0110 precision = vb_meginfo_get_precision(meg_info);
0111 end
0112 end
0113
0114 if save_externally
0115
0116 ch_labels = vb_megfile_get_channel_label_whole(meg_file);
0117 ch_len = size(ch_labels, 1);
0118
0119 fprintf('\n...now on chopping data ...\n');
0120 LINE_LEN = 30;
0121
0122 for i_ch = 1:ch_len
0123 load_spec.ChannelName = {ch_labels{i_ch}};
0124 load_spec.ChannelSwitch = true;
0125 load_spec.ChannelType = 'ALL';
0126 load_spec.ActiveChannel = 0;
0127 load_spec.Trigger = trigger;
0128 load_spec.Pretrigger = pretrigger;
0129 load_spec.Posttrigger = posttrigger;
0130 chopped_data = vb_load_meg_data(meg_file, load_spec);
0131
0132
0133 new_file = sprintf('%s/%s.ch.meg.dat', new_bin_dir, ch_labels{i_ch});
0134 fid = fopen(new_file, 'wb');
0135 if fid == -1
0136 warning('(%s) cannot open file : %s\n', mfilename, new_file);
0137 continue;
0138 end
0139 fwrite(fid, chopped_data, precision);
0140 fclose(fid);
0141
0142 fprintf('.');
0143 if ~rem(i_ch, LINE_LEN), fprintf('\n'); end
0144 end
0145
0146
0147 bexp = [];
0148 bexp_ext = [];
0149 refmg = [];
0150
0151 else
0152
0153
0154
0155
0156 ch_labels = vb_megfile_get_channel_label_meg(meg_file);
0157 ch_id = vb_meginfo_get_channel_index_meg(meg_info);
0158 ch_len = size(ch_labels, 1);
0159
0160 fprintf('\n...now on processing MEG data ...\n');
0161
0162 bexp = zeros(ch_len, n_sample, n_trial);
0163
0164 for i_ch = 1:ch_len
0165 load_spec.ChannelName = {ch_labels{i_ch}};
0166 load_spec.ChannelSwitch = true;
0167 load_spec.ChannelType = 'MEG';
0168 load_spec.ActiveChannel = 0;
0169
0170 load_spec.Trigger = trigger;
0171 load_spec.Pretrigger = pretrigger;
0172 load_spec.Posttrigger = posttrigger;
0173 bexp(i_ch,:,:) = vb_load_meg_data(meg_file, load_spec);
0174
0175 fprintf('.');
0176 end
0177
0178
0179
0180 ch_labels = vb_megfile_get_channel_label_extra(meg_file);
0181 ch_id = vb_meginfo_get_channel_index_extra(meg_info);
0182 ch_len = size(ch_labels, 1);
0183
0184 fprintf('\n...now on processing EXT data ...\n');
0185
0186 bexp_ext = zeros(ch_len, n_sample, n_trial);
0187
0188 for i_ch = 1:ch_len
0189 load_spec.ChannelName = {ch_labels{i_ch}};
0190 load_spec.ChannelSwitch = true;
0191 load_spec.ChannelType = 'EXT';
0192 load_spec.ActiveChannel = 0;
0193 load_spec.Trigger = trigger;
0194 load_spec.Pretrigger = pretrigger;
0195 load_spec.Posttrigger = posttrigger;
0196 bexp_ext(i_ch,:,:) = vb_load_meg_data(meg_file, load_spec);
0197
0198 fprintf('.');
0199 end
0200
0201
0202
0203 ch_labels = vb_megfile_get_channel_label_refmg(meg_file);
0204 ch_id = vb_meginfo_get_channel_index_refmg(meg_info);
0205 ch_len = size(ch_labels, 1);
0206
0207 fprintf('\n...now on processing REF data ...\n');
0208
0209 refmg = zeros(ch_len, n_sample, n_trial);
0210
0211 for i_ch = 1:ch_len
0212 load_spec.ChannelName = {ch_labels{i_ch}};
0213 load_spec.ChannelSwitch = true;
0214 load_spec.ChannelType = 'REF';
0215 load_spec.ActiveChannel = 0;
0216 load_spec.Trigger = trigger;
0217 load_spec.Pretrigger = pretrigger;
0218 load_spec.Posttrigger = posttrigger;
0219 refmg(i_ch,:,:) = vb_load_meg_data(meg_file, load_spec);
0220
0221 fprintf('.');
0222 end
0223 end
0224 fprintf('\n');
0225
0226
0227
0228
0229
0230 meg = load(meg_file);
0231 new_meginfo = meg.MEGinfo;
0232 meg.ParentInfo = meg.MEGinfo;
0233
0234 if ~isempty(proc_spec.new_file)
0235
0236 if save_externally
0237 new_meginfo.saveman.data_dir = proc_spec.new_bin_dir;
0238 else
0239 new_meginfo.saveman = [];
0240 end
0241
0242 for i_trial = 1:n_trial
0243 new_meginfo.Trial(i_trial).number = i_trial;
0244 new_meginfo.Trial(i_trial).sample = ix_trial(:,i_trial);
0245
0246 new_meginfo.Trial(i_trial).Active = 1;
0247
0248
0249
0250
0251
0252 end
0253 new_meginfo.Trial = new_meginfo.Trial';
0254
0255 new_meginfo.ActiveTrial = ones(n_trial,1);
0256 new_meginfo = vb_meginfo_set_acqtype(new_meginfo, 'Evoked_Raw');
0257
0258 new_meginfo.Nsample = n_sample;
0259 new_meginfo.Nrepeat = n_trial;
0260 new_meginfo.Pretrigger = pretrigger;
0261 new_meginfo.cond_id = cond_id;
0262
0263 meg.MEGinfo = new_meginfo;
0264
0265 meg.bexp = bexp;
0266 meg.bexp_ext = bexp_ext;
0267 meg.refmg = refmg;
0268
0269 fprintf('\n');
0270
0271 save_file = fullfile(proc_spec.root_dir, proc_spec.new_file);
0272 vb_save_struct(save_file, meg);
0273 end
0274 return;
0275
0276
0277
0278
0279
0280
0281
0282 function [meg_file, proc_spec] = inner_check_arguments(meg_file, proc_spec)
0283 func_ = mfilename;
0284
0285
0286 if isempty(meg_file)
0287 error('(%s) meg_file is a required parameter', func_);
0288 end
0289
0290 if exist(meg_file, 'file') ~= 2
0291 error('(%s) cannot find meg_file : %s', func_, meg_file);
0292 end
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303 if isempty(proc_spec)
0304 error('(%s) proc_spec is a required parameter', func_);
0305 end
0306
0307
0308
0309
0310 if ~isfield(proc_spec, 'trigger') || isempty(proc_spec.trigger)
0311 error('(%s) trigger is a required field of proc_spec', func_);
0312 end
0313
0314
0315 if ~isfield(proc_spec, 'pretrigger_ms') || isempty(proc_spec.pretrigger_ms)
0316 error('(%s) pretrigger_ms is a required field of proc_spec', func_);
0317 end
0318
0319
0320 if ~isfield(proc_spec, 'posttrigger_ms') || isempty(proc_spec.posttrigger_ms)
0321 error('(%s) posttrigger_ms is a required field of proc_spec', func_);
0322 end
0323
0324
0325 if ~isfield(proc_spec, 'sample_freq') || isempty(proc_spec.sample_freq)
0326 error('(%s) sample_freq is a required field of proc_spec', func_);
0327 end
0328
0329
0330
0331
0332
0333 [fpath] = vb_get_file_parts(meg_file);
0334
0335
0336 if ~isfield(proc_spec, 'root_dir') || isempty(proc_spec.root_dir)
0337 proc_spec.root_dir = fpath;
0338 end
0339
0340
0341 if ~isfield(proc_spec, 'new_file')
0342 proc_spec.new_file = '';
0343 end
0344
0345
0346 if ~isfield(proc_spec, 'new_bin_dir')
0347 proc_spec.new_bin_dir = '';
0348 end
0349 return;
0350
0351
0352
0353
0354
0355