function [l p] = waldcirc(z)
% WALDCIRC  Wald's type (ML) circularity detector assuming random sample from 
%           complex normal distribution
%
% INPUT PARAMETERS:
%    z       complex sample (vector of length n)
% OUTPUT 
%   l        value of the test statistic 
%   p        P-value 
% 
% (c) Esa Ollila, contact: esollila AT wooster.hut.fi
%
% If you use this program, please cite 
%    E. Ollila, J. Eriksson, V. Koivunen, "Complex univariate elliptically symmetric distributions", 
%       submitted to IEEE Transactions on Signal Processing, Dec. 2009. 
%-------------------------------------------------------------------------------

n = length(z);
vz  = var(z,1); % sample variance of z
pvz = mean((z-mean(z)).^2) ; % sample pseudo-variance of z 
cc = abs(pvz)/vz;  % sample circularity coefficient 
l = n*cc^2;

if license('test','statistics_toolbox')
   p =  1- chi2cdf(l,2); % p-value 
else 
   warning('Computation of the p-value requires STATISTICS toolbox'); 
   p = [ ] ;
end

