% illustraction of GCV
% plot of GCV, plot of R,
% plot real signal, plot noiy and denoised signal
% minimisation of GCV, using Fibonacci-method 

N = 1024;
testsig = makesig((1:N),3); % generate a signal
figure(1)
clear gcv, clear R, clear th

sigvar = var(testsig,1);
sn = 18;
% sig = 1.1925;
sig = 0.2;

randn('seed',931316785);

% Gaussian noise or uniform ruis:

whitenoise = randn(size(testsig));
% whitenoise = randt(size(testsig),2);
% whitenoise = randexp(size(testsig),1);
% whitenoise = randgamint(size(testsig),1,20);
% whitenoise = whitenoise - mean(whitenoise);
% whitenoise = rand(size(testsig));
% B = fir1(20,0.6,'high');
% B = ...
%  [-1.249952937465984e-18  3.455252881204604e-03 -3.933584849155312e-03 ...
%  -7.221104373597301e-03 2.011451899152559e-02 8.437182327895390e-18 ...
%  -5.173180830302138e-02 5.064302371085747e-02 8.550406147725537e-02 ...
%  -2.965170712303975e-01 4.008138273429261e-01 -2.965170712303975e-01 ...
%  8.550406147725538e-02 5.064302371085747e-02 -5.173180830302140e-02 ...
%  8.437182327895392e-18 2.011451899152559e-02 -7.221104373597303e-03 ...
%  -3.933584849155314e-03 3.455252881204604e-03 -1.249952937465984e-18];
% noise1 = filter(B,1,whitenoise);
noise1 = whitenoise;

% next lines to get desired noise level: compute factor
% needed to multiply the noise to get a SNR or given variance

SNR1 = snr(testsig,noise1);
noivar1 = var(noise1,1);

% factor = 10^((SNR1-sn)/20)

factor = sig /sqrt(noivar1);
noise1 = noise1*factor;
noivar1 = var(noise1,1);
% noise1 = zeros(size(testsig));

% construct noisy signal and  plot

res = testsig + noise1;
plot(res)
title('input with noise')

N = length(res);
J = log2(N);
M = 5;
L = J - M;

% construct wavelet filter

qmf=ortfilt('Daubechies',3);
% qmf=ortfilt('Haar');
% qmf=MakeONFilter('Coiflet',2);
[h, g, ht, gt, sh, sg, sht, sgt] = qmf2hgs(qmf);

% wavelet transform

wcoef = FWT_BIO(res,L,ht,gt,sht,sgt);
vcoef = FWT_BIO(testsig,L,ht,gt,sht,sgt);

%wcru1 bevat de Wavelet Coef. van de RUis zelf

wcru1 = FWT_BIO(noise1,L,ht,gt,sht,sgt);

% find threshold by minimizing gcv and mse

[wthr thr] = mingcv(wcoef(2^L+1:N));
[wwthr opthr]  = minmse(vcoef(2^L+1:N),wcoef(2^L+1:N));

thmax = max(abs(wcoef((2^(L)+1):N)));
m = 50;
% thm = thmax/100000000000000000000000000000000000;
% thm = thmax/1;
% th1 = thm/m;
thm = min(6*opthr,thmax);
th1 = opthr/6;
% thm = thmax
th = (0:m-1)*(thm-th1)/(m-1) + th1; 
mse = MSE1(vcoef(2^L+1:N), wcoef(2^L+1:N),th);
gcv = GCV1(wcoef(2^L+1:N), th);

figure(2), plot(th,gcv,'--'), hold on
% plot(th,mse,'r')
plot(th,mse), hold off
title('GCV and mean square error')

wna = wcoef;
wna(2^L+1:N) = wthr;
wcver = wcoef - wna;

% wcver contains the noise (in waveletcoef) that was eliminated

xh = IWT_BIO(wna,L,h,g,sh,sg);
noise2 = xh - testsig;
wcru2 = FWT_BIO(noise1,L,ht,gt,sht,sgt);
SNR1 = snr(testsig,noise1)
SNR2 = snr(testsig,noise2)

wop = wcoef;
wop(2^L+1:N) = wwthr;
xhop = IWT_BIO(wop,L,h,g,sh,sg);


figure(3),clf, plot(xh)
title('denoised signal')

univthr = sqrt(2*log(N))*sig
wuniv = wcoef;
wuniv(2^L+1:N) = WST(wcoef(2^L+1:N),univthr);
xhuniv = IWT_BIO(wuniv,L,h,g,sh,sg);

