0001 function status=run_csdm_2eeg(noffset,nsamp,avg_cells,bad_chans,ses_fname,reference)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 status = -1;
0025
0026
0027 ses_hdr_offsets_v;
0028 TRUE = 1; FALSE = 0;
0029
0030
0031 if ~((nargin == 6))
0032 error('Number of input arguments must be 6.');
0033 end
0034 if rem(nsamp,2) ~= 0
0035 error ('nsamp must be a multiple of two')
0036 end;
0037 if isempty(reference)
0038 reference = 'average';
0039 end;
0040
0041 ses_fid = -1;
0042 ave_fid = -1;
0043
0044 ses_fid = fopen(ses_fname, 'r');
0045 if ses_fid == -1
0046 error(['Could not open input file' ses_fname '.']);
0047 end
0048
0049 [ses_fhdr,ses_chdr,ses_ename,ses_czeros,ses_cgains,ses_cnames,ses_fcom,ses_ftext,ses_coff]=rd_egis_hdr_v(ses_fid);
0050 if isempty(avg_cells)
0051 avg_cells = [1:ses_fhdr(NCells)]';
0052 end;
0053 if isempty(noffset)
0054 noffset = 0;
0055 end;
0056 if isempty(nsamp)
0057 nsamp = fix(ses_chdr(1,NSamp)/2)*2;
0058 end;
0059
0060
0061 if any(avg_cells > ses_fhdr(NCells))
0062 error('A specified cell number is greater than the number of cells in file.');
0063 end
0064
0065 if length(bad_chans) > ses_fhdr(NChan)
0066 error(['Too many bad channels specified for this data file.']);
0067 end
0068
0069 if any(bad_chans > ses_fhdr(NChan))
0070 error('A specified bad channel is greater than the number of channels in file.');
0071 end
0072
0073
0074 ses_mask = artifact_edit(ses_fname, ses_fhdr, ses_chdr,0);
0075
0076 ses_mask(:, bad_chans) = zeros(size(ses_mask,1),length(bad_chans));
0077
0078 if strcmp(reference,'laplacian')
0079 [xelec,yelec,zelec] = electrodes(NChan+1);
0080 end;
0081 if strcmp(reference(1:2),'co')
0082 [xelec,yelec,zelec] = electrodes(NChan+1);
0083 sources = xyz2tp(xelec,yelec,zelec);
0084 A = transfer_matrix(500,0.2,80,8,8.2,8.7,9.2,7.8,sources,sources);
0085 end;
0086 num_cells_avg = size(avg_cells,1);
0087
0088 for c=1:num_cells_avg
0089 accessed_cell = 0;
0090 ncol = size(find(avg_cells(c,:)),2);
0091 disp(['Processing output cell ' int2str(c)]);
0092 accessed_cell = FALSE;
0093 start_samp = noffset +1;
0094 stop_samp = noffset +nsamp;
0095 ncsdm = (ses_fhdr(NChan)+2)*(ses_fhdr(NChan)+1)/2;
0096 avgdata = zeros(nsamp/2,ncsdm);
0097 for icol = 1:ncol
0098 thecell = avg_cells(c,icol);
0099 for t=1:ses_chdr(thecell, NTrials)
0100 ses_mask_cell_offset = sum(ses_chdr(1:thecell, NTrials)) - ses_chdr(thecell,NTrials);
0101 if sum(ses_mask(ses_mask_cell_offset + t,:)) > 0
0102 if accessed_cell == FALSE
0103 trialdata = rd_onetr_allch(ses_fid, ses_coff(thecell), t, ses_fhdr(NChan), ses_chdr(thecell, NPoints), 'bof');
0104 accessed_cell = TRUE;
0105 else
0106 trialdata = rd_onetr_allch(ses_fid, ses_coff(thecell), t, ses_fhdr(NChan), ses_chdr(thecell, NPoints), 'cof');
0107 end
0108
0109 trialdata2 = cal_gain(trialdata,ses_cgains,ses_czeros);
0110
0111 if ~(strcmp(reference,'laplacian')|strcmp(reference(1:2),'co'))
0112 [ref_trialdata,masknew] = new_reference(trialdata2,ses_mask(ses_mask_cell_offset + t,:),reference);
0113 ref_trialdata = ref_trialdata.*(ones(size(ref_trialdata,1),1)*masknew);
0114 ses_mask(ses_mask_cell_offset+t,:) = masknew;
0115 elseif strcmp(reference,'laplacian')
0116 good_chan = find(ses_mask(ses_mask_cell_offset + t,:));
0117 ref_trialdata = laplacian_trial([trialdata2 zeros(size(trialdata2,1),1)],good_chan,xelec,yelec,zelec);
0118 masknew = ses_mask(ses_mask_cell_offset + t,:);
0119 elseif strcmp(reference(1:8),'cortical')
0120 good_chan = find(ses_mask(ses_mask_cell_offset + t,:));
0121 good_trial = zeros(size(trialdata2,1),size(good_chan,2));
0122 trialdata3 = average_reference(trialdata2(:,1:NChan),ses_mask(ses_mask_cell_offset + t,:));
0123 good_trial = trialdata3(:,good_chan);
0124 good_A = zeros(size(good_chan,2),size(good_chan,2));
0125 good_A = A(good_chan,good_chan);
0126 ref_trialdata = zeros(size(trialdata3,1),size(trialdata3,2));
0127 sigma_m = 1;
0128 sigma_v = str2num(reference(9:10));
0129
0130 [m,deviations] = bayes_dipole_trial(good_A,good_trial',sigma_v,sigma_m);
0131 ref_trialdata(:,good_chan) = m';
0132 masknew = ses_mask(ses_mask_cell_offset + t,:);
0133 end
0134
0135
0136 ref_trialdata = zeromean(ref_trialdata,start_samp,stop_samp);
0137
0138
0139 avgdata = avgdata + csdm(ref_trialdata(start_samp:stop_samp,:));
0140
0141
0142 end
0143 end
0144 end;
0145 num_good_trials = zeros(1,ses_fhdr(NChan)+1);
0146 for icol = 1:ncol
0147 thecell = avg_cells(c,icol);
0148 ses_mask_cell_offset = sum(ses_chdr(1:thecell,NTrials)) - ses_chdr(thecell,NTrials);
0149 start_offset = ses_mask_cell_offset + 1;
0150 stop_offset = start_offset + ses_chdr(thecell,NTrials) - 1;
0151 num_good_trials = num_good_trials+ sum(ses_mask(start_offset:stop_offset,:));
0152 end;
0153
0154 icount = 1;
0155 good_cross = zeros(1,size(avgdata,2));
0156 for ichan = 1:ses_fhdr(NChan)+1
0157 for jchan = ichan:ses_fhdr(NChan)+1
0158 good_cross(icount) = min([num_good_trials(ichan) num_good_trials(jchan)]);
0159 if good_cross(icount) == 0
0160 good_cross(icount) = 1;
0161 end;
0162 icount = icount+1;
0163 end;
0164 end;
0165 avgdata = avgdata./(ones(nsamp/2,1)*good_cross);
0166 disp(['Writing csdm data file output cell:' int2str(c)]);
0167 ave_fname = [ses_fname(5:12) 'tc' int2str(c) '.' reference(1:4) '.csdm'];
0168 ave_fid = fopen(ave_fname,'wb');
0169 version = -1;
0170 fwrite(ave_fid,version,'int16');
0171 SampRate
0172 ses_chdr(c,SampRate)
0173 Epoch = nsamp/ses_chdr(c,SampRate);
0174 fwrite(ave_fid,Epoch,'int16');
0175 fwrite(ave_fid,ses_chdr(c,NTrials),'int16')
0176 fwrite(ave_fid,max(good_cross),'int16');
0177 Nbad_chan = size(bad_chans,2);
0178 fwrite(ave_fid,Nbad_chan,'int16');
0179 fwrite(ave_fid,bad_chans,'int16');
0180 if strcmp(reference,'average')
0181 ref_flag = 1;
0182 elseif strcmp(reference,'avgmast')
0183 ref_flag = 2;
0184 elseif strcmp(reference,'perimeter');
0185 ref_flag = 3;
0186 elseif strcmp(reference,'vertex');
0187 ref_flag = 4;
0188 elseif strcmp(reference,'laplacian');
0189 ref_flag = 5;
0190 elseif strcmp(reference(1:8),'cortical');
0191 ref_flag = 7;
0192 else
0193 ref_flag = 6;
0194 end;
0195 fwrite(ave_fid,ref_flag,'int16');
0196 if ref_flag == 6
0197 fwrite(ave_fid,size(reference,2),'int16');
0198 fwrite(ave_fid,reference,'int16');
0199 end
0200 if ref_flag == 7
0201 sigma_v = str2num(reference(9:10));
0202 fwrite(ave_fid,sigma_v,'int16');
0203 end;
0204 max_freq = 50*Epoch + 1;
0205 fwrite(ave_fid,max_freq,'int16');
0206 fwrite(ave_fid,size(avgdata,2),'int16');
0207 num_written = fwrite(ave_fid, (real(avgdata(1:max_freq,:)))', 'float');
0208 num_written = fwrite(ave_fid, (imag(avgdata(1:max_freq,:)))', 'float');
0209 fclose(ave_fid);
0210 end
0211
0212 fclose('all');
0213 disp('Finished CSDM Calculation.');
0214 status = 1;
0215
0216
0217
0218
0219
0220