function [q,mdl]=mdltest(l,n,data)
%MDLTEST MDL criterion for determining eigenvalue multiplicty.
%
% [Q,MDL]=MDLTEST(L,N,DATA)
%
% Inputs
% L : A dim(P) vector of unsorted eigenvalues.
% N : Sample size.
% DATA : 'REAL' for real data and 'COMPLEX' for complex data.
%
% Outputs
% Q : Estimated number of distinct eigenvalues (model order).
% MDL : A dim(P) vector. MDL(k+1) is the MDL value for the model with k
% distinct eigenvalues, k=0,...,P-1, Q=argmin_{k} MDL(k+1).
%
% Assumptions
% 1. The observations are multivariate normal.
% 2. Stochastic sources.
%
% References
% Mati Wax and Thomas Kailath, "Detection of Signals by Information
% Criteria", IEEE Trans. ASSP, vol.33, no.2, pp.387-92, April 1985.
% RB 17.02.05
l=flipud(sort(l(:)));
p=length(l);
if ~(exist('data')==1)
data='complex';
end;
switch lower(data)
case 'real'
for k=0:p-1
mdl(k+1)=-n*(p-k)*log(geomean(l(k+1:p))/mean(l(k+1:p)))+k*(p-k+1)*log(n);
end;
otherwise % default to 'complex'
for k=0:p-1
mdl(k+1)=-2*n*(p-k)*log(geomean(l(k+1:p))/mean(l(k+1:p)))+(k*(2*p-k)+1)*log(n);
end;
end;
[tmp ind]=sort(mdl);
q=ind(1)-1;
%a j
%disp(strcat('l =', format_vec('\t%8.2f', ',', l)));
%disp(strcat('mdl=', format_vec('\t%8.2f', ',', mdl)));
%disp(strcat('MDL Source Count =', sprintf('\t%8d', q)));
%{
the algorithm is not tested well. more tests should follow.
the algorith is not compared with other samailer algorithms.
for test code:
addpath '../../thirdparty/l1_ls';
addpath '../../thirdparty/cvx';
addpath '../../thirdparty/SpaRSA';
addpath '../../thirdparty/SB2_Release_200';
addpath '../../thirdparty';
addpath '../';
%}
return;