1 ## Copyright (C) 2010, 2011, 2012 Lukas F. Reichlin
3 ## This file is part of LTI Syncope.
5 ## LTI Syncope is free software: you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation, either version 3 of the License, or
8 ## (at your option) any later version.
10 ## LTI Syncope is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ## GNU General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with LTI Syncope. If not, see <http://www.gnu.org/licenses/>.
19 ## Display routine for FRD objects.
21 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
22 ## Created: February 2010
25 function display (sys)
27 sysname = inputname (1);
28 [inname, outname, tsam] = __lti_data__ (sys.lti);
30 [inname, m] = __labels__ (inname, "u");
31 [outname, p] = __labels__ (outname, "y");
33 w = __freq2str__ (sys.w);
38 disp (["Frequency response '", sysname, "' from input '", inname{k}, "' to output ..."]);
40 __disp_resp__ (sys.H(:,k,:), w, outname);
43 display (sys.lti); # display sampling time
46 disp ("Static gain.");
48 disp ("Continuous-time frequency response.");
50 disp ("Discrete-time frequency response.");
56 function __disp_resp__ (H, w, outname)
58 p = rows (H); # number of outputs
59 len = size (H, 3); # number of frequencies
61 H = mat2cell (H, ones (1, p), 1, len)(:);
62 H = cellfun (@__resp2str__, H, outname, "uniformoutput", false);
64 tsize = terminal_size ();
65 col_freq = columns (w);
66 col_resp = cellfun (@columns, H);
67 col_max = tsize(2) - col_freq;
68 width = cumsum (col_resp);
72 while (start < width(end))
73 idx = find (width > start & width <= stop);
76 start = width(idx(end));
77 stop = start + col_max;
80 ## FIXME: Handle case where tsize(2) is not enough
81 ## to display frequencies and one output.
86 function str = __freq2str__ (w, title = "w [rad/s]")
89 str = __vec2str__ (w);
90 line = repmat ("-", 1, max (columns (str), columns (title)));
91 str = strvcat (title, line, str);
92 space = repmat (" ", len+2, 1);
98 function str = __resp2str__ (H, outname)
102 real_str = __vec2str__ (real (H));
105 imag_str = __vec2str__ (abs (im), "i");
106 sign_str = repmat (" + ", len, 1);
108 sign_str(neg, 2) = "-";
109 str = [real_str, sign_str, imag_str];
113 line = repmat ("-", 1, max (columns (str), columns (outname)));
114 str = strvcat (outname, line, str);
115 space = repmat (" ", len+2, 1);
121 function str = __vec2str__ (vec, post)
124 tmp = isfinite (vec);
125 tmp = abs (vec(tmp & vec != 0));
126 if (isempty (tmp) || min (tmp) < 1e-3 || max (tmp) > 1e4)
127 str = arrayfun (@(x) sprintf ("%.3e", x), vec, "uniformoutput", false);
128 elseif (all (floor (tmp) == tmp))
129 str = arrayfun (@(x) sprintf ("%d", x), vec, "uniformoutput", false);
131 str = arrayfun (@(x) sprintf ("%.4f", x), vec, "uniformoutput", false);
133 str = strjust (char (str), "right");
135 str = [str, repmat(post, length (vec), 1)];