function [abal,bbal,cbal,g,t] = obalreal(a,b,c)
%
% [ABAL,BBAL,CBAL,G,T] = OBALREAL(A,B,C) produces a balanced 
%    realization using B. C. Moore's  algorithm.
%  
%           G : diagonal of the balanced grammian (ordered)
%           T : balancing transformation
%

% R. Y. Chiang & M. G. Safonov 8/85
% Copyright (c) 1988 by the MathWorks, Inc.
% All Rights Reserved.
% ------------------------------------------------------------------

p = gram(a,b);
q = gram(a',c');
[up,sp,vp] = svd(p);
sph = diag(diag(sp).^0.5);
t1 = up*sph;
qq = t1'*q*t1;
[uqq,sqq,vqq] = svd(qq);
sq = diag(diag(sqq).^0.25);
t2 = uqq*inv(sq);
t = t1*t2;
abal = t \ a * t;
bbal = t \ b;
cbal = c * t;
g = diag(gram(abal,bbal))';
%
% ----- End of OBALREAL.M --- RYC/MGS 8/85 % 