Home > eeg > egis > scripts > run_csdm_f.m

run_csdm_f

PURPOSE ^

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

SYNOPSIS ^

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

DESCRIPTION ^

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

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