function s=ntest2(data)

% NTEST2      Test of bi-variate normality.
%
%             Example: data=randn(100,2);
%                      s=ntest2(data);
%
%             NTEST2(DATA)
%
%             5% critical values: See Bowman and Foster(1993), pg 536
%
%             Christian C. Beardah 1996

[n,d]=size(data);
ndata=inv(sqrtm(cov(data)))*(data-ones(n,1)*mean(data))';
ndata=ndata';
h=n^(-1/6);
[x,y,z]=kdfft2(ndata,'knorm',128,h,0);
% contour(x,y,z,10);
[X,Y]=meshgrid(x,y);
z1=npdf2(X,Y,[0 0]',(1+h^2)*eye(2));
% hold on; contour(x,y,z1,10);
deltax=x(2)-x(1);
deltay=y(2)-y(1);
s=deltax*deltay*sum(sum((z-z1).^2));