3437
|
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{y}, @var{t}] =} __stepimp__ (@var{sitype}, @var{sys} [, @var{inp}, @var{tstop}, @var{n}]) |
3437
|
21 ## Impulse or step response for a linear system. |
|
22 ## The system can be discrete or multivariable (or both). |
|
23 ## This m-file contains the "common code" of step and impulse. |
|
24 ## |
|
25 ## Produces a plot or the response data for system sys. |
|
26 ## |
|
27 ## Limited argument checking; "do not attempt to do this at home". |
|
28 ## Used internally in @code{impulse}, @code{step}. Use @code{step} |
|
29 ## or @code{impulse} instead. |
|
30 ## @end deftypefn |
|
31 ## @seealso{step and impulse} |
|
32 |
|
33 ## Author: Kai P. Mueller <mueller@ifr.ing.tu-bs.de> |
|
34 ## Created: October 2, 1997 |
|
35 ## based on lsim.m of Scottedward Hodel |
|
36 |
3438
|
37 function [y, t] = __stepimp__ (sitype, sys, inp, tstop, n) |
3437
|
38 |
|
39 if (sitype == 1) IMPULSE = 0; |
|
40 elseif (sitype == 2) IMPULSE = 1; |
3438
|
41 else error("__stepimp__: invalid sitype argument.") |
3437
|
42 endif |
|
43 sys = sysupdate(sys,"ss"); |
|
44 |
|
45 USE_DEF = 0; # default tstop and n if we have to give up |
|
46 N_MIN = 50; # minimum number of points |
|
47 N_MAX = 2000; # maximum number of points |
|
48 T_DEF = 10.0; # default simulation time |
|
49 |
|
50 ## collect useful information about the system |
|
51 [ncstates,ndstates,NIN,NOUT] = sysdimensions(sys); |
|
52 TSAMPLE = sysgettsam(sys); |
|
53 |
|
54 if (nargin < 3) inp = 1; |
|
55 elseif (inp < 1 | inp > NIN) error("Argument inp out of range") |
|
56 endif |
|
57 |
|
58 DIGITAL = is_digital(sys); |
|
59 if (DIGITAL) |
|
60 NSTATES = ndstates; |
|
61 if (TSAMPLE < eps) |
3438
|
62 error("__stepimp__: sampling time of discrete system too small.") |
3437
|
63 endif |
|
64 else NSTATES = ncstates; endif |
|
65 if (NSTATES < 1) |
|
66 error("step: pure gain block (n_states < 1), step response is trivial"); |
|
67 endif |
|
68 if (nargin < 5) |
|
69 ## we have to compute the time when the system reaches steady state |
|
70 ## and the step size |
|
71 ev = eig(sys2ss(sys)); |
|
72 if (DIGITAL) |
|
73 ## perform bilinear transformation on poles in z |
|
74 for i = 1:NSTATES |
|
75 pole = ev(i); |
|
76 if (abs(pole + 1) < 1.0e-10) |
|
77 ev(i) = 0; |
|
78 else |
|
79 ev(i) = 2 / TSAMPLE * (pole - 1) / (pole + 1); |
|
80 endif |
|
81 endfor |
|
82 endif |
|
83 ## remove poles near zero from eigenvalue array ev |
|
84 nk = NSTATES; |
|
85 for i = 1:NSTATES |
3679
|
86 if (abs(real(ev(i))) < 1.0e-10) |
3437
|
87 ev(i) = 0; |
|
88 nk = nk - 1; |
|
89 endif |
|
90 endfor |
|
91 if (nk == 0) |
|
92 USE_DEF = 1; |
|
93 ## printf("##STEPIMP-DEBUG: using defaults.\n"); |
|
94 else |
|
95 ev = ev(find(ev)); |
|
96 x = max(abs(ev)); |
|
97 t_step = 0.2 * pi / x; |
|
98 x = min(abs(real(ev))); |
|
99 t_sim = 5.0 / x; |
|
100 ## round up |
|
101 yy = 10^(ceil(log10(t_sim)) - 1); |
|
102 t_sim = yy * ceil(t_sim / yy); |
|
103 ## printf("##STEPIMP-DEBUG: nk=%d t_step=%f t_sim=%f\n", |
|
104 ## nk, t_step, t_sim); |
|
105 endif |
|
106 endif |
|
107 |
|
108 if (DIGITAL) |
|
109 ## ---- sampled system |
|
110 if (nargin == 5) |
|
111 n = round(n); |
|
112 if (n < 2) |
3438
|
113 error("__stepimp__: n must not be less than 2.") |
3437
|
114 endif |
|
115 else |
|
116 if (nargin == 4) |
|
117 ## n is unknown |
|
118 elseif (nargin >= 1) |
|
119 ## tstop and n are unknown |
|
120 if (USE_DEF) |
|
121 tstop = (N_MIN - 1) * TSAMPLE; |
|
122 else |
|
123 tstop = t_sim; |
|
124 endif |
|
125 endif |
|
126 n = floor(tstop / TSAMPLE) + 1; |
|
127 if (n < 2) n = 2; endif |
|
128 if (n > N_MAX) |
|
129 n = N_MAX; |
|
130 printf("Hint: number of samples limited to %d by default.\n", \ |
|
131 N_MAX); |
|
132 printf(" ==> increase \"n\" parameter for longer simulations.\n"); |
|
133 endif |
|
134 endif |
|
135 tstop = (n - 1) * TSAMPLE; |
|
136 t_step = TSAMPLE; |
|
137 else |
|
138 ## ---- continuous system |
|
139 if (nargin == 5) |
|
140 n = round(n); |
|
141 if (n < 2) |
|
142 error("step: n must not be less than 2.") |
|
143 endif |
|
144 t_step = tstop / (n - 1); |
|
145 else |
|
146 if (nargin == 4) |
|
147 ## only n in unknown |
|
148 if (USE_DEF) |
|
149 n = N_MIN; |
|
150 t_step = tstop / (n - 1); |
|
151 else |
|
152 n = floor(tstop / t_step) + 1; |
|
153 endif |
|
154 else |
|
155 ## tstop and n are unknown |
|
156 if (USE_DEF) |
|
157 tstop = T_DEF; |
|
158 n = N_MIN; |
|
159 t_step = tstop / (n - 1); |
|
160 else |
|
161 tstop = t_sim; |
|
162 n = floor(tstop / t_step) + 1; |
|
163 endif |
|
164 endif |
|
165 if (n < N_MIN) |
|
166 n = N_MIN; |
|
167 t_step = tstop / (n - 1); |
|
168 endif |
|
169 if (n > N_MAX) |
|
170 tstop = (n - 1) * t_step; |
|
171 t_step = tstop / (N_MAX - 1); |
|
172 n = N_MAX; |
|
173 endif |
|
174 endif |
|
175 tstop = (n - 1) * t_step; |
|
176 [jnk,B] = sys2ss(sys); |
|
177 B = B(:,inp); |
|
178 sys = c2d(sys, t_step); |
|
179 endif |
|
180 ## printf("##STEPIMP-DEBUG: t_step=%f n=%d tstop=%f\n", t_step, n, tstop); |
|
181 |
|
182 F = sys.a; |
|
183 G = sys.b(:,inp); |
|
184 C = sys.c; |
|
185 D = sys.d(:,inp); |
|
186 y = zeros(NOUT, n); |
|
187 t = linspace(0, tstop, n); |
|
188 |
|
189 if (IMPULSE) |
|
190 if (!DIGITAL && (D'*D > 0)) |
|
191 error("impulse: D matrix is nonzero, impulse response infinite.") |
|
192 endif |
|
193 if (DIGITAL) |
|
194 y(:,1) = D / t_step; |
|
195 x = G / t_step; |
|
196 else |
|
197 x = B; |
|
198 y(:,1) = C * x; |
|
199 x = F * x; |
|
200 endif |
|
201 for i = 2:n |
|
202 y(:,i) = C * x; |
|
203 x = F * x; |
|
204 endfor |
4375
|
205 if (DIGITAL) |
|
206 y *= t_step; |
|
207 endif |
3437
|
208 else |
|
209 x = zeros(NSTATES, 1); |
|
210 for i = 1:n |
|
211 y(:,i) = C * x + D; |
|
212 x = F * x + G; |
|
213 endfor |
|
214 endif |
3679
|
215 |
|
216 save_automatic_replot = automatic_replot; |
|
217 unwind_protect |
|
218 automatic_replot = 0; |
|
219 if(nargout == 0) |
|
220 ## Plot the information |
|
221 oneplot(); |
|
222 gset nogrid |
|
223 gset nologscale |
|
224 gset autoscale |
|
225 gset nokey |
4422
|
226 if (IMPULSE) |
|
227 gm = zeros(NOUT, 1); |
|
228 tt = "impulse"; |
|
229 else |
4771
|
230 ssys = ss(F, G, C, D, t_step); |
4422
|
231 gm = dcgain(ssys); |
|
232 tt = "step"; |
|
233 endif |
|
234 ncols = floor(sqrt(NOUT)); |
|
235 nrows = ceil(NOUT / ncols); |
4789
|
236 if (ncols > 1 || nrows > 1) |
|
237 clearplot(); |
|
238 endif |
4422
|
239 for i = 1:NOUT |
|
240 subplot(nrows, ncols, i); |
|
241 title(sprintf("%s: | %s -> %s", tt,sysgetsignals(sys,"in",inp,1), ... |
|
242 sysgetsignals(sys,"out",i,1))); |
|
243 if (DIGITAL) |
|
244 [ts, ys] = stairs(t, y(i,:)); |
|
245 ts = ts(1:2*n-2)'; ys = ys(1:2*n-2)'; |
|
246 if (length(gm) > 0) |
|
247 yy = [ys; gm(i)*ones(size(ts))]; |
|
248 else |
|
249 yy = ys; |
|
250 endif |
|
251 grid("on"); |
|
252 xlabel("time [s]"); |
|
253 ylabel("y(t)"); |
|
254 plot(ts, yy); |
3679
|
255 else |
4422
|
256 if (length(gm) > 0) |
|
257 yy = [y(i,:); gm(i)*ones(size(t))]; |
|
258 else |
|
259 yy = y(i,:); |
|
260 endif |
|
261 grid("on"); |
|
262 xlabel("time [s]"); |
|
263 ylabel("y(t)"); |
|
264 plot(t, yy); |
3679
|
265 endif |
4422
|
266 endfor |
|
267 ## leave gnuplot in multiplot mode is bad style |
|
268 oneplot(); |
3679
|
269 y=[]; |
|
270 t=[]; |
3437
|
271 endif |
3679
|
272 ## printf("##STEPIMP-DEBUG: gratulations, successfull completion.\n"); |
|
273 unwind_protect_cleanup |
|
274 automatic_replot = save_automatic_replot; |
|
275 end_unwind_protect |
|
276 endfunction |