Home > vbmeg > functions > device > meg > vb_megfile_filter_ch_data.m

vb_megfile_filter_ch_data

PURPOSE ^

make new external channel data files after filtering

SYNOPSIS ^

function result = vb_megfile_filter_ch_data(meg_file, proc_spec, new_meg_file)

DESCRIPTION ^

 make new external channel data files after filtering
 [usage]
   result = vb_megfile_filter_ch_data(meg_file, proc_spec, new_meg_file)
 [input]
        meg_file : <required> <<file>> MEG-MAT file (.meg.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
                 :  .ch_name      : channel name {Nchannel x 1}
                 :                : if this is given, it is applied ahead of ch_type
    new_meg_file : <required> <<file>> filtered MEG-MAT file(.meg.mat)
                                  : This parameter must include the full extension.
                                  :  ".meg.mat"

 --- Meaning of preprocess parameter
  .bias_flg    : Bias correction flag  (=0/1/2 : OFF/Bias/Linear)
               = N > 5: Bias correction by first N sample

 --- Filter parameters
  .highpass    : Highpass filter cutoff frequency [Hz]
  .lowpass     : Lowpass  filter cutoff frequency [Hz]

 --- Type of filter specification
  .highpass_online : filter order of IIR highpass filter
      = 0: FIR highpass filter (eegfilt)
      = 1: online highpass filter (exponential)
      > 1: Butterworth highpass filter (filtfilt)
      < 0: Butterworth highpass filter (online)

  .lowpass_online : filter order of IIR lowpass filter
      = 0: FIR lowpass filter (eegfilt)
      = 1: online lowpass filter (exponential)
      > 1: Butterworth lowpass filter (filtfilt)
      < 0: Butterworth lowpass filter (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_import_extra_data.m
 [history]
   2009-06-19 (Sako) initial version
   2009-08-20 (Sako) modified to complain if plural trial data
   2010-06-25 (Sako) added ch_name and modified help
   2010-07-02 (Sako) modified progress indicator
   2016-11-25 (rhayashi) modified specification(new_meg_file)
   2018-08-23 (rhayashi) Filtering is done by referring to ch_name.
 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_megfile_filter_ch_data(meg_file, proc_spec, new_meg_file)
0002 % make new external channel data files after filtering
0003 % [usage]
0004 %   result = vb_megfile_filter_ch_data(meg_file, proc_spec, new_meg_file)
0005 % [input]
0006 %        meg_file : <required> <<file>> MEG-MAT file (.meg.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 %                 :  .ch_name      : channel name {Nchannel x 1}
0019 %                 :                : if this is given, it is applied ahead of ch_type
0020 %    new_meg_file : <required> <<file>> filtered MEG-MAT file(.meg.mat)
0021 %                                  : This parameter must include the full extension.
0022 %                                  :  ".meg.mat"
0023 %
0024 % --- Meaning of preprocess parameter
0025 %  .bias_flg    : Bias correction flag  (=0/1/2 : OFF/Bias/Linear)
0026 %               = N > 5: Bias correction by first N sample
0027 %
0028 % --- Filter parameters
0029 %  .highpass    : Highpass filter cutoff frequency [Hz]
0030 %  .lowpass     : Lowpass  filter cutoff frequency [Hz]
0031 %
0032 % --- Type of filter specification
0033 %  .highpass_online : filter order of IIR highpass filter
0034 %      = 0: FIR highpass filter (eegfilt)
0035 %      = 1: online highpass filter (exponential)
0036 %      > 1: Butterworth highpass filter (filtfilt)
0037 %      < 0: Butterworth highpass filter (online)
0038 %
0039 %  .lowpass_online : filter order of IIR lowpass filter
0040 %      = 0: FIR lowpass filter (eegfilt)
0041 %      = 1: online lowpass filter (exponential)
0042 %      > 1: Butterworth lowpass filter (filtfilt)
0043 %      < 0: Butterworth lowpass filter (online)
0044 %
0045 %  .fsamp       : Down sampling frequency [Hz]
0046 %  .common_flg  : Common reference flag (=1/0 : ON/OFF)
0047 %   if these fields are empty, corresponding process is not done
0048 % [output]
0049 %      result : <<struct>> depends on current process
0050 % [note]
0051 %   @see vb_filter_raw_data.m
0052 %   @see vb_megfile_import_extra_data.m
0053 % [history]
0054 %   2009-06-19 (Sako) initial version
0055 %   2009-08-20 (Sako) modified to complain if plural trial data
0056 %   2010-06-25 (Sako) added ch_name and modified help
0057 %   2010-07-02 (Sako) modified progress indicator
0058 %   2016-11-25 (rhayashi) modified specification(new_meg_file)
0059 %   2018-08-23 (rhayashi) Filtering is done by referring to ch_name.
0060 % Copyright (C) 2011, ATR All Rights Reserved.
0061 % License : New BSD License(see VBMEG_LICENSE.txt)
0062 
0063 % --- CHECK ARGUMENTS --- %
0064 if ~exist('meg_file', 'var'), meg_file = ''; end
0065 if ~exist('proc_spec', 'var'), proc_spec = []; end
0066 if ~exist('new_meg_file', 'var'), new_meg_file = ''; end
0067 [meg_file, proc_spec, new_meg_file] = inner_check_arguments(meg_file, proc_spec, new_meg_file);
0068 
0069 meg_info  = vb_load_measurement_info(meg_file);
0070 
0071 % --- this function is for only single trial data
0072 if vb_info_get_Nrepeat(meg_info) > 1
0073   err_msg = ['this filtering function should be applied to ' ...
0074     'continuously measured data file without trial extraction.'];
0075   error('%s', err_msg);
0076 end
0077 
0078 
0079 % --- MAIN PROCEDURE --------------------------------------------------------- %
0080 %
0081 result = [];
0082 
0083 % Preparation for creating new_meg_file
0084 [p_, f_, e_]   = vb_get_file_parts(new_meg_file);
0085 new_meg_dir           = p_;           
0086 new_meg_filename      = [f_, e_];
0087 bin_dir_name          = new_meg_filename(1:strfind(new_meg_filename, '.meg.mat')-1);
0088 proc_spec.new_bin_dir = ['./' bin_dir_name];
0089 
0090 % Create binary directory for new_meg_file
0091 new_bin_dir = fullfile(new_meg_dir, proc_spec.new_bin_dir);
0092 if exist(new_bin_dir, 'dir') ~= 7
0093   vb_mkdir(new_bin_dir);
0094 end
0095 
0096 % set Default: ch_type = 'MEG'
0097 if ~isfield(proc_spec, 'ch_name')
0098     ch_info = vb_load_channel_info(meg_file);
0099     proc_spec.ch_name = ch_info.Name;
0100 end
0101 
0102 % Filtering
0103 inner_filter_ch_data(meg_file, proc_spec, new_meg_file);
0104 
0105 fprintf('save: %s\n',new_meg_file);
0106 
0107 return;
0108 %
0109 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0110 
0111 % --- INNER FUNCTIONS -------------------------------------------------------- %
0112 %
0113 % --- inner_check_arguments()
0114 %
0115 function [meg_file, proc_spec, new_meg_file] = inner_check_arguments(meg_file, proc_spec, new_meg_file)
0116 func_ = mfilename;
0117 
0118 % --- check meg_file
0119 if isempty(meg_file)
0120   error('(%s) meg_file is a required parameter', func_);
0121 end
0122 
0123 if exist(meg_file, 'file') ~= 2
0124   error('(%s) cannot find meg_file : %s', func_, meg_file);
0125 end
0126 
0127 if isempty(proc_spec)
0128   error('(%s) proc_spec is a required parameter', func_);
0129 end
0130 
0131 if ~isfield(proc_spec, 'parm')
0132   error('(%s) parm is a required field of proc_spec', func_);
0133 end
0134 if isempty(new_meg_file)
0135   error('(%s) new_meg_file is a required parameter', func_);
0136 end
0137 if ~strcmp(new_meg_file(end-7:end), '.meg.mat')
0138   error('(%s) specified new_meg_file should have ''.meg.mat''', func_);
0139 end
0140 
0141 if ~isfield(proc_spec, 'ch_type')
0142   proc_spec.ch_type = 'MEG';
0143 end
0144 proc_spec.ch_type = upper(proc_spec.ch_type);
0145 return;
0146 %
0147 % --- end of inner_check_arguments()
0148 
0149 
0150 % --- inner_filter_ch_data()
0151 %
0152 function inner_filter_ch_data(meg_file, proc_spec, new_file)
0153 
0154 ch_info = vb_load_channel_info(meg_file, 'ALL');
0155 
0156 % filterling channel
0157 filtering_ch_list = proc_spec.ch_name;
0158 if ~iscell(filtering_ch_list)
0159     filtering_ch_list = {filtering_ch_list};
0160 end
0161 
0162 % Original information
0163 meg_info  = vb_load_measurement_info(meg_file);
0164 proc_spec.parm.freq = vb_meginfo_get_sampling_frequency(meg_info);
0165 
0166 % Check weather do downsampling or not
0167 do_downsample = false;
0168 if isfield(proc_spec.parm, 'fsamp') && ~isempty(proc_spec.parm.fsamp)
0169     % Downsampling causes format change.
0170     do_downsample = true;
0171     dsamp_parm.freq  = vb_meginfo_get_sampling_frequency(meg_info); % old
0172     dsamp_parm.fsamp = proc_spec.parm.fsamp; % new
0173 end
0174 
0175 % Load data
0176 load_spec.ChannelName   = ch_info.Name;
0177 load_spec.ChannelType   = 'ALL';
0178 load_spec.ActiveChannel = 0; %  all channels
0179 Nall_ch = length(ch_info.Name);
0180 
0181 data = vb_load_meg_data(meg_file, load_spec);
0182 
0183 % Search filtering target
0184 filter_ch_ix     = [];
0185 for n=1:length(filtering_ch_list)
0186     ix = strmatch(filtering_ch_list{n}, ch_info.Name, 'exact');
0187     if isempty(ix)
0188         error('Unknown channel specified : %s', filtering_ch_list{n});
0189     else
0190         filter_ch_ix = [filter_ch_ix; ix];
0191     end
0192 end
0193 
0194 % test filter to get new sampling number.
0195 p_data = vb_filter_raw_data(data(1, :), proc_spec.parm); 
0196 p_data = zeros(Nall_ch, size(p_data, 2));
0197 
0198 % Filtering
0199 if do_downsample
0200     % filtering and downsampling to specified channel
0201     p_data(filter_ch_ix, :) = vb_filter_raw_data(data(filter_ch_ix, :), proc_spec.parm);
0202 
0203     % apply only downsampling to other channel
0204     dsamp_ch_ix = setdiff([1:Nall_ch]', filter_ch_ix);
0205     if ~isempty(dsamp_ch_ix)
0206         p_data(dsamp_ch_ix, :)  = vb_filter_raw_data(data(dsamp_ch_ix, :), dsamp_parm);
0207     end
0208 else
0209     % filtering
0210 
0211     % copy from original
0212     p_data = data;
0213 
0214     % filtering to specified channel
0215     p_data(filter_ch_ix, :) = vb_filter_raw_data(data(filter_ch_ix, :), proc_spec.parm);
0216 end
0217 
0218 % Create new_file
0219 base_meg = load(meg_file);
0220 base_meg.MEGinfo.saveman.data_dir = proc_spec.new_bin_dir;
0221 base_meg.MEGinfo.Nsample     = size(p_data,2);
0222 base_meg.MEGinfo.SampleFreq  = proc_spec.parm.fsamp;
0223 vb_save_struct(new_file, base_meg);
0224 
0225 vb_megfile_import_extra_data(new_file, p_data, ch_info, 1);
0226 return;
0227 %
0228 % --- end of inner_filter_ch_data()

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