7017
|
1 ## Copyright (C) 1996, 1998, 2000, 2003, 2004, 2005, 2006, 2007 |
|
2 ## Auburn University. All rights reserved. |
3431
|
3 ## |
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
7016
|
7 ## under the terms of the GNU General Public License as published by |
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
3431
|
10 ## |
7016
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
3431
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
3431
|
19 |
|
20 ## -*- texinfo -*- |
3501
|
21 ## @deftypefn {Function File} {[@var{mag}, @var{phase}, @var{w}] =} bode (@var{sys}, @var{w}, @var{out_idx}, @var{in_idx}) |
3431
|
22 ## If no output arguments are given: produce Bode plots of a system; otherwise, |
|
23 ## compute the frequency response of a system data structure |
|
24 ## |
|
25 ## @strong{Inputs} |
|
26 ## @table @var |
|
27 ## @item sys |
|
28 ## a system data structure (must be either purely continuous or discrete; |
|
29 ## see is_digital) |
|
30 ## @item w |
|
31 ## frequency values for evaluation. |
|
32 ## |
|
33 ## if @var{sys} is continuous, then bode evaluates @math{G(jw)} where |
|
34 ## @math{G(s)} is the system transfer function. |
|
35 ## |
|
36 ## if @var{sys} is discrete, then bode evaluates G(@code{exp}(jwT)), where |
|
37 ## @itemize @bullet |
3502
|
38 ## @item @math{T} is the system sampling time |
3431
|
39 ## @item @math{G(z)} is the system transfer function. |
|
40 ## @end itemize |
|
41 ## |
3500
|
42 ## @strong{Default} the default frequency range is selected as follows: (These |
5016
|
43 ## steps are @strong{not} performed if @var{w} is specified) |
3431
|
44 ## @enumerate |
3438
|
45 ## @item via routine __bodquist__, isolate all poles and zeros away from |
3431
|
46 ## @var{w}=0 (@var{jw}=0 or @math{@code{exp}(jwT)}=1) and select the frequency |
|
47 ## range based on the breakpoint locations of the frequencies. |
|
48 ## @item if @var{sys} is discrete time, the frequency range is limited |
|
49 ## to @math{jwT} in |
|
50 ## @ifinfo |
|
51 ## [0,2 pi /T] |
|
52 ## @end ifinfo |
|
53 ## @iftex |
|
54 ## @tex |
|
55 ## $[0,2\pi/T]$ |
|
56 ## @end tex |
|
57 ## @end iftex |
|
58 ## @item A "smoothing" routine is used to ensure that the plot phase does |
|
59 ## not change excessively from point to point and that singular |
|
60 ## points (e.g., crossovers from +/- 180) are accurately shown. |
|
61 ## |
|
62 ## @end enumerate |
|
63 ## @item out_idx |
|
64 ## @itemx in_idx |
3455
|
65 ## |
|
66 ## The names or indices of outputs and inputs to be used in the frequency |
|
67 ## response. See @code{sysprune}. |
|
68 ## |
3462
|
69 ## @strong{Example} |
3455
|
70 ## @example |
4844
|
71 ## bode(sys,[],"y_3", @{"u_1","u_4"@}); |
3455
|
72 ## @end example |
3431
|
73 ## @end table |
|
74 ## @strong{Outputs} |
|
75 ## @table @var |
|
76 ## @item mag |
|
77 ## @itemx phase |
|
78 ## the magnitude and phase of the frequency response @math{G(jw)} or |
|
79 ## @math{G(@code{exp}(jwT))} at the selected frequency values. |
|
80 ## @item w |
|
81 ## the vector of frequency values used |
|
82 ## @end table |
|
83 ## |
|
84 ## @enumerate |
|
85 ## @item If no output arguments are given, e.g., |
|
86 ## @example |
|
87 ## bode(sys); |
|
88 ## @end example |
|
89 ## bode plots the results to the screen. Descriptive labels are |
|
90 ## automatically placed. |
|
91 ## |
|
92 ## Failure to include a concluding semicolon will yield some garbage |
|
93 ## being printed to the screen (@code{ans = []}). |
|
94 ## |
5016
|
95 ## @item If the requested plot is for an @acronym{MIMO} system, mag is set to |
3431
|
96 ## @math{||G(jw)||} or @math{||G(@code{exp}(jwT))||} |
|
97 ## and phase information is not computed. |
|
98 ## @end enumerate |
|
99 ## @end deftypefn |
|
100 |
|
101 ## Author: John Ingram <ingraje@eng.auburn.edu> |
|
102 ## Created: July 10, 1996 |
|
103 ## Based on previous code by R. Bruce Tenison, July 13, 1994 |
|
104 ## Modified by David Clem November 13, 1994 |
|
105 ## again by A. S. Hodel July 1995 (smart plot range, etc.) |
|
106 ## Modified by Kai P. Mueller September 28, 1997 (multiplot mode) |
|
107 |
|
108 function [mag_r, phase_r, w_r] = bode (sys, w, outputs, inputs, plot_style) |
|
109 |
|
110 ## check number of input arguments given |
6447
|
111 if (nargin < 1 || nargin > 5) |
6046
|
112 print_usage (); |
3431
|
113 endif |
6447
|
114 if (nargin < 2) |
3431
|
115 w = []; |
|
116 endif |
6447
|
117 if (nargin < 3) |
3431
|
118 outputs = []; |
|
119 endif |
6447
|
120 if (nargin < 4) |
3431
|
121 inputs = []; |
|
122 endif |
6447
|
123 if (nargin < 5) |
3431
|
124 plot_style = "dB"; |
|
125 endif |
|
126 |
|
127 if (strcmp (plot_style, "dB")) |
|
128 do_db_plot = 1; |
|
129 elseif (strcmp (plot_style, "mag")) |
|
130 do_db_plot = 0; |
|
131 else |
|
132 error ("bode: invalid value of plot_style specified"); |
|
133 endif |
|
134 |
3455
|
135 [f, w, sys] = __bodquist__ (sys, w, outputs, inputs, "bode"); |
6447
|
136 bode_nin = sysdimensions (sys, "in"); |
|
137 bode_nout = sysdimensions (sys, "out"); |
3431
|
138 |
6447
|
139 [stname, inname, outname] = sysgetsignals (sys); |
|
140 systsam = sysgettsam (sys); |
3431
|
141 |
|
142 ## Get the magnitude and phase of f. |
6447
|
143 mag = abs (f); |
|
144 phase = unwrap (arg (f)) * 180.0 / pi; |
3431
|
145 |
|
146 if (nargout < 1), |
|
147 ## Plot the information |
6447
|
148 if (is_digital (sys)) |
|
149 xlstr = sprintf ("Digital frequency w=rad/sec. pi/T=%g", pi/systsam); |
|
150 tistr = "(exp(jwT)) "; |
|
151 else |
|
152 xlstr = "Frequency in rad/sec"; |
|
153 tistr = "(jw)"; |
|
154 endif |
|
155 |
|
156 wv = [min(w), max(w)]; |
|
157 |
|
158 is_siso_sys = is_siso (sys); |
|
159 max_mag_positive = max (mag) > 0; |
|
160 |
|
161 if (is_siso_sys) |
|
162 subplot (2, 1, 1); |
|
163 endif |
|
164 |
|
165 if (do_db_plot) |
|
166 md = 20 * log10 (mag); |
|
167 semilogx (w, md); |
|
168 if (max_mag_positive) |
|
169 ylabel ("Gain in dB"); |
|
170 axvec = axis2dlim ([w(:), md(:)]); |
3603
|
171 axvec(1:2) = wv; |
6447
|
172 axis (axvec); |
3431
|
173 endif |
6447
|
174 else |
|
175 loglog (w, mag); |
|
176 ylabel ("Gain |Y/U|") |
|
177 endif |
|
178 xlabel (xlstr); |
|
179 grid ("on"); |
|
180 |
|
181 if (is_siso_sys) |
|
182 title (sprintf ("|[Y/U]%s|, u=%s, y=%s", tistr, inname{1}, outname{1})); |
|
183 else |
|
184 title (sprintf ("||Y(%s)/U(%s)||", tistr, tistr)); |
|
185 disp ("MIMO plot from") |
|
186 disp (__outlist__(inname," ")); |
|
187 disp ("to") |
|
188 disp (__outlist__(outname," ")); |
|
189 endif |
|
190 |
|
191 if (is_siso_sys) |
|
192 subplot (2, 1, 2); |
|
193 axvec = axis2dlim ([w(:), phase(:)]); |
|
194 axvec(1:2) = wv; |
|
195 semilogx (w, phase); |
|
196 axis (axvec); |
|
197 xlabel (xlstr); |
|
198 ylabel ("Phase in deg"); |
|
199 title (sprintf ("phase([Y/U]%s), u=%s, y=%s", |
|
200 tistr, inname{1}, outname{1})); |
|
201 grid ("on"); |
|
202 endif |
3431
|
203 else |
|
204 mag_r = mag; |
|
205 phase_r = phase; |
|
206 w_r = w; |
|
207 endif |
6447
|
208 |
3431
|
209 endfunction |