%----------- B-format dekoder (Semestralni prace) -------------------------
%............birectangle layout loudspeaker feeds......................
% 16.11.2008
% verze 1 
% format vstupnich dat!!!
%--------------------------------------------------------------------------
function [LF RF LB RB LU RU LD RD]=birectangle(W,X,Y,Z,fc,fi)

% 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
 
           azimLF = (fi/2) * (pi/180);
           azimRF = -(fi/2)*(pi/180);
           azimLB = pi - ((fi/2)*(pi/180));
           azimRB = pi + ((fi/2)*(pi/180));
           
           elevLU = (fi/2) * (pi/180);
           elevLD = -(fi/2)*(pi/180);
           elevRD = -(fi/2)*(pi/180);
           elevRU = (fi/2) * (pi/180);
%            elevRD = pi + ((fi/2)*(pi/180));
%            elevRU = pi - ((fi/2)*(pi/180));
 
% loudspeaker feeds

            LF = 0.5*((sqrt(2)*WF) + (cos(azimLF)*cos(0)*XF) + (sin(azimLF)*cos(0)*YF) + (sin(0)*ZF));
            RF = 0.5*((sqrt(2)*WF) + (cos(azimRF)*cos(0)*XF) + (sin(azimRF)*cos(0)*YF) + (sin(0)*ZF));
            LB = 0.5*((sqrt(2)*WF) + (cos(azimLB)*cos(0)*XF) + (sin(azimLB)*cos(0)*YF) + (sin(0)*ZF));
            RB = 0.5*((sqrt(2)*WF) + (cos(azimRB)*cos(0)*XF) + (sin(azimRB)*cos(0)*YF) + (sin(0)*ZF));
            LU = 0.5*((sqrt(2)*WF) + (cos(pi/2)*cos(elevLU)*XF) + (sin(pi/2)*cos(elevLU)*YF) + (sin(elevLU)*ZF));
            RU = 0.5*((sqrt(2)*WF) + (cos(-(pi/2))*cos(elevRU)*XF) + (sin(-(pi/2))*cos(elevRU)*YF) + (sin(elevRU)*ZF));
            LD = 0.5*((sqrt(2)*WF) + (cos(pi/2)*cos(elevLD)*XF) + (sin(pi/2)*cos(elevLD)*YF) + (sin(elevLD)*ZF));
            RD = 0.5*((sqrt(2)*WF) + (cos(-(pi/2))*cos(elevRD)*XF) + (sin(-(pi/2))*cos(elevRD)*YF) + (sin(elevRD)*ZF));

disp('reproduktory jsou v pořadí: LF RF LB RB LU RU LD RD')
disp('(L - left,R - right,F - front,B - back,U - up,D - down)')

% zapis na disk
% 
% mkdir('c:\Dekoder-vystupy','birectangle');
% 
%         wavwrite(0.999*(LF/max(abs(LF))),48000,'c:\Dekoder-vystupy\birectangle\LFfeed');                           
%         wavwrite(0.999*(LB/max(abs(LB))),48000,'c:\Dekoder-vystupy\birectangle\LBfeed');        
%         wavwrite(0.999*(RB/max(abs(RB))),48000,'c:\Dekoder-vystupy\birectangle\RBfeed');
%         wavwrite(0.999*(RF/max(abs(RF))),48000,'c:\Dekoder-vystupy\birectangle\RFfeed');
%         wavwrite(0.999*(LU/max(abs(LU))),48000,'c:\Dekoder-vystupy\birectangle\LUfeed');
%         wavwrite(0.999*(LD/max(abs(LD))),48000,'c:\Dekoder-vystupy\birectangle\LDfeed');
%         wavwrite(0.999*(RD/max(abs(RD))),48000,'c:\Dekoder-vystupy\birectangle\RDfeed');
%         wavwrite(0.999*(RU/max(abs(RU))),48000,'c:\Dekoder-vystupy\birectangle\RUfeed');
%         