3431
|
1 ## Copyright (C) 1996, 1998 Auburn University. All rights reserved. |
|
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 |
|
44 ## @item via routine bodquist, isolate all poles and zeros away from |
|
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 |
|
64 ## the indices of the output(s) and input(s) to be used in |
|
65 ## the frequency response; see @code{sysprune}. |
|
66 ## @end table |
|
67 ## @strong{Outputs} |
|
68 ## @table @var |
|
69 ## @item mag |
|
70 ## @itemx phase |
|
71 ## the magnitude and phase of the frequency response @math{G(jw)} or |
|
72 ## @math{G(@code{exp}(jwT))} at the selected frequency values. |
|
73 ## @item w |
|
74 ## the vector of frequency values used |
|
75 ## @end table |
|
76 ## |
|
77 ## @strong{Notes} |
|
78 ## @enumerate |
|
79 ## @item If no output arguments are given, e.g., |
|
80 ## @example |
|
81 ## bode(sys); |
|
82 ## @end example |
|
83 ## bode plots the results to the screen. Descriptive labels are |
|
84 ## automatically placed. |
|
85 ## |
|
86 ## Failure to include a concluding semicolon will yield some garbage |
|
87 ## being printed to the screen (@code{ans = []}). |
|
88 ## |
|
89 ## @item If the requested plot is for an MIMO system, mag is set to |
|
90 ## @math{||G(jw)||} or @math{||G(@code{exp}(jwT))||} |
|
91 ## and phase information is not computed. |
|
92 ## @end enumerate |
|
93 ## @end deftypefn |
|
94 |
|
95 ## Author: John Ingram <ingraje@eng.auburn.edu> |
|
96 ## Created: July 10, 1996 |
|
97 ## Based on previous code by R. Bruce Tenison, July 13, 1994 |
|
98 ## Modified by David Clem November 13, 1994 |
|
99 ## again by A. S. Hodel July 1995 (smart plot range, etc.) |
|
100 ## Modified by Kai P. Mueller September 28, 1997 (multiplot mode) |
|
101 |
|
102 function [mag_r, phase_r, w_r] = bode (sys, w, outputs, inputs, plot_style) |
|
103 |
|
104 ## check number of input arguments given |
|
105 if (nargin < 1 | nargin > 5) |
|
106 usage("[mag,phase,w] = bode(sys[,w,outputs,inputs,plot_style])"); |
|
107 endif |
|
108 if(nargin < 2) |
|
109 w = []; |
|
110 endif |
|
111 if(nargin < 3) |
|
112 outputs = []; |
|
113 endif |
|
114 if(nargin < 4) |
|
115 inputs = []; |
|
116 endif |
|
117 if(nargin < 5) |
|
118 plot_style = "dB"; |
|
119 endif |
|
120 |
|
121 if (strcmp (plot_style, "dB")) |
|
122 do_db_plot = 1; |
|
123 elseif (strcmp (plot_style, "mag")) |
|
124 do_db_plot = 0; |
|
125 else |
|
126 error ("bode: invalid value of plot_style specified"); |
|
127 endif |
|
128 |
|
129 [f, w] = bodquist(sys,w,outputs,inputs,"bode"); |
|
130 |
|
131 [stname,inname,outname] = sysgetsignals(sys); |
|
132 systsam = sysgettsam(sys); |
|
133 |
|
134 ## Get the magnitude and phase of f. |
|
135 mag = abs(f); |
|
136 phase = arg(f)*180.0/pi; |
|
137 |
|
138 if (nargout < 1), |
|
139 ## Plot the information |
|
140 if(gnuplot_has_multiplot) |
|
141 oneplot(); |
|
142 endif |
|
143 gset autoscale; |
|
144 if(gnuplot_has_multiplot) |
|
145 gset nokey; |
|
146 endif |
|
147 clearplot(); |
|
148 gset data style lines; |
|
149 if(is_digital(sys)) |
|
150 xlstr = ["Digital frequency w=rad/sec. pi/T=",num2str(pi/systsam)]; |
|
151 tistr = "(exp(jwT)) "; |
|
152 else |
|
153 xlstr = "Frequency in rad/sec"; |
|
154 tistr = "(jw)"; |
|
155 endif |
|
156 xlabel(xlstr); |
|
157 if(is_siso(sys)) |
|
158 if (gnuplot_has_multiplot) |
|
159 subplot(2,1,1); |
|
160 endif |
|
161 title(["|[Y/U]",tistr,"|, u=", nth(inname,1),", y=",nth(outname,1)]); |
|
162 else |
|
163 title([ "||Y(", tistr, ")/U(", tistr, ")||"]); |
|
164 disp("MIMO plot from") |
|
165 disp(outlist(inname," ")); |
|
166 disp("to") |
|
167 disp(outlist(outname," ")); |
|
168 endif |
|
169 wv = [min(w), max(w)]; |
|
170 if(do_db_plot && max(mag) > 0) |
|
171 ylabel("Gain in dB"); |
|
172 md = 20*log10(mag); |
|
173 axvec = axis2dlim([vec(w),vec(md)]); |
|
174 axvec(1:2) = wv; |
|
175 axis(axvec); |
|
176 else |
|
177 ylabel("Gain |Y/U|") |
|
178 md = mag; |
|
179 endif |
|
180 |
|
181 grid("on"); |
|
182 if (do_db_plot) |
|
183 semilogx(w,md); |
|
184 else |
|
185 loglog(w,md); |
|
186 endif |
|
187 if (is_siso(sys)) |
|
188 if (gnuplot_has_multiplot) |
|
189 subplot(2,1,2); |
|
190 else |
|
191 prompt("Press any key for phase plot"); |
|
192 endif |
|
193 axvec = axis2dlim([vec(w),vec(phase)]); |
|
194 axvec(1:2) = wv; |
|
195 axis(axvec); |
|
196 xlabel(xlstr); |
|
197 ylabel("Phase in deg"); |
|
198 title([ "phase([Y/U]", tistr, ... |
|
199 "), u=", nth(inname,1),", y=",nth(outname,1)]); |
|
200 grid("on"); |
|
201 semilogx(w,phase); |
|
202 ## This should be the default for subsequent plot commands. |
|
203 if(gnuplot_has_multiplot) |
|
204 oneplot(); |
|
205 endif |
|
206 endif |
|
207 else |
|
208 mag_r = mag; |
|
209 phase_r = phase; |
|
210 w_r = w; |
|
211 endif |
|
212 endfunction |