Home > vbmeg > functions > gui > preAnalysis > meeg_processor > signal_processor > vb_signal_processor_apply_processing.m

vb_signal_processor_apply_processing

PURPOSE ^

Apply filters to input_files and put output_files.

SYNOPSIS ^

function vb_signal_processor_apply_processing(input_files,output_files,filter_list,mode)

DESCRIPTION ^

 Apply filters to input_files and put output_files.
 [USAGE]
 vb_signal_processor_apply_processing(input_files, ...
                                             output_files, ...
                                             filter_list);
 [IN]
    input_files  : input MEG/EEG files.    [cell string]
    output_files : filtered MEG/EEG files. [cell string]
     filter_list : filter list             [cell struct]
            mode : display mode
                   = 0 : progress is shown by command line. [default]
                   = 1 : progress is shown by dialog.
 [OUT]
    none

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function vb_signal_processor_apply_processing(input_files, ...
0002                                               output_files, ...
0003                                               filter_list, ...
0004                                               mode)
0005 % Apply filters to input_files and put output_files.
0006 % [USAGE]
0007 % vb_signal_processor_apply_processing(input_files, ...
0008 %                                             output_files, ...
0009 %                                             filter_list);
0010 % [IN]
0011 %    input_files  : input MEG/EEG files.    [cell string]
0012 %    output_files : filtered MEG/EEG files. [cell string]
0013 %     filter_list : filter list             [cell struct]
0014 %            mode : display mode
0015 %                   = 0 : progress is shown by command line. [default]
0016 %                   = 1 : progress is shown by dialog.
0017 % [OUT]
0018 %    none
0019 %
0020 % Copyright (C) 2011, ATR All Rights Reserved.
0021 % License : New BSD License(see VBMEG_LICENSE.txt)
0022 
0023 %
0024 % --- Previous check
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 % --- Main Procedure
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         % progress by command line
0054         vb_disp(str);
0055     elseif mode == 1
0056         % progress by dialog
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     % EEG Common reference
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'; % load all at once;
0075             end
0076         end
0077     end
0078         
0079     %%%%%%%%%%%%%%%%%%%%%%%%%%%
0080     % external format section
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         % Create precision settings
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             % MEG case, save_precision is only defined one.
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             % EEG case, precision is defined for each channel.
0115             filt_ch_precision = save_precision(1:Nch);
0116             ext_ch_precision  = save_precision(Nch+1:end);
0117         end
0118             
0119         % Process channel data
0120         for ch=1:Nch
0121             % load single channel data
0122             data = load_ch_data(device, input_file, filter_ch{ch});
0123             data_freq = org_freq;
0124             
0125             % filtering
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             % save ch data
0133             save_data_to_dir(output_dir, ext, data, filter_ch{ch}, filt_ch_precision{ch});
0134         end
0135  
0136         % Process extra channel data
0137         for ch=1:Nch_extra
0138             % load single channel data
0139             data = load_ch_data(device, input_file, extra_ch{ch});
0140             data_freq = org_freq;
0141             
0142             % filtering(downsampling only)
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             % save ch data
0150             save_data_to_dir(output_dir, ext, data, extra_ch{ch}, ext_ch_precision{ch});
0151         end
0152         
0153         % Modifying header information
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                 % create MEG-MAT
0164                 vb_saver_meg_copy_info(input_file, output_file, new_info);
0165             case 'EEG'
0166                 % create EEG-MAT
0167                 vb_saver_eeg_copy_info(input_file, output_file, new_info);
0168         end
0169 
0170     else
0171     %%%%%%%%%%%%%%%%%%%%%%%%%%%
0172     % internal format section
0173     %%%%%%%%%%%%%%%%%%%%%%%%%%%
0174         if strcmp(device, 'MEG')
0175             % MEG internal format
0176             new_data = struct;
0177             %
0178             % --- channel data
0179             %
0180             new_data.bexp = vb_load_meg_data(input_file);
0181             data_freq     = org_freq;
0182             if ~isempty(new_data.bexp)
0183                 % filtering
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             % --- extra channel
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                 % filtering(downsampling only)
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             % --- reference data
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                 % filtering(downsampling only)
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             % Modifying header information
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             % create MEG-MAT
0234             vb_saver_meg_copy_info(input_file, output_file, new_info);
0235             
0236             % save MEG data
0237             vb_msrmnt_store_data(input_file, new_data, filter_list, output_file);
0238         else
0239             % EEG internal format
0240 
0241             %
0242             % --- channel data
0243             %
0244             data = vb_load_meg_data(input_file);
0245             data_freq     = org_freq;
0246             if ~isempty(data)
0247                 % filtering
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             % --- extra channel
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                 % filtering(downsampling only)
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             % Modifying header information
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             % create EEG-MAT
0279             vb_saver_eeg_copy_info(input_file, output_file, new_info);
0280             
0281             % save EEG data
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 % load data
0296 % device      : 'MEG' or 'EEG'
0297 % device_file : MEG-MAT or EEG-MAT
0298 % ch_name     : char or cell string
0299 
0300 if ischar(ch_name)
0301     ch_name = {ch_name};
0302 end
0303 load_spec.ChannelName = ch_name;
0304 load_spec.ChannelSwitch = true; % Channek Name is to read
0305 load_spec.ChannelType   = device;
0306 load_spec.ActiveChannel = 0; % all channels
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 % save data
0312 % device      : 'MEG' or 'EEG'
0313 % device_file : MEG-MAT or EEG-MAT
0314 % ch_name     : char or cell string
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 % [Input]
0326 %   data               : measurement data(ch x time x trial)
0327 %   freq               : sampling frequency of the data.
0328 %   filter             : filter parameter for vb_filter_data.
0329 %   only_down_sampling : true or false [default:false]
0330 % [Output]
0331 %   data               : processed data.
0332 %   freq               : sampling frequency of the processed data.
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         % fall through
0341     otherwise
0342         if only_down_sampling
0343             return;
0344         end
0345 end
0346 
0347 %
0348 % --- preprocess data
0349 %
0350 
0351 % convert gui parameter to library parameter
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 % device     : 'MEG' or 'EEG'
0358 % format     : 'internal' or 'external'
0359 % Nsample    : Sampling number of the file.
0360 % freq       : Sampling frequency of the file.
0361 % pretrigger : pretrigger length of the file.
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     % innner format
0380     format = 'internal';
0381 else
0382     format = 'external';
0383 end

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005