function [glob,lok] = extremal(f)
%
%  Seek the global and local extremal points from a vector 
%  of function values.
%
%  Input
%    f         function values
%
%  Output
%    glob      index links to global minimum and maximum, that is,
%              f(glob(1)) = min(f) and f(glob(2)) = max(f)
%    lok       local minima and maxima
%              (including the global minimum and maximum)
%
%  Author:     Erkki Heikkola, emsh@mit.jyu.fi
%  Date:       November 15, 2000
%

glob = ones(1,2);
lok  = [];

n = length(f);
for k=2:n-1
%  global extremal points
   if f(k)>f(glob(1))
      glob(1) = k;
   elseif f(k)<f(glob(2))
      glob(2) = k;
   end
%  local maximum
   if f(k)>f(k-1) & f(k)>f(k+1)
      lok = [lok k];
   end
%  local minimum
   if f(k)<f(k-1) & f(k)<f(k+1)
      lok = [lok k];
   end
end

if f(n)>f(glob(1))
   glob(1) = n;
elseif f(n)<f(glob(2))
   glob(2) = n;
end
