0001 function vb_signal_processor_apply_processing(input_files, ...
0002 output_files, ...
0003 filter_list, ...
0004 mode)
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026 if ~exist('input_files', 'var') || ~iscellstr(input_files)
0027 error('input files must be cell string');
0028 end
0029 if ~exist('output_files', 'var') || ~iscellstr(output_files)
0030 error('output files must be cell string');
0031 end
0032 if ~exist('filter_list', 'var')
0033 error('filter_list is a required parameter.');
0034 end
0035 if ~exist('mode', 'var')
0036 mode = 0;
0037 end
0038 if ~(mode == 0 || mode == 1)
0039 error('Invalid mode was specified.');
0040 end
0041
0042
0043
0044
0045 Nfiles = length(input_files);
0046 Nfilters = length(filter_list);
0047 d = vb_define_signal_processor;
0048
0049 vb_define_device;
0050 for k=1:Nfiles
0051 str = sprintf('Now Processing... (%d/%d)', k, Nfiles);
0052 if mode == 0
0053
0054 vb_disp(str);
0055 elseif mode == 1
0056
0057 if exist('h', 'var') && ~isempty(h) && ishandle(h)
0058 delete(h);
0059 end
0060 h = msgbox(str, 'Please wait', 'modal');
0061 set(findobj(h, 'Tag', 'OKButton'), 'Visible', 'off');
0062 drawnow;
0063 end
0064
0065 input_file = input_files{k};
0066 output_file = output_files{k};
0067
0068
0069 [device, format, Nsample, org_freq, pretrig] = get_file_info(input_file);
0070 for f=1:length(filter_list)
0071 if strcmp(filter_list{f}.type, d.TYPE_PROCESSING_COMMON_REFERENCE)
0072 if strcmp(format, 'external')
0073 vb_disp('All the EEG data will be loaded at once to apply common reference filter even though the file format is external.');
0074 format = 'internal';
0075 end
0076 end
0077 end
0078
0079
0080
0081
0082 if strcmp(format, 'external')
0083 info = vb_load_measurement_info(input_file);
0084 if strcmp(device, 'MEG')
0085 ext = FILE_EXT_BIN_CH_MEG;
0086 output_dir = [strrep(output_file, '.meg.mat', '') '_bin'];
0087 save_precision = vb_meginfo_get_precision(info);
0088 [p, f] = vb_get_file_parts(output_dir);
0089 rel_output_dir = ['./', f];
0090 filter_ch = vb_megfile_get_channel_label_meg(input_file);
0091 extra_ch = [vb_megfile_get_channel_label_extra(input_file); ...
0092 vb_megfile_get_channel_label_refmg(input_file)];
0093 else
0094 ext = FILE_EXT_BIN_CH_EEG;
0095 output_dir = [strrep(output_file, '.eeg.mat', '') '_bin'];
0096 save_precision = vb_eeginfo_get_datatype(info);
0097 [p, f] = vb_get_file_parts(output_dir);
0098 rel_output_dir = ['./', f];
0099 filter_ch = vb_eegfile_get_channel_label(input_file, [], 1);
0100 extra_ch = vb_eegfile_get_channel_label(input_file, [], 2);
0101 end
0102
0103
0104 Nch = size(filter_ch, 1);
0105 Nch_extra = size(extra_ch, 1);
0106 if isempty(save_precision)
0107 save_precision = 'float64';
0108 end
0109 if size(save_precision, 1) == 1
0110
0111 filt_ch_precision = cellstr(repmat(save_precision, Nch, 1));
0112 ext_ch_precision = cellstr(repmat(save_precision, Nch_extra, 1));
0113 elseif iscellstr(save_precision)
0114
0115 filt_ch_precision = save_precision(1:Nch);
0116 ext_ch_precision = save_precision(Nch+1:end);
0117 end
0118
0119
0120 for ch=1:Nch
0121
0122 data = load_ch_data(device, input_file, filter_ch{ch});
0123 data_freq = org_freq;
0124
0125
0126 only_down_sampling = false;
0127 for j=1:Nfilters
0128 [data, data_freq] = ...
0129 apply_filter(data, data_freq, ...
0130 filter_list{j}, only_down_sampling);
0131 end
0132
0133 save_data_to_dir(output_dir, ext, data, filter_ch{ch}, filt_ch_precision{ch});
0134 end
0135
0136
0137 for ch=1:Nch_extra
0138
0139 data = load_ch_data(device, input_file, extra_ch{ch});
0140 data_freq = org_freq;
0141
0142
0143 only_down_sampling = true;
0144 for j=1:Nfilters
0145 [data, data_freq] = ...
0146 apply_filter(data, data_freq, ...
0147 filter_list{j}, only_down_sampling);
0148 end
0149
0150 save_data_to_dir(output_dir, ext, data, extra_ch{ch}, ext_ch_precision{ch});
0151 end
0152
0153
0154 new_info = struct;
0155 if org_freq ~= data_freq
0156 new_info.n_sample = size(data, 2);
0157 new_info.sampling_freq = data_freq;
0158 new_info.pretrigger = round((data_freq/org_freq)*pretrig);
0159 end
0160 new_info.bin_dir = rel_output_dir;
0161 switch(device)
0162 case 'MEG'
0163
0164 vb_saver_meg_copy_info(input_file, output_file, new_info);
0165 case 'EEG'
0166
0167 vb_saver_eeg_copy_info(input_file, output_file, new_info);
0168 end
0169
0170 else
0171
0172
0173
0174 if strcmp(device, 'MEG')
0175
0176 new_data = struct;
0177
0178
0179
0180 new_data.bexp = vb_load_meg_data(input_file);
0181 data_freq = org_freq;
0182 if ~isempty(new_data.bexp)
0183
0184 only_down_sampling = false;
0185 for j=1:Nfilters
0186 [new_data.bexp, data_freq] = ...
0187 apply_filter(new_data.bexp, data_freq, ...
0188 filter_list{j}, only_down_sampling);
0189 end
0190 end
0191
0192
0193
0194
0195 load_spec = struct;
0196 load_spec.ChannelType = 'EXTRA';
0197 new_data.bexp_ext = vb_load_meg_data(input_file, load_spec);
0198 data_freq = org_freq;
0199 if ~isempty(new_data.bexp_ext)
0200
0201 only_down_sampling = true;
0202 for j=1:Nfilters
0203 [new_data.bexp_ext, data_freq] = ...
0204 apply_filter(new_data.bexp_ext, data_freq, ...
0205 filter_list{j}, only_down_sampling);
0206 end
0207 end
0208
0209
0210
0211
0212 load_spec = struct;
0213 load_spec.ChannelType = 'REFERENCE';
0214 new_data.refmg = vb_load_meg_data(input_file, load_spec);
0215 data_freq = org_freq;
0216 if ~isempty(new_data.refmg)
0217
0218 only_down_sampling = true;
0219 for j=1:Nfilters
0220 [new_data.refmg, data_freq] = ...
0221 apply_filter(new_data.refmg, data_freq, ...
0222 filter_list{j}, only_down_sampling);
0223 end
0224 end
0225
0226
0227 new_info = struct;
0228 if org_freq ~= data_freq
0229 new_info.n_sample = size(new_data.bexp, 2);
0230 new_info.sampling_freq = data_freq;
0231 new_info.pretrigger = round((data_freq/org_freq)*pretrig);
0232 end
0233
0234 vb_saver_meg_copy_info(input_file, output_file, new_info);
0235
0236
0237 vb_msrmnt_store_data(input_file, new_data, filter_list, output_file);
0238 else
0239
0240
0241
0242
0243
0244 data = vb_load_meg_data(input_file);
0245 data_freq = org_freq;
0246 if ~isempty(data)
0247
0248 only_down_sampling = false;
0249 for j=1:Nfilters
0250 [data, data_freq] = ...
0251 apply_filter(data, data_freq, ...
0252 filter_list{j}, only_down_sampling);
0253 end
0254 end
0255
0256
0257
0258 load_spec = struct;
0259 load_spec.ChannelType = 'EXTRA';
0260 ext_data = vb_load_meg_data(input_file, load_spec);
0261 data_freq = org_freq;
0262 if ~isempty(ext_data)
0263
0264 only_down_sampling = true;
0265 for j=1:Nfilters
0266 [ext_data, data_freq] = ...
0267 apply_filter(ext_data, data_freq, ...
0268 filter_list{j}, only_down_sampling);
0269 end
0270 end
0271
0272 new_info = struct;
0273 if org_freq ~= data_freq
0274 new_info.n_sample = size(data, 2);
0275 new_info.sampling_freq = data_freq;
0276 new_info.pretrigger = round((data_freq/org_freq)*pretrig);
0277 end
0278
0279 vb_saver_eeg_copy_info(input_file, output_file, new_info);
0280
0281
0282 new_data = struct;
0283 new_data.eeg_data = [data; ext_data];
0284 vb_msrmnt_store_data(input_file, new_data, filter_list, output_file);
0285 end
0286 end
0287 end
0288 if exist('h', 'var') && ~isempty(h) && ishandle(h)
0289 delete(h);
0290 else
0291 vb_disp('done.');
0292 end
0293
0294 function data = load_ch_data(device, device_file, ch_name)
0295
0296
0297
0298
0299
0300 if ischar(ch_name)
0301 ch_name = {ch_name};
0302 end
0303 load_spec.ChannelName = ch_name;
0304 load_spec.ChannelSwitch = true;
0305 load_spec.ChannelType = device;
0306 load_spec.ActiveChannel = 0;
0307
0308 data = vb_load_meg_data(device_file, load_spec);
0309
0310 function save_data_to_dir(data_dir, ext, data, ch_name, precision)
0311
0312
0313
0314
0315 if isempty(precision), precision = 'float64'; end
0316
0317 save_spec.ch_label = ch_name;
0318 save_spec.data = data;
0319 save_spec.dir = data_dir;
0320 save_spec.precision= precision;
0321 save_spec.ext = ext;
0322 vb_saver_cmn_make_binary_file(save_spec);
0323
0324 function [data, freq] = apply_filter(data, freq, filter, only_down_sampling)
0325
0326
0327
0328
0329
0330
0331
0332
0333 if ~exist('only_down_sampling', 'var')
0334 only_down_sampling = false;
0335 end
0336
0337 d = vb_define_signal_processor;
0338 switch(filter.type)
0339 case d.TYPE_PROCESSING_DOWNSAMPLE
0340
0341 otherwise
0342 if only_down_sampling
0343 return;
0344 end
0345 end
0346
0347
0348
0349
0350
0351
0352 lib_filter = vb_signal_processor_util_convert_parm_to_lib_parm(filter, freq);
0353 [data, freq] = vb_filter_data(data, lib_filter);
0354
0355 function [device, format, Nsample, freq, pretrigger] = ...
0356 get_file_info(device_file)
0357
0358
0359
0360
0361
0362
0363 device = vb_load_device(device_file);
0364 info = vb_load_measurement_info(device_file);
0365
0366 Nsample = vb_info_get_sample_number(info);
0367 freq = vb_info_get_sampling_frequency(info);
0368 pretrigger = vb_info_get_pre_trigger(info);
0369
0370 switch(device)
0371 case 'MEG'
0372 [state, guide_def] = vb_util_check_variable_in_matfile(device_file, 'bexp');
0373 case 'EEG'
0374 [state, guide_def] = vb_util_check_variable_in_matfile(device_file, 'eeg_data');
0375 otherwise
0376 error('Unknown Device data');
0377 end
0378 if (state == guide_def.VALID)
0379
0380 format = 'internal';
0381 else
0382 format = 'external';
0383 end