function [Gred Tl Tr] = baltrunc(Gfull,m)
% function [Gred Tl Tr] = baltrunc(Gfull,m)
%
%	Reduce the full system Gfull(s) to the reduced system Gred(s)
%	using the following 2-step procedure:
%
%	1. Find a balanced realization of the full system Gfull,
%
%		[sysb,hsv,T,Ti] = balreal(Gfull)
%
%	2. Truncate the system such that only m states remain, using the
%	parition of T and Ti as reported in the report, giving the
%	reduced-order system
%
%		\dot z = Tl A Tr + Tl B u
%		\hat y = C Tr z  + D u
%
%	3. Note that z = Tl x is unique, where x are the states of the
%	full-order system
%
%						made Jan 6 2010

% 1. Find a balanced realization of the full order system
[sysb,hsv,T,Ti] = balreal(Gfull);

% 2. Partion the matrices T and Ti into Tl and Tr
try
    Tl = T(1:m,:);
    Tr = Ti(:,1:m);
catch
    error('dimension mismatch, probably m > number of states in Gfull')
end

% construct the reduced order system
[A B C D] = deal(Gfull.a,Gfull.b,Gfull.c,Gfull.d); 
Gred = ss(Tl*A*Tr,Tl*B,C*Tr,D);
