function [ P, Q ] = OrthProj( D, opt, tol, ind )
% OrthProj construct an orthogonal projection matrix onto the span of the columns of D.
%
% for opt=1 it uses the matlab function orth to orthogonalize D
% for opt=2 it uses the routine DREOrp (recommended when the columns of D are quasi
%           Linearly Dependent up tolerance tol, see DREor) 
% for opt=3 it uses the QR decomposition matlab function.
%
% Usage:    [ P, Q ] = OrthProj( D, opt, tol, ind );
%           [ P, Q ] = OrthProj( D );
%
% Inputs:
%   D       matrix the columns of which is a dictionary of normalized atoms
%   opt     to chose the method to calculate the projector (see above) default for opt=3
%   tol     (optional) the tolerance set for considering linearly dependant columns
%           [default tol = 1.0000e-7] 
%   ind     (optional) the indices of vectors to start the orthogonalization (see DreOr)
%
% Outputs:
%   Q       Orthogonal vectors such that P=Q*Q' 
%   P       Projector onto span of the columns of Q
%
% See also DREOrp DREOr qr orth

% See   http://www.ncrg.aston.ac.uk/Projects/HNLApprox/
%       http://www.nonlinear-approx.info/

% setting of default tolerance parameter
if (nargin<4) |(isempty(ind)==1); ind=[];      end
if (nargin<3) |(isempty(tol)==1); tol=1e-7;    end
if (nargin<2) |(isempty(opt)==1); opt=3;  end
%
switch opt 
 case 1
 Q=orth(D);
 P=Q*Q';
 case 2
 [Q]=DREOrp(D,tol,ind);
 P=Q*Q';
 case 3
 [Q,R]=qr(D,0);
 P=Q*Q';
 otherwise
 disp('option "opt" should be 1 2 or 3');
end
%
%Laura REBOLLO-NEIRA 2008
%
