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 -*- |
3500
|
20 ## @deftypefn {Function File} {[@var{realp}, @var{imagp}, @var{w}] =} nyquist (@var{sys}, @var{w}, @var{out_idx}, @var{in_idx}, @var{atol}) |
|
21 ## @deftypefnx {Function File} {} nyquist (@var{sys}, @var{w}, @var{out_idx}, @var{in_idx}, @var{atol}) |
3431
|
22 ## Produce Nyquist plots of a system; if no output arguments are given, Nyquist |
|
23 ## plot is printed to the screen. |
|
24 ## |
|
25 ## Compute the frequency response of a system. |
|
26 ## @strong{Inputs} (pass as empty to get default values) |
|
27 ## @table @var |
|
28 ## @item sys |
|
29 ## system data structure (must be either purely continuous or discrete; |
|
30 ## see is_digital) |
|
31 ## @item w |
|
32 ## frequency values for evaluation. |
|
33 ## if sys is continuous, then bode evaluates @math{G(jw)} |
|
34 ## if sys is discrete, then bode evaluates @math{G(exp(jwT))}, where |
3502
|
35 ## @math{T} is the system sampling time. |
3431
|
36 ## @item default |
|
37 ## the default frequency range is selected as follows: (These |
|
38 ## steps are NOT performed if @var{w} is specified) |
|
39 ## @end table |
|
40 ## @enumerate |
3438
|
41 ## @item via routine __bodquist__, isolate all poles and zeros away from |
3431
|
42 ## @var{w}=0 (@var{jw}=0 or @math{exp(@var{jwT})=1}) and select the frequency |
|
43 ## range based on the breakpoint locations of the frequencies. |
|
44 ## @item if @var{sys} is discrete time, the frequency range is limited |
|
45 ## to @var{jwT} in |
|
46 ## @ifinfo |
|
47 ## [0,2p*pi] |
|
48 ## @end ifinfo |
|
49 ## @iftex |
|
50 ## $[0,2p*\pi]$ |
|
51 ## @end iftex |
|
52 ## @item A "smoothing" routine is used to ensure that the plot phase does |
|
53 ## not change excessively from point to point and that singular |
|
54 ## points (e.g., crossovers from +/- 180) are accurately shown. |
|
55 ## @end enumerate |
3462
|
56 ## outputs, inputs: names or indices of the output(s) and input(s) to be |
|
57 ## used in the frequency response; see sysprune. |
3431
|
58 ## |
|
59 ## @strong{Inputs} (pass as empty to get default values) |
|
60 ## @table @var |
|
61 ## @item atol |
|
62 ## for interactive nyquist plots: atol is a change-in-slope tolerance |
|
63 ## for the of asymptotes (default = 0; 1e-2 is a good choice). This allows |
|
64 ## the user to ``zoom in'' on portions of the Nyquist plot too small to be |
|
65 ## seen with large asymptotes. |
|
66 ## @end table |
|
67 ## @strong{Outputs} |
|
68 ## @table @var |
|
69 ## @item realp |
|
70 ## @itemx imagp |
|
71 ## the real and imaginary parts of the frequency response |
|
72 ## @math{G(jw)} or @math{G(exp(jwT))} at the selected frequency values. |
|
73 ## @item w |
|
74 ## the vector of frequency values used |
|
75 ## @end table |
|
76 ## |
|
77 ## If no output arguments are given, nyquist plots the results to the screen. |
|
78 ## If @var{atol} != 0 and asymptotes are detected then the user is asked |
|
79 ## interactively if they wish to zoom in (remove asymptotes) |
|
80 ## Descriptive labels are automatically placed. |
|
81 ## |
|
82 ## Note: if the requested plot is for an MIMO system, a warning message is |
|
83 ## presented; the returned information is of the magnitude |
|
84 ## ||G(jw)|| or ||G(exp(jwT))|| only; phase information is not computed. |
|
85 ## @end deftypefn |
|
86 |
|
87 ## Author: R. Bruce Tenison <btenison@eng.auburn.edu> |
|
88 ## Created: July 13, 1994 |
|
89 ## A. S. Hodel July 1995 (adaptive frequency spacing, |
|
90 ## remove acura parameter, etc.) |
|
91 ## Revised by John Ingram July 1996 for system format |
|
92 |
|
93 function [realp, imagp, w] = nyquist (sys, w, outputs, inputs, atol) |
|
94 |
|
95 ## Both bode and nyquist share the same introduction, so the common |
3438
|
96 ## parts are in a file called __bodquist__.m. It contains the part that |
3431
|
97 ## finds the number of arguments, determines whether or not the system |
|
98 ## is SISO, andd computes the frequency response. Only the way the |
|
99 ## response is plotted is different between the two functions. |
|
100 |
|
101 ## check number of input arguments given |
|
102 if (nargin < 1 | nargin > 5) |
|
103 usage("[realp,imagp,w] = nyquist(sys[,w,outputs,inputs,atol])"); |
|
104 endif |
|
105 if(nargin < 2) |
|
106 w = []; |
|
107 endif |
|
108 if(nargin < 3) |
|
109 outputs = []; |
|
110 endif |
|
111 if(nargin < 4) |
|
112 inputs = []; |
|
113 endif |
|
114 if(nargin < 5) |
|
115 atol = 0; |
|
116 elseif(!(is_sample(atol) | atol == 0)) |
|
117 error("atol must be a nonnegative scalar.") |
|
118 endif |
|
119 |
3438
|
120 ## signal to __bodquist__ who's calling |
3431
|
121 |
3455
|
122 [f, w, sys] = __bodquist__ (sys, w, outputs, inputs, "nyquist"); |
3431
|
123 |
|
124 ## Get the real and imaginary part of f. |
|
125 realp = real(f); |
|
126 imagp = imag(f); |
|
127 |
|
128 ## No output arguments, then display plot, otherwise return data. |
|
129 if (nargout == 0) |
|
130 dnplot = 0; |
|
131 while(!dnplot) |
4422
|
132 oneplot(); |
|
133 gset key; |
3431
|
134 clearplot(); |
|
135 grid ("on"); |
|
136 gset data style lines; |
|
137 |
|
138 if(is_digital(sys)) |
|
139 tstr = " G(e^{jw}) "; |
|
140 else |
|
141 tstr = " G(jw) "; |
|
142 endif |
|
143 xlabel(["Re(",tstr,")"]); |
|
144 ylabel(["Im(",tstr,")"]); |
|
145 |
|
146 [stn, inn, outn] = sysgetsignals(sys); |
|
147 if(is_siso(sys)) |
|
148 title(sprintf("Nyquist plot from %s to %s, w (rad/s) in [%e, %e]", ... |
4771
|
149 inn{1}, outn{1}, w(1), w(length(w))) ) |
3431
|
150 endif |
|
151 |
|
152 gset nologscale xy; |
|
153 |
|
154 axis(axis2dlim([[vec(realp),vec(imagp)];[vec(realp),-vec(imagp)]])); |
|
155 plot(realp,imagp,"- ;+w;",realp,-imagp,"-@ ;-w;"); |
|
156 |
|
157 ## check for interactive plots |
|
158 dnplot = 1; # assume done; will change later if atol is satisfied |
|
159 if(atol > 0 & length(f) > 2) |
|
160 |
|
161 ## check for asymptotes |
|
162 fmax = max(abs(f)); |
|
163 fi = max(find(abs(f) == fmax)); |
|
164 |
|
165 ## compute angles from point to point |
|
166 df = diff(f); |
|
167 th = atan2(real(df),imag(df))*180/pi; |
|
168 |
|
169 ## get angle at fmax |
|
170 if(fi == length(f)) fi = fi-1; endif |
|
171 thm = th(fi); |
|
172 |
|
173 ## now locate consecutive angles within atol of thm |
|
174 ith_same = find(abs(th - thm) < atol); |
|
175 ichk = union(fi,find(diff(ith_same) == 1)); |
|
176 |
|
177 ## locate max, min consecutive indices in ichk |
|
178 loval = max(complement(ichk,1:fi)); |
|
179 if(isempty(loval)) loval = fi; |
|
180 else loval = loval + 1; endif |
|
181 |
|
182 hival = min(complement(ichk,fi:length(th))); |
|
183 if(isempty(hival)) hival = fi+1; endif |
|
184 |
|
185 keep_idx = complement(loval:hival,1:length(w)); |
|
186 |
|
187 if(length(keep_idx)) |
|
188 resp = input("Remove asymptotes and zoom in (y or n): ",1); |
|
189 if(resp(1) == "y") |
|
190 dnplot = 0; # plot again |
|
191 w = w(keep_idx); |
|
192 f = f(keep_idx); |
|
193 realp = real(f); |
|
194 imagp = imag(f); |
|
195 endif |
|
196 endif |
|
197 |
|
198 endif |
|
199 endwhile |
|
200 w = []; |
|
201 realp=[]; |
|
202 imagp=[]; |
|
203 endif |
|
204 |
|
205 endfunction |