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

vb_read_ch_data_eeg

PURPOSE ^

read experiment data from channel binary data file

SYNOPSIS ^

function [new_EEGinfo, exp_data, read_ch_info] = vb_read_ch_data_eeg(old_EEGinfo, to_read_swt, ch_list, time_from, time_to, verbose_swt)

DESCRIPTION ^

 read experiment data from channel binary data file
 [usage]
   [new_EEGinfo, exp_data, read_ch_info] = vb_read_ch_data_eeg( ...
     old_EEGinfo, output_path, ...
     to_read_swt, ch_list, time_from, time_to, verbose_swt)
 [input]
    old_EEGinfo : <required> <<struct>> EEGinfo
      : struct that is output from "vb_read_bdf_save_ch_bin"
      : EEGinfo.Measurement           : measurement type 'EEG'
      : EEGinfo.Device                : device name 'BIOSEMI'
      : EEGinfo.File.namebase         : base name of channel data file
      : EEGinfo.File.ext              : extension of channel data file
      : EEGinfo.File.labeltype        : 'HARDWARE' or 'SOFTWARE'
      : EEGinfo.Nchannel              : number of channel of original data
      : EEGinfo.SampleFrequency       : sample rate of original data [Hz]
      : EEGinfo.Nsample               : total number of sample
      :                               :  SampleFrequency * RecordTime
      : EEGinfo.DataType              : type of binary data (e.g. 'float32')
      : EEGinfo.Coord                 : position coordinates of sensor
      :                               : [n_sensor x 3]
      : EEGinfo.CoodType              : coordinates' information

      : EEGinfo.device_info           : device dependent data
                                      :   TransInfo
                                      :   Reference
                                      :   RecordTime
                                      :   Header
                                      :   Version
                                      :   History

    to_read_swt : <optional> (true or false) purpose of "ch_list". [false]
                :   true : ch_list is to read
                :  false : ch_list is to omit
        ch_list : <conditionally optional>
                :   list of channel label that you want to get
                :   if this "ch_list" is empty and ...
                :     1) if "to_read_swt" is true ---> error
                :     2) if "to_read_swt" is false --> read every channel
      time_from : <optional> [msec]
                :   beginning time that you want to get [0] (>=0)
        time_to : <optional> [msec]
                :   end of time that you want to get [end of data]
                :   (>(time_from))
    verbose_swt : <optional> switch to output verbose message [true]
 [output]
    new_EEGinfo : new information struct data
       exp_data : experiment data ([Nchannel x Nsample x Ntrial])
                :   -- converted to phisical value by
                :      [exp_data = eeg * EDF.Cal + EDF.Off]
                :   -- (EDF = EEGinfo.header)
 [note]
   @see vb_eeginfo_add_history
   @see vb_util_arrange_list get_channel_idx
   *** This function is DEPRECATED due to its incomplete check. ***
 [history]
   2006-12-21 (Sako) initial version
   2011-05-30 (Sako) modified according to the new data format
   2011-07-29 (Sako) replaced searching index function

 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 [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 % read experiment data from channel binary data file
0004 % [usage]
0005 %   [new_EEGinfo, exp_data, read_ch_info] = vb_read_ch_data_eeg( ...
0006 %     old_EEGinfo, output_path, ...
0007 %     to_read_swt, ch_list, time_from, time_to, verbose_swt)
0008 % [input]
0009 %    old_EEGinfo : <required> <<struct>> EEGinfo
0010 %      : struct that is output from "vb_read_bdf_save_ch_bin"
0011 %      : EEGinfo.Measurement           : measurement type 'EEG'
0012 %      : EEGinfo.Device                : device name 'BIOSEMI'
0013 %      : EEGinfo.File.namebase         : base name of channel data file
0014 %      : EEGinfo.File.ext              : extension of channel data file
0015 %      : EEGinfo.File.labeltype        : 'HARDWARE' or 'SOFTWARE'
0016 %      : EEGinfo.Nchannel              : number of channel of original data
0017 %      : EEGinfo.SampleFrequency       : sample rate of original data [Hz]
0018 %      : EEGinfo.Nsample               : total number of sample
0019 %      :                               :  SampleFrequency * RecordTime
0020 %      : EEGinfo.DataType              : type of binary data (e.g. 'float32')
0021 %      : EEGinfo.Coord                 : position coordinates of sensor
0022 %      :                               : [n_sensor x 3]
0023 %      : EEGinfo.CoodType              : coordinates' information
0024 %
0025 %      : EEGinfo.device_info           : device dependent data
0026 %                                      :   TransInfo
0027 %                                      :   Reference
0028 %                                      :   RecordTime
0029 %                                      :   Header
0030 %                                      :   Version
0031 %                                      :   History
0032 %
0033 %    to_read_swt : <optional> (true or false) purpose of "ch_list". [false]
0034 %                :   true : ch_list is to read
0035 %                :  false : ch_list is to omit
0036 %        ch_list : <conditionally optional>
0037 %                :   list of channel label that you want to get
0038 %                :   if this "ch_list" is empty and ...
0039 %                :     1) if "to_read_swt" is true ---> error
0040 %                :     2) if "to_read_swt" is false --> read every channel
0041 %      time_from : <optional> [msec]
0042 %                :   beginning time that you want to get [0] (>=0)
0043 %        time_to : <optional> [msec]
0044 %                :   end of time that you want to get [end of data]
0045 %                :   (>(time_from))
0046 %    verbose_swt : <optional> switch to output verbose message [true]
0047 % [output]
0048 %    new_EEGinfo : new information struct data
0049 %       exp_data : experiment data ([Nchannel x Nsample x Ntrial])
0050 %                :   -- converted to phisical value by
0051 %                :      [exp_data = eeg * EDF.Cal + EDF.Off]
0052 %                :   -- (EDF = EEGinfo.header)
0053 % [note]
0054 %   @see vb_eeginfo_add_history
0055 %   @see vb_util_arrange_list get_channel_idx
0056 %   *** This function is DEPRECATED due to its incomplete check. ***
0057 % [history]
0058 %   2006-12-21 (Sako) initial version
0059 %   2011-05-30 (Sako) modified according to the new data format
0060 %   2011-07-29 (Sako) replaced searching index function
0061 %
0062 % Copyright (C) 2011, ATR All Rights Reserved.
0063 % License : New BSD License(see VBMEG_LICENSE.txt)
0064 
0065 % load common definitions for biosemi functions of vbmeg
0066 vb_define_device;
0067 
0068 % --- CHECK ARGUMENTS --- %
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 % get local variables
0081 [EDF, file_base, file_ext, Nch, SR, RT, data_type, data_size] = ...
0082   inner_get_local_var(old_EEGinfo);
0083 
0084 % list of channel label as baseline
0085 eeg_labels = vb_eeginfo_get_channel_label(old_EEGinfo);
0086 
0087 % get list of label to read
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 % --- MAIN PROCEDURE --------------------------------------------------------- %
0092 %
0093 func_ = mfilename;
0094 
0095 % set channel_list [N x 1]
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   % starting pointer
0129   p_begin = (sample_from-1) * data_size;
0130   fseek(fid, p_begin, 'bof');
0131 
0132   % data type
0133   if status_ch
0134     cur_data_type = status_data_type;
0135   else
0136     cur_data_type = common_data_type;
0137   end
0138   
0139 %   this_eeg = (fread(fid, data_len, data_type))';
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   % convert to phisical value
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 % update EEGinfo
0176 new_EEGinfo = inner_update_EEGinfo(new_EEGinfo, ...
0177   new_idx, sample_from, sample_to, data_len, SR, channel_list, new_Nchannel);
0178 %
0179 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0180 
0181 
0182 % --- INNER FUNCTIONS -------------------------------------------------------- %
0183 %
0184 % --- inner_check_argument()
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 % required arguments
0193 if isempty(old_EEGinfo)
0194   error('old_EEGinfo is a required parameter');
0195 else
0196   % required fields
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   % --- device dependent data
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 % --- end of inner_check_argument()
0275 
0276 % --- inner_update_EEGinfo()
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 % history
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 % --- end of inner_update_EEGinfo()
0308 
0309 % --- inner_get_local_var()
0310 %
0311 function [EDF, file_base, file_ext, Nch, SR, RT, data_type, data_size] = ...
0312   inner_get_local_var(old_EEGinfo)
0313 
0314 % prepare EDF
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 % get local variables from EEGinfo
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; % [byte] (float32)
0328 %
0329 % --- end of inner_get_local_var()
0330 
0331 % --- END OF FILE --- %

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