//inline definition (see function)
function [x]=hztobin(hz, Fs)
[hzm,fr]=frmag(hz,256);
hzm = hzm.^2
a = length(hzm);
hzm(1) = 1;
hzm(a) = 1;
hzm = 20 * log10(hzm)
fr = fr .*Fs
subplot(2, 2, 1);
plot2d('ln',fr',hzm')
xtitle('Discrete IIR filter',' ',' ');
xgrid
q=poly(0,'q');
hzd=horner(hz,1/q)
subplot(2, 2, 2);
plzr(hz);
a = hz(2);
b = hz(3);
a = coeff(a);
b = coeff(b);
b = -b
g = a;
h = b;
e = -b;
c = 4 * (a < 0);
d = 4 * (b < 0);
a = a + c;
b = b + d;
a = a * 2^33;
b = b * 2^33;
a = round(a);
b = round(b);
a = dec2bin(a, 35);
b = dec2bin(b, 35);
//disp(hz);
//disp(a);
//disp(b);
x = [a b(2) b(1)];
endfunction



function [C]=IIRConstantsSet(Fs)

//---------- Configurate filter biquad 1 channel 1 ----------
passBand = 800/Fs;
hz=iir(2,'hp','butt',passBand, [.08 .03]);
[c1]=hztobin(hz, Fs)
//c1 = ['01000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000']

//---------- Configurate filter biquad 2 channel 1 ----------
passBand = 800/Fs;
hz=iir(2,'hp','butt',passBand, [.08 .03]);
[c2]=hztobin(hz, Fs)
//c2 = ['01000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000']

//---------- Configurate filter biquad 1 channel 2 ----------
//passBand = 80/Fs;
//hz=iir(2,'hp','butt',passBand, [.08 .03]);
//[c3]=hztobin(hz, Fs)
c3 = ['01000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000']

//---------- Configurate filter biquad 2 channel 2 ----------
//passBand = 80/Fs;
//hz=iir(2,'hp','butt',passBand, [.08 .03]);
//[c4]=hztobin(hz, Fs)
c4 = ['01000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000']

//---------- Configurate filter biquad 3 channel 2 ----------
passBand = 800/Fs;
hz=iir(2,'lp','butt',passBand, [.08 .03]);
[c5]=hztobin(hz, Fs)
//c5 = ['01000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000']

//---------- Configurate filter biquad 4 channel 2 ----------
passBand = 800/Fs;
hz=iir(2,'lp','butt',passBand, [.08 .03]);
[c6]=hztobin(hz, Fs)
//c6 = ['01000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000']

//---------- Configurate filter biquad 1 channel 3 ----------
//passBand = 80/Fs;
//hz=iir(2,'lp','butt',passBand, [.08 .03]);
//[c7]=hztobin(hz, Fs)
c7 = ['01000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000']

//---------- Configurate filter biquad 2 channel 3 ----------
//passBand = 3300/Fs;
//hz=iir(2,'lp','butt',passBand, [.08 .03]);
//[c8]=hztobin(hz, Fs)
c8 = ['01000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000']

c9 = ['00000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000' '00000000000000000000000000000000000']

C = [c1 c2 c3 c4 c5 c6 c7 c8 c9 c9 c9 c9 c9 c9]
endfunction

//---------- Set preset levels ----------
Level = [6 0 0]; // Write value -10 - +10 or 11 to configure after programing, one for each channel
Delay = [8 0 0]; // Write 0 - 58 or 59 - 255 to configure after programing, one for each channel

Level = 2 * (Level + 10);
Level = round (Level);
Level = dec2bin(Level, 8);
Delay = round(Delay);
Delay = dec2bin(Delay, 8);


[D1] = IIRConstantsSet(44100);
[D2] = IIRConstantsSet(48000);
[D3] = IIRConstantsSet(88200);
[D4] = IIRConstantsSet(96000);
[D5] = IIRConstantsSet(176400);
[D6] = IIRConstantsSet(192000);

D = [D1 D2 D3 D4 D5 D6]

// Open a file as text with write property and write to file.
fd_w = mopen('IIRROM.vhd','w');
mputl('-- Automaticalay generated file with constants for IIR filter.', fd_w);

//---------- Edit line below to add coments about the filter in the VHDL file. ----------
mputl('-- 24 dB/oct 800Hz Chan 1 and 2. Chan3 full range.', fd_w);
mputl('library IEEE;', fd_w);

mputl('use IEEE.STD_LOGIC_1164.ALL;', fd_w);
mputl('use IEEE.NUMERIC_STD.ALL;', fd_w);



mputl('entity IIRROM is', fd_w);
mputl('    Port ( Address	: in  STD_LOGIC_VECTOR (5 downto 0);', fd_w);
mputl('           Clk			: in  STD_LOGIC;', fd_w);
mputl('           Data 		: out  STD_LOGIC_VECTOR (34 downto 0);', fd_w);
mputl('           SampleFs	: in  STD_LOGIC_VECTOR (2 downto 0);', fd_w);
mputl('           Ch1Level	: out  STD_LOGIC_VECTOR (7 downto 0);', fd_w);
mputl('           Ch2Level	: out  STD_LOGIC_VECTOR (7 downto 0);', fd_w);
mputl('           Ch3Level	: out  STD_LOGIC_VECTOR (7 downto 0);', fd_w);
mputl('           Ch1Delay	: out  STD_LOGIC_VECTOR (7 downto 0);', fd_w);
mputl('           Ch2Delay	: out  STD_LOGIC_VECTOR (7 downto 0);', fd_w);
mputl('           Ch3Delay	: out  STD_LOGIC_VECTOR (7 downto 0));', fd_w);
mputl('end IIRROM;', fd_w);

mputl('architecture Behavioral of IIRROM is', fd_w);
mputl('signal intAddress: integer range 0 to 383;', fd_w);
mputl('type ConstantType is array (0 to 383) of signed (34 downto 0);', fd_w);
mputl('constant Constants : ConstantType := ( ' + ascii(34) + D(1) + ascii(34) + ',', fd_w);
  for j = 2 : 383
      mputl('                                       ' + ascii(34) + D(j) + ascii(34) + ',', fd_w);
   end
mputl('                                       ' + ascii(34) + D(384) + ascii(34) + ');', fd_w);


mputl('begin', fd_w);
mputl('process(Clk)', fd_w);
mputl('begin', fd_w);
mputl('  if rising_Edge(Clk)', fd_w);
mputl('  then', fd_w);
mputl('    if ((unsigned(Address) < 40) and (unsigned(SampleFs) < 6))', fd_w);
mputl('    then', fd_w);
mputl('      intAddress <= to_integer (unsigned(SampleFs & Address));', fd_w);
mputl('    else', fd_w);
mputl('      intAddress <= 0;', fd_w);
mputl('    end if;', fd_w);
mputl('  end if;', fd_w);
mputl('end process;', fd_w);
mputl('Data <= std_logic_vector(Constants(intAddress));', fd_w);
mputl('Ch1Level <= ' + ascii(34) + Level(1) + ascii(34) +';', fd_w);
mputl('Ch2Level <= ' + ascii(34) + Level(2) + ascii(34) +';', fd_w);
mputl('Ch3Level <= ' + ascii(34) + Level(3) + ascii(34) +';', fd_w);
mputl('Ch1Delay <= ' + ascii(34) + Delay(1) + ascii(34) +';', fd_w);
mputl('Ch2Delay <= ' + ascii(34) + Delay(2) + ascii(34) +';', fd_w);
mputl('Ch3Delay <= ' + ascii(34) + Delay(3) + ascii(34) +';', fd_w);
mputl('end Behavioral;', fd_w);
mclose(fd_w);

