0001 function status=make_cortical(sigma_v,sigma_m,bad_chan,bad_obs,infname,outfname)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 status = -1;
0023
0024
0025 if ~((nargin == 6)|(nargin == 2)|(nargin == 4))
0026 error('Number of input arguments must be 2, 4 or 6.');
0027 end
0028 if nargin == 2
0029 bad_chan = [];bad_obs=[];
0030 end;
0031 if sigma_m == 0
0032 sigma_m = 1;
0033 end;
0034
0035 srcfid = -1;
0036 destfid = -1;
0037
0038 if nargin == 6
0039 srcfid = fopen(infname, 'r');
0040 if srcfid == -1
0041 error(['Could not open input file' infname '.']);
0042 end
0043 destfid = fopen(outfname, 'wb');
0044 if destfid == -1
0045 temp =fclose(srcfid);
0046 error(['Could not open output file ' outfname '.']);
0047 end
0048
0049 else
0050 while srcfid == -1
0051 [srcfid, infname]=get_fid('r','*.ave*', 'Open Average File:');
0052 end
0053 while destfid == -1
0054 [destfid, outfname]=put_fid('wb','new.ave_crt','Save New CorticalAverage File As:');
0055 end
0056 end
0057
0058
0059 ave_hdr_offsets_v;
0060
0061 [fhdr,chdr,ename,czeros,cgains,cnames,fcom,ftext, coff]=rd_egis_hdr_v(srcfid);
0062
0063
0064 frewind(srcfid);
0065 [temp, countin] = fread(srcfid, fhdr(LHeader), 'char');
0066 countout = fwrite(destfid, temp, 'char');
0067 if countin ~= countout
0068 error('Error copying header information');
0069 end
0070 if (nargin > 2)&(bad_chan ~= [])
0071 if size(bad_chan,1) ~= chdr(1,NObs)
0072 error('mismatch between bad_chan and NObs')
0073 end;
0074 end;
0075 [xelec,yelec,zelec] = electrodes(fhdr(NChan));
0076 sources = xyz2tp(xelec,yelec,zelec);
0077 A = transfer_matrix(500,0.2,80,8.0,8.2,8.7,9.2,7.8,sources,sources);
0078
0079
0080 for c=1:fhdr(NCells)
0081 disp(['Processing cell: ' int2str(c)]);
0082
0083 for t=1:chdr(c,NObs)
0084 if ~any(t==bad_obs)
0085 trialdata = rd_onetr_allch(srcfid, coff(c), t, fhdr(NChan), chdr(c, NPoints));
0086 trial_mask = ones(1,fhdr(NChan));
0087 if (nargin > 2)&(bad_chan ~= [])
0088 trial_mask(1,bad_chan(t,find(bad_chan(t,:)))) = zeros(1,size(find(bad_chan(t,:)),2));
0089 end;
0090 good_chan = find(trial_mask);
0091 good_trial = zeros(size(trialdata,1),size(good_chan,2));
0092 good_trial = trialdata(:,good_chan);
0093 good_A = zeros(size(good_chan,2),size(good_chan,2));
0094 good_A = A(good_chan,good_chan);
0095
0096
0097 cortical_trial = zeros(size(trialdata,1),size(trialdata,2));
0098 [m,deviations] = bayes_dipole_trial(good_A,good_trial',sigma_v,sigma_m);
0099 cortical_trial(:,good_chan) = m';
0100 cortical_trial = cortical_trial*10;
0101 fwrite(destfid,cortical_trial', 'int16');
0102 else
0103 cortical_trial=zeros(chdr(c, NPoints),fhdr(NChan));
0104 fwrite(destfid,cortical_trial', 'int16');
0105 end
0106 end
0107 end
0108
0109 disp('Welcome to the cortical surface. Transfer function fee of $5 applies');
0110 temp=fclose(srcfid);
0111 temp=fclose(destfid);
0112 status = 1;