File: bode.m

package info (click to toggle)
octave-control 1.0.11-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,628 kB
  • ctags: 160
  • sloc: makefile: 64; sh: 4
file content (208 lines) | stat: -rw-r--r-- 6,083 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
## Copyright (C) 1996, 1998, 2000, 2003, 2004, 2005, 2006, 2007
##               Auburn University.  All rights reserved.
##
##
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or (at
## your option) any later version.
##
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} {[@var{mag}, @var{phase}, @var{w}] =} bode (@var{sys}, @var{w}, @var{out_idx}, @var{in_idx})
## If no output arguments are given: produce Bode plots of a system; otherwise,
## compute the frequency response of a system data structure
##
## @strong{Inputs}
## @table @var
## @item   sys
## a system data structure (must be either purely continuous or discrete;
## see is_digital)
## @item   w
## frequency values for evaluation.
##
## if @var{sys} is continuous, then bode evaluates @math{G(jw)} where
## @math{G(s)} is the system transfer function.
##
## if @var{sys} is discrete, then bode evaluates G(@code{exp}(jwT)), where
## @itemize @bullet
## @item @math{T} is the system sampling time
## @item @math{G(z)} is the system transfer function.
## @end itemize
##
## @strong{Default} the default frequency range is selected as follows: (These
## steps are @strong{not} performed if @var{w} is specified)
## @enumerate
## @item via routine __bodquist__, isolate all poles and zeros away from
## @var{w}=0 (@var{jw}=0 or @math{@code{exp}(jwT)}=1) and select the frequency
## range based on the breakpoint locations of the frequencies.
## @item if @var{sys} is discrete time, the frequency range is limited
##               to @math{jwT} in
## @ifinfo
## [0,2 pi /T]
## @end ifinfo
## @iftex
## @tex
## $[0,2\pi/T]$
## @end tex
## @end iftex
## @item A "smoothing" routine is used to ensure that the plot phase does
## not change excessively from point to point and that singular
## points (e.g., crossovers from +/- 180) are accurately shown.
##
## @end enumerate
## @item out_idx
## @itemx in_idx
##
## The names or indices of outputs and inputs to be used in the frequency
## response.  See @code{sysprune}.
##
## @strong{Example}
## @example
## bode(sys,[],"y_3", @{"u_1","u_4"@});
## @end example
## @end table
## @strong{Outputs}
## @table @var
## @item mag
## @itemx phase
## the magnitude and phase of the frequency response @math{G(jw)} or
## @math{G(@code{exp}(jwT))} at the selected frequency values.
## @item w
## the vector of frequency values used
## @end table
##
## @enumerate
## @item If no output arguments are given, e.g.,
## @example
## bode(sys);
## @end example
## bode plots the results to the screen.  Descriptive labels are
## automatically placed.
##
## Failure to include a concluding semicolon will yield some garbage
## being printed to the screen (@code{ans = []}).
##
## @item If the requested plot is for an @acronym{MIMO} system, mag is set to
## @math{||G(jw)||} or @math{||G(@code{exp}(jwT))||}
## and phase information is not computed.
## @end enumerate
## @end deftypefn

## Author: John Ingram <ingraje@eng.auburn.edu>
## Created: July 10, 1996
## Based on previous code by R. Bruce Tenison, July 13, 1994
## Modified by David Clem November 13, 1994
## again by A. S. Hodel July 1995 (smart plot range, etc.)
## Modified by Kai P. Mueller September 28, 1997 (multiplot mode)

function [mag_r, phase_r, w_r] = bode (sys, w, outputs, inputs, plot_style)

  ## check number of input arguments given
  if (nargin < 1 || nargin > 5)
    print_usage ();
  endif
  if (nargin < 2)
    w = [];
  endif
  if (nargin < 3)
    outputs = [];
  endif
  if (nargin < 4)
    inputs = [];
  endif
  if (nargin < 5)
    plot_style = "dB";
  endif

  if (strcmp (plot_style, "dB"))
    do_db_plot = 1;
  elseif (strcmp (plot_style, "mag"))
    do_db_plot = 0;
  else
    error ("bode: invalid value of plot_style specified");
  endif

  [f, w, sys] = __bodquist__ (sys, w, outputs, inputs, "bode");
  bode_nin = sysdimensions (sys, "in");
  bode_nout = sysdimensions (sys, "out");

  [stname, inname, outname] = sysgetsignals (sys);
  systsam = sysgettsam (sys);

  ## Get the magnitude and phase of f.
  mag = abs (f);
  phase = unwrap (arg (f)) * 180.0 / pi;

  if (nargout < 1),
    ## Plot the information
    if (is_digital (sys))
      xlstr = sprintf ("Digital frequency w=rad/sec.  pi/T=%g", pi/systsam);
      tistr = "(exp(jwT)) ";
    else
      xlstr = "Frequency in rad/sec";
      tistr = "(jw)";
    endif

    wv = [min(w), max(w)];

    is_siso_sys = is_siso (sys);
    max_mag_positive = max (mag) > 0;

    if (is_siso_sys)
      subplot (2, 1, 1);
    endif

    if (do_db_plot)
      md = 20 * log10 (mag);
      semilogx (w, md);
      if (max_mag_positive)
	ylabel ("Gain in dB");
	axvec = axis2dlim ([w(:), md(:)]);
	axvec(1:2) = wv;
	axis (axvec);
      endif
    else
      loglog (w, mag);
      ylabel ("Gain |Y/U|")
    endif
    xlabel (xlstr);
    grid ("on");

    if (is_siso_sys)
      title (sprintf ("|[Y/U]%s|, u=%s, y=%s", tistr, inname{1}, outname{1}));
    else
      title (sprintf ("||Y(%s)/U(%s)||", tistr, tistr));
      disp ("MIMO plot from")
      disp (__outlist__(inname,"     "));
      disp ("to")
      disp (__outlist__(outname,"    "));
    endif

    if (is_siso_sys)
      subplot (2, 1, 2);
      axvec = axis2dlim ([w(:), phase(:)]);
      axvec(1:2) = wv;
      semilogx (w, phase);
      axis (axvec);
      xlabel (xlstr);
      ylabel ("Phase in deg");
      title (sprintf ("phase([Y/U]%s), u=%s, y=%s",
		      tistr, inname{1}, outname{1}));
      grid ("on");
    endif
  else
    mag_r = mag;
    phase_r = phase;
    w_r = w;
  endif

endfunction