%----------- B-format dekoder (Semestralni prace) -------------------------
%............tetrahedron layout loudspeaker feeds......................
% 16.11.2008
% verze 1 
%--------------------------------------------------------------------------
function [LFU RFD LBD RBU]=tetrahedron(W,X,Y,Z,fc)

% shelf filtry   
  
    m = tan(pi*fc/48000);
    

% high-frequency boost k1
    
    G1 = sqrt(2);
    k1 = 10^(G1/20);
    jmenovatel1 = 1 + sqrt(2)*m + m^2;
    b01 = (k1 + sqrt(2*k1)*m + m^2) / jmenovatel1;
    b11 = (2*(m^2 - k1)) / jmenovatel1;
    b21 = (k1 - sqrt(2*k1)*m + m^2) / jmenovatel1;
    a11 = (2*(m^2 - 1)) / jmenovatel1;
    a21 = (1 - sqrt(2)*m + m^2) / jmenovatel1;
    
    b1 = [b01 b11 b21];
    a1 = [1 a11 a21];
    
% low-frequency boost k2
    
    G2 = sqrt(3);
    k2 = 10^(G2/20);
    jmenovatel2 = 1 + sqrt(2)*m + m^2;
    b021 = (1 + sqrt(2*k2)*m + k2*m^2) / jmenovatel2;
    b121 = (2*(k2*m^2 - 1)) / jmenovatel2;
    b221 = (1 - sqrt(2*k2)*m + k2*m^2) / jmenovatel2;
    a121 = (2*(m^2 - 1)) / jmenovatel2;
    a221 = (1 - sqrt(2)*m + m^2) / jmenovatel2;
    
    b21 = [b021 b121 b221];
    a21 = [1 a121 a221];
    
% high-frequency boost k2
    
    G2 = sqrt(2);
    k2 = 10^(G2/20);
    jmenovatel2 = 1 + sqrt(2)*m + m^2;
    b022 = (k2 + sqrt(2*k2)*m + m^2) / jmenovatel2;
    b122 = (2*(m^2 - k2)) / jmenovatel2;
    b222 = (k2 - sqrt(2*k2)*m + m^2) / jmenovatel2;
    a122 = (2*(m^2 - 1)) / jmenovatel2;
    a222 = (1 - sqrt(2)*m + m^2) / jmenovatel2;
    
    b22 = [b022 b122 b222];
    a22 = [1 a122 a222];    

% vysledek

    WF = filter(b1,a1, W);
    
    XF1 = filter(b21,a21, X);
    YF1 = filter(b21,a21, Y);
    ZF1 = filter(b21,a21, Z);
    
    XF = filter(b22,a22, XF1);
    YF = filter(b22,a22, YF1);
    ZF = filter(b22,a22, ZF1);
    
 
% loudspeaker feeds

            LFU = 0.5*((sqrt(2)*WF) + (cos(pi/4)*cos(pi/4)*XF) + (sin(pi/4)*cos(pi/4)*YF) + (sin(pi/4)*ZF));
            RFD = 0.5*((sqrt(2)*WF) + (cos(-pi/4)*cos(-pi/4)*XF) + (sin(-pi/4)*cos(-pi/4)*YF) + (sin(-pi/4)*ZF));
            LBD = 0.5*((sqrt(2)*WF) + (cos(pi-(pi/4))*cos(-pi/4)*XF) + (sin(pi-(pi/4))*cos(-pi/4)*YF) + (sin(-pi/4)*ZF));
            RBU = 0.5*((sqrt(2)*WF) + (cos(pi+(pi/4))*cos(pi/4)*XF) + (sin(pi+(pi/4))*cos(pi/4)*YF) + (sin(pi/4)*ZF));

disp('reproduktory jsou v pořadí: LFU RFD LBD RBU')
disp('(L - left,R - right,F - front,B - back,U - up,D - down)')

% zapis na disk
% 
% mkdir('c:\Dekoder-vystupy','tetrahedron');
% 
%         wavwrite(0.999*(LFU/max(abs(LFU))),48000,'c:\Dekoder-vystupy\tetrahedron\LFUfeed');                           
%         wavwrite(0.999*(RFD/max(abs(RFD))),48000,'c:\Dekoder-vystupy\tetrahedron\RFDfeed');        
%         wavwrite(0.999*(LBD/max(abs(LBD))),48000,'c:\Dekoder-vystupy\tetrahedron\LBDfeed');
%         wavwrite(0.999*(RBU/max(abs(RBU))),48000,'c:\Dekoder-vystupy\tetrahedron\RBUfeed');
%    
%         
        
        