0001 function result = vb_eegfile_filter_ch_data(eeg_file, proc_spec)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
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
0051
0052 result = [];
0053
0054
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
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
0072 [root_dir] = vb_get_file_parts(eeg_file);
0073
0074 proc_spec.parm.freq = vb_info_get_sampling_frequency(eeg_info);
0075
0076
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
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
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;
0099 load_spec.ChannelType = 'MEG';
0100 load_spec.ActiveChannel = 0;
0101 cur_data = vb_load_meg_data(eeg_file, load_spec);
0102
0103
0104
0105
0106
0107 filtered_data = vb_filter_raw_data(cur_data, proc_spec.parm);
0108
0109
0110 new_file = sprintf('%s/%s.ch.eeg.dat', new_bin_dir, ch_labels{i_ch});
0111
0112
0113
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
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;
0136
0137 load_spec.ChannelType = 'ALL';
0138 load_spec.ActiveChannel = 0;
0139 cur_data = vb_load_meg_data(eeg_file, load_spec);
0140
0141
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
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
0167 cur_eeg = load(eeg_file);
0168 new_eeginfo = cur_eeg.EEGinfo;
0169
0170
0171
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
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
0192
0193
0194
0195
0196
0197 function [eeg_file, proc_spec] = inner_check_arguments(eeg_file, proc_spec)
0198 func_ = mfilename;
0199
0200
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
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
0236
0237
0238
0239