%----------- B-format dekoder (Semestralni prace) -------------------------
%............regular polygonal loudspeaker feeds......................
% 9.11.2008
% verze 1 
%--------------------------------------------------------------------------
function [varargout]=regPol(W,X,Y,fc)

    n = nargout;
    a = (2*pi)/n ;

% 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 0 rad
%           for i=0:(n-1)
%               uhel1(i+1) = i*a;
%           end
        
% loudspeaker at 0 + a/2 rad
            
            b = a/2;
            for i=0:(n-1)
                uhel(i+1,1) = b + (i*a);
            end

% loudspeaker feeds

            for k=1:n
                varargout{k}= 0.5*((sqrt(2)*WF) + (cos(uhel(k,1))*XF) + (sin(uhel(k,1))*YF));
            end

disp('Reproduktory jsou číslovány od předního levého proti směru hod. ručiček. ')

% zapis na disk
% 
% mkdir('c:\Dekoder-vystupy','regPolygon');
% 
%     for i=1:n
%         speakerFeed=varargout{i};
%         cislo=num2str(i);
%         text=['c:\Dekoder-vystupy\regPolygon\speaker',cislo];
%         wavwrite(0.999*(speakerFeed/max(abs(speakerFeed))),48000,text);
%     end            
%             
