function [cg,ph] = dcgloci2(Z1,Z2,Z3,Z4,Z5,Z6,Z7)
%
% Discrete Characteristic Gain/Phase Frequency Response.
%
% [CG,PH] = DCGLOCI2(SS_, Type, W, Ts) or
% [CG,PH] = DCGLOCI2(A, B, C, D, Type, W, Ts) produces the matrix G & PH 
% containing the characteristic Gain/Phase values for the discrete system
% sampled at Ts sec:
%              
%                x(k+1) = Ax(k) + Bu(k)
%                y(k) = Cx(k) + Du(k)
%                                                   -1
%     with the frequency response G(z) = C(zI - A)  B + D.
% DCGLOCI calculates the eigenvalues of one of the following depending 
% on the value of Type : 
%
%     Type = 1   ----   char( G(z) ) 
%     Type = 2   ----   char( inv( G(z) ) )
%     Type = 3   ----   char( I + G(z) ) 
%     Type = 4   ----   char( I + inv( G(z) ) 
%
% Vector W contains the frequencies at which the frequency response
% is to be evaluated.  The CG and PH matrices have rows
% which correspond to the char. gain/phase in decending order.
%

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

inargs='(a,b,c,d,Type,w,Ts)';
eval(mkargs(inargs,nargin,'ss'))

[mg] = dfreqrc(a,b,c,d,w,Ts);
[rmg,cmg] = size(mg);
[rb,cb] = size(b);
[rc,cc] = size(c);
gg = ones(rc,cb);
for is = 1 : cmg
  gg(:) = mg(:,is);
  if (Type == 1)
    char = eig(gg);
    cg(:,is) = sort(abs(char)); 
    ph(:,is) = sort(180./pi*imag(log(char))); 
  end
  if (Type == 2)
    char = eig(inv(gg));
    cg(:,is) = sort(abs(char));
    ph(:,is) = sort(180./pi*imag(log(char)));
  end
  if (Type == 3)
    [tmp1,tmp2]=size(gg);
    char = eig(eye(tmp1,tmp2)+gg);
    cg(:,is) = sort(abs(char)); 
    ph(:,is) = sort(180./pi*imag(log(char))); 
  end
  if (Type == 4)
    [tmp1,tmp2]=size(gg);
    char = eig(eye(tmp1,tmp2)+inv(gg));
    cg(:,is) = sort(abs(char)); 
    ph(:,is) = sort(180./pi*imag(log(char))); 
  end
end
cg = cg(cb:-1:1,:);
ph = ph(cb:-1:1,:);
%
% ----- End of DCGLOCI2.M ---- RYC/MGS 6/29/87 %
