Home > functions > device > eeg > biosemi > vb_read_bdf_save_ch_bin.m

vb_read_bdf_save_ch_bin

PURPOSE ^

read BDF file and make initial EEGinfo data and file

SYNOPSIS ^

function [EEGfile, EEGinfo] = vb_read_bdf_save_ch_bin(bdf_file,figure_info, eeg_file, output_dir, device, eeginfo_version, verbose_swt)

DESCRIPTION ^

 read BDF file and make initial EEGinfo data and file

 [usage]
   [EEGfile, EEGinfo] = vb_read_bdf_save_ch_bin(bdf_file, figure_info, ...
     eeg_file, output_dir, device, eeginfo_version, verbose_swt)

 [input]
          bdf_file : <required> <<file>> BDF file
       figure_info : <required> <<struct>> information to complete BDF file
                   :  .SensorPosition : [N x 3]
                   :  .CoordType      : coordinate type e.g. 'SPM_Right_m' etc.
                   :  .ChannelLabel   : cell array of string {N x 1}
                   :  .Vcenter        : center coordinate [1 x 3]
                   :  .Vradius        : spherical radius  [x1]
          eeg_file : <required> <<file>> output EEG-MAT file with directory
        output_dir : <optional> output directory of binary data ['./']
            device : <optional> device name ['BIOSEMI']
   eeginfo_version : <optional> verson of EEGinfo struct [vb_latest_version()]
                   :  - usually unused
       verbose_swt : <optional> <<boolean>> [true]
                   :  switch for output verbose message

 [output]
   EEGfile : EEG-MAT file with its relative path
   EEGinfo : <struct> eeg data structure
     : .Measurement           : measurement type 'EEG'
     : .File                  : <<struct>> file information in/out
     :   .DataFile            : usually input BDF file
     :   .OutputDir           : output directory for binary files
     :   .EEG_File            : EEG-MAT file which will be made
     : .ChannelName : channel name list {Nch x 1 cell}
     : .ChannelID   : channel index except extra channels
     : .Nchannel              : number of channel
     : .SampleFrequency       : sample rate of original data [Hz]
     : .Nsample               : total number of sample
     :                        :  --> SampleFrequency * RecordTime
     : .DataType              : list of type of binary data (e.g. 'float32')
     :                        :  {Nch x 1 cell}
     : .Coord                 : position coordinates of sensor [NallCh x 3]
     : .CoodType              : coordinate type e.g. 'SPM_Right_m'
     : .Device                : device name e.g. 'BIOSEMI'
     : .Vcenter               : center of spherical head [1 x 3]
     : .Vradius               : radius of spherical head [x1]
     : .Trial                 : <<struct>> trial information
     :   .number              : trial index [x1]
     :   .sample              : sample list [Nsample x 1]
     --- device dependent data
     : .device_info
         .Reference           : how to refer
         .RecordTime          : record time of original data [sec]
         .Header              : <<struct>> Biosemi header
         .Version             : version of EEGinfo - unused
         .History             : history of EEGinfo

 [note]
   See also
     vb_latest_version, vb_mkdir, vb_eeginfo_add_history
     vb_openbdf, vb_readbdf

 [history]
   2006-12-21 (Sako) initial version
   2007-04-13 (Sako) got precise information of BIOSEMI's channel label
   2007-08-10 (Sako) added sensor index to EEGinfo
   2007-08-22 (Sako) modified not to be necessary digit and face file
   2008-02-14 (Sako) thoroughly revised
   2011-05-27 (Sako) modified according to the new data format

 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 [EEGfile, EEGinfo] = vb_read_bdf_save_ch_bin(bdf_file, ...
0002   figure_info, eeg_file, output_dir, device, eeginfo_version, verbose_swt)
0003 % read BDF file and make initial EEGinfo data and file
0004 %
0005 % [usage]
0006 %   [EEGfile, EEGinfo] = vb_read_bdf_save_ch_bin(bdf_file, figure_info, ...
0007 %     eeg_file, output_dir, device, eeginfo_version, verbose_swt)
0008 %
0009 % [input]
0010 %          bdf_file : <required> <<file>> BDF file
0011 %       figure_info : <required> <<struct>> information to complete BDF file
0012 %                   :  .SensorPosition : [N x 3]
0013 %                   :  .CoordType      : coordinate type e.g. 'SPM_Right_m' etc.
0014 %                   :  .ChannelLabel   : cell array of string {N x 1}
0015 %                   :  .Vcenter        : center coordinate [1 x 3]
0016 %                   :  .Vradius        : spherical radius  [x1]
0017 %          eeg_file : <required> <<file>> output EEG-MAT file with directory
0018 %        output_dir : <optional> output directory of binary data ['./']
0019 %            device : <optional> device name ['BIOSEMI']
0020 %   eeginfo_version : <optional> verson of EEGinfo struct [vb_latest_version()]
0021 %                   :  - usually unused
0022 %       verbose_swt : <optional> <<boolean>> [true]
0023 %                   :  switch for output verbose message
0024 %
0025 % [output]
0026 %   EEGfile : EEG-MAT file with its relative path
0027 %   EEGinfo : <struct> eeg data structure
0028 %     : .Measurement           : measurement type 'EEG'
0029 %     : .File                  : <<struct>> file information in/out
0030 %     :   .DataFile            : usually input BDF file
0031 %     :   .OutputDir           : output directory for binary files
0032 %     :   .EEG_File            : EEG-MAT file which will be made
0033 %     : .ChannelName : channel name list {Nch x 1 cell}
0034 %     : .ChannelID   : channel index except extra channels
0035 %     : .Nchannel              : number of channel
0036 %     : .SampleFrequency       : sample rate of original data [Hz]
0037 %     : .Nsample               : total number of sample
0038 %     :                        :  --> SampleFrequency * RecordTime
0039 %     : .DataType              : list of type of binary data (e.g. 'float32')
0040 %     :                        :  {Nch x 1 cell}
0041 %     : .Coord                 : position coordinates of sensor [NallCh x 3]
0042 %     : .CoodType              : coordinate type e.g. 'SPM_Right_m'
0043 %     : .Device                : device name e.g. 'BIOSEMI'
0044 %     : .Vcenter               : center of spherical head [1 x 3]
0045 %     : .Vradius               : radius of spherical head [x1]
0046 %     : .Trial                 : <<struct>> trial information
0047 %     :   .number              : trial index [x1]
0048 %     :   .sample              : sample list [Nsample x 1]
0049 %     --- device dependent data
0050 %     : .device_info
0051 %         .Reference           : how to refer
0052 %         .RecordTime          : record time of original data [sec]
0053 %         .Header              : <<struct>> Biosemi header
0054 %         .Version             : version of EEGinfo - unused
0055 %         .History             : history of EEGinfo
0056 %
0057 % [note]
0058 %   See also
0059 %     vb_latest_version, vb_mkdir, vb_eeginfo_add_history
0060 %     vb_openbdf, vb_readbdf
0061 %
0062 % [history]
0063 %   2006-12-21 (Sako) initial version
0064 %   2007-04-13 (Sako) got precise information of BIOSEMI's channel label
0065 %   2007-08-10 (Sako) added sensor index to EEGinfo
0066 %   2007-08-22 (Sako) modified not to be necessary digit and face file
0067 %   2008-02-14 (Sako) thoroughly revised
0068 %   2011-05-27 (Sako) modified according to the new data format
0069 %
0070 % Copyright (C) 2011, ATR All Rights Reserved.
0071 % License : New BSD License(see VBMEG_LICENSE.txt)
0072 tic;
0073 
0074 vb_define_device;
0075 
0076 % --- CHECK ARGUMENT --- %
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 % --- PREPARATION --- %
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 % --- MAIN PROCEDURE --------------------------------------------------------- %
0101 %
0102 func_ = mfilename;
0103 
0104 % get file id of each channel data
0105 [sid, data_type_list] = inner_make_ch_file_id( ...
0106   channel_label, output_dir, data_type, VERBOSE);
0107 
0108 %
0109 % --- make binary data files
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 % make return values
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   % files
0133 EEGinfo.File.DataFile    = bdf_file;
0134 EEGinfo.File.OutputDir   = output_dir;
0135 EEGinfo.File.EEG_File    = eeg_file;
0136 
0137   % trial
0138 Trial.number             = 1;
0139 Trial.sample             = 1:EEGinfo.Nsample;
0140 EEGinfo.Trial            = Trial;
0141 
0142 % --- device dependent data
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 % ----- record history
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 % measurement information
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 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0164 
0165 
0166 % --- INNER FUNCTIONS -------------------------------------------------------- %
0167 %
0168 % --- inner_check_arguments()
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 % --- BDF FILE
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 % --- FIGURE INFO
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 % --- OUTPUT FILE / OUTPUT DIRECTORY
0209 %     -- if directories do not exist, create them here
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 % --- end of inner_check_arguments()
0241 
0242 % --- inner_get_local_var_bdf_file()
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 % procedure parameters
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 % data_size  = STANDARD_BIN_DATA_SIZE;
0258 
0259 ChannelLabel_hardware = cell(1,Nch);
0260 
0261 % get channel label for hardware
0262 for ch = 1:Nch
0263 %   EEGinfo.ChannelLabel  = DAT.Head.Label;
0264   ChannelLabel_hardware{ch} = deblank(DAT.Head.Label(ch,:));
0265 end
0266 %
0267 % --- end of inner_get_local_var_bdf_file()
0268 
0269 % --- inner_rearrange_channel_label()
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 % reset label size to [n_channel x 1]
0278 ChannelLabel_coordinate = vb_util_arrange_list(ChannelLabel_coordinate);
0279 ChannelLabel_hardware = vb_util_arrange_list(ChannelLabel_hardware);
0280 
0281 % unmatch check
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 %   fprintf('--- (%s)there are some unmatch sensor channels\n', func_);
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 % --- end of inner_rearrange_channel_label()
0298 
0299 % --- inner_make_ch_file_id()
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 % support 'Status' channel
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   % data type
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 % --- end of inner_make_ch_file_id()
0342 
0343 % --- inner_write_channel_data()
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 % --- end of inner_write_channel_data()
0371 %
0372 % --- END OF INNER FUNCTIONS ------------------------------------------------- %
0373 
0374 % --- END OF FILE --- %

Generated on Tue 27-Aug-2013 11:46:04 by m2html © 2005