eeg_load_scan3_avg - Read a NeuroScan 3.x AVG File USEAGE: [avg] = eeg_load_scan3_avg(FILENAME) FILENAME input Neuroscan .avg file (version 3.x) avg output data structure, with fields: avg.signal - ERP signal (uV, Npnts x Mchan) avg.variance - variance of the signal (Npnts x Mchan) avg.chan_names - electrode labels avg.pnts - number of points in ERP waveform avg.rate - sample rate (Hz) avg.xmin - prestimulus epoch start (e.g., -100 msec) avg.xmax - poststimulus epoch end (e.g., 900 msec) avg.nsweeps - number of accepted trials/sweeps in avg e.g. avg = eeg_load_scan3_avg( 'test.avg' ); plot( avg.signal );
0001 function [avg] = eeg_load_scan3_avg(FILENAME) 0002 0003 % eeg_load_scan3_avg - Read a NeuroScan 3.x AVG File 0004 % 0005 % USEAGE: [avg] = eeg_load_scan3_avg(FILENAME) 0006 % 0007 % FILENAME input Neuroscan .avg file (version 3.x) 0008 % avg output data structure, with fields: 0009 % 0010 % avg.signal - ERP signal (uV, Npnts x Mchan) 0011 % avg.variance - variance of the signal (Npnts x Mchan) 0012 % avg.chan_names - electrode labels 0013 % avg.pnts - number of points in ERP waveform 0014 % avg.rate - sample rate (Hz) 0015 % avg.xmin - prestimulus epoch start (e.g., -100 msec) 0016 % avg.xmax - poststimulus epoch end (e.g., 900 msec) 0017 % avg.nsweeps - number of accepted trials/sweeps in avg 0018 % 0019 % e.g. 0020 % avg = eeg_load_scan3_avg( 'test.avg' ); 0021 % plot( avg.signal ); 0022 % 0023 0024 0025 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:33 $ 0026 0027 0028 % This program is distributed under the GNU GPL; you can redistribute 0029 % it and/or modify it. This program is distributed in the hope that it 0030 % will be useful, but WITHOUT ANY WARRANTY; without even the implied 0031 % warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 0032 0033 % Version 1.1, arno_delorme@salk.edu 0034 % Version 1.2, Darren.Weber_at_radiology.ucsf.edu 0035 % 0036 % Average data is stored as 4-byte floats in vectored format for each 0037 % channel. Each channel has a 5-byte header that is no longer used. Thus, 0038 % after the main file header, there is an unused 5-byte header followed by 0039 % erp.pnts of 4-byte floating point numbers for the first channel; then a 0040 % 5-byte header for channel two followed by erp.pnts*sizeof(float) bytes, 0041 % etc. Therefore, the total number of bytes after the main header is: 0042 % erp.nchannels * (5 + erp.pnts*sizeof(float)). To scale a data point to 0043 % microvolts, multiply by the channel-specific calibration factor (i.e., for 0044 % electrode j: channel[j]->calib) and divide by the number of sweeps in the 0045 % average (i.e., channel[j]->n). 0046 0047 % UPDATE version remark 0048 % 1062001 0.1 primitive version based on a C program 0049 % 1092001 1.0 fully working version based on loadegg.m that I programmed 0050 % 1102001 1.1 adding channel names loading 0051 % 03/2002 1.2 Darren.Weber_at_radiology.ucsf.edu 0052 % - S_nsweeps_offset from 362 to 364; 0053 % so that it finds ACCEPTED not TOTAL SWEEPS, which 0054 % has a great impact on conversion to uV. 0055 % - modified xmin/xmax to msec from sec. 0056 % - modified output to structure with fields 0057 % - Changed the name of the function (from loadavg). 0058 % 0059 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0060 0061 if nargin<1, 0062 help eeg_load_scan3_avg; return; 0063 end; 0064 0065 fid = fopen(FILENAME,'r','ieee-le'); 0066 0067 if fid<0, 0068 msg = sprintf('EEG_LOAD_SCAN3_AVG: Cannot find:\n... %s\n', FILENAME); 0069 error(msg); 0070 end; 0071 0072 BOOL='int16'; 0073 ULONG='int32'; 0074 FLOAT='float32'; 0075 0076 % read # of channels, # of samples, variance flag, and time bounds 0077 % ---------------------------------------------------------------- 0078 S_nsweeps_offset = 364; % Darren Weber modified this from 362 0079 S_pnts_offset = 368; 0080 S_nchans_offset = 370; 0081 S_variance_offset = 375; 0082 S_rate_offset = 376; 0083 S_xmin_offset = 505; 0084 S_xmax_offset = 509; 0085 packed_sizeof_SETUP = 900; 0086 0087 fseek(fid, S_nsweeps_offset, 'bof'); avg.nsweeps = fread(fid, 1, 'ushort'); 0088 fseek(fid, S_pnts_offset, 'bof'); avg.pnts = fread(fid, 1, 'ushort'); 0089 fseek(fid, S_nchans_offset, 'bof'); chan = fread(fid, 1, 'ushort'); 0090 fseek(fid, S_variance_offset, 'bof'); variance_flag = fread(fid, 1, 'uchar'); 0091 fseek(fid, S_rate_offset, 'bof'); avg.rate = fread(fid, 1, 'ushort'); 0092 fseek(fid, S_xmin_offset, 'bof'); avg.xmin = fread(fid, 1, 'float32') * 1000; 0093 fseek(fid, S_xmax_offset, 'bof'); avg.xmax = fread(fid, 1, 'float32') * 1000; 0094 fseek(fid, packed_sizeof_SETUP, 'bof'); 0095 0096 fprintf('number of channels : %d\n', chan); 0097 fprintf('number of points : %d\n', avg.pnts); 0098 fprintf('sampling rate (Hz) : %f\n', avg.rate); 0099 fprintf('xmin (msec) : %f\n', avg.xmin); 0100 fprintf('xmax (msec) : %f\n', avg.xmax); 0101 fprintf('Accepted sweeps : %d\n', avg.nsweeps); 0102 0103 % read electrode configuration 0104 % ---------------------------- 0105 fprintf('Electrode configuration\n'); 0106 for elec = 1:chan, 0107 channel_label_tmp = fread(fid, 10, 'uchar'); 0108 avg.chan_names(elec,:) = channel_label_tmp'; 0109 for index = 2:9, 0110 if avg.chan_names(elec,index) == 0, 0111 avg.chan_names(elec,index) = ' '; 0112 end; 0113 end; 0114 erp = fread(fid, 47-10, 'uchar'); 0115 baseline(elec) = fread(fid, 1, 'ushort'); 0116 erp = fread(fid, 10, 'uchar'); 0117 sensitivity(elec) = fread(fid, 1, 'float32'); 0118 erp = fread(fid, 8, 'uchar'); 0119 calib(elec) = fread(fid, 1, 'float32'); 0120 fprintf('%s: baseline: %d\tsensitivity: %f\tcalibration: %f\n', avg.chan_names(elec,1:4), baseline(elec), sensitivity(elec), calib(elec)); 0121 factor(elec) = calib(elec) * sensitivity(elec) / 204.8; 0122 end; 0123 0124 % Read signal data (amplifier units) 0125 signal = zeros(avg.pnts, chan); 0126 for elec = 1:chan, 0127 fseek(fid, 5, 'cof'); % skip sweeps header 0128 signal(:, elec) = fread(fid, avg.pnts, 'float32'); 0129 end; 0130 0131 if variance_flag, 0132 variance = zeros(avg.pnts, chan); 0133 for elec = 1:chan, 0134 variance(:, elec) = fread(fid, avg.pnts, 'float32'); 0135 end; 0136 avg.variance = variance'; 0137 else 0138 avg.variance = []; 0139 end; 0140 0141 % Convert signal to microvolts 0142 baseline = repmat(baseline,avg.pnts,1); 0143 calib = repmat(calib, avg.pnts,1); 0144 0145 if avg.nsweeps, 0146 signal = (signal - baseline) .* calib ./ avg.nsweeps; 0147 else 0148 signal = (signal - baseline) .* calib; 0149 end 0150 0151 avg.signal = signal'; 0152 0153 fclose(fid); 0154 return;