0001 function [EEGfile, EEGinfo] = vb_read_bdf_save_ch_bin(bdf_file, ...
0002 figure_info, eeg_file, output_dir, device, eeginfo_version, 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
0067
0068
0069
0070
0071
0072 tic;
0073
0074 vb_define_device;
0075
0076
0077 if ~exist('bdf_file', 'var'), bdf_file = []; end
0078 if ~exist('figure_info', 'var'), figure_info = []; end
0079 if ~exist('eeg_file', 'var'), eeg_file = []; end
0080 if ~exist('output_dir', 'var'), output_dir = []; end
0081 if ~exist('device', 'var'), device = []; end
0082 if ~exist('eeginfo_version', 'var'), eeginfo_version = []; end
0083 if ~exist('verbose_swt', 'var'), verbose_swt = []; end
0084
0085 [bdf_file, figure_info, eeg_file, output_dir, device, eeginfo_version, ...
0086 VERBOSE] = inner_check_arguments(bdf_file, figure_info, ...
0087 eeg_file, output_dir, device, eeginfo_version, verbose_swt);
0088
0089
0090 SensorPosition = figure_info.SensorPosition;
0091 CoordType = figure_info.CoordType;
0092 ChannelName_coordinate = figure_info.ChannelLabel;
0093
0094 [DAT, header, RecordTime, Nch, SampleRate, data_type, ChannelName_hardware] ...
0095 = inner_get_local_var_bdf_file(bdf_file);
0096
0097 [channel_label, match_sensor_idx] = ...
0098 inner_rearrange_channel_label(ChannelName_coordinate, ChannelName_hardware);
0099
0100
0101
0102 func_ = mfilename;
0103
0104
0105 [sid, data_type_list] = inner_make_ch_file_id( ...
0106 channel_label, output_dir, data_type, VERBOSE);
0107
0108
0109
0110
0111 inner_write_channel_data(sid, DAT, RecordTime, Nch, data_type_list, VERBOSE);
0112
0113 if VERBOSE
0114 fprintf('\n--- record time: %d [sec], sample rate:%d[Hz]\n', ...
0115 RecordTime, SampleRate);
0116 end
0117
0118
0119 EEGinfo.Measurement = 'EEG';
0120 EEGinfo.Device = device;
0121 EEGinfo.ChannelID = match_sensor_idx;
0122 EEGinfo.ChannelName = channel_label;
0123 EEGinfo.Nchannel = Nch;
0124 EEGinfo.SampleFrequency = SampleRate;
0125 EEGinfo.Nsample = RecordTime * SampleRate;
0126 EEGinfo.DataType = data_type_list;
0127 EEGinfo = vb_eeginfo_set_sensor_position(EEGinfo, SensorPosition);
0128 EEGinfo.CoordType = CoordType;
0129 EEGinfo.Vcenter = figure_info.Vcenter;
0130 EEGinfo.Vradius = figure_info.Vradius;
0131
0132
0133 EEGinfo.File.DataFile = bdf_file;
0134 EEGinfo.File.OutputDir = output_dir;
0135 EEGinfo.File.EEG_File = eeg_file;
0136
0137
0138 Trial.number = 1;
0139 Trial.sample = 1:EEGinfo.Nsample;
0140 EEGinfo.Trial = Trial;
0141
0142
0143 EEGinfo = vb_eeginfo_set_reference(EEGinfo, EEGINFO_REFERENCE_COMMON);
0144 EEGinfo = vb_eeginfo_set_rectime(EEGinfo, RecordTime);
0145 EEGinfo = vb_eeginfo_set_version(EEGinfo, eeginfo_version);
0146 EEGinfo = vb_eeginfo_set_header(EEGinfo, header);
0147
0148
0149 history = ...
0150 sprintf('%s(''%s'', ''%s'', ''%s'')', func_, bdf_file, device, eeg_file);
0151 EEGinfo = vb_eeginfo_add_history(EEGinfo, history);
0152 EEGfile = eeg_file;
0153
0154
0155 if VERBOSE, fprintf('--- now making EEG-MAT file(%s) ...', EEGfile); end
0156
0157 Measurement = 'EEG';
0158 vb_fsave(EEGfile, 'EEGinfo', 'Measurement');
0159
0160 if VERBOSE, fprintf(' done!\n'); end
0161 if VERBOSE, fprintf('=== READ BDF (%f[sec])=== \n', toc); end
0162
0163
0164
0165
0166
0167
0168
0169
0170 function [bdf_file, figure_info, eeg_file, output_dir, ...
0171 device, eeginfo_version, verbose_swt] = ...
0172 inner_check_arguments(bdf_file, figure_info, eeg_file, output_dir, ...
0173 device, eeginfo_version, verbose_swt)
0174 func_ = mfilename;
0175
0176 vb_define_device;
0177
0178
0179 if isempty(bdf_file)
0180 error('(%s)bdf_file is a required parameter', func_);
0181 end
0182
0183 if exist(bdf_file, 'file') ~= 2
0184 error('(%s)cannot read bdf_file : %s', func_, bdf_file);
0185 end
0186
0187
0188 if isempty(figure_info)
0189 error('(%s)figure_info is a required parameter', func_);
0190 end
0191
0192 if ~isfield(figure_info, 'SensorPosition')
0193 error('(%s)figure_info must have SensorPosition field', func_);
0194 end
0195
0196 if ~isfield(figure_info, 'CoordType')
0197 error('(%s)figure_info must have CoordType field', func_);
0198 end
0199
0200 if ~isfield(figure_info, 'ChannelLabel')
0201 error('(%s)figure_info must have ChannelLabel field', func_);
0202 end
0203
0204 if ~isfield(figure_info, 'Vcenter')
0205 error('(%s)figure_info must have Vcenter field', func_);
0206 end
0207
0208
0209
0210 if isempty(eeg_file)
0211 error('(%s)eeg_file is a required parameter', func_);
0212 end
0213
0214 [eeg_file_dir] = vb_get_file_parts(eeg_file);
0215 if ~isempty(eeg_file_dir) && exist(eeg_file_dir, 'dir') ~= 7
0216 vb_mkdir(eeg_file_dir);
0217 end
0218
0219 if isempty(output_dir)
0220 output_dir = './';
0221 end
0222
0223 if exist(output_dir, 'dir') ~= 7
0224 vb_mkdir(output_dir);
0225 end
0226
0227 if isempty(device)
0228 device = 'BIOSEMI';
0229 end
0230
0231 if isempty(eeginfo_version)
0232 eeginfo_version = vb_latest_version('EEGinfo');
0233 end
0234
0235 if isempty(verbose_swt)
0236 verbose_swt = true;
0237 end
0238 return;
0239
0240
0241
0242
0243
0244 function [DAT, header, RecordTime, Nch, SampleRate, data_type, ...
0245 ChannelLabel_hardware] = inner_get_local_var_bdf_file(bdf_file)
0246
0247 vb_define_device;
0248
0249 [DAT,H1] = vb_openbdf(bdf_file);
0250
0251
0252 header = DAT.Head;
0253 RecordTime = DAT.Head.NRec;
0254 Nch = DAT.Head.NS;
0255 SampleRate = max(DAT.Head.SampleRate);
0256 data_type = STANDARD_BIN_DATA_TYPE;
0257
0258
0259 ChannelLabel_hardware = cell(1,Nch);
0260
0261
0262 for ch = 1:Nch
0263
0264 ChannelLabel_hardware{ch} = deblank(DAT.Head.Label(ch,:));
0265 end
0266
0267
0268
0269
0270
0271 function [channel_label, match_sensor_idx] = ...
0272 inner_rearrange_channel_label(ChannelLabel_coordinate, ChannelLabel_hardware)
0273 func_ = mfilename;
0274
0275 vb_define_device;
0276
0277
0278 ChannelLabel_coordinate = vb_util_arrange_list(ChannelLabel_coordinate);
0279 ChannelLabel_hardware = vb_util_arrange_list(ChannelLabel_hardware);
0280
0281
0282 [idx_list] = ...
0283 vb_util_get_cross_checked_list(ChannelLabel_hardware, ChannelLabel_coordinate);
0284
0285 match_sensor_idx = find(idx_list(:,2) ~= -1);
0286 NChannelCoord = length(ChannelLabel_coordinate);
0287 NMatchChannel = length(match_sensor_idx);
0288 if size(NChannelCoord > NMatchChannel)
0289
0290 fprintf('--- (%s)number of channel : coordinate:%d, match:%d\n', ...
0291 func_, NChannelCoord, NMatchChannel);
0292 end
0293
0294 channel_label = ChannelLabel_hardware;
0295 return;
0296
0297
0298
0299
0300
0301 function [sid, data_type_list] = ...
0302 inner_make_ch_file_id(channel_label, output_dir, data_type, VERBOSE)
0303
0304 func_ = mfilename;
0305
0306 vb_define_device;
0307
0308 Nch = size(channel_label,1);
0309
0310 DataFile = cell(Nch,1);
0311 sid = zeros(Nch,1);
0312 data_type_list = cell(Nch,1);
0313
0314
0315 common_data_type = data_type;
0316 status_data_type = STATUS_CH_DATA_TYPE;
0317 status_channel_label = STATUS_CH_LABEL;
0318
0319 for ch = 1:Nch
0320 ch_name = channel_label{ch};
0321
0322 DataFile{ch} = sprintf('%s%s%s%s', ...
0323 output_dir, filesep, ch_name, FILE_EXT_EEG_BIN_CH);
0324 sid(ch) = fopen(DataFile{ch},'w');
0325
0326 if sid(ch) < 0
0327 error('(%s)cannot open : %s', func_, DataFile{ch});
0328 end
0329
0330
0331 if strcmp(ch_name, status_channel_label) == 1
0332 data_type_list{ch} = status_data_type;
0333 else
0334 data_type_list{ch} = common_data_type;
0335 end
0336
0337 if VERBOSE, fprintf(' >>> open %s\n', DataFile{ch}); end
0338 end
0339 return;
0340
0341
0342
0343
0344
0345 function inner_write_channel_data(sid, DAT, RecordTime, Nch, data_type, VERBOSE)
0346
0347 if VERBOSE
0348 PLOT_EVERY = 30;
0349 fprintf('--- now reading and storing BDF data (plot every %d/%d [sec])', ...
0350 PLOT_EVERY, RecordTime);
0351 end
0352
0353 ReadMode = 0;
0354
0355 for r = 1:RecordTime
0356 [DAT,DataBuf] = vb_readbdf(DAT, r, ReadMode);
0357
0358 for ch = 1:Nch
0359 fwrite(sid(ch), DataBuf(:,ch), data_type{ch});
0360 end
0361 if VERBOSE && rem(r,PLOT_EVERY) == 0, fprintf('.'); end
0362 end
0363 if VERBOSE, fprintf('\n'); end
0364
0365 for ch=1:Nch
0366 fclose(sid(ch));
0367 end
0368 return;
0369
0370
0371
0372
0373
0374