0001 function status=make_ave_bf(prestimsamps1,prestimsamps2,lowpass,highpass,lowfiltord,highfiltord,infname,outfname)
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 status = -1;
0029
0030
0031 if ~((nargin == 4)|(nargin == 6)|(nargin == 8))
0032 error('Number of input arguments must be either 4, 6, or 8.');
0033 end
0034
0035 srcfid = -1;
0036 destfid = -1;
0037
0038 if nargin == 8
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 if highfiltord==[]
0049 highfiltord=3;
0050 end
0051 if lowfiltord==[]
0052 lowfiltord=20;
0053 end
0054
0055 else
0056 while srcfid == -1
0057 [srcfid, infname]=get_fid('r','*ave*', 'Open Average File:');
0058 end
0059 while destfid == -1
0060 [destfid, outfname]=put_fid('wb','new.ave_bf','Save New Average File As:');
0061 end
0062 if nargin==6
0063 if highfiltord==[]
0064 highfiltord=3;
0065 end
0066 if lowfiltord==[]
0067 lowfiltord=20;
0068 end
0069 else
0070 highfiltord=3;
0071 lowfiltord=20;
0072 end
0073 end
0074
0075
0076 ave_hdr_offsets_v;
0077
0078 [fhdr,chdr,ename,czeros,cgains,cnames,fcom,ftext, coff]=rd_egis_hdr_v(srcfid);
0079
0080 nyquist=zeros(1,fhdr(NCells));
0081 for c=1:fhdr(NCells)
0082 nyquist(c) = chdr(c, SampRate)/2;
0083 end
0084 if any(lowpass > nyquist)
0085 temp=fclose(srcfid);
0086 temp=fclose(destfid);
0087 error(['The lowpass freq. exceeds nyquist freq., ' int2str(nyquist) ', in some cells.']);
0088 end
0089
0090
0091
0092 frewind(srcfid);
0093 [temp, countin] = fread(srcfid, fhdr(LHeader), 'char');
0094 countout = fwrite(destfid, temp, 'char');
0095 if countin ~= countout
0096 error('Error copying header information');
0097 end
0098
0099
0100 for c=1:fhdr(NCells)
0101 disp(['Processing cell: ' int2str(c)]);
0102
0103 if highpass ~= 0
0104
0105 cutoff = [(highpass/nyquist(c)) (lowpass/nyquist(c))];
0106 b_bandpass=[]; a_bandpass=[];
0107 [b_bandpass, a_bandpass] = butter(highfiltord, cutoff);
0108 if c == 1
0109 freqz(b_bandpass,a_bandpass);
0110 figure
0111 end;
0112
0113 end;
0114
0115
0116 cutoff = lowpass/nyquist(c);
0117 b_lowpass=[]; a_lowpass=[];
0118 [b_lowpass,a_lowpass] = butter(lowfiltord,cutoff);
0119 if c == 1
0120 freqz(b_lowpass,a_lowpass);
0121 end;
0122
0123
0124 for t=1:chdr(c,NObs)
0125 trialdata = rd_onetr_allch(srcfid, coff(c), t, fhdr(NChan), chdr(c, NPoints));
0126 for ch=1:fhdr(NChan)
0127 filtdata(:,ch) = filtfilt(b_lowpass, a_lowpass, trialdata(:,ch));
0128 end;
0129
0130 if highpass ~= 0
0131 for ch=1:fhdr(NChan)
0132 filtdata(:,ch) = filtfilt(b_bandpass, a_bandpass, filtdata(:,ch));
0133 end;
0134 end;
0135 bfiltdata= zeros(size(filtdata,1),size(filtdata,2));
0136 bfiltdata = zeromean(filtdata,prestimsamps1,prestimsamps2);
0137 fwrite(destfid, bfiltdata', 'int16');
0138 end
0139 end
0140
0141 disp('Finished running make_ave_bf.');
0142 temp=fclose(srcfid);
0143 temp=fclose(destfid);
0144 status = 1;