close all;
clear;

%zaddnie_1
iter = 5;
wav = 'db2';
figure('name','ukazka ze pomocou phi(t) sa da vyskladat polynom p(t) zvoleneho stupna');
[phi,psi,xval] = wavefun(wav,iter);
pole=zeros(1,500)
pole(1:97)=phi(1:97)
hold on
for i=0:10
pole(1+(i*32):97+(i*32))= pole(1+(i*32):97+(i*32)) + (1-0.5*(i+1)) * phi(1:97)
end
plot (pole(100:300))


%zaddnie_2
skladani=3;
sklon=1;
rad_polynomu=6;
figure('name','ukazka ze psi(t) nuluje polynom p(t) zvoleneho stupna');
[phi,psi,xval] = wavefun(wav,iter);
[x, velkost] = size(xval);
nosic = xval(velkost);
total_size=floor(velkost+skladani*velkost/nosic)+10;
func_long = zeros(1, total_size);
for(i=1:total_size) 
    func_long(i)=i-1;
end
func_long=(sklon*func_long/total_size).^rad_polynomu;
plot(func_long,'green');
hold on
posun = floor(velkost/nosic);
for i=1:skladani
    pole = zeros(1, total_size);
    pole(1+posun*i:1+(posun)*i+(velkost-1)) = psi(1:velkost);
    plot(pole);
    skalarny_sucin=func_long*pole';
    msg=sprintf('Skalarny sucin [%d] = %f' , i, skalarny_sucin);
    disp(msg);
end

%zaddnie_3


%zaddnie_4


% nastavenia waveletov

%zaddnie_5
typ='db3';
iter=5;
[phi,psi,t]=wavefun(typ,iter);
figure('name','phi(t) je ortogonalne na svoje posuny');
hold on;
plot(t,phi,'green');
plot(t,psi,'red');

%zaddnie_6
integral_phi=integral(phi,t)

%zaddnie_7
figure('name','h(n) je ortogonalne na svoje parne posuny');
pocet=length(t);
min=t(1);
max=t(pocet);
dlzka=max-min;
[h_,g_,h,g]=wfilters(typ);
hold on;
stem(h,'-og');
stem(g,'-.xr');
poceth=length(h);
k=-(floor(max));
for j=1:(2*max)+1
    m=round(k*pocet/dlzka);
    for i=1:pocet
        
        if (i-m)>0 & (i-m)<=pocet
            sucin(i)=phi(i)*phi(i-m);
        else
            sucin(i)=0;
        end  
    end
    autokorelacia(j)=integral(sucin,t);
    K(j)=k;
    if(k==0)
%        plot(t-k,phi,'red');
    else
 %       plot(t-k,phi,'blue');
    end
    k=k+1;
    clear sucin;
end
figure('name','h(n) je ortogonalne na g(n) a jeho posuny');

for j=1:2*poceth
    k=j-poceth;
    sucet=0;
    for n=1:poceth
        if n-2*k>0 & n-2*k<=poceth
            sucet=sucet+h(n)*h(n-2*k);
        end
    end
    autokorelaciah(j)=sucet;
    K(j)=2*k;
end

%zaddnie_8
figure('name','h(n) je ortogonalne na g(n) a jeho posuny');
stem(K,autokorelaciah,'-.xr');

for j=1:2*poceth
    k=j-poceth;
    sucet=0;
    for n=1:poceth
        if n-2*k>0 & n-2*k<=poceth
            sucet=sucet+h(n)*g(n-2*k);
        end
    end
    kroskorelacia(j)=sucet;
    K(j)=2*k;
end

stem(K,kroskorelacia,'-.xg');
axis([1-2*poceth,2*poceth,0,1]);

suma_hn=sum(h)
suma_gn=sum(g)


%1) V oblasti OMEGA, ze plati veta 2.2

Hom=fft(h);
for i=1:poceth/2
    suma22(i)=(abs(Hom(i)))^2+(abs(Hom(i+poceth/2))^2);
end

disp('Suma podla vety 2.2:');

suma22'

%2) ze H(Z) a G(z) su energeticky komplementarne filtre

Hom=fft(h);
Gom=fft(g)
for i=1:poceth
    suma(i)=(abs(Hom(i)))^2+(abs(Gom(i))^2);
end

disp('| H(e^j.Om) |^2 + | G(e^j.Om) |^2 pre celu periodu:');
suma'

%3) ze DP filtre formuju polpasmovy filter P(z) a HP filtre P(-z)

P=conv(h_,h)
P_=conv(g_,g)

%pozn: niekde chyba v znamienku




