function ox=adjph(x)
%ADJPH	Normalization of columns of a complex matrix.
%
%	ox=ADJPH(x) produces a complex matrix ox  
%	obtained by multiplying column vectors in x 
%	with phase factors exp(i*phi) such that the 
%	real and imaginary parts of column vectors in ox   
%	are orthogonal and the norm of the real part is 
%	at least the norm of the imaginary part.
%
%	As a lower-level routine, ADJPH is called by ARMODE.

%	Modified 2-Sep-97
%	Author: Tapio Schneider
%		AOS Program, Princeton University
%		Princeton, NJ 08544-0710
%		tapio@splash.Princeton.EDU

for j=1:size(x,2)
    a = real(x(:,j));
    b = imag(x(:,j));
    phi = .5*atan(2*dot(a,b)/(b'*b-a'*a));
    bnorm = norm(sin(phi).*a+cos(phi).*b); % norm of new imaginary part
    anorm = norm(cos(phi).*a-sin(phi).*b); % norm of new real part
    if bnorm > anorm 
       if phi < 0
          phi = phi-pi/2;
       else
          phi = phi+pi/2;
       end
    end
    ox(:,j) = x(:,j).*exp(i*phi);
end







