Home > 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)

DESCRIPTION ^

 make new external channel data files after filtering
 [usage]
   result = vb_eegfile_filter_ch_data(eeg_file, proc_spec)
 [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
             :  .new_eeg   : new EEG filename
             :             :  ['<old_eeg_file>_filt_yymmdd-HHMMSS']
             :  .new_bin_dir : new binary data directory
             :               : (proc_id = 1)['./' new_eeg]
             :  .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

 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)
0002 % make new external channel data files after filtering
0003 % [usage]
0004 %   result = vb_eegfile_filter_ch_data(eeg_file, proc_spec)
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 %             :  .new_eeg   : new EEG filename
0018 %             :             :  ['<old_eeg_file>_filt_yymmdd-HHMMSS']
0019 %             :  .new_bin_dir : new binary data directory
0020 %             :               : (proc_id = 1)['./' new_eeg]
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 %
0042 % Copyright (C) 2011, ATR All Rights Reserved.
0043 % License : New BSD License(see VBMEG_LICENSE.txt)
0044 
0045 % --- CHECK ARGUMENTS --- %
0046 if ~exist('eeg_file', 'var'), eeg_file = ''; end
0047 if ~exist('proc_spec', 'var'), proc_spec = []; end
0048 [eeg_file, proc_spec] = inner_check_arguments(eeg_file, proc_spec);
0049 
0050 % --- MAIN PROCEDURE --------------------------------------------------------- %
0051 %
0052 result = [];
0053 
0054 % --- process for each channel
0055 if strcmp(proc_spec.ch_type, 'MEG') ~= 1
0056   error('(%s) not be supported channel_type : %s', ...
0057     mfilename, proc_spec.ch_type);
0058 end
0059 
0060 ch_labels = vb_eegfile_get_channel_label(eeg_file,[],1);
0061 eeg_info  = vb_load_measurement_info(eeg_file);
0062 ch_len    = size(ch_labels, 1);
0063 
0064 % --- this function is for only single trial data
0065 if vb_info_get_Nrepeat(eeg_info) > 1
0066   err_msg = ['this filtering function should be applied to ' ...
0067     'continuously measured data file without trial extraction.'];
0068   error('%s', err_msg);
0069 end
0070 
0071 % --- PRELIMINARILY PROCESS
0072 [root_dir] = vb_get_file_parts(eeg_file);
0073 
0074 proc_spec.parm.freq = vb_info_get_sampling_frequency(eeg_info);
0075     
0076 % --- re-store environment
0077 if ~isfield(proc_spec, 'new_bin_dir') || isempty(proc_spec.new_bin_dir)
0078   bin_dir = proc_spec.new_eeg;
0079   bin_dir = bin_dir(1:strfind(bin_dir,'.eeg.mat')-1);
0080   proc_spec.new_bin_dir = ['./' bin_dir];
0081 end
0082 
0083 % proc_spec.parm
0084 
0085 new_bin_dir = [root_dir '/' proc_spec.new_bin_dir];
0086 if exist(new_bin_dir, 'dir') ~= 7
0087   vb_mkdir(new_bin_dir);
0088 end
0089 precision = vb_eeginfo_get_datatype(eeg_info);
0090 
0091 % --- PROCESS FOR EACH CHANNEL
0092 %
0093 for i_ch = 1:ch_len
0094 
0095   fprintf('[%s]', ch_labels{i_ch});
0096 
0097   load_spec.ChannelName = {ch_labels{i_ch}};
0098   load_spec.ChannelSwitch = true; % ChannelName is to read
0099   load_spec.ChannelType = 'MEG';
0100   load_spec.ActiveChannel = 0; % all channels
0101   cur_data = vb_load_meg_data(eeg_file, load_spec);
0102 
0103 % --- verbose message
0104 %  fprintf('cur_data(%s) [%d x %d]\n', ...
0105 %    ch_labels{i_ch}, size(cur_data,1), size(cur_data,2));
0106   
0107   filtered_data = vb_filter_raw_data(cur_data, proc_spec.parm);
0108 
0109   % --- re-stored filtered data
0110   new_file = sprintf('%s/%s.ch.eeg.dat', new_bin_dir, ch_labels{i_ch});
0111   
0112 % --- verbose message
0113 %   fprintf('%s/%s.ch.eeg.dat\n', new_bin_dir, ch_labels{i_ch});
0114 
0115   fid = fopen(new_file, 'wb');
0116   if fid == -1
0117     warining('(%s) cannot open file : %s\n', mfilename, new_file);
0118     continue;
0119   end
0120   fwrite(fid, filtered_data, precision{i_ch});
0121   fclose(fid);
0122 end
0123 
0124 n_sample = length(filtered_data);
0125 
0126 % --- EXTRA CHANNELS and REFFERENCE CHANNELS
0127 ch_labels_extra = vb_eegfile_get_channel_label(eeg_file, [], 2);
0128 ch_len_extra = size(ch_labels_extra, 1);
0129 
0130 for i_ch = 1:ch_len_extra
0131 
0132   fprintf(' === EXTRA CHANNEL:[%s]', ch_labels_extra{i_ch});
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 ~isempty(proc_spec.parm.fsamp) ...
0143       && proc_spec.parm.fsamp~=proc_spec.parm.freq,
0144     
0145       fsamp = proc_spec.parm.freq;
0146       fsnew = proc_spec.parm.fsamp;
0147       T = size(cur_data,2);
0148       t = round(1:fsamp/fsnew:T);
0149       cur_data = cur_data(:,t);
0150   else
0151       proc_spec.parm.fsamp = proc_spec.parm.freq;
0152   end
0153   
0154   % --- re-stored filtered data
0155   new_file = sprintf('%s/%s.ch.eeg.dat', new_bin_dir, ch_labels_extra{i_ch});
0156   fid = fopen(new_file, 'wb');
0157   if fid == -1
0158     warining('(%s) cannot open file : %s\n', mfilename, new_file);
0159     continue;
0160   end
0161   fwrite(fid, cur_data, precision{ch_len+i_ch});
0162   fclose(fid);
0163 end
0164 
0165 
0166 % --- POSTERIORI PROCESS
0167 cur_eeg = load(eeg_file);
0168 new_eeginfo = cur_eeg.EEGinfo;
0169 
0170 % --- update fields
0171 % channel info is not changed
0172 new_eeginfo.Nsample  = n_sample;
0173 new_eeginfo.SampleFrequency = proc_spec.parm.fsamp;
0174 new_eeginfo.File.DataDir = proc_spec.new_bin_dir;
0175     
0176 % --- undefined
0177 new_eeginfo.FilterInfo = proc_spec.parm;
0178 
0179 cur_eeg.EEGinfo = new_eeginfo;
0180 eeg_data     = [];
0181 
0182 new_eeg_file = [root_dir '/' proc_spec.new_eeg];
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] = inner_check_arguments(eeg_file, proc_spec)
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 
0217 if ~isfield(proc_spec, 'new_eeg')
0218   [fpath, tmp_fname] = vb_get_file_parts(eeg_file);
0219   % --- tmp_fname may include '.eeg'
0220   if strfind(tmp_fname, '.eeg')
0221     [fpath, fname] = vb_get_file_parts(tmp_fname);
0222   else
0223     fname = tmp_fname;
0224   end
0225   proc_spec.new_eeg = ...
0226     sprintf('%s_filt_%s', fname, datestr(now, 'yymmdd-HHMMSS'));
0227 end
0228 
0229 if ~isfield(proc_spec, 'ch_type')
0230   proc_spec.ch_type = 'MEG';
0231 end
0232 proc_spec.ch_type = upper(proc_spec.ch_type);
0233 return;
0234 %
0235 % --- end of inner_check_arguments()
0236 %
0237 % --- END OF INNER FUNCTIONS ------------------------------------------------- %
0238 
0239 %%% END OF FILE %%%

Generated on Tue 27-Aug-2013 11:46:04 by m2html © 2005