
function [y, thr] = mingcv(x)

% mingcv -- soft thresholding with min gcv threshold in 1D
%  Usage
%    [y, thr] = mingcv(x)
%  Inputs
%    x      input coefficients
%  Outputs
%    y      the shrunk output coefficients
%  Description
%    x is shrunk with threshold thr and placed in y
%    inverse wavelet transform of y gives result
%  Note
%    The optimal thr is found by Fibonacci search for gcv function

y = x;

%initialization Fibonacci - numbers
F0 = 1;
F1 = 1;
F2 = 2;
Niter = 1;

%compute number of iteration steps
m = 50;
b = max(abs(x)); 
a =  b / m;%(we can not compute the GCV in 0)
eps = 0.0001;
while (b - a) / F2 > eps
  F0 = F1;
  F1 = F2;
  F2 = F0 + F1;
  Niter = Niter + 1;
end

firstmove = 0;
while (firstmove == 0)

     v = a + (F1/F2) * (b - a);
     fv = GCV(x,v); 
     u = a + (F0/F2) * (b - a);
     fu = GCV(x,u);
     
     for k = 1:Niter - 1
      if (fu > fv)
         a = u;
         u = v;
         fu = fv;
         v = b-u+a;
         fv = GCV(x,v);
     firstmove = 1;
      else
         b = v;
         v = u;
         fv = fu;
         u = a+b-v;
         fu = GCV(x,u);
      end
     end
     
     if (b < 10^(-10))
       firstmove = 1;
     end % if
     
     a = b/m;
     
end %while (firstmove == 0)


thr = b;


y = WST(x,thr);
