function dsk_fir(bin,fsin)
%
% DSK_FIR(BIN,FS)
%
% W. J. Gomes III, MSEE
% University of Massachusetts, Dartmouth
% gomes_w@code80.npt.nuwc.navy.mil
%
% M-function which generates a coefficients file and downloads it
% with the approiate code to C3x DSK board to run FIR filters in real-time.
% 
% Input:
% 		BIN - the FIR filter coefficients
% 		FS - the desired sample frequency
%
if nargin < 2
   disp('Wrong number of arguements');
else
   %
   % Calculate the BW of the DSK's output low pass filter based on fs/2. Then use that 
   % to get the filters clock frequency SCF.  With that we can get the TA and TB value 
   % load in to the AIC D/A interrupt generator registers.  These values are also used
   % for the A/D.  TA and TB are further broken into two byte hex strings to generate the 
   % assembler code for the DSK assembler.
   %
   MCLK=6.25e6;
   BW=(fsin/2)-250;
	SCF=(BW*288e3)/3600;
   TA=round(MCLK/(2*SCF));
   TB=round(MCLK/(2*TA*fsin));
   A1=dec2hex(bitshift(TA,1));
   A2=dec2hex(bitshift(TA,2));
   B1=dec2hex(bitshift(TB,1));
   B2=dec2hex(bitor(bitshift(TB,2),2));
   %
   % Start FIR filter code
   %
   coefflen=length(bin);
   alignbuf=round(2 .^ ceil(log2(coefflen)));	%<--- next power of 2
   firorder=coefflen-1;
   fid = fopen('firmat.cof','wt');
   fprintf(fid,'; FIR filter coefficents file for use with the DSK31 board\n'); 
   fprintf(fid,'; Filter is of order %d\n',firorder);
   fprintf(fid,'\n        .data \n');
   fprintf(fid,'COEFF   .float   ');
   j=0;
   for i=coefflen:-1:2
      if j > 4
         j=0;
         fprintf(fid,'\n        .float   ');
      end
      fprintf(fid,'%2.4E,',bin(i));
      j=j+1;
   end
   fprintf(fid,'\nH0      .float   %2.4E\n',bin(1));
   fprintf(fid,'\nLENGTH  .set     %d                 ;LENGTH = %d\n',coefflen,coefflen);
   fprintf(fid,'AICSEC  .word  0%s%sh,1h,0%s%sh,63h ;AIC configuration data\n',A1,A2,B1,B2);
   fprintf(fid,'        .brstart "XN_BUFF",%d      ;align samples buffer',alignbuf);
   fclose(fid);
   dos ('dsk3a firmat');
   dos ('dsk3load firmat BOOT');
end
  
