0001 function [bexp, bexp_ext, refmg] = ...
0002 vb_combine_meg_files(in_files, out_file, ext_spec)
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
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094 if ~exist('in_files', 'var'), in_files = []; end
0095 if ~exist('out_file', 'var'), out_file = []; end
0096 if ~exist('ext_spec', 'var'), ext_spec = []; end
0097 [in_files, out_file, info, measurement, coord_type, saveman, d_path] = ...
0098 inner_check_arguments(in_files, out_file, ext_spec);
0099
0100
0101
0102
0103 pick_sum = [];
0104 Qpick_sum = [];
0105 ref_pick_sum = [];
0106 ref_Qpick_sum = [];
0107
0108 vcenter_sum = [];
0109 vradius_sum = 0;
0110
0111 n_pick = 0;
0112 n_Qpick = 0;
0113 n_ref_pick = 0;
0114 n_ref_Qpick = 0;
0115
0116 n_vcenter = 0;
0117 n_vradius = 0;
0118
0119 n_trial = 0;
0120
0121 n_file = length(in_files);
0122
0123
0124 if ~isempty(saveman)
0125
0126 const = vb_define_yokogawa;
0127 data_ext = const.EXT_CHANNEL_BIN;
0128
0129 prec = vb_saveman_get_precision(saveman);
0130 end
0131
0132 bexp = [];
0133 bexp_ext = [];
0134 refmg = [];
0135
0136
0137 for i_file = 1:n_file
0138 load(in_files{i_file}, 'pick', 'Qpick', 'ref_pick', 'ref_Qpick');
0139
0140
0141 if exist('pick', 'var')
0142 if isempty(pick_sum)
0143 pick_sum = zeros(size(pick,1), size(pick,2));
0144 end
0145 pick_sum = pick_sum + pick;
0146 n_pick = n_pick + 1;
0147 end
0148
0149 if exist('Qpick', 'var')
0150 if isempty(Qpick_sum)
0151 Qpick_sum = zeros(size(Qpick,1), size(Qpick,2));
0152 end
0153 Qpick_sum = Qpick_sum + Qpick;
0154 n_Qpick = n_Qpick + 1;
0155 end
0156
0157 if exist('ref_pick', 'var')
0158 if isempty(ref_pick_sum)
0159 ref_pick_sum = zeros(size(ref_pick,1), size(ref_pick,2));
0160 end
0161 ref_pick_sum = ref_pick_sum + ref_pick;
0162 n_ref_pick = n_ref_pick + 1;
0163 end
0164
0165 if exist('ref_Qpick', 'var')
0166 if isempty(ref_Qpick_sum)
0167 ref_Qpick_sum = zeros(size(ref_Qpick,1), size(ref_Qpick,2));
0168 end
0169 ref_Qpick_sum = ref_Qpick_sum + ref_Qpick;
0170 n_ref_Qpick = n_ref_Qpick + 1;
0171 end
0172
0173 if isfield(info{i_file}, 'Vcenter')
0174 if isempty(vcenter_sum)
0175 vcenter_sum = ...
0176 zeros(size(info{i_file}.Vcenter,1), size(info{i_file}.Vcenter,2));
0177 end
0178 vcenter_sum = vcenter_sum + info{i_file}.Vcenter;
0179 n_vcenter = n_vcenter + 1;
0180 end
0181
0182 if isfield(info{i_file}, 'Vradius')
0183 vradius_sum = vradius_sum + info{i_file}.Vradius;
0184 n_vradius = n_vradius + 1;
0185 end
0186
0187
0188 loadspec.ActiveChannel = 0;
0189 loadspec.ChannelType = 'MEG';
0190 cur_bexp = vb_load_meg_data(in_files{i_file}, loadspec);
0191
0192 loadspec.ChannelType = 'EXTRA';
0193 cur_bexp_ext = vb_load_meg_data(in_files{i_file}, loadspec);
0194
0195 loadspec.ChannelType = 'REFERENCE';
0196 cur_refmg = vb_load_meg_data(in_files{i_file}, loadspec);
0197
0198
0199 if ~isempty(cur_bexp)
0200 trial_begin = n_trial + 1;
0201 trial_end = trial_begin + size(cur_bexp, 3) - 1;
0202 n_trial = trial_end;
0203
0204 elseif ~isempty(cur_bexp_ext)
0205 trial_begin = n_trial + 1;
0206 trial_end = trial_begin + size(cur_bexp_ext, 3) - 1;
0207 n_trial = trial_end;
0208
0209 elseif ~isempty(cur_refmg)
0210 trial_begin = n_trial + 1;
0211 trial_end = trial_begin + size(cur_refmg, 3) - 1;
0212 n_trial = trial_end;
0213 end
0214
0215 if ~isempty(saveman)
0216 meginfo = info{1};
0217 end
0218
0219 if ~isempty(cur_bexp)
0220 if ~isempty(saveman)
0221 meg_labels = vb_meginfo_get_channel_label_meg(meginfo);
0222
0223 n_ch = length(meg_labels);
0224 for i_ch = 1:n_ch
0225 vb_util_make_external_data_file(d_path, meg_labels{i_ch}, ...
0226 data_ext, prec, cur_bexp(i_ch,:,:), 'ab' );
0227 end
0228
0229 else
0230 bexp(:,:, (trial_begin:trial_end)) = cur_bexp;
0231 end
0232 end
0233
0234 if ~isempty(cur_bexp_ext)
0235 if ~isempty(saveman)
0236 ext_labels = vb_meginfo_get_channel_label_extra(meginfo);
0237
0238 n_ch = length(ext_labels);
0239 for i_ch = 1:n_ch
0240 vb_util_make_external_data_file(d_path, ext_labels{i_ch}, ...
0241 data_ext, prec, cur_bexp_ext(i_ch,:,:), 'ab' );
0242 end
0243
0244 else
0245 bexp_ext(:,:, (trial_begin:trial_end)) = cur_bexp_ext;
0246 end
0247 end
0248
0249 if ~isempty(cur_refmg)
0250 if ~isempty(saveman)
0251 ref_labels = vb_meginfo_get_channel_label_refmg(meginfo);
0252
0253 n_ch = length(ref_labels);
0254 for i_ch = 1:n_ch
0255 vb_util_make_external_data_file(d_path, ref_labels{i_ch}, ...
0256 data_ext, prec, cur_refmg(i_ch,:,:), 'ab' );
0257 end
0258
0259 else
0260 refmg(:,:, (trial_begin:trial_end)) = cur_refmg;
0261 end
0262 end
0263 end
0264
0265
0266 if n_pick > 0
0267 pick = pick_sum / n_pick;
0268 end
0269
0270 if n_Qpick > 0
0271 Qpick = Qpick_sum / n_Qpick;
0272 end
0273
0274 if n_ref_pick > 0
0275 ref_pick = ref_pick_sum / n_ref_pick;
0276 end
0277
0278 if n_ref_Qpick > 0
0279 ref_Qpick = ref_Qpick_sum / n_ref_Qpick;
0280 end
0281
0282
0283 if isfield(info{1}, 'Measurement')
0284 MEGinfo.Measurement = info{1}.Measurement;
0285 else
0286 MEGinfo.Measurement = measurement{1};
0287 end
0288 MEGinfo.device = info{1}.device;
0289 MEGinfo.Nchannel = info{1}.Nchannel;
0290 MEGinfo.Nsample = info{1}.Nsample;
0291 MEGinfo.Nrepeat = n_trial;
0292 MEGinfo.SampleFreq = info{1}.SampleFreq;
0293 MEGinfo.Pretrigger = info{1}.Pretrigger;
0294 MEGinfo.MEG_ID = '';
0295 MEGinfo.MRI_ID = info{1}.MRI_ID;
0296
0297 if ~isempty(saveman)
0298 MEGinfo.saveman = saveman;
0299 end
0300
0301 if isfield(info{1}, 'sensor_weight')
0302 MEGinfo.sensor_weight = info{1}.sensor_weight;
0303 end
0304
0305 if isfield(info{1}, 'MEGch_id')
0306 MEGinfo.MEGch_id = info{1}.MEGch_id;
0307 end
0308
0309 if isfield(info{1}, 'MEGch_name')
0310 MEGinfo.MEGch_name = info{1}.MEGch_name;
0311 end
0312
0313 if n_vcenter > 0
0314 MEGinfo.Vcenter = vcenter_sum / n_vcenter;
0315 end
0316
0317 if n_vradius > 0
0318 MEGinfo.Vradius = vradius_sum / n_vradius;
0319 end
0320
0321
0322 MEGinfo.Trial = [];
0323 MEGinfo.ActiveTrial = [];
0324 prev_n_trial = 0;
0325 prev_n_sample = 0;
0326
0327 for i_file = 1:n_file
0328 cur_n_trial = length(info{i_file}.Trial);
0329 cur_n_sample = info{i_file}.Nsample;
0330
0331 for i_tr = 1:cur_n_trial
0332 info{i_file}.Trial(i_tr).sample = ...
0333 info{i_file}.Trial(i_tr).sample + prev_n_sample;
0334 info{i_file}.Trial(i_tr).number = ...
0335 info{i_file}.Trial(i_tr).number + prev_n_trial;
0336 end
0337 MEGinfo.Trial = [MEGinfo.Trial; info{i_file}.Trial];
0338 MEGinfo.ActiveTrial = [MEGinfo.ActiveTrial; info{i_file}.ActiveTrial];
0339 prev_n_trial = prev_n_trial + cur_n_trial;
0340 prev_n_sample = prev_n_sample + cur_n_sample;
0341 end
0342
0343 if isfield(info{1}, 'ChannelInfo')
0344 MEGinfo.ChannelInfo = info{1}.ChannelInfo;
0345 end
0346
0347 if isfield(info{1}, 'ActiveChannel')
0348 MEGinfo.ActiveChannel = info{1}.ActiveChannel;
0349 end
0350
0351 if isfield(info{1}, 'ExtraChannelInfo')
0352 MEGinfo.ExtraChannelInfo = info{1}.ExtraChannelInfo;
0353 end
0354
0355
0356 Measurement = measurement{1};
0357 CoordType = coord_type{1};
0358
0359
0360 target_key = 'PositionFile';
0361 PositionFile = '';
0362
0363 for i_file = 1:n_file
0364 [state, guide_def] = ...
0365 vb_util_check_variable_in_matfile(in_files{i_file}, target_key);
0366 if state == guide_def.VALID
0367 load(in_files{i_file}, target_key);
0368 break;
0369 end
0370 end
0371
0372 if ~isempty(out_file)
0373 if ~isempty(saveman)
0374 if (n_ref_pick > 0) && (n_ref_Qpick > 0)
0375 vb_fsave(out_file, 'Measurement', ...
0376 'pick','Qpick', 'ref_pick', 'ref_Qpick', 'CoordType', 'MEGinfo', ...
0377 'PositionFile');
0378 else
0379 vb_fsave(out_file, 'Measurement', ...
0380 'pick','Qpick', 'CoordType', 'MEGinfo', ...
0381 'PositionFile');
0382 end
0383 else
0384 if (n_ref_pick > 0) && (n_ref_Qpick > 0)
0385 vb_fsave(out_file, 'Measurement', 'bexp', 'bexp_ext', 'refmg', ...
0386 'pick','Qpick', 'ref_pick', 'ref_Qpick', 'CoordType', 'MEGinfo', ...
0387 'PositionFile');
0388 else
0389 vb_fsave(out_file, 'Measurement', 'bexp', 'bexp_ext', 'refmg', ...
0390 'pick','Qpick', 'CoordType', 'MEGinfo', ...
0391 'PositionFile');
0392 end
0393 end
0394 end
0395 return;
0396
0397
0398
0399
0400
0401
0402
0403 function [in_files, out_file, info, measurement, coord_type, ...
0404 saveman, real_dir] = ...
0405 inner_check_arguments(in_files, out_file, ext_spec)
0406 func_ = mfilename;
0407
0408 if isempty(in_files)
0409 error('(%s) in_files is a required parameter', func_);
0410 end
0411
0412 if isempty(out_file)
0413
0414 end
0415
0416 if ~iscell(in_files)
0417 in_files = {in_files};
0418 end
0419
0420
0421 n_files = length(in_files);
0422
0423 for i_file = 1:n_files
0424 if exist(in_files{i_file}, 'file') ~= 2
0425 error('(%s) cannot find in_file{%d} (%s)', ...
0426 func_, i_file, in_files{i_file});
0427 end
0428
0429
0430 info{i_file} = vb_load_measurement_info(in_files{i_file});
0431
0432 load(in_files{i_file}, 'Measurement', 'CoordType');
0433 measurement{i_file} = Measurement;
0434 coord_type{i_file} = CoordType;
0435 end
0436
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452 n_channel_base = info{1}.Nchannel;
0453 n_sample_base = info{1}.Nsample;
0454 sample_freq_base = info{1}.SampleFreq;
0455 pretrigger_base = info{1}.Pretrigger;
0456 device_base = info{1}.device;
0457
0458
0459 if isfield(info{1}, 'sensor_weight')
0460 sensor_weight_base = info{1}.sensor_weight;
0461 else
0462 sensor_weight_base = [];
0463 end
0464
0465 if isfield(info{1}, 'MEGch_id')
0466 MEGch_id_base = info{1}.MEGch_id;
0467 else
0468 MEGch_id_base = [];
0469 end
0470
0471 if isfield(info{1}, 'MEGch_name')
0472 MEGch_name_base = info{1}.MEGch_name;
0473 else
0474 MEGch_name_base = {};
0475 end
0476
0477 if isfield(info{1}, 'ActiveChannel')
0478 ActiveChannel_base = info{1}.ActiveChannel;
0479 else
0480 ActiveChannel_base = {};
0481 end
0482
0483 measurement_base = measurement{1};
0484 coord_type_base = coord_type{1};
0485
0486 for i_file = 2:n_files
0487
0488
0489
0490 if info{i_file}.Nchannel ~= n_channel_base
0491 error('(%s - %s) all numbers of channel must be the same', ...
0492 func_, in_files{i_file});
0493 end
0494
0495
0496 if info{i_file}.Nsample ~= n_sample_base
0497 error('(%s - %s) all numbers of sample must be the same', ...
0498 func_, in_files{i_file});
0499 end
0500
0501
0502 if info{i_file}.SampleFreq ~= sample_freq_base
0503 error('(%s - %s) all sampling frequencies must be the same', ...
0504 func_, in_files{i_file});
0505 end
0506
0507
0508 if info{i_file}.Pretrigger ~= pretrigger_base
0509 error('(%s - %s) all pretriggers must be the same', ...
0510 func_, in_files{i_file});
0511 end
0512
0513
0514 if ~strcmp(info{i_file}.device, device_base)
0515 error('(%s - %s) each device must be the same', ...
0516 func_, in_files{i_file});
0517 end
0518
0519
0520 if ~strcmp(measurement{i_file}, measurement_base)
0521 error('(%s - %s) each Measurement must be the same', ...
0522 func_, in_files{i_file});
0523 end
0524
0525
0526
0527 if isfield(info{i_file}, 'sensor_weight')
0528 if ~isequal(info{i_file}.sensor_weight, sensor_weight_base)
0529 error('(%s - %s) each sensor_weight must be the same', ...
0530 func_, in_files{i_file});
0531 end
0532 end
0533
0534
0535 if isfield(info{i_file}, 'MEGch_id')
0536 if ~isequal(info{i_file}.MEGch_id, MEGch_id_base)
0537 error('(%s - %s) each MEGch_id must be the same', ...
0538 func_, in_files{i_file});
0539 end
0540 end
0541
0542
0543 if isfield(info{i_file}, 'MEGch_name')
0544 if ~isequal(info{i_file}.MEGch_name, MEGch_name_base)
0545 error('(%s - %s) each MEGch_name must be the same', ...
0546 func_, in_files{i_file});
0547 end
0548 end
0549
0550
0551 if ~strcmp(coord_type{i_file}, coord_type_base)
0552 error('(%s - %s) each CoordType must be the same', ...
0553 func_, in_files{i_file});
0554 end
0555
0556
0557 if isfield(info{i_file}, 'ActiveChannel')
0558 if ~isequal(info{i_file}.ActiveChannel, ActiveChannel_base)
0559 error('(%s - %s) each ActiveChannel must be the same', ...
0560 func_, in_files{i_file});
0561 end
0562 end
0563
0564 end
0565
0566 saveman = [];
0567 real_dir = [];
0568
0569 if ~isempty(out_file)
0570 if isempty(ext_spec) ...
0571 || ~isfield(ext_spec, 'saveman') ...
0572 || ~vb_saveman_get_switch(ext_spec.saveman)
0573 [ext_spec.saveman.data_dir, real_dir] = vb_device_make_data_dir(out_file);
0574 ext_spec.saveman.precision = 'float64';
0575 else
0576 f_path = vb_get_file_parts(out_file);
0577 real_dir = [f_path '/' ext_spec.saveman.data_dir];
0578 end
0579 saveman = ext_spec.saveman;
0580 end
0581 return;
0582
0583
0584
0585
0586
0587