Home > vbmeg > functions > device > eeg > vb_eegfile_filter_ch_data.m

vb_eegfile_filter_ch_data

PURPOSE ^

make new external channel data files after filtering

SYNOPSIS ^

function result = vb_eegfile_filter_ch_data(eeg_file, proc_spec, new_eeg_file)

DESCRIPTION ^

 make new external channel data files after filtering
 [usage]
   result = vb_eegfile_filter_ch_data(eeg_file, proc_spec, new_meg_file)
 [input]
        eeg_file : <required> <<file>> EEG-MAT file (.eeg.mat)
       proc_spec : <required> <<struct>> struct defined process specifications
                 :       .parm   : <required> <<struct>>
                 :               :  .filt_suffix
                 :               :  .bias_flg
                 :               :  .highpass
                 :               :  .lowpass
                 :               :  .highpass_online
                 :               :  .lowpass_online
                 :               :  .common_flg
                 :               :  .fsamp
                 :  .ch_type : channel type ['MEG'] - case-insensitive
    new_eeg_file : <required> <<file>> filtered EEG-MAT file(.eeg.mat)
                                  : This parameter must include the full extension.
                                  :  ".eeg.mat"
             :  .ch_type : channel type ['MEG'] - case-insensitive

 --- Meaning of preprocess parameter
  .bias_flg    : Bias correction flag  (=0/1/2 : OFF/Bias/Linear)
               = N > 5: Bias correction by first N sample
  .highpass    : Highpass filter cutoff frequency [Hz]
  .lowpass     : Lowpass  filter cutoff frequency [Hz]
  .highpass_online > 0: online highpass filter is used (exponential)
  .lowpass_online  > 0: online lowpass filter is used (order=lowpass_online)
  .fsamp       : Down sampling frequency [Hz]
  .common_flg  : Common reference flag (=1/0 : ON/OFF)
   if these fields are empty, corresponding process is not done
 [output]
      result : <<struct>> depends on current process
 [note]
   @see vb_filter_raw_data.m
   @see vb_megfile_filter_ch_data.m
 [history]
   2009-07-17 (Sako) initial version
   2009-08-20 (Sako) modified to complain if plural trial data
   2016-11-25 (rhayashi) modified specification(new_eeg_file)

 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 result = vb_eegfile_filter_ch_data(eeg_file, proc_spec, new_eeg_file)
0002 % make new external channel data files after filtering
0003 % [usage]
0004 %   result = vb_eegfile_filter_ch_data(eeg_file, proc_spec, new_meg_file)
0005 % [input]
0006 %        eeg_file : <required> <<file>> EEG-MAT file (.eeg.mat)
0007 %       proc_spec : <required> <<struct>> struct defined process specifications
0008 %                 :       .parm   : <required> <<struct>>
0009 %                 :               :  .filt_suffix
0010 %                 :               :  .bias_flg
0011 %                 :               :  .highpass
0012 %                 :               :  .lowpass
0013 %                 :               :  .highpass_online
0014 %                 :               :  .lowpass_online
0015 %                 :               :  .common_flg
0016 %                 :               :  .fsamp
0017 %                 :  .ch_type : channel type ['MEG'] - case-insensitive
0018 %    new_eeg_file : <required> <<file>> filtered EEG-MAT file(.eeg.mat)
0019 %                                  : This parameter must include the full extension.
0020 %                                  :  ".eeg.mat"
0021 %             :  .ch_type : channel type ['MEG'] - case-insensitive
0022 %
0023 % --- Meaning of preprocess parameter
0024 %  .bias_flg    : Bias correction flag  (=0/1/2 : OFF/Bias/Linear)
0025 %               = N > 5: Bias correction by first N sample
0026 %  .highpass    : Highpass filter cutoff frequency [Hz]
0027 %  .lowpass     : Lowpass  filter cutoff frequency [Hz]
0028 %  .highpass_online > 0: online highpass filter is used (exponential)
0029 %  .lowpass_online  > 0: online lowpass filter is used (order=lowpass_online)
0030 %  .fsamp       : Down sampling frequency [Hz]
0031 %  .common_flg  : Common reference flag (=1/0 : ON/OFF)
0032 %   if these fields are empty, corresponding process is not done
0033 % [output]
0034 %      result : <<struct>> depends on current process
0035 % [note]
0036 %   @see vb_filter_raw_data.m
0037 %   @see vb_megfile_filter_ch_data.m
0038 % [history]
0039 %   2009-07-17 (Sako) initial version
0040 %   2009-08-20 (Sako) modified to complain if plural trial data
0041 %   2016-11-25 (rhayashi) modified specification(new_eeg_file)
0042 %
0043 % Copyright (C) 2011, ATR All Rights Reserved.
0044 % License : New BSD License(see VBMEG_LICENSE.txt)
0045 
0046 % --- CHECK ARGUMENTS --- %
0047 if ~exist('eeg_file', 'var'), eeg_file = ''; end
0048 if ~exist('proc_spec', 'var'), proc_spec = []; end
0049 [eeg_file, proc_spec, new_eeg_file] = inner_check_arguments(eeg_file, proc_spec, new_eeg_file);
0050 
0051 % --- MAIN PROCEDURE --------------------------------------------------------- %
0052 %
0053 result = [];
0054 LINE_LEN = 15;
0055 
0056 % --- re-store environment
0057 [p_, f_, e_]   = vb_get_file_parts(new_eeg_file);
0058 
0059 new_eeg_dir           = p_;           
0060 new_eeg_filename      = [f_, e_];
0061 bin_dir_name          = new_eeg_filename(1:strfind(new_eeg_filename, '.eeg.mat')-1);
0062 proc_spec.new_bin_dir = ['./' bin_dir_name];
0063 
0064 % --- process for each channel
0065 if strcmp(proc_spec.ch_type, 'MEG') ~= 1
0066   error('(%s) not be supported channel_type : %s', ...
0067     mfilename, proc_spec.ch_type);
0068 end
0069 
0070 ch_labels = vb_eegfile_get_channel_label(eeg_file,[],1);
0071 eeg_info  = vb_load_measurement_info(eeg_file);
0072 ch_len    = size(ch_labels, 1);
0073 
0074 % --- this function is for only single trial data
0075 if vb_info_get_Nrepeat(eeg_info) > 1
0076   err_msg = ['this filtering function should be applied to ' ...
0077     'continuously measured data file without trial extraction.'];
0078   error('%s', err_msg);
0079 end
0080 
0081 % --- PRELIMINARILY PROCESS
0082 proc_spec.parm.freq = vb_info_get_sampling_frequency(eeg_info);
0083     
0084 % Create binary directory
0085 new_bin_dir = fullfile(new_eeg_dir, proc_spec.new_bin_dir);
0086 if exist(new_bin_dir, 'dir') ~= 7
0087   vb_mkdir(new_bin_dir);
0088 end
0089 
0090 precision = vb_eeginfo_get_datatype(eeg_info);
0091 
0092 % --- PROCESS FOR EACH CHANNEL
0093 %
0094 for i_ch = 1:ch_len
0095 
0096   fprintf('[%s]', ch_labels{i_ch});
0097   if ~rem(i_ch, LINE_LEN), fprintf('\n'); end
0098 
0099   load_spec.ChannelName = {ch_labels{i_ch}};
0100   load_spec.ChannelSwitch = true; % ChannelName is to read
0101   load_spec.ChannelType = 'MEG';
0102   load_spec.ActiveChannel = 0; % all channels
0103   cur_data = vb_load_meg_data(eeg_file, load_spec);
0104 
0105 % --- verbose message
0106 %  fprintf('cur_data(%s) [%d x %d]\n', ...
0107 %    ch_labels{i_ch}, size(cur_data,1), size(cur_data,2));
0108   
0109   filtered_data = vb_filter_raw_data(cur_data, proc_spec.parm);
0110 
0111   % --- re-stored filtered data
0112   new_file = sprintf('%s/%s.ch.eeg.dat', new_bin_dir, ch_labels{i_ch});
0113   
0114 % --- verbose message
0115 %   fprintf('%s/%s.ch.eeg.dat\n', new_bin_dir, ch_labels{i_ch});
0116 
0117   fid = fopen(new_file, 'wb');
0118   if fid == -1
0119     warning('(%s) cannot open file : %s\n', mfilename, new_file);
0120     continue;
0121   end
0122   fwrite(fid, filtered_data, precision{i_ch});
0123   fclose(fid);
0124 end
0125 
0126 n_sample = length(filtered_data);
0127 
0128 % --- EXTRA CHANNELS and REFFERENCE CHANNELS
0129 ch_labels_extra = vb_eegfile_get_channel_label(eeg_file, [], 2);
0130 ch_len_extra = size(ch_labels_extra, 1);
0131 
0132 for i_ch = 1:ch_len_extra
0133 
0134   load_spec.ChannelName = {ch_labels_extra{i_ch}};
0135   load_spec.ChannelSwitch = true; % ChannelName is to read
0136 %   load_spec.ChannelType = 'EXT';
0137   load_spec.ChannelType = 'ALL';
0138   load_spec.ActiveChannel = 0; % all channels
0139   cur_data = vb_load_meg_data(eeg_file, load_spec);
0140 
0141   % Down sampling for external signal (No filtering)
0142   if isfield(proc_spec.parm, 'fsamp') && ...
0143     ~isempty(proc_spec.parm.fsamp) ...
0144       && proc_spec.parm.fsamp~=proc_spec.parm.freq,
0145     
0146     fprintf(' === EXTRA CHANNEL:[%s]', ch_labels_extra{i_ch});
0147      fsamp = proc_spec.parm.freq;
0148       fsnew = proc_spec.parm.fsamp;
0149       T = size(cur_data,2);
0150       t = round(1:fsamp/fsnew:T);
0151       cur_data = cur_data(:,t);
0152   else
0153       proc_spec.parm.fsamp = proc_spec.parm.freq;
0154   end
0155   
0156   % --- re-stored filtered data
0157   new_file = sprintf('%s/%s.ch.eeg.dat', new_bin_dir, ch_labels_extra{i_ch});
0158   fid = fopen(new_file, 'wb');
0159   if fid == -1
0160     warning('(%s) cannot open file : %s\n', mfilename, new_file);
0161     continue;
0162   end
0163   fwrite(fid, cur_data, precision{ch_len+i_ch});
0164   fclose(fid);
0165 end
0166 
0167 
0168 % --- POSTERIORI PROCESS
0169 cur_eeg = load(eeg_file);
0170 new_eeginfo = cur_eeg.EEGinfo;
0171 
0172 % --- update fields
0173 % channel info is not changed
0174 new_eeginfo.Nsample  = n_sample;
0175 new_eeginfo.SampleFrequency = proc_spec.parm.fsamp;
0176 new_eeginfo.File.DataDir = proc_spec.new_bin_dir;
0177     
0178 % --- undefined
0179 new_eeginfo.FilterInfo = proc_spec.parm;
0180 
0181 cur_eeg.EEGinfo = new_eeginfo;
0182 eeg_data     = [];
0183 
0184 fprintf('\n');
0185 fprintf('save: %s\n',new_eeg_file);
0186 
0187 save_cmd = sprintf('save ''%s'' -struct cur_eeg', new_eeg_file);
0188 eval(save_cmd);
0189 return;
0190 %
0191 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0192 
0193 % --- INNER FUNCTIONS -------------------------------------------------------- %
0194 %
0195 % --- inner_check_arguments()
0196 %
0197 function [eeg_file, proc_spec, new_eeg_file] = inner_check_arguments(eeg_file, proc_spec, new_eeg_file)
0198 func_ = mfilename;
0199 
0200 % --- check meg_file
0201 if isempty(eeg_file)
0202   error('(%s) eeg_file is a required parameter', func_);
0203 end
0204 
0205 if exist(eeg_file, 'file') ~= 2
0206   error('(%s) cannot find eeg_file : %s', func_, eeg_file);
0207 end
0208 
0209 if isempty(proc_spec)
0210   error('(%s) proc_spec is a required parameter', func_);
0211 end
0212 
0213 if ~isfield(proc_spec, 'parm')
0214   error('(%s) parm is a required field of proc_spec', func_);
0215 end
0216 if isempty(new_eeg_file)
0217   error('(%s) proc_spec is a required parameter', func_);
0218 end
0219 if ~strcmp(new_eeg_file(end-7:end), '.eeg.mat')
0220   error('(%s) specified new_meg_file should have ''.eeg.mat''', func_);
0221 end
0222 
0223 if ~isfield(proc_spec, 'ch_type')
0224   proc_spec.ch_type = 'MEG';
0225 end
0226 proc_spec.ch_type = upper(proc_spec.ch_type);
0227 return;
0228 %
0229 % --- end of inner_check_arguments()
0230 %
0231 % --- END OF INNER FUNCTIONS ------------------------------------------------- %
0232 
0233 %%% END OF FILE %%%

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