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