home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / control / zp2ss.m < prev    next >
Text File  |  1999-03-05  |  4KB  |  144 lines

  1. # Copyright (C) 1996 A. Scottedward Hodel 
  2. #
  3. # This file is part of Octave. 
  4. #
  5. # Octave is free software; you can redistribute it and/or modify it 
  6. # under the terms of the GNU General Public License as published by the 
  7. # Free Software Foundation; either version 2, or (at your option) any 
  8. # later version. 
  9. # Octave is distributed in the hope that it will be useful, but WITHOUT 
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  11. # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
  12. # for more details.
  13. # You should have received a copy of the GNU General Public License 
  14. # along with Octave; see the file COPYING.  If not, write to the Free 
  15. # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  16.  
  17. function [a,b,c,d] = zp2ss(zer,pol,k)
  18. # [A,B,C,D] = zp2ss(zer,pol,k)
  19. # Conversion from zero / pole to state space.
  20. # Inputs: 
  21. #   zer,  pol: vectors of (possibly) complex poles and zeros of a transfer
  22. #              function.  Complex values must come in conjugate pairs
  23. #              (i.e., x+jy in zer means that x-jy is also in zer)
  24. #   k:  real scalar (leading coefficient)
  25. # Outputs:
  26. #  A, B, C, D:
  27. # The state space system
  28. #      .
  29. #      x = Ax + Bu
  30. #      y = Cx + Du
  31. #
  32. # is obtained from a vector of zeros and a vector of poles via the
  33. # function call [a,b,c,d] = zp2ss(zer,pol,k).  The vectors 'zer' and 
  34. # 'pol' may either be row or column vectors.  Each zero and pole that
  35. # has an imaginary part must have a conjugate in the list.
  36. # The number of poles must at least equal the number of zeros.
  37. # k is a gain that is associated with the zero vector.
  38.  
  39. # Written by David Clem August 15, 1994
  40.  
  41.   sav_val = empty_list_elements_ok;
  42.   empty_list_elements_ok = 1;
  43.  
  44.   if(nargin != 3)
  45.     error("Incorrect number of input arguments");
  46.   endif
  47.  
  48.   if(! (is_vector(zer) | isempty(zer)) )
  49.     error(["zer(",num2str(rows(zer)),",",num2str(columns(zer)), ...
  50.     ") should be a vector"]);
  51.   elseif(! (is_vector(pol) | isempty(pol) ) )
  52.     error(["pol(",num2str(rows(pol)),",",num2str(columns(pol)), ...
  53.     ") should be a vector"]);
  54.   elseif(! is_scalar(k))
  55.     error(["k(",num2str(rows(k)),",",num2str(columns(k)), ...
  56.     ") should be a scalar"]);
  57.   elseif( k != real(k))
  58.     warning("zp2ss: k is complex")
  59.   endif
  60.  
  61.   zpsys = ss2sys([],[],[],k);
  62.  
  63.   # Find the number of zeros and the number of poles
  64.   nzer=length(zer);
  65.   npol =length(pol);
  66.  
  67.   if(nzer > npol)
  68.     error([num2str(nzer)," zeros, exceeds number of poles=",num2str(npol)]);
  69.   endif
  70.  
  71.   # Sort to place complex conjugate pairs together
  72.   zer=sortcom(zer);
  73.   pol=sortcom(pol);
  74.  
  75.   # construct the system as a series connection of poles and zeros
  76.   # problem: poles and zeros may come in conjugate pairs, and not
  77.   # matched up!
  78.  
  79.   # approach: remove poles/zeros from the list as they are included in
  80.   # the ss system
  81.  
  82.   while(length(pol))
  83.  
  84.     # search for complex poles, zeros
  85.     cpol=[];    czer = [];
  86.     if(!isempty(pol))
  87.       cpol = find(imag(pol) != 0);
  88.     endif
  89.     if(!isempty(zer))
  90.       czer = find(imag(zer) != 0);
  91.     endif
  92.  
  93.     if(isempty(cpol) & isempty(czer))
  94.       pcnt = 1;
  95.     else 
  96.       pcnt = 2;
  97.     endif
  98.  
  99.     num=1;    # assume no zeros left.
  100.     switch(pcnt)
  101.     case(1)
  102.       # real pole/zero combination
  103.       if(length(zer))
  104.         num = [1 -zer(1)];  
  105.         zer = zer(2:length(zer));
  106.       endif
  107.       den = [1 -pol(1)];
  108.       pol = pol(2:length(pol));
  109.     case(2)
  110.       # got a complex pole or zero, need two roots (if available)
  111.       if(length(zer) > 1)
  112.         [num,zer] = zp2ssg2(zer);    # get two zeros
  113.       elseif(length(zer) == 1)
  114.         num = [1 -zer];            # use last zero (better be real!)
  115.         zer = [];
  116.       endif
  117.       [den,pol] = zp2ssg2(pol);        # get two poles
  118.     otherwise
  119.       error(["pcnt = ",num2str(pcnt)])
  120.     endswitch
  121.  
  122.     # pack tf into system form and put in series with earlier realization
  123.     zpsys1 = tf2sys(num,den,0,"u","yy");
  124.  
  125.     # change names to avoid warning messages from sysgroup
  126.     zpsys  = syssetsignals(zpsys,"in","u1",1);
  127.     zpsys1 = sysupdate(zpsys1,"ss");
  128.     nn     = sysdimensions(zpsys);        # working with continuous system
  129.     zpsys  = syssetsignals(zpsys,"st", sysdefioname(nn,"x"));
  130.     nn1    = sysdimensions(zpsys1);
  131.     zpsys1 = syssetsignals(zpsys1,"st",sysdefioname(nn1,"xx"));
  132.  
  133.     zpsys = sysmult(zpsys,zpsys1);
  134.  
  135.   endwhile 
  136.  
  137.   [a,b,c,d] = sys2ss(zpsys);
  138.  
  139.   empty_list_elements_ok = sav_val;
  140. endfunction
  141.  
  142.