function [xp,xWTprah] = pilot(signal,oddo,filtry,PocetUrovni,Ksigma,prahovani);
% filtrace s redundantni DTWT
% signal ... vstupni signal
% sumod, sumdo ... indexy pro odhad urovne sumu (od, do)
% filtry ... banka filtru, napr. 'bior2.2'
% PocetUrovni ... pocet stupnu rozkladu, 
% Ksigma ... konstanta nasobici smerodatnou dochylku pro stanoveni prahu
% prahovani ... tvrde (1), mekke (2), garrote (3)
%
% vystupy:
% xp ... filtrovany signal
% xWTprah ... vektor DTWT vstupu po prahovani
%         pasma pro PocetUrovni=4: ... 4. 3. 2. 1. a 0. (0. je vystup DP)

x=signal;
sumod = oddo(1);
sumdo = oddo(2);
% ---------------------------------------------------------------------------
% DTWT bez podvzorkovani, 
% vstup: x,	vystup: xDWT

[xDWT, index, tau1] = NonDwt(x,filtry,PocetUrovni,1);
% xWT = xDWT; 

% ---------------------------------------------------------------------------
% prahovani v pasmech DTWT signalu - intuitivni prah K*sigma
% odhad smerodatne odchylky sumu z useku kazdeho pasma zvlast
% vstup: xDWT
% vystup: xDWT

pasem = PocetUrovni+1;
z = 1;
for k = 1:pasem
    z = z+index(k);
    zac(k) = z;
end

delka=sumdo-sumod+1;

for k = PocetUrovni : -1 : 1
    stred=sum(xDWT(zac(k)+sumod:zac(k)+sumdo))/delka;
    sigma=sqrt(sum((xDWT(zac(k)+sumod:zac(k)+sumdo)-stred).^2)/delka);
    th(k)=Ksigma*sigma;
    for i=zac(k):zac(k)+index(k+1)-1
      if abs(xDWT(i))<= th(k)   
        xDWT(i)=0;
      else
          if prahovani == 2
              xDWT(i)=sign(xDWT(i))*(abs(xDWT(i))-th(k));
          end
          if prahovani == 3
              xDWT(i)=xDWT(i)-th(k)^2/xDWT(i);
          end
      end  
    end   
end

xWTprah = xDWT;

% -----------------------------------------------------------------------
% IDTWT, korekce zpozdeni, kresby
% vstup: xDWT
% vystup: xp
[xIDWT, tau2] = NonIDwt(xDWT,index,filtry,PocetUrovni);

xIDWT = CancelDelay(xIDWT,filtry,PocetUrovni,tau1,tau2);
%tau1
%tau2
xp = xIDWT;

