function [vystup] = dwt()
%--------------------------------------------------------------------%
x=[0;0;1;0;0;0;0;0]               % Vstupny vektor (Pre  DWT (C_0)) - Parny mocnina 2
                            %                (Pre IDWT (C_m(n),d_m(n))
wavelet='db1'               % Typ pouziteho waveletu   (max 'db45')

transformacia='idwt'         % Typ Transformacie
%--------------------------------------------------------------------%
size_x = size(x);
M = log2(size_x(1));
s_x = size_x(1);

%% Tvorba Transformacnej Matice 
[t1 t2 h g] = wfilters(wavelet); % Funkcia ktora nam da diletacne koeficienty (h,g)
[t1 s_h] = size(h)
[t1 s_g] = size(g);

for j = 1:M 
    r = size_x(1)/2^j;
    c = size_x(1)/2^(j-1);
    H = zeros(r,c);        
    G = zeros(r,c);        
   
    for i = 1:r
        if 2*i - 1 + s_h - 1 > c                    
            H(i, 2*i-1 : end) = h(1: c-2*i+2);        
            G(i, 2*i-1 : end) = g(1: c-2*i+2);        
            if s_h - (c-2*i+2) > c                   
                z = c-2*i+3;
                rep = fix((s_h - (c-2*i+2)) / c)     
                for k = 1:rep
                    H(i,:) = H(i,:) + h(z + c*(k-1) : z + c*(k-1) + c-1); 
                    G(i,:) = G(i,:) + g(z + c*(k-1) : z + c*(k-1) + c-1); 
                end
                H(i, 1 : s_h - (z + c*(rep) - 1)) = H(i, 1 : s_h - (z + c*(rep) - 1)) + h(z + c*(rep) : end);
                G(i, 1 : s_h - (z + c*(rep) - 1)) = G(i, 1 : s_h - (z + c*(rep) - 1)) + g(z + c*(rep) : end);
            else
                H(i, 1 : s_h - (c-2*i+2) ) = H(i, 1 : s_h - (c-2*i+2) ) + h(c-2*i+3:end);     
                G(i, 1 : s_h - (c-2*i+2) ) = G(i, 1 : s_h - (c-2*i+2) ) + g(c-2*i+3:end);  
            end
        else
            H(i, 2*i-1 : 2*i-1+s_h-1) = h;          
            G(i, 2*i-1 : 2*i-1+s_g-1) = g;          
        end
       

    end
    T{j} = [H;G];
end

switch transformacia
    case 'dwt'
        y = x;
        coef = zeros(size_x);
        for j = 1:M
            Tj = cell2mat(T(j));     % Zlucovanie matic do jednej
            dwt = Tj*y;               % DWT
            [s_ys t1] = size(dwt);
            y = dwt(1 : s_ys/2);
            coef(s_x/(2^j) + 1 : s_x/(2^j) + s_ys/2) = dwt(s_ys/2 + 1 : end); % Skladanie c_m(n) a D_m(n) do vysledneho vektora   
        end
        coef(1)= y;
        vystup = coef;
        
    case 'idwt'
        ys = x(1); 
        y = 0;
        for j = M:-1:1;
            [s_ys t1] = size(ys);
            y = zeros(2*s_ys,1);    
            y(1 : s_ys) = ys;
            y(s_ys + 1 : end) = x(s_ys + 1 : 2*s_ys);
            Tj = cell2mat(T(j));    % Zlucenie matic 
            ys = Tj'*y;             % IDWT
       
        end
        vystup = ys;
        
end

return