function S = stepinfo (varargin) % STEPINFO Step-response performance metrics (MATLAB-compatible). % % S = stepinfo (SYS) compute from a tf/ss/zpk system % S = stepinfo (Y, T) from a pre-computed step response % S = stepinfo (Y, T, YFINAL) pass explicit steady-state value % S = stepinfo (..., 'SettlingTimeThreshold', alpha) default 0.02 % S = stepinfo (..., 'RiseTimeLimits', [lo hi]) default [0.10 0.90] % % Returned struct fields (same names as MATLAB): % RiseTime, SettlingTime, SettlingMin, SettlingMax, % Overshoot, Undershoot, Peak, PeakTime % % This is the MEC524 Octave drop-in for MATLAB's stepinfo, which is % indexed but not implemented in the Octave control package. % % Example % pkg load control % G = tf (2, [1 2 2]); % S = stepinfo (G) alpha = 0.02; rlimits = [0.10 0.90]; args = varargin; k = 1; while k <= numel (args) if ischar (args{k}) switch lower (args{k}) case 'settlingtimethreshold' alpha = args{k+1}; args(k:k+1) = []; case 'risetimelimits' rlimits = args{k+1}; args(k:k+1) = []; otherwise error ('stepinfo: unknown option ''%s''', args{k}); end else k = k + 1; end end if numel (args) == 1 sys = args{1}; [y, t] = step (sys); yfinal = dcgain (sys); if ~isfinite (yfinal) yfinal = y(end); end elseif numel (args) == 2 y = args{1}(:); t = args{2}(:); yfinal = y(end); elseif numel (args) == 3 y = args{1}(:); t = args{2}(:); yfinal = args{3}; else error ('stepinfo: expected (SYS) or (Y,T) or (Y,T,YFINAL).'); end [peakAbs, ipk] = max (abs (y)); Peak = peakAbs; PeakTime = t(ipk); if yfinal == 0 Overshoot = NaN; Undershoot = NaN; else yover = max (y) - yfinal; Overshoot = 100 * max (0, yover * sign (yfinal)) / abs (yfinal); yunder = -min (y) * sign (yfinal); Undershoot = 100 * max (0, yunder) / abs (yfinal); end band = alpha * abs (yfinal); idx = find (abs (y - yfinal) > band, 1, 'last'); if isempty (idx) || idx == numel (t) SettlingTime = t(end); else SettlingTime = t(idx + 1); end ylo = rlimits(1) * yfinal; yhi = rlimits(2) * yfinal; if yfinal >= 0 ilo = find (y >= ylo, 1, 'first'); ihi = find (y >= yhi, 1, 'first'); else ilo = find (y <= ylo, 1, 'first'); ihi = find (y <= yhi, 1, 'first'); end if isempty (ilo) || isempty (ihi) RiseTime = NaN; else RiseTime = t(ihi) - t(ilo); end jstart = max (1, find (t >= SettlingTime, 1, 'first')); if isempty (jstart), jstart = numel (t); end SettlingMin = min (y(jstart:end)); SettlingMax = max (y(jstart:end)); S = struct ( ... 'RiseTime', RiseTime, ... 'SettlingTime', SettlingTime, ... 'SettlingMin', SettlingMin, ... 'SettlingMax', SettlingMax, ... 'Overshoot', Overshoot, ... 'Undershoot', Undershoot, ... 'Peak', Peak, ... 'PeakTime', PeakTime); endfunction