home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / control / freqresp.m < prev    next >
Text File  |  1999-03-05  |  4KB  |  118 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 [ff,w] = freqresp(sys,USEW,w);
  18.   # function [ff,w] = freqresp(sys,USEW{,w});
  19.   # Frequency response function - used internally by bode, nyquist.
  20.   # minimal argument checking; "do not attempt to do this at home"
  21.   # USEW returned by freqchkw 
  22.   # w: optional, must be present if USEW is given
  23.   #
  24.   # returns: ff = vector of finite G(j*w) entries (or || G(j*w) || for MIMO)
  25.   #          w = vector of frequencies used
  26.   #      ff and w are both returned as row vectors
  27.  
  28.   #  Written by: R. Bruce Tenison July 11, 1994
  29.   # SYS_INTERNAL accesses members of system data structure
  30.  
  31.   save_val = empty_list_elements_ok;
  32.   empty_list_elements_ok = 1;
  33.  
  34.   # Check Args
  35.   if( (nargin < 2) || (nargin > 4) )
  36.     usage ("[ff,w] = freqresp(sys,USEW{,w})");
  37.   elseif( USEW & (nargin < 3) )
  38.     error("USEW=1 but w was not passed.");
  39.   elseif( USEW & isempty(w))
  40.     warning("USEW=1 but w is empty; setting USEW=0");
  41.     USEW=0;
  42.   endif
  43.  
  44.   DIGITAL = is_digital(sys);
  45.  
  46.   # compute default w if needed
  47.   if(!USEW)
  48.     if(is_siso(sys))
  49.       sys = sysupdate(sys,"zp");
  50.       [zer,pol] = sys2zp(sys);
  51.     else
  52.       zer = tzero(sys);
  53.       pol = eig(sys2ss(sys));
  54.     endif
  55.  
  56.     # get default frequency range
  57.     [wmin,wmax] = bode_bounds(zer,pol,DIGITAL,sysgettsam(sys));
  58.     w = logspace(wmin,wmax,50);
  59.   else
  60.     w = reshape(w,1,length(w));     # make sure it's a row vector
  61.   endif
  62.  
  63.   # now get complex values of s or z
  64.   if(DIGITAL)
  65.     jw = exp(i*w*sysgettsam(sys));
  66.   else
  67.     jw = i*w;
  68.   endif
  69.  
  70.   [nn,nz,mm,pp] = sysdimensions(sys);
  71.  
  72.   # now compute the frequency response - divide by zero yields a warning
  73.   if (strcmp(sysgettype(sys),"zp"))
  74.     # zero-pole form (preferred)
  75.     [zer,pol,sysk] = sys2zp(sys);
  76.     ff = ones(size(jw));
  77.     l1 = min(length(zer)*(1-isempty(zer)),length(pol)*(1-isempty(pol)));
  78.     for ii=1:l1
  79.       ff = ff .* (jw - zer(ii)) ./ (jw - pol(ii));
  80.     endfor
  81.  
  82.     # require proper  transfer function, so now just get poles.
  83.     for ii=(l1+1):length(pol)
  84.       ff = ff ./ (jw - pol(ii));
  85.     endfor
  86.     ff = ff*sysk;
  87.  
  88.   elseif (strcmp(sysgettype(sys),"tf"))
  89.     # transfer function form 
  90.     [num,den] = sys2tf(sys);
  91.     ff = polyval(num,jw)./polyval(den,jw);
  92.   elseif (mm==pp)
  93.     # The system is square; do state-space form bode plot
  94.     [sysa,sysb,sysc,sysd,tsam,sysn,sysnz] = sys2ss(sys);
  95.     n = sysn + sysnz;
  96.     for ii=1:length(jw);
  97.       ff(ii) = det(sysc*((jw(ii).*eye(n)-sysa)\sysb)+sysd);
  98.     endfor;
  99.   else
  100.     # Must be state space... bode                            
  101.     [sysa,sysb,sysc,sysd,tsam,sysn,sysnz] = sys2ss(sys);
  102.     n = sysn + sysnz;
  103.     for ii=1:length(jw);
  104.       ff(ii) = norm(sysc*((jw(ii)*eye(n)-sysa)\sysb)+sysd);
  105.     endfor
  106.     
  107.   endif
  108.  
  109.   w = reshape(w,1,length(w));
  110.   ff = reshape(ff,1,length(ff));
  111.  
  112.   # restore global variable
  113.   empty_list_elements_ok = save_val;
  114. endfunction
  115.  
  116.