3455
|
1 ## Copyright (C) 1996, 1998, 2000 Auburn University. All rights reserved. |
3431
|
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 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but WITHOUT |
|
11 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 ## for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301 USA. |
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 |
|
111 if (nargin < 1 | nargin > 5) |
6046
|
112 print_usage (); |
3431
|
113 endif |
|
114 if(nargin < 2) |
|
115 w = []; |
|
116 endif |
|
117 if(nargin < 3) |
|
118 outputs = []; |
|
119 endif |
|
120 if(nargin < 4) |
|
121 inputs = []; |
|
122 endif |
|
123 if(nargin < 5) |
|
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"); |
3462
|
136 bode_nin = sysdimensions(sys,"in"); |
|
137 bode_nout = sysdimensions(sys,"out"); |
3431
|
138 |
|
139 [stname,inname,outname] = sysgetsignals(sys); |
|
140 systsam = sysgettsam(sys); |
|
141 |
|
142 ## Get the magnitude and phase of f. |
|
143 mag = abs(f); |
|
144 phase = arg(f)*180.0/pi; |
|
145 |
|
146 if (nargout < 1), |
|
147 ## Plot the information |
3603
|
148 save_automatic_replot = automatic_replot; |
|
149 unwind_protect |
5926
|
150 automatic_replot(0); |
4422
|
151 oneplot(); |
5215
|
152 __gnuplot_set__ autoscale; |
|
153 __gnuplot_set__ nokey; |
3603
|
154 clearplot(); |
5215
|
155 __gnuplot_set__ data style lines; |
3603
|
156 if(is_digital(sys)) |
|
157 xlstr = ["Digital frequency w=rad/sec. pi/T=",num2str(pi/systsam)]; |
|
158 tistr = "(exp(jwT)) "; |
|
159 else |
|
160 xlstr = "Frequency in rad/sec"; |
|
161 tistr = "(jw)"; |
|
162 endif |
|
163 xlabel(xlstr); |
|
164 if(is_siso(sys)) |
4422
|
165 subplot(2,1,1); |
4771
|
166 title(["|[Y/U]",tistr,"|, u=", inname{1},", y=",outname{1}]); |
3603
|
167 else |
|
168 title([ "||Y(", tistr, ")/U(", tistr, ")||"]); |
|
169 disp("MIMO plot from") |
|
170 disp(__outlist__(inname," ")); |
|
171 disp("to") |
|
172 disp(__outlist__(outname," ")); |
3431
|
173 endif |
3603
|
174 wv = [min(w), max(w)]; |
|
175 if(do_db_plot && max(mag) > 0) |
|
176 ylabel("Gain in dB"); |
|
177 md = 20*log10(mag); |
|
178 axvec = axis2dlim([vec(w),vec(md)]); |
|
179 axvec(1:2) = wv; |
|
180 axis(axvec); |
|
181 else |
|
182 ylabel("Gain |Y/U|") |
|
183 md = mag; |
|
184 endif |
3431
|
185 |
3603
|
186 grid("on"); |
|
187 if (do_db_plot) |
|
188 semilogx(w,md); |
3431
|
189 else |
3603
|
190 loglog(w,md); |
3431
|
191 endif |
3603
|
192 if (is_siso(sys)) |
4422
|
193 subplot(2,1,2); |
3603
|
194 axvec = axis2dlim([vec(w),vec(phase)]); |
|
195 axvec(1:2) = wv; |
|
196 axis(axvec); |
|
197 xlabel(xlstr); |
|
198 ylabel("Phase in deg"); |
|
199 title([ "phase([Y/U]", tistr, ... |
4771
|
200 "), u=", inname{1},", y=",outname{1}]); |
3603
|
201 grid("on"); |
|
202 semilogx(w,phase); |
|
203 ## This should be the default for subsequent plot commands. |
4422
|
204 oneplot(); |
3431
|
205 endif |
3603
|
206 unwind_protect_cleanup |
5926
|
207 automatic_replot(save_automatic_replot); |
3603
|
208 end_unwind_protect |
3431
|
209 else |
|
210 mag_r = mag; |
|
211 phase_r = phase; |
|
212 w_r = w; |
|
213 endif |
|
214 endfunction |