function [AB,BB,CB,DB] = bilin(Z1,Z2,Z3,Z4,Z5,Z6,Z7)
% [SS_B] = BILIN(SS_,VERS,TYPE,AUG) or
% [AB,BB,CB,DB] = BILIN(A,B,C,D,VERS,TYPE,AUG) performs a frequency
%      plane bilinear transform:
%          az + d                           d - bs
%    s = ---------  (if vers = 1); or z = ----------  (if vers = -1)
%          cz + b                           cs - a
%   such that
%                    -1                                -1
%       G(s) = C(Is-A) B + D <-------> G(z) = Cb(Iz-Ab)  Bb + Db       
% --------------------------------------------------------------------------
%   'Tustin' ----- Tustin Transform (s = 2(z-1)/T(z+1), aug = T).
%   'P_Tust' ----- Prewarped Tustin (s = w0(z-1)/tan(w0*T/2)(z+1),
%                                  aug = (T,w0), w0:prewarped freq.)
%   'BwdRec' ----- Backward Rectangular (s = (z-1)/Tz, aug = T).
%   'FwdRec' ----- Forward Rectangular (s = (z-1)/T, aug = T).
%   'S_Tust' ----- Shifted Tustin (2(z-1)/T(z/sh+1), aug = (T,sh), 
%                                sh: shifted circle coeff.).
%   'Sft_jw' ----- Shifted jw-axis Bilinear (aug = (b1,a1), circle coeff.).
%   'G_Bili' ----- General Bilinear (s = (az+d)/(cz+b), aug = (a,b,c,d)).
% -------------------------------------------------------------------------

% R. Y. Chiang & M. G. Safonov 7/15/88
% Copyright (c) 1988 by the MathWorks, Inc.
% All Rights Reserved.

inargs = '(A,B,C,D,vers,Type,aug)';
eval(mkargs(inargs,nargin,'ss'))

%
% ---- Transform Identification :
%
if Type == 'Tustin'
   T = aug;
   if vers == 1
      a = 2/T; b = 1; c = 1;  d = -a;
   else
      a = T/2; b = 1; c = -a; d = 1;
   end
end
%
if Type == 'P_Tust'
   T = aug(1,1);
   w0 = aug(1,2);
   if vers == 1
      a = w0/tan(w0*T/2); b = 1; c = 1;    d = -a;
   else
      a = tan(w0*T/2)/w0; b = 1; c = -a;   d = 1;
   end
end
%
if Type == 'BwdRec'
   T = aug; 
   if vers == 1
      a = 1;    b = 0; c = T;    d = -1;
   else
      a = 0;    b = 1; c = -T;   d = 1;
   end
end
%
if Type == 'FwdRec'
   T = aug;
   if vers == 1
      a = 1;   b = T; c = 0;     d = -1;
   else
      a = T;  b = 1;  c = 0;     d = 1;

   end
end
%
if Type == 'S_Tust'
   T = aug(1,1);
   sh = aug(1,2);
   if vers == 1
      a = 2/T; b = 1; d = -a; c = 1/sh;
   else
      a = 1; b = 2/T; c = -1/sh; d = b;
   end 
end
%
if Type == 'Sft_jw'
   xx = aug;
   if vers == 1
      a = 1; b = 1; c = 1/xx(1,1); d = xx(1,2);
   else
      a = -1; b = -1; c = 1/xx(1,1); d = xx(1,2);
   end
end
%
if Type == 'G_Bili'
   xx = aug;
   if vers == 1
      a = xx(1,1); b = xx(1,2); c = xx(1,3);, d = xx(1,4);
   else
      a = -xx(1,2); b = -xx(1,1); c = xx(1,3); d = xx(1,4);
   end
end
%
% ---- State-Space Representation :
%
[ra,ca] = size(A);
iidd = inv(a*eye(ra)-c*A);
AB = (b*A-d*eye(ra))*iidd;
BB = (a*b-c*d)*iidd*B;
CB = C*iidd;
DB = D + c*C*iidd*B;
%
if xsflag
   AB = mksys(AB,BB,CB,DB);
end
%
% ------- End of BILIN.M --- RYC/MGS 7/15/1987 ---%
