home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / control / bode.m < prev    next >
Text File  |  1999-03-05  |  5KB  |  152 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 [mag,phase,w] = bode(sys,w,outputs,inputs)
  18. # [mag,phase,w] = bode(sys[,w,outputs,inputs])
  19. # Produce Bode plots of a system
  20. #
  21. # Compute the frequency response of a system.
  22. # inputs:
  23. #   sys: system data structure (must be either purely continuous or discrete;
  24. #     see is_digital)
  25. #   w: frequency values for evaluation.
  26. #      if sys is continuous, then bode evaluates G(jw)
  27. #      if sys is discrete, then bode evaluates G(exp(jwT)), where T=sys.tsam
  28. #         (the system sampling time)
  29. #      default: the default frequency range is selected as follows: (These
  30. #        steps are NOT performed if w is specified)
  31. #          (1) via routine bodquist, isolate all poles and zeros away from
  32. #              w=0 (jw=0 or exp(jwT)=1) and select the frequency
  33. #             range based on the breakpoint locations of the frequencies.
  34. #          (2) if sys is discrete time, the frequency range is limited
  35. #              to jwT in [0,2p*pi]
  36. #          (3) A "smoothing" routine is used to ensure that the plot phase does
  37. #              not change excessively from point to point and that singular
  38. #              points (e.g., crossovers from +/- 180) are accurately shown.
  39. #   outputs, inputs: the indices of the output(s) and input(s) to be used in
  40. #     the frequency response; see sysprune.
  41. # outputs:
  42. #    mag, phase: the magnitude and phase of the frequency response
  43. #       G(jw) or G(exp(jwT)) at the selected frequency values.
  44. #    w: the vector of frequency values used
  45. # If no output arguments are given, bode plots the results to the screen.
  46. # Descriptive labels are automatically placed.  See xlabel, ylable, title,
  47. # and replot.
  48. #
  49. # Note: if the requested plot is for an MIMO system, mag is set to
  50. # ||G(jw)|| or ||G(exp(jwT))|| and phase information is not computed.
  51.  
  52. # Written by John Ingram  July 10th, 1996
  53. # Based on previous code
  54. # By R. Bruce Tenison, July 13, 1994
  55. # Modified by David Clem November 13, 1994
  56. # again by A. S. Hodel July 1995 (smart plot range, etc.)
  57. # Modified by Kai P. Mueller September 28, 1997 (multiplot mode)
  58.  
  59.   # check number of input arguments given
  60.   if (nargin < 1 | nargin > 4)
  61.     usage("[mag,phase,w] = bode(sys[,w,outputs,inputs])");
  62.   endif
  63.   if(nargin < 2)
  64.     w = [];
  65.   endif
  66.   if(nargin < 3)
  67.     outputs = [];
  68.   endif
  69.   if(nargin < 4)
  70.     inputs = [];
  71.   endif
  72.  
  73.   [f, w] = bodquist(sys,w,outputs,inputs,"bode");
  74.  
  75.   [stname,inname,outname] = sysgetsignals(sys);
  76.   systsam = sysgettsam(sys);
  77.  
  78.   # Get the magnitude and phase of f.
  79.   mag = abs(f);
  80.   phase = arg(f)*180.0/pi;
  81.  
  82.   if (nargout < 1),
  83.     # Plot the information
  84.     if(gnuplot_has_multiplot)
  85.       oneplot();
  86.     endif
  87.     gset autoscale;
  88.     if(gnuplot_has_multiplot)
  89.       gset nokey;
  90.     endif
  91.     clearplot();
  92.     gset data style lines;
  93.     if(is_digital(sys))
  94.       xlstr = ["Digital frequency w=rad/sec.  pi/T=",num2str(pi/systsam)];
  95.       tistr = "(exp(jwT)) ";
  96.     else
  97.       xlstr = "Frequency in rad/sec";
  98.       tistr = "(jw)";
  99.     endif
  100.     xlabel(xlstr);
  101.     if(is_siso(sys))
  102.       if (gnuplot_has_multiplot)
  103.         subplot(2,1,1);
  104.       endif
  105.       title(["|[Y/U]",tistr,"|, u=", nth(inname,1),", y=",nth(outname,1)]);
  106.     else
  107.       title([ "||Y(", tistr, ")/U(", tistr, ")||"]);
  108.       disp("MIMO plot from")
  109.       disp(outlist(inname,"    "));
  110.       disp("to")
  111.       disp(outlist(outname,"    "));
  112.     endif
  113.     wv = [min(w), max(w)];
  114.     if(max(mag) > 0)
  115.       ylabel("Gain in dB");
  116.       md = 20*log10(mag);
  117.     else
  118.       ylabel("Gain |Y/U|")
  119.       md = mag;
  120.     endif
  121.  
  122.     axvec = axis2dlim([vec(w),vec(md)]);
  123.     axvec(1:2) = wv;
  124.     axis(axvec);
  125.     grid("on");
  126.     semilogx(w,md);
  127.     if (is_siso(sys))
  128.       if (gnuplot_has_multiplot)
  129.         subplot(2,1,2);
  130.       else
  131.         prompt('Press any key for phase plot');
  132.       endif
  133.       axvec = axis2dlim([vec(w),vec(phase)]);
  134.       axvec(1:2) = wv;
  135.       axis(axvec);
  136.       xlabel(xlstr);
  137.       ylabel("Phase in deg");
  138.       title([ "phase([Y/U]", tistr, ...
  139.      "), u=", nth(inname,1),", y=",nth(outname,1)]);
  140.       grid("on");
  141.       semilogx(w,phase);
  142.       # This should be the default for subsequent plot commands.
  143.       if(gnuplot_has_multiplot)
  144.         oneplot();
  145.       endif
  146.     endif
  147.     mag = phase = w = [];
  148.   endif
  149. endfunction
  150.