%The following script will write to an fds file for Labview Filter
%Generation. Type in the a and b coefficients, gain, Sampling Frequency,
%and Type this program will generate a useable fds file for input into
%Labview for use on the Speedy-33 board
%Author: William Lois for use in Russell Kraft's Computer Application
%Laboratory course @ RPI
%Version: 1.0

clear all
close all


format long g
[FileName,PathName] = uiputfile('*.fds','Select the Output fds Coefficient');
fid = fopen([PathName FileName], 'w', 'b');

%Change A and B Coefs in Cascaded Second Order Sections Form II format
a_coefs = [-1.49111812558251, 0.933622289041048, -1.57615038161793, 0.820158244745261, -1.70041526831876, 0.750389661000574];
b_coefs = [0.0413964320823316, 0.0827928641646632, 0.0413964320823316, 0.0413964320823316, 0.0827928641646632, 0.0413964320823316, 0.0413964320823316, 0.0827928641646632, 0.0413964320823316];


fs    = 18000;   % Sampling Frequency  
versionc= '1.0'; % Version (No Need to Change)
type    = 11;    % Type in Cascaded Second Order Sections Form II format
gain    = 1.0;   % Filter Gain

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%
Num_A_coefs    = length(a_coefs);
Num_B_coefs    = length(b_coefs);
order          = Num_B_coefs;

%The following will write to the output file 

fwrite(fid, 256 ,'uint32');         %No idea what this word means, but data wil not load into NI otherwise
fwrite(fid, fs ,'double');          %Print Sampling Frequency
fwrite(fid, 3 ,'uint32');           %No idea what this word means, but data wil not load into NI otherwise
fwrite(fid, versionc ,'char');      %Print Version Frequency
fwrite(fid, 0 ,'uint32');           %No idea what this word means, seems to have no impact on load
fwrite(fid, type ,'uint16');        %Print Filter Type for Original Filter
fwrite(fid, gain ,'double');        %Print Gain for Original Filter
fwrite(fid, Num_A_coefs ,'uint32'); %Print Number of A Coefficients for Original Filter
fwrite(fid, a_coefs ,'double');     %Print A Coefficients for Original Filter
fwrite(fid, Num_B_coefs ,'uint32'); %Print Number of B Coefficients for Original Filter
fwrite(fid, b_coefs ,'double');     %Print B Coefficients for Original Filter
fwrite(fid, type ,'uint16');        %Print A Coefficients for Current Filter
fwrite(fid, gain ,'double');        %Print Gain for Current Filter
fwrite(fid, Num_A_coefs ,'uint32'); %Print Number of A Coefficients for Current Filter
fwrite(fid, a_coefs ,'double');     %Print A Coefficients for Current Filter
fwrite(fid, Num_B_coefs ,'uint32'); %Print Number of B Coefficients for Current Filter
fwrite(fid, b_coefs ,'double');     %Print B Coefficients for Current Filter
fwrite(fid, 0 ,'bit1');             %haven't tested quantized coeficients
for e = 1:12                    % 
    fwrite(fid, 0 ,'uint16');       %Unknown but necessary for file generation
end
fwrite(fid, order ,'uint16');       %Print Filter Order for Current Filter

fclose(fid);