% test funkce wiener.m

load s38_v3
x=xIDWT;
brum = 100*cos(2*pi*50/500*linspace(0,length(x)-1,length(x))); % pro pokus o filtraci brumu
x=x+brum;                                                     

figure(1)
plot(x)

% zadani vstupnich hodnot: *********************************
[xx,yy] = ginput(2);
xx = round(xx);
od=xx(1); do=xx(2);

PocetUrovni = 4;
filtryp = 'bior2.2';

Ksigma = 2;
prahovani = 2; % 1=tvrde, 2=mekke, 3=garrote

% potrebujeme zpozdit meze intervalu pro odhad urovne sumu,
% protoze jsou stanoveny pro vstupni signal
[xDWT,index,tau1] = NonDwt(x,filtryp,PocetUrovni,1);
odp = od + 2*tau1;
dop = do + 2*tau1;

% *********************************************************

[xp,xDWTprah] = pilot(x,odp,dop,filtryp,PocetUrovni,Ksigma,prahovani);

% *********************************************************

figure(2) % kresby pasem po DTWD a po prahovani --------------------------
pasem = PocetUrovni+1;
pasmo = pasem;
z = 1;

for k = 1:pasem
    subplot(pasem,1,pasmo), plot(xDWT(z:z+index(k)-1),'red'); hold on
    subplot(pasem,1,pasmo), plot(xDWTprah(z:z+index(k)-1),'black'); hold off
    origmax=1.1*max(xDWT(z:z+index(k)-1));
    origmin=1.1*min(xDWT(z:z+index(k)-1));
    axis([1 length(xDWT(z:z+index(k)-1)) origmin origmax]);
    line([odp odp],[origmin origmax])
    line([dop dop],[origmin origmax])
    z = z+index(k);
    zac(k) = z;
    pasmo = pasmo-1;
end
tit=['DTWT vstupu (cervene) a po prahovani (cerne)'];
tit=[tit ', filtry: ' filtryp ];
title(tit);

figure(3) % kresba vstupu, vystupu a jejich rozdilu ----------------------
subplot(2,1,1), plot(x,'red'), hold on, grid on,
subplot(2,1,1), plot(xp,'black'), hold off
tit=['vstup x(n) (cervene), vystup y(n) (cerne)'];
tit=[tit ', filtry: ' filtryp ];
title(tit);
ylabel('x(n), y(n) [uV]')

chyba=[]; 
chyba=x-xp;
subplot(2,1,2), plot(chyba,'black'), grid on
ylabel('e(n)=x(n)-y(n) [uV]'),

% ************************************************************************
% zadani vstupnich hodnot pro wienerovsky filtr: *************************

filtryw = 'bior2.2';

% potrebujeme zpozdit meze intervalu pro odhad urovne sumu,
% protoze jsou stanoveny pro vstupni signal
[xDWT,index,tau1w] = NonDwt(x,filtryw,PocetUrovni,1);
odw = od + 2*tau1w;
dow = do + 2*tau1w;

% ************************************************************************

% [y,xWTwien]=wiener(x,xp,wfiltr,PocetUrovni,sumod,sumdo);
[y,xDWTwien]=wiener(x,xp,filtryw,PocetUrovni,odw,dow);

% ************************************************************************

figure(4) % kresby pasem po DTWD a po wienerovske korekci ----------------
pasem = PocetUrovni+1;
pasmo = pasem;
z = 1;

for k = 1:pasem
    subplot(pasem,1,pasmo), plot(xDWT(z:z+index(k)-1),'red'); hold on
    subplot(pasem,1,pasmo), plot(xDWTwien(z:z+index(k)-1),'black'); hold off
    origmax=1.1*max(xDWT(z:z+index(k)-1));
    origmin=1.1*min(xDWT(z:z+index(k)-1));
    axis([1 length(xDWT(z:z+index(k)-1)) origmin origmax]);
    line([odw odw],[origmin origmax])
    line([dow dow],[origmin origmax])
    z = z+index(k);
    zac(k) = z;
    pasmo = pasmo-1;
end
tit=['DTWT vstupu (cervene) a po wienerovske korekci (cerne)'];
tit=[tit ', filtry: ' filtryp ];
title(tit);

figure(5) % kresba vstupu, vystupu a jejich rozdilu ----------------------
subplot(2,1,1), plot(x,'red'), hold on, grid on,
subplot(2,1,1), plot(y,'black'), hold off
tit=['vstup x(n) (cervene), vystup y(n) (cerne)'];
tit=[tit ', filtry pilot/wiener: ' filtryp '/' filtryw];
title(tit);
ylabel('x(n), y(n) [uV]')

chyba=[]; 
chyba=x-y;
subplot(2,1,2), plot(chyba,'black'), grid on
ylabel('e(n)=x(n)-y(n) [uV]'),






