0001 function status=replace_chan_ave_ave(bad_chan,infname)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 status = -1;
0017
0018
0019 if ~((nargin == 1)|(nargin == 2)|(nargin == 0))
0020 error('Number of input arguments must be either 1 2 or 0 moron.');
0021 end
0022
0023 srcfid = -1;
0024 destfid = -1;
0025 if nargin == 0
0026 bad_chan = [];
0027 end;
0028
0029 if nargin == 2
0030 srcfid = fopen(infname, 'r');
0031 if srcfid == -1
0032 error(['Could not open input file ' infname '.']);
0033 end
0034 outfname = [infname '_rep'];
0035 destfid = fopen(outfname, 'wb');
0036 if destfid == -1
0037 temp =fclose(srcfid);
0038 error(['Could not open output file ' outfname '.']);
0039 end
0040
0041 else
0042 while srcfid == -1
0043 [srcfid, infname]=get_fid('r','*', 'Open Average File:');
0044 end
0045 outfname = [infname '_rep'];
0046 destfid = fopen(outfname, 'wb');
0047 if destfid == -1
0048 temp =fclose(srcfid);
0049 error(['Could not open output file ' outfname '.']);
0050 end
0051 end
0052 end
0053
0054
0055 ave_hdr_offsets_v;
0056
0057 [fhdr,chdr,ename,czeros,cgains,cnames,fcom,ftext, coff]=rd_egis_hdr_v(srcfid);
0058
0059
0060
0061 frewind(srcfid);
0062 [temp, countin] = fread(srcfid, fhdr(LHeader), 'char');
0063 countout = fwrite(destfid, temp, 'char');
0064 if countin ~= countout
0065 error('Error copying header information');
0066 end
0067
0068 good_chan_mask = ones(chdr(1,NObs),fhdr(NChan));
0069
0070
0071 if bad_chan ~= []
0072
0073 nrows_bad = size(bad_chan,1);
0074 if chdr(1,NObs) ~= nrows_bad
0075 disp(['WARNING: Number of observations (' int2str(nobs) ') doesn''t match number of rows in bad_chan (' int2str(nrows_bad) ')']);
0076 if nrows_bad == 1
0077 disp('Assuming bad_chan applies to all observations');
0078 bad_chan = ones(size(good_chan_mask,1))*bad_chan;
0079 else
0080 error('Drastic mismatch between nobs and bad_chan')
0081 end
0082 end
0083
0084 for isub = 1:size(good_chan_mask,1)
0085 bchan = find(bad_chan(isub,:));
0086 good_chan_mask(isub,bad_chan(isub,bchan)) = zeros(1,size(bchan,2));
0087 end;
0088
0089
0090 end;
0091
0092 [xelec,yelec,zelec] = electrodes(fhdr(NChan));
0093
0094
0095 for c=1:fhdr(NCells)
0096 disp(['Processing cell: ' int2str(c)]);
0097
0098
0099 for t=1:chdr(c,NObs)
0100 trialdata = rd_onetr_allch(srcfid, coff(c), t, fhdr(NChan), chdr(c, NPoints));
0101 lap_trialdata = zeros(size(trialdata,1),size(trialdata,2));
0102 good_chan = find(good_chan_mask(t,:));
0103 lap_trialdata = replace_trial(trialdata,good_chan,xelec,yelec,zelec);
0104 fwrite(destfid, lap_trialdata', 'int16');
0105
0106
0107 end
0108 end
0109
0110 disp('Finished running replace_chan_ave. Have a nice day');
0111 temp=fclose(srcfid);
0112 temp=fclose(destfid);
0113 status = 1;