home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts / control / c2d.m < prev    next >
Encoding:
Text File  |  1999-12-15  |  5.2 KB  |  170 lines

  1. ## Copyright (C) 1993, 1994, 1995 John W. Eaton
  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. ## 
  10. ## Octave is distributed in the hope that it will be useful, but WITHOUT
  11. ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. ## FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13. ## for more details.
  14. ## 
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
  18.  
  19. ## -*- texinfo -*-
  20. ## @deftypefn {Function File } { @var{dsys} =} c2d (@var{sys}@{, @var{opt}, @var{T}@})
  21. ## @deftypefnx {Function File } { @var{dsys} =} c2d (@var{sys}@{, @var{T}@})
  22. ## 
  23. ## @strong{Inputs}
  24. ## @table @var
  25. ## @item sys
  26. ##  system data structure (may have both continuous time and discrete time subsystems)
  27. ## @item opt
  28. ## string argument; conversion option (optional argument; 
  29. ## may be omitted as shown above) 
  30. ## @table @code
  31. ## @item "ex" 
  32. ## use the matrix exponential (default)
  33. ## @item "bi" 
  34. ## use the bilinear transformation
  35. ## @end table
  36. ## @example
  37. ##     2(z-1)
  38. ## s = -----
  39. ##     T(z+1)
  40. ## @end example
  41. ## FIXME: This option exits with an error if @var{sys} is not purely 
  42. ## continuous. (The @code{ex} option can handle mixed systems.)
  43. ## @item @var{T}
  44. ## sampling time; required if sys is purely continuous.
  45. ## 
  46. ## @strong{Note} If the 2nd argument is not a string, @code{c2d} assumes that
  47. ## the 2nd argument is @var{T} and performs appropriate argument checks.
  48. ## @end table
  49. ## 
  50. ## @strong{Outputs}
  51. ## @var{dsys} discrete time equivalent via zero-order hold, 
  52. ## sample each @var{T} sec.
  53. ## 
  54. ## converts the system data structure describing
  55. ## @example
  56. ## .
  57. ## x = Ac x + Bc u
  58. ## @end example
  59. ## into a discrete time equivalent model
  60. ## @example
  61. ## x[n+1] = Ad x[n] + Bd u[n]
  62. ## @end example
  63. ## via the matrix exponential or bilinear transform
  64. ## 
  65. ## @strong{Note} This function adds the suffix  @code{_d}
  66. ## to the names of the new discrete states.   
  67. ## @end deftypefn
  68.  
  69. function dsys = c2d (sys, opt, T)
  70.  
  71.   ## Written by R.B. Tenison (btenison@eng.auburn.edu)
  72.   ## October 1993
  73.   ## Updated by John Ingram for system data structure August 1996
  74.  
  75.   ## parse input arguments
  76.   if(nargin < 1 | nargin > 3)
  77.     usage("dsys=c2d(sys[,T])");
  78.   elseif (!is_struct(sys))
  79.     error("sys must be a system data structure");
  80.   elseif (nargin == 1)
  81.     opt = "ex";
  82.   elseif (nargin == 2 & !isstr(opt) )
  83.     T = opt;
  84.     opt = "ex";
  85.   endif
  86.  
  87.   ## check if sampling period T was passed.
  88.   Ts = sysgettsam(sys);
  89.   if(!exist("T"))
  90.     T = Ts;
  91.     if(T == 0)
  92.       error("sys is purely continuous; no sampling period T provided");
  93.     endif
  94.   elseif (T != Ts & Ts > 0)
  95.     warning(["c2d: T=",num2str(T),", system tsam==",num2str(Ts), ...
  96.       ": using T=", num2str(min(T,Ts))]);
  97.     T = min(T,Ts);
  98.   endif
  99.  
  100.   if (!is_sample(T))
  101.     error("sampling period T must be a postive, real scalar");
  102.   elseif( ! (strcmp(opt,"ex") | strcmp(opt,"bi") ) )
  103.     error(["illegal option passed: ",opt])
  104.   endif
  105.  
  106.   sys = sysupdate(sys,"ss");
  107.   [n,nz,m,p] = sysdimensions(sys);
  108.  
  109.   if (n == 0)
  110.     warning("c2d: sys has no continuous states; setting outputs to discrete");
  111.     dsys = syssetsignals(sys,"yd",ones(1:p));
  112.   elseif(strcmp(opt,"ex"))
  113.     ## construct new state-space (a,b,c,d) for continuous subsystem
  114.     [csys,Acd] = syscont(sys);       # extract continuous subsystem
  115.     [csys_a, csys_b, csys_c, csys_d] = sys2ss(csys);
  116.     [ sys_a,  sys_b,  sys_c,  sys_d] = sys2ss( sys);
  117.     if(isempty(Acd))                Bmat = sys_b;
  118.     elseif(isempty(csys_b))         Bmat = Acd;
  119.     else                            Bmat = [Acd, csys_b];     endif
  120.     
  121.     row_zer = columns(Bmat);
  122.     csysn = sysdimensions(csys);
  123.     col_zer = csysn + row_zer;
  124.  
  125.     [csysa,csysb,csysc,csysd] = sys2ss(csys);
  126.     if(isempty(Bmat) )
  127.       warning("c2d: no inputs to continuous subsystem.");
  128.       mat = csysa;
  129.     else
  130.       mat = [csysa, Bmat ; zeros( row_zer,col_zer) ];
  131.     endif
  132.  
  133.     matexp = expm(mat * T);
  134.   
  135.     Abar = matexp( 1:csysn , 1:(csysn + columns(Acd)) );  
  136.     Bbar = matexp( 1:csysn , (columns(Abar) + 1):columns(matexp) );
  137.  
  138.     newnz = rows(Abar);
  139.     outlist = ones(1,rows(csysc));
  140.     [stnames,innames,outnames] = sysgetsignals(csys);
  141.     dsys = ss2sys(Abar,Bbar,csysc,csysd,T,0,newnz,stnames,innames, ...
  142.     outnames,outlist);
  143.     ## rename states
  144.     for ii=1:newnz
  145.       strval = sprintf("%s_d",sysgetsignals(dsys,"st",ii,1));
  146.       dsys = syssetsignals(dsys,"st",strval,ii);
  147.     endfor
  148.  
  149.   elseif(strcmp(opt,"bi"))
  150.     if(is_digital(sys))
  151.       error("c2d: system is already digital")
  152.     else
  153.       ## convert with bilinear transform
  154.       [a,b,c,d,tsam,n,nz,stname,inname,outname,yd] = sys2ss(sys);
  155.       IT = (2/T)*eye(size(a));
  156.       A = (IT+a)/(IT-a);
  157.       iab = (IT-a)\b;
  158.       tk=2/sqrt(T);
  159.       B = tk*iab;
  160.       C = tk*(c/(IT-a));
  161.       D = d + (c*iab);
  162.       stnamed = strappend(stname,"_d");
  163.       dsys = ss2sys(A,B,C,D,T,0,rows(A),stnamed,inname,outname);
  164.     endif
  165.   else
  166.     error(["Bad option=",opt])
  167.   endif
  168.   
  169. endfunction
  170.