function [ DS0, Di0, beta0, c0, Q0 ] = OOMPKSwapRefi( f, D, tol, No, ind, swi, sws )
% OOMPKSwapRefi Refiment of OOMP by kswapping and backward deleting steps
%
% It creates an atomic decomposition for approximation a signal using OOMP method up to a
% given tolerance. When possible, the sparsity is improved afterwards by a combination of
% kswapping and backward deleting steps. 
%
% You can choose a tolerance, the maximum number of atoms in the decomposition and an
% initial subspace to influence the OOMP algorithm. Non-selected atoms subtracted by their
% component in the chosen space are also available. 
%
% Usage:    [ D0, Di0 ] = OOMPKSwapRefi( f, D, tol );
%           [ DS0, Di0, Q0, beta0, c0 ] = OOMPKSwapRefi( f, D, tol, No, ind );
%
% Inputs:
%   f       analyzing signal
%   D       dictionary of normalized atoms
%   tol     desired distance between f and its approximation the routine will stop if
%           norm(f'-Dsub*(f*beta)')*sqrt(delta)<tol where delta=1/L (L is number of points 
%           in a sample) or delta=1, which is the default in OOMPF (to change this
%           uncomment the corresponding line in OOMPF)
%   No      (optional) maximal number of atoms to choose, if the number of chosen atoms
%           equals to No, OOMP routine will stop (default No=size(D,2) 
%   ind     (optional) indices determining  the initial subspace for OOMP
%   swi     (optional) minimum number of atoms to swap (defaul=1)
%   sws     (optional) maximum number of atoms to swap (defaul=all) 
%   []      can be used for sws, swi, ind, No, and tol
%
% Outputs:
%   DS0     the dictionary D rearranged according to the selection process DS0(:,1:k)
%           contains the atoms chosen into the atomic decomposition
%   Di0     indices of atoms in DS0 written w.r.t the original D
%   beta0   'k' biorthogonal functions corresponding to new DS0(:,1:k)
%   c0      'k' coefficients of the atomic decomposition
%   Q0      Q(:,1:k) contains orthonormal functions spanning  DS0(:,1:k), Q(:,k+1:N)
%           contains DS0(:,k+1:N) subtracted by the projection onto the space generated by
%           Q0(:,1:k) (resp. DS0(:,1:k))

% See   http://www.ncrg.aston.ac.uk/Projects/HNLApprox/
%       http://www.nonlinear-approx.info/
name='OOMP_KSWAP_REFI';  %name of routine
[L,N]=size(D);
if (nargin<7)| (isempty(sws)==1) sws=[];end
if (nargin<6)| (isempty(swi)==1) swi=1;end
if (nargin<5)| (isempty(ind)==1) ind=[];end
if (nargin<4)|(isempty(No)==1)   No=N;end
if (nargin<3)|(isempty(tol)==1)  tol=0.01*norm(f);end;

%first stage:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%OOMP to select "No" atoms or  achieve error tol 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
[DS0,Di0,beta0,c0,Q0]=OOMP(f,D,tol,No);
%
if isempty(sws)==1 sws=(size(beta0,2)); end
flag=1;
%
%loop till possible
%
while flag==1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Swapping to improve the previous error, if possible 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
tols=1e-14;
[re,resid,DS1,Di1,beta1,c1,Q1]=KSwapping(f,DS0,Di0,beta0,c0,Q0,swi,sws,tols);
%
[r1,s2]=min(resid)

[re,resid,DS1,Di1,beta1,c1,Q1]=KSwapping(f,DS0,Di0,beta0,c0,Q0,s2,s2,tols);

sdi=size(beta1,2);
sdi0=size(beta0,2);
err1=norm(f*beta1*D(:,Di1(1:sdi))'-f);
err0=norm(f*beta0*D(:,Di0(1:sdi0))'-f);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%BOOMP to  get back to error  err0 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
      if err1 < err0
       clear DS0 Di0 Q0 beta0
%
       [DS0,Di0,beta0,c0,Q0]=BOOMPQ(f,DS1,Di1,beta1,Q1,err0);
        sdi=size(beta1,2);
        sdi0=size(beta0,2);
               if sdi0==sdi; break; end
                 clear DS1 Di1 Q1 beta1
       else 
           flag=2; 
       end
end 
