Home > bioelectromagnetism > eeg_load_scan_eeg.m

eeg_load_scan_eeg

PURPOSE ^

eeg_load_scan_eeg - Load Neuroscan .EEG format

SYNOPSIS ^

function [signal, accept, typeeeg, rt, response, chan_names, pnts, nsweeps, rate, xmin, xmax]=loadeeg( FILENAME, chanlist, TrialList, typerange, acceptype, rtrange, responsetype)

DESCRIPTION ^

 eeg_load_scan_eeg - Load Neuroscan .EEG format
 
 Usage: [signal, accept, typeeeg, rt, response, chan_names, pnts, nsweeps, rate, xmin, xmax]=loadeeg( FILENAME, chanlist, TrialList, typerange, acceptype, rtrange, responsetype)

      FILENAME     input Neuroscan .avg file      
      signal        output signal    
      variance     variance of the signal 
      chan_names   array that represent the name of the electrodes

      i.e. 
      [signal] = loadeeg( 'test.eeg' );     % load data into the array named 'signal'
         plot( signal(1,:) );              % plot the signal for the first electrode of the first sweep

      data are organised into an array of Number_of_electrode x (Number_of_points_per_trial*Number_of_sweeps)
      for a file with 32 electrodes, 700 points per trial and 300 sweeps, the resulting array is 
      of 32 collumn and 700*300 rows (300 consecutive blocs of 700 points)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [signal, accept, typeeeg, rt, response, chan_names, pnts, nsweeps, rate, xmin, xmax]=loadeeg( FILENAME, chanlist, TrialList, typerange, acceptype, rtrange, responsetype)
0002 % eeg_load_scan_eeg - Load Neuroscan .EEG format
0003 %
0004 % Usage: [signal, accept, typeeeg, rt, response, chan_names, pnts, nsweeps, rate, xmin, xmax]=loadeeg( FILENAME, chanlist, TrialList, typerange, acceptype, rtrange, responsetype)
0005 %
0006 %      FILENAME     input Neuroscan .avg file
0007 %      signal        output signal
0008 %      variance     variance of the signal
0009 %      chan_names   array that represent the name of the electrodes
0010 %
0011 %      i.e.
0012 %      [signal] = loadeeg( 'test.eeg' );     % load data into the array named 'signal'
0013 %         plot( signal(1,:) );              % plot the signal for the first electrode of the first sweep
0014 %
0015 %      data are organised into an array of Number_of_electrode x (Number_of_points_per_trial*Number_of_sweeps)
0016 %      for a file with 32 electrodes, 700 points per trial and 300 sweeps, the resulting array is
0017 %      of 32 collumn and 700*300 rows (300 consecutive blocs of 700 points)
0018 %
0019 
0020 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:33 $
0021 
0022 % Licence:  GNU GPL, no implied or express warranty
0023 % History:  01/2001, arno_delorme@salk.edu
0024 %    1062001        0.0        primitive version
0025 %    1102001        1.0        fully working version
0026 %    1112001        1.1        more parameters
0027 %    1152001        1.2        fix bugs
0028 %
0029 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0030 
0031 if nargin<1 
0032     fprintf('Not enought arguments\n'); 
0033     help loadeeg 
0034     return;
0035 end;
0036 if nargin<2 CHAN='all'; end;
0037 if nargin<3 TrialList='all'; end;
0038 if nargin<4 typerange='all'; end;
0039 if nargin<5 acceptype='all'; end;
0040 if nargin<6 rtrange  ='all'; end;
0041 if nargin<7 responsetype='all'; end;
0042 
0043 % open file for reading
0044 % ---------------------
0045 fid=fopen(FILENAME,'r','ieee-le');
0046 if fid<0
0047     fprintf(2,['Error LOADEEG: File ' FILENAME ' not found\n']);  
0048     return;
0049 end;
0050 
0051 % read general part of the erp header and set variables
0052 % -----------------------------------------------------
0053 erp = fread(fid, 362, 'uchar');    % skip the firsts 368 bytes
0054 nsweeps = fread(fid, 1, 'ushort');    % number of sweeps
0055 erp = fread(fid, 4, 'uchar');     % skip 4 bytes
0056 pnts= fread(fid, 1, 'ushort');    % number of point per waveform
0057 chan= fread(fid, 1, 'ushort');  % number of channels
0058 erp = fread(fid, 4, 'uchar');     % skip 4 bytes
0059 rate= fread(fid, 1, 'ushort');  % sample rate (Hz)
0060 erp = fread(fid, 127, 'uchar');    % skip 125 bytes
0061 xmin= fread(fid, 1, 'float32'); % in s
0062 xmax= fread(fid, 1, 'float32'); % in s
0063 erp = fread(fid, 387, 'uchar');    % skip 387 bytes
0064 
0065 fprintf('number of channels         : %d\n', chan);
0066 fprintf('number of points per trial : %d\n', pnts);
0067 fprintf('sampling rate (Hz)         : %f\n', rate);
0068 fprintf('xmin (s)                   : %f\n', xmin);
0069 fprintf('xmax (s)                   : %f\n', xmax);
0070 
0071 % read electrode configuration
0072 % ----------------------------
0073 fprintf('Electrode configuration\n');
0074 for elec = 1:chan
0075        channel_label_tmp = fread(fid, 10, 'uchar');
0076     chan_names(elec,:) = channel_label_tmp';
0077     for index = 2:9 if chan_names(elec,index) == 0 chan_names(elec,index)=' '; end; end;
0078     erp = fread(fid, 47-10, 'uchar');
0079     baseline(elec) = fread(fid, 1, 'ushort');
0080     erp = fread(fid, 10, 'uchar');
0081     sensitivity(elec) = fread(fid, 1, 'float32');
0082     erp = fread(fid, 8, 'uchar');
0083     calib(elec) = fread(fid, 1, 'float32');
0084     fprintf('%s: baseline: %d\tsensitivity: %f\tcalibration: %f\n', chan_names(elec,1:4), baseline(elec), sensitivity(elec), calib(elec));
0085     factor(elec) = calib(elec) * sensitivity(elec) / 204.8;
0086 end;
0087 %fprintf('Electrode configuration\n');
0088 %for elec = 1:chan
0089 %    erp = fread(fid, 47, 'uchar');
0090 %    baseline(elec) = fread(fid, 1, 'ushort');
0091 %    erp = fread(fid, 10, 'uchar');
0092 %    sensitivity(elec) = fread(fid, 1, 'float32');
0093 %    erp = fread(fid, 8, 'uchar');
0094 %    calib(elec) = fread(fid, 1, 'float32');
0095 %    fprintf('baseline: %d\tsensitivity: %f\tcalibration: %f\n', baseline(elec), sensitivity(elec), calib(elec));
0096 %    factor(elec) = calib(elec) * sensitivity(elec) / 204.8;
0097 %end;
0098 
0099 xsize    = chan * pnts;
0100 buf_size = chan * pnts ;            % size in shorts
0101 
0102 % set tags for conditions
0103 % -----------------------
0104 if size(chanlist)  == size('all')    chanlist = [1:chan]; end;
0105 if size(TrialList) == size('all')    trialtagI     = 1; else trialtagI     = 0; end;
0106 if size(acceptype) == size('all')    acceptagI     = 1; else acceptagI     = 0; end;
0107 if size(typerange) == size('all')    typetagI      = 1; else typetagI      = 0; end;
0108 if size(responsetype) == size('all')    responsetagI  = 1; else responsetagI  = 0; end;
0109 if size(rtrange)      == size('all')    rttagI        = 1; else rttagI        = 0; end;
0110 
0111 count_selected = 1;
0112 fprintf('Reserving array (can take some time)\n');
0113 signal = zeros( chan, pnts*nsweeps);
0114 fprintf('Array reserved, scanning file\n');
0115 
0116 for sweep = 1:nsweeps
0117 
0118     % read sweeps header
0119     % ------------------
0120     s_accept   = fread(fid, 1, 'uchar');
0121     s_type     = fread(fid, 1, 'ushort');
0122     s_correct  = fread(fid, 1, 'ushort');
0123     s_rt       = fread(fid, 1, 'float32');
0124     s_response = fread(fid, 1, 'ushort');
0125     s_reserved = fread(fid, 1, 'ushort');
0126 
0127     unreaded_buf = 1;
0128 
0129     % store the sweep or reject the sweep
0130     % -----------------------------------
0131     if trialtagI trialtag = 1;        else trialtag = ismember(sweep, TrialList); end;
0132     if acceptagI acceptag = 1;        else acceptag =  ismember(s_accept, acceptype); end;
0133     if typetagI  typetag  = 1;       else typetag  =  ismember(s_type, typerange); end;
0134     if responsetagI responsetag  = 1; else responsetag  = ismember(s_response, responsetype); end;
0135     if rttagI       rttag  = 1;       else rttag  =  ismember(s_rt, rtrange); end;
0136 
0137     if typetag
0138         if trialtag
0139             if acceptag
0140                 if responsetag
0141                     if rttag
0142 
0143                         buf = fread(fid, [chan pnts], 'short');
0144                         unreaded_buf = 0;
0145 
0146                         % copy information to array
0147                         % -------------------------
0148                         accept(count_selected)   = s_accept;
0149                         typeeeg(count_selected)  = s_type;
0150                         rt(count_selected)       = s_rt;
0151                         response(count_selected) = s_response;
0152         
0153                         % demultiplex the data buffer and convert to microvolts
0154                         % -----------------------------------------------------
0155                         for elec = 1:chan
0156                             buf(elec, :) = (buf(elec, :)-baseline(elec)-0.0)*factor(elec);
0157                         end;
0158                         signal(:,[((count_selected-1)*pnts+1):count_selected*pnts]) = buf;
0159                         count_selected = count_selected + 1;
0160                         if not(mod(count_selected,10)) fprintf('%d sweeps selected out of %d\n', count_selected-1, sweep); end;
0161                     end;
0162                 end;
0163             end;
0164         end;
0165     end;
0166 
0167     if unreaded_buf fseek(fid, buf_size*2, 'cof'); end;                
0168 end;
0169 nsweeps = count_selected-1;
0170 fclose(fid);
0171 
0172 % restrincting array
0173 % ---------------------------------------
0174 fprintf('rereservation of variables\n');
0175 signal = signal(chanlist, 1:(count_selected-1)*pnts);
0176 chan_names = chan_names(chanlist,:);
0177 
0178 return;
0179 
0180 
0181 
0182 
0183 % Frequency domain EEG File format
0184 %
0185 % The frequency domain epoched EEG format has
0186 % the same header as that  described in the appendix
0187 % of the SCAN manual (see the file sethead.h on the
0188 % download page). For each sweep of data, there is a
0189 % sweep header that is identical to that described
0190 % on page Headers-5.
0191 %
0192 % At this point there is a difference.  After the sweep
0193 % header, the frequency domain data for each sweep has
0194 % the following format:
0195 %
0196 % for each channel (erp.nchannels):
0197 %
0198 % for each frequency bin (erp.pnts):
0199 %
0200 % real value of the FFT stored as a 4-byte float;
0201 %
0202 % for each frequency bin (erp.pnts):
0203 %
0204 % imaginary value of the FFT stored as a 4-byte float;
0205 %
0206 % The data have been scaled to microvolts prior to the FFT,
0207 % and it is these results which are stored to the file.

Generated on Mon 15-Aug-2005 15:36:19 by m2html © 2003