function y = threshold(x,t1,type,t2)
% THRESHOLD - Prahovani vstupnich hodnot
%   THRESHOLD provadi prahovani vstupniho signalu X na zaklade zvolene
%   hodnoty prahu T1 resp. T2 a typu prahuvani TYPE.
%   
%   Obecny tvar funkce:
%   Y = THRESHOLD(X,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'

switch type 
    case 'hard'
        if exist('t2','var')==0 || t2<1
            t2 =1;
        end
        t = t1*t2;
        y = x.* (abs(x) > t);
    case 'soft'
        s = abs(x) - t1;
        s = (s + abs(s))/2;
        y = sign(x).*s;
    case 'firm'
        s = abs(x) - t1;
        if t2>t1
            tmp1 = x .* (abs(x) >= t1);
            tmp2 = x .* (abs(tmp1) > t2);
            tmpp = sign(tmp1 - tmp2) .*((t2*s)/(t2 -t1));
            y = tmpp +tmp2;
            else error('t2<t1')
        end
    case 'garrote'
        y = (x-((t1^2)./x)) .* (abs(x) > t1);
    case 'hyperbolic'
        y = (sign(x).*(sqrt((x.^2)-(t1.^2)))) .* (abs(x) > t1);
end