0001 function result = vb_eegfile_filter_ch_data(eeg_file, proc_spec, new_eeg_file)
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
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
0052
0053 result = [];
0054 LINE_LEN = 15;
0055
0056
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
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
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
0082 proc_spec.parm.freq = vb_info_get_sampling_frequency(eeg_info);
0083
0084
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
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;
0101 load_spec.ChannelType = 'MEG';
0102 load_spec.ActiveChannel = 0;
0103 cur_data = vb_load_meg_data(eeg_file, load_spec);
0104
0105
0106
0107
0108
0109 filtered_data = vb_filter_raw_data(cur_data, proc_spec.parm);
0110
0111
0112 new_file = sprintf('%s/%s.ch.eeg.dat', new_bin_dir, ch_labels{i_ch});
0113
0114
0115
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
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;
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 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
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
0169 cur_eeg = load(eeg_file);
0170 new_eeginfo = cur_eeg.EEGinfo;
0171
0172
0173
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
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
0192
0193
0194
0195
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
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
0230
0231
0232
0233