function out = wpakthresh(in,t1,type,t2)
% WPAKTHRESH - Prahovani keoficientu transformace Wavelet Packet
%   WPAKTHRESH prahuje keoficienty IN podle T1 prip. T2 zavislosti 
%   na zvolenem typu prahovani TYPE. Vystupem jsou prahovane hodnoty OUT.
% 
% Obecny tvar funkce:
% OUT = WPAKTHRESH(IN,T1,TYPE,T2)
% 
%   Typ prahovani TYPE muze byt zvolen z nasledujicich:
% 
%           'hard'       -   Hard threshold
%                               T2 < 1 ==> T2 = 1
%                               obvykle T2 = 3
%                               T2 nepovinne
%           'soft'       -   Soft threshold
%                               X, T1
%           'firm'       -   Semi-soft threshold
%                               X, T1, T2
%                               T2 > t1
%           'garrote'    -   Non-negative Garrotte
%                               X, T1
%           'hyperbolic' -   Hyperbolic threshold
%                               X, T1
% 
%   T1 - udava hodnotu prahu 
%   T2 - udava pridavnou hodnotu potrebnou pri prahovani typou 'garrote',
%        prip. 'hard'
%   T1 a T2 se voli z rozsahu [0 1]

%% Urceni delky bunky
leng = length(in);
%% Definice nejvyssi urovne
in{1}{1} = [];
%% Ponechani potrebnych vzorku
for i = 2:1:leng
    d = 1;
    for k = 1:4:length(in{i})
        if isempty(in{i}{k}) == 0 || isempty(in{i}{k+1}) == 0 || isempty(in{i}{k+2}) == 0 || isempty(in{i}{k+3}) == 0
           in{i-1}{d} = [];
            d = d+1; 
        else d = d+1; 
        end        
    end    
end
%% Urceni maximalni hodnoty pro normalizaci
maxi = 0;
d = 1;
for i = leng:-1:2
    for k = 1:length(in{i})
        if isempty(in{i}{k}) == 0
            maxii = max(max(in{i}{k}));
            if maxii > maxi
                maxi = maxii;
            end
            d = d+1; 
        end
    end    
end
%% Definice pomocnych promennych
t1 = maxi*t1;
if exist('t2','var')==1
    t2 =t2*maxi;
end
%% Slozeni vystupu do puvodniho tvaru s ponechanim CFS keoficientu
switch type
    case 'hard'
        if exist('t2','var')==0 || t2<1
            t2 =1;
        end
        t = t1*t2;
        for i = 2:leng
            f = 1;
            for k = 1:length(in{i})
                if isempty(in{i}{f})==0
                    out{i}{f} = in{i}{f} .* (abs(in{i}{f})>t1);
                    f = f+1;
                else out{i}{f} = [];
                     f=f+1;
                end 
            end
        end
    case 'soft'
        for i = 2:leng
            f = 1;
            for k = 1:length(in{i})
                if isempty(in{i}{f})==0
                    st{i}{f} = abs(in{i}{f}) - t1;
                    st{i}{f} = (st{i}{f} + abs(st{i}{f}))/2;
                    out{i}{f} = sign(in{i}{f}).*st{i}{f};
                    f = f+1;
                else out{i}{f} = [];
                     f=f+1;
                end 
            end
        end
    case 'firm'
        for i = 2:leng
            f = 1;
            for k = 1:length(in{i})
                if isempty(in{i}{f})==0
                    st{i}{f} = abs(in{i}{f}) - t1;
                    if t2>t1
                        tmp1{i}{f} =  in{i}{f}.* (abs(in{i}{f})>=t1);
                        tmp2{i}{f} =  in{i}{f}.* (tmp1{i}{f}>t2);
                        tmpp{i}{f} = sign(tmp1{i}{f}-tmp2{i}{f}).* ((t2*st{i}{f})/(t2-t1));
                        out{i}{f} = tmpp{i}{f} +tmp2{i}{f};
                        f = f+1;
                    else error('t2<t1');
                    end
                else out{i}{f} = [];
                     f=f+1;
                end 
            end
        end
    case 'garrote'
        for i = 2:leng
            f = 1;
            for k = 1:length(in{i})
                if isempty(in{i}{f})==0
                    out{i}{f} =(in{i}{f}-((t1^2)./in{i}{f})) .* (abs(in{i}{f}) > t1);
                    f = f+1;
                else out{i}{f} = [];
                     f=f+1;
                end 
            end
        end
    case 'hyperbolic'
        for i = 2:leng
            f = 1;
            for k = 1:length(in{i})
                if isempty(in{i}{f})==0
                    out{i}{f} = (sign(in{i}{f}).*(sqrt((in{i}{f}.^2)-(t1.^2)))) .* (abs(in{i}{f}) > t1);
                    f = f+1;
                else out{i}{f} = [];
                     f=f+1;
                end 
            end
        end
        
end