Home > eeg > egis > net_utils > raw_eeg_csdm.m

raw_eeg_csdm

PURPOSE ^

[Epoch,NEpoch_used,Nbad_chan,bad_chan,avgcsdm] = raw_eeg_csdm(rawfname,

SYNOPSIS ^

function [Epoch,NEpoch_used,Nbad_chan,bad_chan,avgcsdm] = raw_eeg_csdm(rawfname,add_bad_chan,windows,reference,Epoch,NEpoch,Nbad_chan,bad_chan,mask);

DESCRIPTION ^

[Epoch,NEpoch_used,Nbad_chan,bad_chan,avgcsdm] = raw_eeg_csdm(rawfname,
add_bad_chan,windows,reference,Epoch,NEpoch,Nbad_chan,bad_chan,mask);

this routine calculates the cross spectral density function.  It can be either
 be called serially after dealing with artifact editing, or else itself grab a% preexisting edit code file.   

input arguments

 rawfname = NSFragger format raw data file
 add_bad_chan = bad_channels to add (optional), e.g. [8 7 92]
 
 windows = windows in units of epoch seconds to use in the 
NSFRagger file starting at the 
beginning of the file, e.g., [1:6 100:152 170:191] specifies 82 seconds to
be included in the calculation starting at the beginning of the file, if an
Epoch length of 1 second is requested, either during editing or within this
call.  If Epoch =2 this choice of windows implies 164 seconds to be used in
the average. This parameter defaults to the entire file (optional). 
 
 reference = reference to use.  The valid types of references are found in
the m file new_reference.m If desired more should be added there. e.g., 
'average' to indicate the average reference, defaults to average reference 
(optional) 'laplacian' can be used as can 'cortical05', 'cortical10', etc. 
where the number indicates the level of smoothing

 the remaining parameters are only used if the edit code information was 
 calculated in the pevious call and is sitting in memory. These parameters 
 are from the '.mask' header.  If you are reading it internally to this       
 m-file do not provide those parameters.  
 (see read_mask.m)
      
output arguments

 Epoch = epoch_length in seconds
 NEpoch = actual number of epochs used in spectral calculations
 NBad_chan = actual number of bad_channels
 bad_chan = updated bad channel list
 avgcsdm = cross spectral density function

 writtin by R.S. V.1.0

 Note: this is being provided to you for your research
 please do not redistribute without my permission

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [Epoch,NEpoch_used,Nbad_chan,bad_chan,avgcsdm] = raw_eeg_csdm(rawfname,add_bad_chan,windows,reference,Epoch,NEpoch,Nbad_chan,bad_chan,mask);
0002 %[Epoch,NEpoch_used,Nbad_chan,bad_chan,avgcsdm] = raw_eeg_csdm(rawfname,
0003 %add_bad_chan,windows,reference,Epoch,NEpoch,Nbad_chan,bad_chan,mask);
0004 %
0005 %this routine calculates the cross spectral density function.  It can be either
0006 % be called serially after dealing with artifact editing, or else itself grab a% preexisting edit code file.
0007 %
0008 %input arguments
0009 %
0010 % rawfname = NSFragger format raw data file
0011 % add_bad_chan = bad_channels to add (optional), e.g. [8 7 92]
0012 %
0013 % windows = windows in units of epoch seconds to use in the
0014 %NSFRagger file starting at the
0015 %beginning of the file, e.g., [1:6 100:152 170:191] specifies 82 seconds to
0016 %be included in the calculation starting at the beginning of the file, if an
0017 %Epoch length of 1 second is requested, either during editing or within this
0018 %call.  If Epoch =2 this choice of windows implies 164 seconds to be used in
0019 %the average. This parameter defaults to the entire file (optional).
0020 %
0021 % reference = reference to use.  The valid types of references are found in
0022 %the m file new_reference.m If desired more should be added there. e.g.,
0023 %'average' to indicate the average reference, defaults to average reference
0024 %(optional) 'laplacian' can be used as can 'cortical05', 'cortical10', etc.
0025 %where the number indicates the level of smoothing
0026 %
0027 % the remaining parameters are only used if the edit code information was
0028 % calculated in the pevious call and is sitting in memory. These parameters
0029 % are from the '.mask' header.  If you are reading it internally to this
0030 % m-file do not provide those parameters.
0031 % (see read_mask.m)
0032 %
0033 %output arguments
0034 %
0035 % Epoch = epoch_length in seconds
0036 % NEpoch = actual number of epochs used in spectral calculations
0037 % NBad_chan = actual number of bad_channels
0038 % bad_chan = updated bad channel list
0039 % avgcsdm = cross spectral density function
0040 %
0041 % writtin by R.S. V.1.0
0042 %
0043 % Note: this is being provided to you for your research
0044 % please do not redistribute without my permission
0045 %
0046 if nargout ~= 5
0047     error('too many or too little output arguments')
0048 end; 
0049 if nargin < 1
0050     error('filename not specified');
0051 end;
0052 if isempty(rawfname)
0053     [fid, rawfname] = get_fid('rb');
0054     fclose(fid)
0055     fid = fopen(rawfname,'rb','b');
0056     if nargin <= 4
0057             [Epoch,NEpoch,Max_microv,Min_Chan,Nbad_chan,bad_chan,mask] = read_mask(rawfname);
0058     end;        
0059 
0060 else
0061     if (nargin <= 4)
0062     fid = fopen(rawfname,'rb','b');
0063     if (fid < 0)
0064         error('error opening data file')
0065         fclose('all')
0066     end;
0067     [Epoch,NEpoch,Max_microv,Min_Chan,Nbad_chan,bad_chan,mask] = read_mask(rawfname);
0068     else
0069     fid = fopen(rawfname,'rb','b');
0070     if (fid < 0)
0071         error('error opening data file')
0072         fclose('all')
0073     end
0074     end;
0075 end;    
0076 if ~(nargin ==1 | nargin ==2|nargin == 3| nargin ==4|nargin == 9)
0077     error('inproper number of input arguments')
0078 end;
0079 if nargin == 1
0080     add_bad_chan = [];
0081     windows = [1:size(mask,1)];
0082     reference = [];
0083 end;
0084 if nargin == 2
0085     windows = [1:size(mask,1)];
0086     reference = [];
0087 end;
0088 if nargin == 3
0089     reference = [];
0090 end;
0091 if isempty(windows)
0092     windows = [1:size(mask,1)];
0093 end;
0094 bad_chan = [bad_chan add_bad_chan];
0095 Nbad_chan = Nbad_chan + size(add_bad_chan,2);
0096 mask(:,bad_chan) = zeros(size(mask,1),size(bad_chan,2));
0097 mask_win = zeros(size(mask,1),size(mask,2));
0098 if ~isempty(windows)
0099     mask_win(windows,:) = ones(size(windows,2),size(mask,2));
0100 end;
0101 mask = mask.*mask_win;
0102 [header_array, EventCodes,Samp_Rate, NChan, scale, NSamp, NEvent] = rd_fragger_hdr(fid);
0103 avgcsdm = zeros(Samp_Rate*Epoch/2,(NChan+2)*(NChan + 1)/2);
0104 if strcmp(reference,'laplacian')
0105     [xelec,yelec,zelec] = electrodes(NChan+1);
0106 end;
0107 if strcmp(reference(1:2),'co')
0108     [xelec,yelec,zelec] = electrodes(NChan+1);
0109     sources = xyz2tp(xelec,yelec,zelec);
0110     A = transfer_matrix(500,0.2,80,8,8.2,8.7,9.2,7.8,sources,sources);
0111 end;
0112 for i = 1:NEpoch
0113 if i <= size(mask,1)        
0114     if sum(mask(i,:)) > 1
0115         trialdata = fread(fid,[NChan+NEvent,Samp_Rate*Epoch],'integer*2');
0116         trialdata2 = trialdata(1:NChan,:)';
0117         trialdata2 = trialdata2*scale;
0118         if ~isempty(reference)
0119             if ~(strcmp(reference,'laplacian')|strcmp(reference(1:2),'co'))
0120             [ref_trialdata,masknew] = new_reference(trialdata2,mask(i,:),reference);
0121             ref_trialdata = ref_trialdata.*(ones(size(ref_trialdata,1),1)*masknew);
0122             elseif strcmp(reference,'laplacian')
0123             good_chan = find(mask(i,:));    
0124             ref_trialdata = laplacian_trial([trialdata2 zeros(size(trialdata2,1),1)],good_chan,xelec,yelec,zelec);
0125             masknew = mask(i,:);
0126             elseif strcmp(reference(1:8),'cortical')                    good_chan = find(mask(i,:));
0127             good_trial = zeros(size(trialdata2,1),size(good_chan,2));
0128             trialdata3 = average_reference(trialdata2(:,1:NChan),mask(i,:));
0129             good_trial = trialdata3(:,good_chan);
0130             good_A = zeros(size(good_chan,2),size(good_chan,2));
0131             good_A = A(good_chan,good_chan);
0132             ref_trialdata = zeros(size(trialdata3,1),size(trialdata3,2));
0133             sigma_m = 1;
0134             sigma_v = str2num(reference(9:10));
0135             
0136             [m,deviations] = bayes_dipole_trial(good_A,good_trial',sigma_v,sigma_m);
0137             ref_trialdata(:,good_chan) = m';
0138             masknew = mask(i,:);
0139             end
0140         else
0141             ref_trialdata = average_reference(trialdata2,mask(i,:));
0142         end;
0143         
0144         avgcsdm = avgcsdm + csdm(ref_trialdata);
0145         mask(i,:) = masknew;
0146     else
0147         skip_bytes = (NChan+NEvent)*Samp_Rate*Epoch*2;
0148         fseek(fid,skip_bytes,'cof');
0149         
0150     end;
0151     
0152 end;
0153 end;
0154 num_good_trials = sum(mask);
0155 icount = 1;
0156 good_cross = zeros(1,size(avgcsdm,2));
0157 for ichan = 1:NChan+1
0158     for jchan = ichan:NChan+1
0159         good_cross(icount) = min([num_good_trials(ichan) num_good_trials(jchan)]);
0160         if good_cross(icount) == 0
0161             good_cross(icount) = 1;
0162         end;
0163         icount = icount+1;
0164     end;
0165 end;
0166     
0167 avgcsdm = avgcsdm./(ones(size(avgcsdm,1),1)*good_cross);
0168 
0169 NEpoch_used = max(good_cross); 
0170 
0171 
0172 
0173 
0174 
0175 
0176 
0177 
0178 
0179

Generated on Wed 20-Sep-2023 04:00:50 by m2html © 2003