Home > bioelectromagnetism > avg_open.m

avg_open

PURPOSE ^

avg_open - function to handle various eeg_load commands

SYNOPSIS ^

function [p] = avg_open(p,parent)

DESCRIPTION ^

 avg_open - function to handle various eeg_load commands
 
 Usage: [p] = avg_open(p,[parentgui])
 
 p is a parameter structure. See eeg_toolbox_defaults for more 
 information on this parameter structure.
 
 In this function, p must contain the fields:
 
 p.volt.path - the directory location of the file to load
 p.volt.file - the name of the file to load
 p.volt.type - the file format string, one of:
 
 'ASCII'
 'EMSE'
 'Scan4x'
 'Scan3x'
 'Matlab'

 These are the only AVG file types currently supported. 
 See functions eeg_load* for details.
 
 The most important return value is the ERP data in 
 p.volt.data.  If the file format is scan4x, various 
 ERP parameters are returned also.
 
 If the ASCII or Matlab type is given, the routine will try 
 to load an associated variance file.  This file must be 
 located in the same path, with the same file name as the 
 voltage file, but the file extension should be '.var'
 
 See also: EEG_LOAD, EEG_LOAD_ASCII, EMSE_READ_AVG,
           EEG_LOAD_SCAN4_AVG, EEG_LOAD_SCAN3_AVG

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [p] = avg_open(p,parent)
0002 
0003 % avg_open - function to handle various eeg_load commands
0004 %
0005 % Usage: [p] = avg_open(p,[parentgui])
0006 %
0007 % p is a parameter structure. See eeg_toolbox_defaults for more
0008 % information on this parameter structure.
0009 %
0010 % In this function, p must contain the fields:
0011 %
0012 % p.volt.path - the directory location of the file to load
0013 % p.volt.file - the name of the file to load
0014 % p.volt.type - the file format string, one of:
0015 %
0016 % 'ASCII'
0017 % 'EMSE'
0018 % 'Scan4x'
0019 % 'Scan3x'
0020 % 'Matlab'
0021 %
0022 % These are the only AVG file types currently supported.
0023 % See functions eeg_load* for details.
0024 %
0025 % The most important return value is the ERP data in
0026 % p.volt.data.  If the file format is scan4x, various
0027 % ERP parameters are returned also.
0028 %
0029 % If the ASCII or Matlab type is given, the routine will try
0030 % to load an associated variance file.  This file must be
0031 % located in the same path, with the same file name as the
0032 % voltage file, but the file extension should be '.var'
0033 %
0034 % See also: EEG_LOAD, EEG_LOAD_ASCII, EMSE_READ_AVG,
0035 %           EEG_LOAD_SCAN4_AVG, EEG_LOAD_SCAN3_AVG
0036 %
0037 
0038 % $Revision: 1.1 $ $Date: 2004/11/12 01:30:25 $
0039 
0040 % Licence:  GNU GPL, no express or implied warranties
0041 % History:  02/2002, Darren.Weber_at_radiology.ucsf.edu
0042 %           04/2002, Darren.Weber_at_radiology.ucsf.edu
0043 %                    added variance handling
0044 %           08/2002, Darren.Weber_at_radiology.ucsf.edu
0045 %                    added EMSE avg handling
0046 %                    added interpolation of zero point option
0047 %
0048 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0049 
0050 if ~exist('p','var'),[p] = eeg_toolbox_defaults; end
0051 
0052 eegversion = '$Revision: 1.1 $';
0053 fprintf('AVG_OPEN [v %s]\n',eegversion(11:15)); tic;
0054 
0055 
0056 [path,name,ext] = fileparts(strcat(p.volt.path, filesep, p.volt.file));
0057 file = fullfile(path,[name ext]);
0058 
0059 if ~isequal(exist(file),2),
0060   lookfile = which(file);
0061   if isempty(lookfile),
0062     msg = sprintf('...cannot locate %s\n', file);
0063     error(msg);
0064   else
0065     file = lookfile;
0066   end
0067 end
0068 
0069 type = lower(p.volt.type);
0070 
0071 switch type,
0072   
0073   case 'ascii',
0074     
0075     [ p.volt.data, p.volt.var ] = eeg_load_ascii(file);
0076     
0077   case 'emse',
0078     
0079     avg = emse_read_avg(file);
0080     
0081     % load variance data?
0082     if isfield(avg,'volt'),
0083       if isempty(avg.volt),
0084         error('failed to read file.');
0085       end
0086     else
0087       error('failed to read file.');
0088     end
0089     
0090     p.volt.data          = avg.volt;
0091     %p.volt.var           = avg.variance;
0092     %p.volt.channelNames  = avg.chan_names;
0093     p.volt.channels      = avg.channels;
0094     p.volt.points        = avg.pnts;
0095     p.volt.sampleMsec    = avg.rate;
0096     p.volt.epochStart    = avg.xmin;
0097     %p.volt.epochEnd      = avg.xmax;
0098     p.volt.sampleHz      = 1000 / avg.rate;
0099     %p.volt.sweeps        = avg.nsweeps;
0100     
0101     
0102   case 'scan4x',
0103     
0104     avg = eeg_load_scan4_avg(file);
0105     
0106     if ~isfield(avg,'data'),
0107       msg = sprintf('...failed to load scan4.x avg file:\n... %s\n',file);
0108       error(msg);
0109     end
0110     if isequal([avg.header.domain],1),
0111       msg = sprintf('...cannot open frequency domain file:\n... %s\n',file);
0112       error(msg);
0113     end
0114     
0115     p.volt.points = avg.header.pnts;
0116     p.volt.sampleHz = avg.header.rate;
0117     p.volt.sampleMsec = 1000/ avg.header.rate;
0118     p.volt.channels = avg.header.nchannels;
0119     p.volt.epochStart = avg.header.xmin * 1000; % convert to msec
0120     p.volt.epochEnd   = avg.header.xmax * 1000; % convert to msec
0121     p.volt.sweeps = avg.header.acceptcnt;
0122     
0123     ampData = [avg.data.samples]; % elect in col, samples in row
0124     baseline = repmat([avg.electloc.baseline],p.volt.points,1);
0125     calibration = repmat([avg.electloc.calib],p.volt.points,1);
0126     n = repmat([avg.electloc.n],p.volt.points,1);
0127     % Convert to uV
0128     p.volt.data = ( ampData - baseline ) .* calibration ./ n;
0129     p.volt.var = [avg.variance.samples];
0130     
0131     
0132   case 'scan3x',
0133     
0134     avg = eeg_load_scan3_avg(file);
0135     
0136     if isfield(avg,'signal'),
0137       p.volt.data          = avg.signal;
0138       p.volt.var           = avg.variance;
0139       p.volt.channelNames  = avg.chan_names;
0140       p.volt.points        = avg.pnts;
0141       p.volt.sampleHz      = avg.rate;
0142       p.volt.epochStart    = avg.xmin;
0143       p.volt.epochEnd      = avg.xmax;
0144       p.volt.sampleMsec    = avg.rate / 1000;
0145       p.volt.sweeps        = avg.nsweeps;
0146     else
0147       msg = sprintf('...failed to load scan3.x datafile: %s\n',file);
0148       error(msg);
0149     end
0150     
0151   case 'matlab',
0152     
0153     p.volt.data = eeg_load(file);
0154     % Attempt to load associated variance file
0155     varfile = fullfile(path,strcat(name,'.var'));
0156     if isequal(exist(varfile),2),
0157       p.volt.var = eeg_load(varfile);
0158     else
0159       lookfile = which(varfile);
0160       if isempty(lookfile),
0161         fprintf('...cannot locate Matlab variance file:\n... %s\n', varfile);
0162       else
0163         varfile = lookfile;
0164         p.volt.var = eeg_load(varfile);
0165       end
0166     end
0167     
0168   otherwise,
0169     msg = sprintf('\nPlease specify voltage data type: ASCII | EMSE | Scan4x | Scan3x | Matlab?\n\n');
0170     error(msg);
0171 end
0172 
0173 % Try to arrange electrodes in columns (assuming more sample points than electrodes)
0174 s = size(p.volt.data);
0175 if s(1) < s(2),
0176   fprintf('...rotating voltage data from %s : ',[name ext]);
0177   p.volt.data = p.volt.data';
0178   s = size(p.volt.data);
0179   fprintf('%d rows, %d cols\n', s(1), s(2));
0180 end
0181 p.volt.channels = s(2);
0182 p.volt.points = s(1);
0183 
0184 s = size(p.volt.var);
0185 if s(1) < s(2),
0186   if isequal(p.volt.type,'ASCII'), file = varfile; end
0187   if isequal(p.volt.type,'Matlab'), file = varfile; end
0188   fprintf('...rotating variance data from:\n... %s : ',varfile);
0189   p.volt.var = p.volt.var';
0190   s = size(p.volt.var);
0191   fprintf('%d rows, %d cols\n', s(1), s(2));
0192 end
0193 
0194 
0195 % Verify that essential ERP parameters are set
0196 if isempty(p.volt.sampleHz) | isempty(p.volt.epochStart) | isempty(p.volt.epochEnd),
0197   
0198   if exist('parent','var'),
0199     if ~isempty(parent),
0200       %help = helpdlg('Please specify ERP parameters as follows...','EEG OPEN HELP');
0201       %movegui(help,'center'); waitfor(help);
0202       
0203       data = get(parent,'UserData');
0204       data.p = p;
0205       set(parent,'UserData',data);
0206       
0207       tmpgui = gui_eeg_ascii_parameters(parent);
0208       
0209       data = get(parent,'UserData');
0210      [p] = data.p; 
0211       clear data parent tmpgui;
0212     end
0213   else
0214     fprintf('...please specify ERP parameters in p structure.\n');
0215   end
0216 end
0217 
0218 
0219 % --- Setup data structures for timing array
0220 
0221 % Interpolate the Zero value
0222 if p.volt.interpZero,
0223   
0224   if p.volt.epochStart & p.volt.epochEnd & p.volt.sampleMsec,
0225     
0226     if mod(p.volt.epochStart,fix(p.volt.epochStart)) < .1,
0227       % round the epochStart value, if its remainder is within .1 msec
0228       p.volt.epochStart = fix(p.volt.epochStart);
0229     end
0230     
0231     p.volt.timeArray = [p.volt.epochStart:p.volt.sampleMsec:p.volt.epochEnd]';
0232     
0233     timeNonZero = find(p.volt.timeArray);
0234     timeZero = find(p.volt.timeArray == 0);
0235     
0236     volt = p.volt.data;
0237     
0238     InterpZero = interp1( p.volt.timeArray(timeNonZero), volt, 0, 'cubic' );
0239     volt = [volt(1:timeZero-1,:); InterpZero; volt(timeZero:end,:)];
0240     p.volt.data = volt; 
0241     
0242     clear InterpZero timeNonZero timeZero volt;
0243   end
0244 else
0245   p.volt.timeArray = [1:p.volt.points]';
0246   if and(p.volt.epochStart,p.volt.sampleMsec),
0247     t = p.volt.epochStart;
0248     for i=1:p.volt.points,
0249       p.volt.timeArray(i,1) = t;
0250       t = t + p.volt.sampleMsec;
0251     end
0252   end   
0253 end
0254 
0255 p.volt.points = size(p.volt.data,1);
0256 
0257 t = toc; fprintf('...done (%6.2f sec).\n\n',t);
0258 
0259 return

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