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

  1. # Copyright (C) 1996,1998 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 outsys = tf2sys(num,den,tsam,inname,outname)
  18.   #
  19.   # sys = tf2sys(num,den{,tsam,inname,outname})
  20.   # build system data structure from transfer function format data
  21.   # inputs:
  22.   #   num, den: coefficients of numerator/denominator polynomials
  23.   #   tsam: sampling interval. default: 0 (continuous time)
  24.   #   inname, outname: input/output signal names (string variables)
  25.   # outputs: sys = system data structure
  26.    
  27.   #  Written by R. Bruce Tenison  July 29, 1994
  28.   #  Name changed to TF2SYS July 1995
  29.   #  updated for new system data structure format July 1996
  30.  
  31.   save_val = implicit_str_to_num_ok;
  32.   implicit_str_to_num_ok = 1;
  33.  
  34.   #  Test for the correct number of input arguments
  35.   if ((nargin < 2) || (nargin > 5))
  36.     usage('outsys=tf2sys(num,den[,tsam,inname,outname])');
  37.     return
  38.   endif
  39.  
  40.   # check input format 
  41.   if( ! ( (is_vector(num) || is_scalar(num)) && ...
  42.     (is_vector(den) || is_scalar(den))) )
  43.     error(['num (',num2str(rows(num)),'x',num2str(columns(num)), ...
  44.       ') and den (',num2str(rows(den)),'x',num2str(columns(den)), ...
  45.       ') must be vectors'])
  46.   endif
  47.   
  48.   # strip leading zero coefficients
  49.   num = tf2sysl(num);
  50.   den = tf2sysl(den);
  51.  
  52.   if (length(num) >  length(den))
  53.     error("# of poles (%d) < # of zeros (%d)",length(den)-1, length(num)-1);
  54.   endif
  55.  
  56.   # check sampling interval (if any)
  57.   if(nargin <= 2)           tsam = 0;        # default
  58.   elseif (isempty(tsam))    tsam = 0;           endif
  59.   if ( (! (is_scalar(tsam) && (imag(tsam) == 0) )) || (tsam < 0) )
  60.     error('tsam must be a positive real scalar')
  61.   endif
  62.  
  63.   outsys.num = num;
  64.   outsys.den = den;
  65.  
  66.   #  Set the system vector:  active = 0(tf), updated = [1 0 0];
  67.   outsys.sys = [0, 1, 0, 0];
  68.  
  69.   #  Set defaults
  70.   outsys.tsam = tsam;
  71.   outsys.n = length(den)-1;
  72.   outsys.nz = 0;
  73.   outsys.yd = 0;    # assume discrete-time
  74.   # check discrete time
  75.   if(tsam > 0)
  76.     [outsys.n,outsys.nz] = swap(outsys.n, outsys.nz);
  77.     outsys.yd = 1;
  78.   endif
  79.  
  80.   outsys.inname  = sysdefioname(1,"u");
  81.   outsys.outname = sysdefioname(1,"y");
  82.   outsys.stname  = sysdefstname(outsys.n,outsys.nz);
  83.  
  84.   #  Set name of input
  85.   if (nargin > 3)
  86.     # make sure its a list of a single string
  87.     if(!isempty(inname))
  88.       if(!is_list(inname))  inname = list(inname);  endif
  89.       if( !is_signal_list(inname) )
  90.         error("inname must be a string or list of strings");
  91.       endif
  92.       if(length(inname) > 1)
  93.         warning("tf2sys: %d input names provided; first used",length(inname));
  94.         inname = inname(1);
  95.       endif
  96.       outsys = syssetsignals(outsys,"in",inname);
  97.     endif
  98.   endif
  99.  
  100.   #  Set name of output
  101.   if (nargin > 4)
  102.     if(!isempty(outname))
  103.       if(!is_list(outname))  outname = list(outname);  endif
  104.       if(!is_signal_list(outname))
  105.         error("outname must be a string or a list of strings");
  106.       endif
  107.       if(length(outname) > 1)
  108.         warning("tf2sys: %d output names provided; first used",length(outname));
  109.         outname = outname(1);
  110.       endif
  111.       outsys = syssetsignals(outsys,"out",outname);
  112.     endif
  113.   endif 
  114.  
  115.   implicit_str_to_num_ok = save_val;
  116. endfunction
  117.