home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / control / zp2sys.m < prev    next >
Text File  |  1999-03-05  |  4KB  |  120 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 = zp2sys (zer,pol,k,tsam,inname,outname)
  18.   # sys = zp2sys (zer,pol,k{,tsam,inname,outname})
  19.   # Create system data structure from zero-pole data
  20.   # inputs:
  21.   #   zer: vector of system zeros
  22.   #   pol: vector of system poles
  23.   #   k: scalar leading coefficient
  24.   #   tsam: sampling period. default: 0 (continuous system)
  25.   #   inname, outname: input/output signal names (strings)
  26.   # outputs: sys: system data structure
  27.  
  28.   #  Modified by John Ingram  July 20, 1996  
  29.  
  30.   save_val = implicit_str_to_num_ok;    # save for restoring later
  31.   implicit_str_to_num_ok = 1;
  32.  
  33.   #  Test for the correct number of input arguments
  34.   if ((nargin < 3) || (nargin > 6))
  35.     usage("outsys = zp2sys(zer,pol,k[,tsam,inname,outname])");
  36.   endif
  37.  
  38.   # check input format 
  39.   if( ! (is_vector(zer) | isempty(zer) ) )
  40.     error("zer must be a vector or empty");
  41.   endif
  42.   zer = reshape(zer,1,length(zer));        # make it a row vector
  43.  
  44.   if( ! (is_vector(pol) | isempty(pol)))
  45.     error("pol must be a vector");
  46.   endif
  47.   pol = reshape(pol,1,length(pol));
  48.  
  49.   if (! is_scalar(k))
  50.      error('k must be a scalar');
  51.   endif
  52.  
  53.   #  Test proper numbers of poles and zeros.  The number of poles must be 
  54.   #  greater than or equal to the number of zeros.
  55.   if (length(zer) >  length(pol))
  56.     error(["number of poles (", num2str(length(pol)), ...
  57.     ") < number of zeros (", num2str(length(zer)),")"]);
  58.   endif
  59.  
  60.   #  Set the system transfer function
  61.   outsys.zer = zer;
  62.   outsys.pol = pol;
  63.   outsys.k = k;
  64.  
  65.   #  Set the system vector:  active = 1, updated = [0 1 0];
  66.   outsys.sys = [1, 0, 1, 0];
  67.  
  68.   #  Set defaults
  69.   outsys.tsam = 0;
  70.    outsys.n = length(pol);
  71.   outsys.nz = 0;
  72.   outsys.yd = 0;    # assume (for now) continuous time outputs
  73.  
  74.   #  Set the type of system
  75.   if (nargin > 3)
  76.     if( !is_scalar(tsam) )
  77.       error("tsam must be a nonnegative scalar");
  78.     endif
  79.     if (tsam < 0)
  80.       error("sampling time must be positve")
  81.     elseif (tsam > 0)
  82.       [outsys.n,outsys.nz] = swap(outsys.n, outsys.nz);
  83.       outsys.yd = 1;        # discrete-time output
  84.     endif
  85.  
  86.     outsys.tsam = tsam;
  87.   endif
  88.  
  89.   outsys.inname = sysdefioname(1,"u");
  90.   outsys.outname = sysdefioname(1,"y");
  91.   outsys.stname = sysdefstname(outsys.n,outsys.nz);
  92.  
  93.   #  Set name of input
  94.   if (nargin > 4)
  95.     # make sure its a string
  96.     if(!isempty(inname))
  97.       if(!is_list(inname))  inname = list(inname); endif
  98.       if(!is_signal_list(inname))
  99.         error("inname must be a single signal name");
  100.       endif
  101.       outsys.inname = inname(1);
  102.     endif
  103.   endif
  104.  
  105.   #  Set name of output
  106.   if (nargin > 5)
  107.     if(!isempty(outname))
  108.       if(!is_list(outname))        outname = list(outname);    endif
  109.       if(!is_signal_list(outname))
  110.         error("outname must be a single signal name");
  111.       endif
  112.       outsys.outname = outname(1);
  113.     endif
  114.   endif 
  115.  
  116.   implicit_str_to_num_ok = save_val;
  117. endfunction
  118.