Home > eeg > egis > scripts > run_csdm.m

run_csdm

PURPOSE ^

status=run_csdm(noffset,nsamp,nwin,avg_cells,bad_chans,ses_fname,ave_fname)

SYNOPSIS ^

function status=run_csdm(noffset,nsamp,nwin,avg_cells,bad_chans,ses_fname,ave_fname)

DESCRIPTION ^

status=run_csdm(noffset,nsamp,nwin,avg_cells,bad_chans,ses_fname,ave_fname)
version 1    1/8/96

parameters

    noffset = offset in samples from beginning of trial to begin analysis
    nsamp = number of samples, must be even (converted into Npoints)
       nwin = # of windows (converted into NObs)run_csdm_c
    avg_cells = arrays of cells to use (optional)
    bad_chans = additional bad_channels not to use (optionals)
     ses_fname = filename including path containing data
    ave_fname = filename containing csdm matrices. 

Modification History
     Created by Ramesh Srinivasan 1/8/96

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function status=run_csdm(noffset,nsamp,nwin,avg_cells,bad_chans,ses_fname,ave_fname)
0002 %status=run_csdm(noffset,nsamp,nwin,avg_cells,bad_chans,ses_fname,ave_fname)
0003 %version 1    1/8/96
0004 %
0005 %parameters
0006 %
0007 %    noffset = offset in samples from beginning of trial to begin analysis
0008 %    nsamp = number of samples, must be even (converted into Npoints)
0009 %       nwin = # of windows (converted into NObs)run_csdm_c
0010 %    avg_cells = arrays of cells to use (optional)
0011 %    bad_chans = additional bad_channels not to use (optionals)
0012 %     ses_fname = filename including path containing data
0013 %    ave_fname = filename containing csdm matrices.
0014 %
0015 %Modification History
0016 %     Created by Ramesh Srinivasan 1/8/96
0017 
0018 %Initialize return argument
0019 status = -1;
0020 
0021 %Define constants
0022 ses_hdr_offsets_v;
0023 ave_hdr_offsets_v;
0024 TRUE = 1; FALSE = 0;
0025 
0026 %Check number of input arguments
0027 if ~((nargin == 3)|(nargin == 5)|(nargin == 7))
0028     error('Number of input arguments must be either 3, 5, or 7.');
0029 end
0030 if rem(nsamp,2) ~= 0
0031     error ('nsamp must be a power of two')
0032 end;
0033 %Initialize input and output fids
0034 ses_fid = -1;
0035 ave_fid = -1;
0036 %First try batch mode
0037 if nargin == 7
0038     ses_fid = fopen(ses_fname, 'r');
0039     if ses_fid == -1
0040         error(['Could not open input file' ses_fname '.']);
0041     end
0042     ave_fid = fopen(ave_fname, 'wb');
0043     if ave_fid == -1
0044         temp =fclose(ses_fid);
0045         error(['Could not open output file ' ave_fname '.']);
0046     end
0047 %Otherwise run interactive mode
0048 else
0049     while ses_fid == -1
0050         [ses_fid, ses_fname]=get_fid('r','*.ses*', 'Open Session File:');
0051     end
0052     while ave_fid == -1
0053         [ave_fid, ave_fname]=put_fid('wb','.csdm','Save New csdm File As:');
0054     end
0055 end
0056 
0057 %Read the session file EGIS header
0058 [ses_fhdr,ses_chdr,ses_ename,ses_czeros,ses_cgains,ses_cnames,ses_fcom,ses_ftext,ses_coff]=rd_egis_hdr_v(ses_fid);
0059 
0060 %Assign defaults based on header info
0061 if nargin == 3
0062     bad_chans = [];
0063     avg_cells = [1:ses_fhdr(NCells)];
0064 end
0065 if ((nargin == 5)&(length(avg_cells) == 0))
0066     avg_cells = [1:ses_fhdr(NCells)];
0067 end
0068 
0069 %More error checking. Compare arguments to header info.
0070 %Too many cell?
0071 %if length(avg_cells) > ses_fhdr(NCells)
0072 %     error(['Too many cells specified for this data file.']);
0073 %end
0074 %Bullshit cell number?
0075 if any(avg_cells > ses_fhdr(NCells))
0076     error('A specified cell number is greater than the number of cells in file.');
0077 end
0078 %Too many bad_chans?
0079 if length(bad_chans) > ses_fhdr(NChan)
0080      error(['Too many bad channels specified for this data file.']);
0081 end
0082 %Check bad_chans argument
0083 if any(bad_chans > ses_fhdr(NChan))
0084     error('A specified bad channel is greater than the number of channels in file.');
0085 end
0086 
0087 
0088 
0089 %Read the MacAverager .edc edit codes file
0090 ses_mask = artifact_edit(ses_fname, ses_fhdr, ses_chdr,0);
0091 %add the bad channels mask argument to the .edc information
0092 ses_mask(:, bad_chans) = zeros(size(ses_mask,1),length(bad_chans));
0093 
0094 %Construct the new output average file header
0095 disp('Constructing the average header');
0096 % Copy the session file header
0097 num_cells_avg = size(avg_cells,1);
0098 ave_fhdr = ses_fhdr; 
0099 ave_fhdr(NChan) = (ses_fhdr(NChan)+1)*(ses_fhdr(NChan) + 2)/2;
0100 ave_chdr(1:num_cells_avg,:) = ses_chdr(avg_cells(:,1),:); 
0101 ave_ename = ses_ename; ave_fcom = ses_fcom; ave_ftext = ses_ftext;
0102 ave_fhdr(NCells) = num_cells_avg;
0103 ave_fhdr(LastDone) = 1;
0104 ave_cnames(1:num_cells_avg,:) = ses_cnames(avg_cells(:,1),:);
0105 
0106 % Copy the experiment info from the session file to the average file tspecs
0107 [ave_chdr] = sesfhdr2avetspecs(ses_fhdr, ave_chdr);
0108 
0109 %Determine the number of good trials in the average
0110 for c = 1:num_cells_avg
0111     ave_chdr(c,NAvg) = 0;        % Zero the number of good trials
0112     ave_chdr(c,NObs) = nwin;
0113     ave_chdr(c,NPoints) = nsamp;
0114     cell_offset_mask(c) = sum(ses_chdr(1:avg_cells(c,1), NTrials)) - ses_chdr(avg_cells(c,1), NTrials);
0115     for t=1:ses_chdr(avg_cells(c,1),NTrials)
0116         trial_offset = cell_offset_mask(c) + t;
0117         if sum(ses_mask(trial_offset,:)) > 0
0118             ave_chdr(c, NAvg) = ave_chdr(c, NAvg) + 1;
0119         end
0120     end
0121 end
0122 
0123 
0124 % Write out the average file header
0125 wt_csdm_hdr_v(ave_fid,ave_fhdr,ave_chdr,ave_ename,ave_cnames,ave_fcom,ave_ftext);
0126 fseek(ave_fid, 0, 'eof');
0127 
0128 
0129 
0130 %Begin csdm calculation
0131 for c=1:num_cells_avg
0132     accessed_cell = 0;
0133     ncol = size(find(avg_cells(c,:)),2)
0134     disp(['Processing output cell ' int2str(c)]);
0135     for iwin = 1:nwin
0136         disp(['  Observation ' int2str(iwin)]);
0137         accessed_cell = FALSE;
0138         start_samp = noffset +1 +(iwin-1)*nsamp;
0139         stop_samp = noffset +iwin*nsamp;
0140         avgdata = zeros(ave_chdr(c,NPoints)/2, ave_fhdr(NChan));
0141         power_trialdata = zeros(ave_chdr(c,NPoints)/2, ave_fhdr(NChan));
0142         for icol = 1:ncol
0143         thecell = avg_cells(c,icol);
0144             for t=1:ses_chdr(thecell, NTrials)
0145                 ses_mask_cell_offset = sum(ses_chdr(1:thecell, NTrials)) - ses_chdr(thecell,NTrials);
0146                 if sum(ses_mask(ses_mask_cell_offset + t,:)) > 0    
0147                     if accessed_cell == FALSE
0148                         trialdata = rd_onetr_allch(ses_fid, ses_coff(thecell), t, ses_fhdr(NChan), ses_chdr(thecell, NPoints), 'bof');
0149                         accessed_cell = TRUE;
0150                     else
0151                         trialdata = rd_onetr_allch(ses_fid, ses_coff(thecell), t, ses_fhdr(NChan), ses_chdr(thecell, NPoints), 'cof');
0152                     end
0153                 %Convert matrix to microvolts
0154                     vol_trialdata = cal_gain(trialdata,ses_cgains,ses_czeros);
0155                 %Average Reference Matrix
0156                     avref_trialdata= average_reference(vol_trialdata, ses_mask(ses_mask_cell_offset + t,:));
0157                 %zero average epoch
0158                     avref_trialdata = zeromean(avref_trialdata,start_samp,stop_samp);            
0159                 %    call to csdm
0160                     power_trialdata = csdm(avref_trialdata(start_samp:stop_samp,:));        
0161                 %add to average stack
0162                     avgdata = avgdata + power_trialdata;
0163                 %else
0164                 %disp(['Skipping trial ' int2str(t)]);
0165                 end
0166             end
0167         end;
0168         num_good_trials = zeros(1,ses_fhdr(NChan)+1)
0169         for icol = 1:ncol
0170             thecell = avg_cells(c,icol);
0171             ses_mask_cell_offset = sum(ses_chdr(1:thecell,NTrials)) - ses_chdr(thecell,NTrials);
0172             start_offset = ses_mask_cell_offset + 1;
0173             stop_offset = start_offset + ses_chdr(thecell,NTrials) - 1;
0174             num_good_trials = num_good_trials+ sum(ses_mask(start_offset:stop_offset,:));
0175         end;
0176         %divides are different for amplitude and phase
0177         icount = 1;
0178         good_cross = zeros(1,size(avgdata,2));
0179         for ichan = 1:ses_fhdr(NChan)+1
0180             for jchan = ichan:ses_fhdr(NChan)+1
0181                 good_cross(icount) = min([num_good_trials(ichan) num_good_trials(jchan)]);
0182                 if good_cross(icount) == 0
0183                     good_cross(icount) = 1;
0184                 end;
0185                 icount = icount+1;
0186             end;
0187         end;
0188         avgdata = avgdata./(ones(ave_chdr(c,NPoints)/2,1)*good_cross);
0189         disp('Writing data');
0190         num_written = fwrite(ave_fid, (real(avgdata))', 'float');
0191         num_written = fwrite(ave_fid, (imag(avgdata))', 'float');
0192         if thecell==1 & iwin == 2
0193             save avgcsdm.mat avgdata
0194         end;
0195         if num_written ~= size(avgdata,1) * size(avgdata,2)
0196             error(['Failed to write data of cell ' int2str(c)]);
0197         end
0198     end;
0199 end
0200 
0201 fclose('all');
0202 disp('Finished CSDM Calculation.');
0203 status = 1;
0204 
0205

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