# FIR Low Pass Filter Coefficient Generator Octave Script # Copyright D-TACQ Solutions 2005 # uses the remez FIR filter algorith from octave-forge # 15th August May 2005 # Written by John McLean function filtergen132( ncoeffs, pb1, sb1, samplerate ) # ncoeffs - number of coefficients + 1 # pb1 - pass band corner, kHz # sb1 - stop band start, kHz # samplerate kHz [default=32000] if (nargin < 4) samplerate=32000; if (nargin < 3) printf("filtergen132( ncoeffs, pb1, sb1, samplerate )\n"); return endif endif # now do the filters #setup the Fequency characteristic nyquist = samplerate/2; f = [0 pb1/nyquist sb1/nyquist 1]; #setup the gain / roll-off a = [1 1 0 0]; #calculate the coefficients b1 = remez(ncoeffs,f,a); # scale the output tot=sum(b1); b1 = b1/tot; [h1,w1] = freqz(b1,1,512); #plot(f,a,w/pi,abs(h)); freq = w1/pi * nyquist; for x=1:512 amp = abs(h1(x)); logamp1(x) = 20 * log10(amp); endfor title1 = sprintf("FIR Low Pass Filter Design Remez Implementation of Parks-McClellan \n"); title2 = sprintf("Passband = %dkHz, Stopband = %dkHz, Samplerate %dkSPS with %d coefficients\n",pb1,sb1,samplerate,ncoeffs-1); thetitle = strcat(title1,title2); plot(freq,logamp1); title(thetitle); grid("on"); #gset timestamp; axis ([0,nyquist,-100,1]); xlabel ("Frequency (kHz)"); ylabel ("Amplitude (dB)"); #legend('No Filter','Boxcar','Filter3','Filter4','Filter5','Filter6','Filter7','Filter8'); # save off the Filter Coefficients. #scale the data to 18 bit coefficients b1 = 131071 * b1; #datafile = strcat(filename,datafile); datafile = "FIR_132.coe"; myfile=fopen( datafile, "w" ); fprintf( myfile, "radix=10;\n"); #fprintf( myfile, "coefdata="); fprintf( myfile, "coefdata=%d",b1(2)); for x=3:ncoeffs; fprintf( myfile, ",%d",b1(x)); endfor fclose( myfile ); filename=sprintf("acq132filter_%dtaps_%dkHz_%dkHz_%d_kHz",ncoeffs,pb1,sb1,samplerate); psfile = strcat(filename,".ps"); outfilecommand = sprintf("print -dpsc2 -landscape \"%s\"",psfile); #printf("%s \n",outfilecommand); eval(outfilecommand);