function Dreg = regularisedictionary(D)
% REGULARISEDICTIONARY regularise dictionary for sparse representation
%
% Example
% Dreg = regularisedictionary(D)
%
% Input
% - D: initial dictionary matrix.
%
% Output
% - Dreg: regularised dictionary
%
% References:
%
% See also

% Author(s): Daniele Barchiesi
% Copyright 2011-2011

[n m] = size(D);	%dimension and number of atoms
[U S V] = svd(D);	%SVD decomposition of the dictionary
G = V*(S'*S)*V';	%gram matrix (equivalent to D'*D)
I = eye(m);			%identity matrix
lambda = 1;		    %regularisation coefficient

% Optimise the gram matrix
cvx_begin							%start cvx optimisation problem
	variable Greg(m,m) symmetric;	%declare optimisation variable to be an mxm symmetric matrix
	expression residual(m,m)		%declare residual as an intermediate calculation step
	residual = G-Greg;				%define residual
	minimise (max(max(abs(Greg-I))));	%define objective function
	subject to						%declare constraints
		Greg == semidefinite(m);	%positive semidefinite cone membership
		diag(Greg) == ones(m,1);	%unit diagonal
		norm(residual,'fro') <= lambda;	%
cvx_end

[~, Sgram Vgram] = svd(Greg);		%SVD decomposition of gramian
Snew = sqrt(Sgram(1:n,:)).*sign(S); %calculate singular values of the regularised dictionary
Dreg = U*Snew*Vgram';				%calculate regularised dictionary
Dreg = normc(Dreg);					%normalise columns of regularised dictionary
