0001 function [new_EEGinfo, exp_data, read_ch_info] = vb_read_ch_data_eeg( ...
0002 old_EEGinfo, to_read_swt, ch_list, time_from, time_to, verbose_swt)
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
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066 vb_define_device;
0067
0068
0069 if ~exist('old_EEGinfo', 'var'), old_EEGinfo = []; end
0070 if ~exist('to_read_swt', 'var'), to_read_swt = []; end
0071 if ~exist('ch_list', 'var'), ch_list = []; end
0072 if ~exist('time_from', 'var'), time_from = []; end
0073 if ~exist('time_to', 'var'), time_to = []; end
0074 if ~exist('verbose_swt', 'var'), verbose_swt = []; end
0075
0076 [old_EEGinfo, to_read_swt, ch_list, time_from, time_to, VERBOSE,...
0077 sample_from, sample_to] = inner_check_argument( ...
0078 old_EEGinfo, to_read_swt, ch_list, time_from, time_to, verbose_swt);
0079
0080
0081 [EDF, file_base, file_ext, Nch, SR, RT, data_type, data_size] = ...
0082 inner_get_local_var(old_EEGinfo);
0083
0084
0085 eeg_labels = vb_eeginfo_get_channel_label(old_EEGinfo);
0086
0087
0088 channel_list = vb_util_get_labels_to_read(eeg_labels, ch_list, to_read_swt);
0089 if isempty(channel_list), error('cannot make channel list'); end
0090
0091
0092
0093 func_ = mfilename;
0094
0095
0096 channel_list = vb_util_arrange_list(channel_list);
0097
0098 new_Nchannel = size(channel_list,1);
0099 data_len = (sample_to + 1) - sample_from;
0100
0101 exp_data = zeros(new_Nchannel, data_len);
0102
0103 new_EEGinfo = old_EEGinfo;
0104 new_idx = [];
0105
0106 common_data_type = data_type;
0107 status_data_type = STATUS_CH_DATA_TYPE;
0108 status_channel_label = STATUS_CH_LABEL;
0109
0110 for ch = 1:new_Nchannel
0111 file_path = sprintf('%s/%s%s', file_base, channel_list{ch}, file_ext);
0112
0113 if exist(file_path, 'file') ~= 2
0114 error('(%s)cannot find file : %s', func_, file_path);
0115 end
0116
0117 fid = fopen(file_path, 'r');
0118 if fid < 0
0119 error('(%s)cannot open : %s', func_, file_path);
0120 end
0121
0122 if strcmp(channel_list{ch}, status_channel_label) == 1
0123 status_ch = true;
0124 else
0125 status_ch = false;
0126 end
0127
0128
0129 p_begin = (sample_from-1) * data_size;
0130 fseek(fid, p_begin, 'bof');
0131
0132
0133 if status_ch
0134 cur_data_type = status_data_type;
0135 else
0136 cur_data_type = common_data_type;
0137 end
0138
0139
0140 this_eeg = (fread(fid, data_len, cur_data_type{ch}))';
0141
0142 idx = vb_util_get_matching_label_index(eeg_labels, channel_list{ch});
0143 new_idx = [new_idx idx];
0144
0145
0146 if status_ch
0147 exp_data(ch,:) = this_eeg;
0148 else
0149 exp_data(ch,:) = this_eeg * EDF.Cal(idx) + EDF.Off(idx);
0150 end
0151
0152 channel_info.label = channel_list{ch};
0153 channel_info.sample_begin = sample_from;
0154 channel_info.sample_end = sample_to;
0155
0156 read_ch_info(ch) = channel_info;
0157
0158 fclose(fid);
0159
0160 if VERBOSE
0161
0162 fprintf(' -- read channel: ''%s''\n', channel_list{ch});
0163 fprintf(' -- target sample: (%d -> %d)\n', sample_from, sample_to);
0164 fprintf(' ------------------------------------------------------\n');
0165
0166
0167 msg = sprintf(' -- read channel: ''%s''', channel_list{ch});
0168 msg = sprintf('%s\n -- target sample: (%d -> %d)', ...
0169 msg, sample_from, sample_to);
0170 msg = sprintf('%s\n ----------------------------------------', msg);
0171 vb_disp(msg);
0172 end
0173 end
0174
0175
0176 new_EEGinfo = inner_update_EEGinfo(new_EEGinfo, ...
0177 new_idx, sample_from, sample_to, data_len, SR, channel_list, new_Nchannel);
0178
0179
0180
0181
0182
0183
0184
0185
0186 function [old_EEGinfo, to_read_swt, ch_list, time_from, time_to, VERBOSE, ...
0187 sample_from, sample_to] = inner_check_argument( ...
0188 old_EEGinfo, to_read_swt, ch_list, time_from, time_to, verbose_swt)
0189
0190 vb_define_device;
0191
0192
0193 if isempty(old_EEGinfo)
0194 error('old_EEGinfo is a required parameter');
0195 else
0196
0197 if ~isfield(old_EEGinfo, 'File')
0198 error('old_EEGinfo must have %s field', 'FILE');
0199 end
0200 if ~isfield(old_EEGinfo, 'Device')
0201 error('old_EEGinfo must have %s field', 'Device');
0202 end
0203 if ~isfield(old_EEGinfo, 'Nchannel')
0204 error('old_EEGinfo must have %s field', 'Nchannel');
0205 end
0206 if ~isfield(old_EEGinfo, 'SampleFrequency')
0207 error('old_EEGinfo must have %s field', 'SampleFrequency');
0208 end
0209 if ~isfield(old_EEGinfo, 'Nsample')
0210 error('old_EEGinfo must have %s field', 'Nsample');
0211 end
0212 if ~isfield(old_EEGinfo, 'DataType')
0213 error('old_EEGinfo must have %s field', 'DataType');
0214 end
0215
0216
0217 if ~isfield(old_EEGinfo, 'device_info')
0218 error('old_EEGinfo must have %s field', 'device_info');
0219 else
0220 if ~isfield(old_EEGinfo.device_info, 'Header')
0221 error('old_EEGinfo.device_info must have %s field', 'Header');
0222 end
0223 if ~isfield(old_EEGinfo.device_info, 'RecordTime')
0224 error('old_EEGinfo.device_info must have %s field', 'RecordTime');
0225 end
0226 end
0227 end
0228
0229 if isempty(to_read_swt)
0230 to_read_swt = false;
0231 end
0232
0233 if isempty(time_from)
0234 sample_from = 1;
0235 elseif ~isnumeric(time_from)
0236 error('[time_from] must be numeric');
0237 end;
0238
0239 if isempty(time_to)
0240 sample_to = old_EEGinfo.Nsample;
0241 elseif ~isnumeric(time_to)
0242 error('[time_to] must be numeric');
0243 end;
0244
0245 SR = old_EEGinfo.SampleFrequency;
0246
0247 if ~exist('sample_from', 'var')
0248 sample_from = round(time_from * SR/1000);
0249 end
0250 if ~exist('sample_to', 'var')
0251 sample_to = round(time_to * SR/1000);
0252 end
0253
0254 if sample_from < 0 || old_EEGinfo.Nsample < sample_from
0255 error('time_from %d[msec] is out of range (Nsample:%d)', ...
0256 time_from, old_EEGinfo.Nsample);
0257 end
0258
0259 if sample_to < sample_from || old_EEGinfo.Nsample < sample_to
0260 error('time_to %d[msec] is out of range (time_from:%d,Nsample:%d)', ...
0261 time_to, time_from, old_EEGinfo.Nsample);
0262 end
0263
0264 if isempty(verbose_swt)
0265 verbose_swt = true;
0266 end
0267
0268 if verbose_swt == true
0269 VERBOSE = true;
0270 else
0271 VERBOSE = false;
0272 end
0273
0274
0275
0276
0277
0278 function EEGinfo = inner_update_EEGinfo(EEGinfo, ...
0279 new_idx, sample_from, sample_to, data_len, SR, ch_list, new_Nchannel);
0280
0281 vb_define_device;
0282
0283 EEGinfo.Nsample = (sample_to + 1) - sample_from;
0284 EEGinfo.Nchannel = new_Nchannel;
0285 old_SensorPosition = vb_eeginfo_get_sensor_position(EEGinfo);
0286 pos_idx = find(size(old_SensorPosition,1) >= new_idx);
0287 EEGinfo = vb_eeginfo_set_sensor_position( ...
0288 EEGinfo, old_SensorPosition(pos_idx,:));
0289
0290 EEGinfo = vb_eeginfo_set_rectime(EEGinfo, (data_len / SR));
0291
0292
0293 ch_str = [];
0294 for n_ch = 1:size(ch_list,1)
0295 if isempty(ch_str)
0296 ch_str = sprintf('%s', ch_list{n_ch});
0297 else
0298 ch_str = sprintf('%s,%s', ch_str, ch_list{n_ch});
0299 end
0300 end
0301
0302 history = ...
0303 sprintf('vb_read_eeg_ch_data(sample{%d, %d}, ch:%s)', ...
0304 sample_from, sample_to, ch_str);
0305 EEGinfo = vb_eeginfo_add_history(EEGinfo, history);
0306
0307
0308
0309
0310
0311 function [EDF, file_base, file_ext, Nch, SR, RT, data_type, data_size] = ...
0312 inner_get_local_var(old_EEGinfo)
0313
0314
0315 EDF = vb_eeginfo_get_header(old_EEGinfo);
0316 if ~isfield(EDF,'Cal'), error('EDF must have %s field', 'Cal'); end
0317 if ~isfield(EDF,'Off'), error('EDF must have %s field', 'Off'); end
0318
0319
0320 f = old_EEGinfo.File;
0321 file_base = [f.OutputDir '/' f.DataDir];
0322 file_ext = '.ch.eeg.dat';
0323 Nch = old_EEGinfo.Nchannel;
0324 SR = old_EEGinfo.SampleFrequency;
0325 RT = vb_eeginfo_get_rectime(old_EEGinfo);
0326 data_type = old_EEGinfo.DataType;
0327 data_size = 4;
0328
0329
0330
0331