0001 function [Epoch,NEpoch_used,Nbad_chan,bad_chan,avgcsdm] = raw_eeg_csdm(rawfname,add_bad_chan,windows,reference,Epoch,NEpoch,Nbad_chan,bad_chan,mask);
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 if nargout ~= 5
0047 error('too many or too little output arguments')
0048 end;
0049 if nargin < 1
0050 error('filename not specified');
0051 end;
0052 if isempty(rawfname)
0053 [fid, rawfname] = get_fid('rb');
0054 fclose(fid)
0055 fid = fopen(rawfname,'rb','b');
0056 if nargin <= 4
0057 [Epoch,NEpoch,Max_microv,Min_Chan,Nbad_chan,bad_chan,mask] = read_mask(rawfname);
0058 end;
0059
0060 else
0061 if (nargin <= 4)
0062 fid = fopen(rawfname,'rb','b');
0063 if (fid < 0)
0064 error('error opening data file')
0065 fclose('all')
0066 end;
0067 [Epoch,NEpoch,Max_microv,Min_Chan,Nbad_chan,bad_chan,mask] = read_mask(rawfname);
0068 else
0069 fid = fopen(rawfname,'rb','b');
0070 if (fid < 0)
0071 error('error opening data file')
0072 fclose('all')
0073 end
0074 end;
0075 end;
0076 if ~(nargin ==1 | nargin ==2|nargin == 3| nargin ==4|nargin == 9)
0077 error('inproper number of input arguments')
0078 end;
0079 if nargin == 1
0080 add_bad_chan = [];
0081 windows = [1:size(mask,1)];
0082 reference = [];
0083 end;
0084 if nargin == 2
0085 windows = [1:size(mask,1)];
0086 reference = [];
0087 end;
0088 if nargin == 3
0089 reference = [];
0090 end;
0091 if isempty(windows)
0092 windows = [1:size(mask,1)];
0093 end;
0094 bad_chan = [bad_chan add_bad_chan];
0095 Nbad_chan = Nbad_chan + size(add_bad_chan,2);
0096 mask(:,bad_chan) = zeros(size(mask,1),size(bad_chan,2));
0097 mask_win = zeros(size(mask,1),size(mask,2));
0098 if ~isempty(windows)
0099 mask_win(windows,:) = ones(size(windows,2),size(mask,2));
0100 end;
0101 mask = mask.*mask_win;
0102 [header_array, EventCodes,Samp_Rate, NChan, scale, NSamp, NEvent] = rd_fragger_hdr(fid);
0103 avgcsdm = zeros(Samp_Rate*Epoch/2,(NChan+2)*(NChan + 1)/2);
0104 if strcmp(reference,'laplacian')
0105 [xelec,yelec,zelec] = electrodes(NChan+1);
0106 end;
0107 if strcmp(reference(1:2),'co')
0108 [xelec,yelec,zelec] = electrodes(NChan+1);
0109 sources = xyz2tp(xelec,yelec,zelec);
0110 A = transfer_matrix(500,0.2,80,8,8.2,8.7,9.2,7.8,sources,sources);
0111 end;
0112 for i = 1:NEpoch
0113 if i <= size(mask,1)
0114 if sum(mask(i,:)) > 1
0115 trialdata = fread(fid,[NChan+NEvent,Samp_Rate*Epoch],'integer*2');
0116 trialdata2 = trialdata(1:NChan,:)';
0117 trialdata2 = trialdata2*scale;
0118 if ~isempty(reference)
0119 if ~(strcmp(reference,'laplacian')|strcmp(reference(1:2),'co'))
0120 [ref_trialdata,masknew] = new_reference(trialdata2,mask(i,:),reference);
0121 ref_trialdata = ref_trialdata.*(ones(size(ref_trialdata,1),1)*masknew);
0122 elseif strcmp(reference,'laplacian')
0123 good_chan = find(mask(i,:));
0124 ref_trialdata = laplacian_trial([trialdata2 zeros(size(trialdata2,1),1)],good_chan,xelec,yelec,zelec);
0125 masknew = mask(i,:);
0126 elseif strcmp(reference(1:8),'cortical') good_chan = find(mask(i,:));
0127 good_trial = zeros(size(trialdata2,1),size(good_chan,2));
0128 trialdata3 = average_reference(trialdata2(:,1:NChan),mask(i,:));
0129 good_trial = trialdata3(:,good_chan);
0130 good_A = zeros(size(good_chan,2),size(good_chan,2));
0131 good_A = A(good_chan,good_chan);
0132 ref_trialdata = zeros(size(trialdata3,1),size(trialdata3,2));
0133 sigma_m = 1;
0134 sigma_v = str2num(reference(9:10));
0135
0136 [m,deviations] = bayes_dipole_trial(good_A,good_trial',sigma_v,sigma_m);
0137 ref_trialdata(:,good_chan) = m';
0138 masknew = mask(i,:);
0139 end
0140 else
0141 ref_trialdata = average_reference(trialdata2,mask(i,:));
0142 end;
0143
0144 avgcsdm = avgcsdm + csdm(ref_trialdata);
0145 mask(i,:) = masknew;
0146 else
0147 skip_bytes = (NChan+NEvent)*Samp_Rate*Epoch*2;
0148 fseek(fid,skip_bytes,'cof');
0149
0150 end;
0151
0152 end;
0153 end;
0154 num_good_trials = sum(mask);
0155 icount = 1;
0156 good_cross = zeros(1,size(avgcsdm,2));
0157 for ichan = 1:NChan+1
0158 for jchan = ichan:NChan+1
0159 good_cross(icount) = min([num_good_trials(ichan) num_good_trials(jchan)]);
0160 if good_cross(icount) == 0
0161 good_cross(icount) = 1;
0162 end;
0163 icount = icount+1;
0164 end;
0165 end;
0166
0167 avgcsdm = avgcsdm./(ones(size(avgcsdm,1),1)*good_cross);
0168
0169 NEpoch_used = max(good_cross);
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179