0001 function status=run_csdm_c(noffset,nsamp,nwin,avg_cells,bad_chans,ses_fname,ave_fname)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 status = -1;
0020
0021
0022 ses_hdr_offsets_v;
0023 ave_hdr_offsets_v;
0024 TRUE = 1; FALSE = 0;
0025
0026
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
0034 ses_fid = -1;
0035 ave_fid = -1;
0036
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
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
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
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
0070
0071
0072
0073
0074
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
0079 if length(bad_chans) > ses_fhdr(NChan)
0080 error(['Too many bad channels specified for this data file.']);
0081 end
0082
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
0090 ses_mask = artifact_edit(ses_fname, ses_fhdr, ses_chdr,0);
0091
0092 ses_mask(:, bad_chans) = zeros(size(ses_mask,1),length(bad_chans));
0093
0094
0095 disp('Constructing the average header');
0096
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
0107 [ave_chdr] = sesfhdr2avetspecs(ses_fhdr, ave_chdr);
0108
0109
0110 for c = 1:num_cells_avg
0111 ave_chdr(c,NAvg) = 0;
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
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
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
0154 vol_trialdata = cal_gain(trialdata,ses_cgains,ses_czeros);
0155
0156 avref_trialdata= average_reference(vol_trialdata, ses_mask(ses_mask_cell_offset + t,:));
0157
0158 avref_trialdata = zeromean(avref_trialdata,start_samp,stop_samp);
0159
0160 power_trialdata = csdm(avref_trialdata(start_samp:stop_samp,:));
0161
0162 avgdata = avgdata + power_trialdata;
0163
0164
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
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