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

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

% high-frequency boost k1
    
    G1 = 1.76;
    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];
    
% high-frequency cut k2    
    
    G2 = 1.25;
    k2 = 10^(-G2/20);
    jmenovatel2 = k2 + sqrt(2*k2)*m + m^2;
    b02 = (1 + sqrt(2)*m + m^2) / jmenovatel2;
    b12 = (2*(m^2 - 1)) / jmenovatel2;
    b22 = (1 - sqrt(2)*m + m^2) / jmenovatel2;
    a12 = (2*(m^2 - k2)) / jmenovatel2;
    a22 = (k2 - sqrt(2*k2)*m + m^2) / jmenovatel2;
    
    b2 = [b02 b12 b22];
    a2 = [1 a12 a22];
    
    
% vysledek

    WF = filter(b1,a1, W);
    XF = filter(b2,a2, X);
    YF = filter(b2,a2, Y);
    
% loudspeaker at fi/2
            
           uhelLF = (fi/2) * (pi/180);
           uhelRF = -(fi/2)*(pi/180);
           uhelLB = pi - ((fi/2)*(pi/180));
           uhelRB = pi + ((fi/2)*(pi/180));

% loudspeaker feeds

           LF = 0.5*(WF + ( (1/sqrt(2)*cos(uhelLF)) * XF) + ( (1/sqrt(2)*sin(uhelLF)) * YF));
           RF = 0.5*(WF + ( (1/sqrt(2)*cos(uhelRF)) * XF) + ( (1/sqrt(2)*sin(uhelRF)) * YF));
           LB = 0.5*(WF + ( (1/sqrt(2)*cos(uhelLB)) * XF) + ( (1/sqrt(2)*sin(uhelLB)) * YF));
           RB = 0.5*(WF + ( (1/sqrt(2)*cos(uhelRB)) * XF) + ( (1/sqrt(2)*sin(uhelRB)) * YF));

disp('reproduktory jsou v pořadí: LF RF LB RB')
disp('(L - left,R - right,F - front,B - back)')

% zapis na disk        
% 
% mkdir('c:\Dekoder-vystupy','rectangular');           
% 
%         wavwrite(0.999*(LF/max(abs(LF))),48000,'c:\Dekoder-vystupy\rectangular\LFfeed');                           
%         wavwrite(0.999*(LB/max(abs(LB))),48000,'c:\Dekoder-vystupy\rectangular\LBfeed');        
%         wavwrite(0.999*(RB/max(abs(RB))),48000,'c:\Dekoder-vystupy\rectangular\RBfeed');
%         wavwrite(0.999*(RF/max(abs(RF))),48000,'c:\Dekoder-vystupy\rectangular\RFfeed');           
