function FIR_cof_gen(name, coeff, type)
%
% Creates filter coefficients in a .cof file 
% name must be a character string, i.e. 'myfile'
% and coeff must be a contain the filter 
% coefficients h[n]
% type must either be 'fixed' or 'float'
% fixed point implementations will be scaled

% Do H inifinity scaling so that the maximum frequency component
% has magnitude one.
Hinf=max(abs(fft([coeff zeros(1,4096-length(coeff))])));
coeff = coeff ./ Hinf;

if (type == 'fixed')
    coeff=round(2^15*coeff); % Scale by 2^15 and round to nearest integer
end

if (type == 'float')
    coeff=coeff;             % No scaling
end


N=length(coeff)-1;                  % Order of FIR filter. Note that impulse
                                    % Response is length N+1
s=[name '.cof'];
fid = fopen(s,'w');		            % Open (or create) file

s=['// FIR Filter Designed in MATLAB''s SPTOOL\n// ' name '.cof \n\n'];
fprintf(fid,s);                     % Label file


s=['#define N ' num2str(N) '\n\n'];
fprintf(fid,s);                     % Define N
                                    % where N is the filter order

if (type == 'fixed')
    s=['short h[' num2str(N+1) ']=\n{\n'];
    fprintf(fid,s);                     % Print "short h[N+1]={" 
                                        % where N+1 is the filter duration
end

if (type == 'float')
    s=['float h[' num2str(N+1) ']=\n{\n'];
    fprintf(fid,s);                     % Print "float h[N+1]={" 
                                        % where N+1 is the filter duration
end

for i=1:N
    fprintf(fid,'%d,' ,coeff(i));   % Print N points
    if mod(i,6) == 0
        fprintf(fid,'\n');
    end
end
    
fprintf(fid,'%d' ,coeff(N+1));      % Print (N+1)th point
fprintf(fid,'};\n');                % Print closing bracket
fclose(fid);						% Close file
