0001 function status=run_csdm_f(noffset,nsamp,nwin,avg_cells,single_trial_f,bad_chans,ses_fname,ave_fname)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 status = -1;
0021
0022
0023 ses_hdr_offsets_v;
0024 ave_hdr_offsets_v;
0025 TRUE = 1; FALSE = 0;
0026
0027
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
0035 ses_fid = -1;
0036 ave_fid = -1;
0037
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
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
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
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
0071
0072 if length(avg_cells) > ses_fhdr(NCells)
0073 error(['Too many cells specified for this data file.']);
0074 end
0075
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
0080 if length(bad_chans) > ses_fhdr(NChan)
0081 error(['Too many bad channels specified for this data file.']);
0082 end
0083
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
0091 ses_mask = artifact_edit(ses_fname, ses_fhdr, ses_chdr,0);
0092
0093 ses_mask(:, bad_chans) = zeros(size(ses_mask,1),length(bad_chans));
0094
0095
0096 disp('Constructing the average header');
0097
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
0108 [ave_chdr] = sesfhdr2avetspecs(ses_fhdr, ave_chdr);
0109
0110
0111 for c = 1:num_cells_avg
0112 ave_chdr(c,NAvg) = 0;
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
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
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
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
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,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
0164 avgdata = avgdata + power_trialdata;
0165
0166
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
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