%----------- B-format dekoder (Semestralni prace) -------------------------
%............octahedron layout loudspeaker feeds......................
% 16.11.2008
% verze 1 
%--------------------------------------------------------------------------
function [FU FD BU BD L R]=octahedron(W,X,Y,Z,fc,eps)

% 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 angles
 
        elevFU = (eps/2) * (pi/180);
        elevFD = -(eps/2)*(pi/180);
        elevBU = (eps/2) * (pi/180);
        elevBD = -(eps/2)*(pi/180);
%         elevBU = pi - ((eps/2)*(pi/180));
%         elevBD = pi + ((eps/2)*(pi/180));
 
% loudspeaker feeds

            FU = 0.5*((sqrt(2)*WF) + (cos(0)*cos(elevFU)*XF) + (sin(0)*cos(elevFU)*YF) + (sin(elevFU)*ZF));
            FD = 0.5*((sqrt(2)*WF) + (cos(0)*cos(elevFD)*XF) + (sin(0)*cos(elevFD)*YF) + (sin(elevFD)*ZF));
            BU = 0.5*((sqrt(2)*WF) + (cos(pi)*cos(elevBU)*XF) + (sin(pi)*cos(elevBU)*YF) + (sin(elevBU)*ZF));
            BD = 0.5*((sqrt(2)*WF) + (cos(pi)*cos(elevBD)*XF) + (sin(pi)*cos(elevBD)*YF) + (sin(elevBD)*ZF));
            L  = 0.5*((sqrt(2)*WF) + (cos(pi/2)*cos(0)*XF) + (sin(pi/2)*cos(0)*YF) + (sin(0)*ZF));
            R  = 0.5*((sqrt(2)*WF) + (cos(-pi/2)*cos(0)*XF) + (sin(-pi/2)*cos(0)*YF) + (sin(0)*ZF));

disp('reproduktory jsou v pořadí: FU FD BU BD L R')
disp('(L - left,R - right,F - front,B - back,U - up,D - down)')       

% zapis na disk
% 
% mkdir('c:\Dekoder-vystupy','octahedron');
% 
%         wavwrite(0.999*(FU/max(abs(FU))),48000,'c:\Dekoder-vystupy\octahedron\FUfeed');                           
%         wavwrite(0.999*(FD/max(abs(FD))),48000,'c:\Dekoder-vystupy\octahedron\FDfeed');        
%         wavwrite(0.999*(BU/max(abs(BU))),48000,'c:\Dekoder-vystupy\octahedron\BUfeed');
%         wavwrite(0.999*(BD/max(abs(BD))),48000,'c:\Dekoder-vystupy\octahedron\BDfeed');
%         wavwrite(0.999*(L/max(abs(L))),48000,'c:\Dekoder-vystupy\octahedron\Lfeed');
%         wavwrite(0.999*(R/max(abs(R))),48000,'c:\Dekoder-vystupy\octahedron\Rfeed');
%                 
%         