function partition = FinalProducePartition( a, b, f, cut )
% FinalProducePartition using the curvature of the signal f, it computes the knots of a 
% partition. 
%
% Usage:        partition = FinalProducePartition( a, b, f, cut );
%
% Inputs:  
%   a,b         the end points of the interval    
%   f           signal 
%   cut         sub-divided level of the partition.
%  
% Outputs:
%  partition    the knots of the partition. The first entry is a and the last one is b

% See   http://www.ncrg.aston.ac.uk/Projects/HNLApprox/
%       http://www.nonlinear-approx.info/
L=max(size(f));
dist=(b-a)/L;
c=zeros(L,1);
for j=2:L-1
    c(j)=abs(((f(j+1)+f(j-1)-2*f(j))/dist^2)/(1+((f(j)-f(j-1))/dist)^2)^(3/2));
  %  fprint('%f ',c(j));
end

k=1;
re_j0=1;
partition(k)=a;
for j=3:L-2
    if c(j)>c(j-1) & c(j)>c(j+1)
        cut0=cut;
        if abs(re_j0-j)<cut
            cut0=floor(abs(re_j0-j));
        end
        re_j0=j;
        qi=partition(k);
        for t=1:cut0
          k=k+1;
          partition(k)=((cut0-t)*qi+t*(a+(j-1)*dist))/cut0;
       end
        
    end
end

   qi=partition(k);
   if abs(L-re_j0)<cut
       cut0=floor(abs(L-j));
   end
    for t=1:cut0
        k=k+1;
        partition(k)=((cut0-t)*qi+t*(b))/cut0;
    end

 % Zhiqiang Xu (2009)