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 |
|
17 ## Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. |
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {[@var{mag}, @var{phase}, @var{w}] =} bode(@var{sys}@{,@var{w}, @var{out_idx}, @var{in_idx}@}) |
|
21 ## If no output arguments are given: produce Bode plots of a system; otherwise, |
|
22 ## compute the frequency response of a system data structure |
|
23 ## |
|
24 ## @strong{Inputs} |
|
25 ## @table @var |
|
26 ## @item sys |
|
27 ## a system data structure (must be either purely continuous or discrete; |
|
28 ## see is_digital) |
|
29 ## @item w |
|
30 ## frequency values for evaluation. |
|
31 ## |
|
32 ## if @var{sys} is continuous, then bode evaluates @math{G(jw)} where |
|
33 ## @math{G(s)} is the system transfer function. |
|
34 ## |
|
35 ## if @var{sys} is discrete, then bode evaluates G(@code{exp}(jwT)), where |
|
36 ## @itemize @bullet |
|
37 ## @item @var{T}=@code{sysgettsam(@var{sys})} (the system sampling time) and |
|
38 ## @item @math{G(z)} is the system transfer function. |
|
39 ## @end itemize |
|
40 ## |
|
41 ## @strong{ Default} the default frequency range is selected as follows: (These |
|
42 ## steps are NOT performed if @var{w} is specified) |
|
43 ## @enumerate |
3438
|
44 ## @item via routine __bodquist__, isolate all poles and zeros away from |
3431
|
45 ## @var{w}=0 (@var{jw}=0 or @math{@code{exp}(jwT)}=1) and select the frequency |
|
46 ## range based on the breakpoint locations of the frequencies. |
|
47 ## @item if @var{sys} is discrete time, the frequency range is limited |
|
48 ## to @math{jwT} in |
|
49 ## @ifinfo |
|
50 ## [0,2 pi /T] |
|
51 ## @end ifinfo |
|
52 ## @iftex |
|
53 ## @tex |
|
54 ## $[0,2\pi/T]$ |
|
55 ## @end tex |
|
56 ## @end iftex |
|
57 ## @item A "smoothing" routine is used to ensure that the plot phase does |
|
58 ## not change excessively from point to point and that singular |
|
59 ## points (e.g., crossovers from +/- 180) are accurately shown. |
|
60 ## |
|
61 ## @end enumerate |
|
62 ## @item out_idx |
|
63 ## @itemx in_idx |
3455
|
64 ## |
|
65 ## The names or indices of outputs and inputs to be used in the frequency |
|
66 ## response. See @code{sysprune}. |
|
67 ## |
|
68 ## @example |
|
69 ## bode(sys,[],"y_3",list("u_1","u_4"); |
|
70 ## @end example |
3431
|
71 ## @end table |
|
72 ## @strong{Outputs} |
|
73 ## @table @var |
|
74 ## @item mag |
|
75 ## @itemx phase |
|
76 ## the magnitude and phase of the frequency response @math{G(jw)} or |
|
77 ## @math{G(@code{exp}(jwT))} at the selected frequency values. |
|
78 ## @item w |
|
79 ## the vector of frequency values used |
|
80 ## @end table |
|
81 ## |
|
82 ## @strong{Notes} |
|
83 ## @enumerate |
|
84 ## @item If no output arguments are given, e.g., |
|
85 ## @example |
|
86 ## bode(sys); |
|
87 ## @end example |
|
88 ## bode plots the results to the screen. Descriptive labels are |
|
89 ## automatically placed. |
|
90 ## |
|
91 ## Failure to include a concluding semicolon will yield some garbage |
|
92 ## being printed to the screen (@code{ans = []}). |
|
93 ## |
|
94 ## @item If the requested plot is for an MIMO system, mag is set to |
|
95 ## @math{||G(jw)||} or @math{||G(@code{exp}(jwT))||} |
|
96 ## and phase information is not computed. |
|
97 ## @end enumerate |
|
98 ## @end deftypefn |
|
99 |
|
100 ## Author: John Ingram <ingraje@eng.auburn.edu> |
|
101 ## Created: July 10, 1996 |
|
102 ## Based on previous code by R. Bruce Tenison, July 13, 1994 |
|
103 ## Modified by David Clem November 13, 1994 |
|
104 ## again by A. S. Hodel July 1995 (smart plot range, etc.) |
|
105 ## Modified by Kai P. Mueller September 28, 1997 (multiplot mode) |
|
106 |
|
107 function [mag_r, phase_r, w_r] = bode (sys, w, outputs, inputs, plot_style) |
|
108 |
|
109 ## check number of input arguments given |
|
110 if (nargin < 1 | nargin > 5) |
|
111 usage("[mag,phase,w] = bode(sys[,w,outputs,inputs,plot_style])"); |
|
112 endif |
|
113 if(nargin < 2) |
|
114 w = []; |
|
115 endif |
|
116 if(nargin < 3) |
|
117 outputs = []; |
|
118 endif |
|
119 if(nargin < 4) |
|
120 inputs = []; |
|
121 endif |
|
122 if(nargin < 5) |
|
123 plot_style = "dB"; |
|
124 endif |
|
125 |
|
126 if (strcmp (plot_style, "dB")) |
|
127 do_db_plot = 1; |
|
128 elseif (strcmp (plot_style, "mag")) |
|
129 do_db_plot = 0; |
|
130 else |
|
131 error ("bode: invalid value of plot_style specified"); |
|
132 endif |
|
133 |
3455
|
134 [f, w, sys] = __bodquist__ (sys, w, outputs, inputs, "bode"); |
3431
|
135 |
|
136 [stname,inname,outname] = sysgetsignals(sys); |
|
137 systsam = sysgettsam(sys); |
|
138 |
|
139 ## Get the magnitude and phase of f. |
|
140 mag = abs(f); |
|
141 phase = arg(f)*180.0/pi; |
|
142 |
|
143 if (nargout < 1), |
|
144 ## Plot the information |
|
145 if(gnuplot_has_multiplot) |
|
146 oneplot(); |
|
147 endif |
|
148 gset autoscale; |
|
149 if(gnuplot_has_multiplot) |
|
150 gset nokey; |
|
151 endif |
|
152 clearplot(); |
|
153 gset data style lines; |
|
154 if(is_digital(sys)) |
|
155 xlstr = ["Digital frequency w=rad/sec. pi/T=",num2str(pi/systsam)]; |
|
156 tistr = "(exp(jwT)) "; |
|
157 else |
|
158 xlstr = "Frequency in rad/sec"; |
|
159 tistr = "(jw)"; |
|
160 endif |
|
161 xlabel(xlstr); |
|
162 if(is_siso(sys)) |
|
163 if (gnuplot_has_multiplot) |
|
164 subplot(2,1,1); |
|
165 endif |
|
166 title(["|[Y/U]",tistr,"|, u=", nth(inname,1),", y=",nth(outname,1)]); |
|
167 else |
|
168 title([ "||Y(", tistr, ")/U(", tistr, ")||"]); |
|
169 disp("MIMO plot from") |
3438
|
170 disp(__outlist__(inname," ")); |
3431
|
171 disp("to") |
3438
|
172 disp(__outlist__(outname," ")); |
3431
|
173 endif |
|
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 |
|
185 |
|
186 grid("on"); |
|
187 if (do_db_plot) |
|
188 semilogx(w,md); |
|
189 else |
|
190 loglog(w,md); |
|
191 endif |
|
192 if (is_siso(sys)) |
|
193 if (gnuplot_has_multiplot) |
|
194 subplot(2,1,2); |
|
195 else |
|
196 prompt("Press any key for phase plot"); |
|
197 endif |
|
198 axvec = axis2dlim([vec(w),vec(phase)]); |
|
199 axvec(1:2) = wv; |
|
200 axis(axvec); |
|
201 xlabel(xlstr); |
|
202 ylabel("Phase in deg"); |
|
203 title([ "phase([Y/U]", tistr, ... |
|
204 "), u=", nth(inname,1),", y=",nth(outname,1)]); |
|
205 grid("on"); |
|
206 semilogx(w,phase); |
|
207 ## This should be the default for subsequent plot commands. |
|
208 if(gnuplot_has_multiplot) |
|
209 oneplot(); |
|
210 endif |
|
211 endif |
|
212 else |
|
213 mag_r = mag; |
|
214 phase_r = phase; |
|
215 w_r = w; |
|
216 endif |
|
217 endfunction |