
function [wt, thr] = minmse(v,w)

% minmse -- thresholding using min mse threshold in 1D
%  Usage
%    [y, thr] = minmse(v,w)
%  Inputs
%    v      clean input coefficients
%    w      noisy input coefficients
%  Outputs
%    wt     the shrunk output coefficients
%    thr    the threshold
%  Description
%    w is shrunk with threshold thr and placed in wt
%    The optimal thr is comupted by minimising the
%    Mean Square Error
%  Note
%    The optimal thr is found by Fibonacci search for gcv function

N = size(w,1)*size(w,2);
wt = w;

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

%compute Niter the number of iteration steps
m = 50;
b = max(abs(w)); 
a =  b / m;
eps = 0.0001;
while (b - a) / F2 > eps
  F0 = F1;
  F1 = F2;
  F2 = F0 + F1;
  Niter = Niter + 1;
end

firstmove = 0;
while (firstmove == 0)

     r = a + (F1/F2) * (b - a);
     fr = MSE(v,w,r); 
     l = a + (F0/F2) * (b - a);
     fl = MSE(v,w,l);
     
     for k = 1:Niter - 1
      if (fl > fr)
         a = l;
         l = r;
         fl = fr;
         r = b-l+a;
         fr = MSE(v,w,r);
     firstmove = 1;
      else
         b = r;
         r = l;
         fr = fl;
         l = a+b-r;
         fl = MSE(v,w,l);
      end
     end
     
     if (b < 10^(-10))
       firstmove = 1;
     end % if
     
     a = b/m;
     
end %while (firstmove == 0)


thr = b;


wt = WST(w,thr);
