]> Creatis software - CreaPhase.git/blob - octave_packages/control-2.3.52/@frd/display.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / control-2.3.52 / @frd / display.m
1 ## Copyright (C) 2010, 2011, 2012   Lukas F. Reichlin
2 ##
3 ## This file is part of LTI Syncope.
4 ##
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.
9 ##
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.
14 ##
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/>.
17
18 ## -*- texinfo -*-
19 ## Display routine for FRD objects.
20
21 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
22 ## Created: February 2010
23 ## Version: 0.2
24
25 function display (sys)
26
27   sysname = inputname (1);
28   [inname, outname, tsam] = __lti_data__ (sys.lti);
29
30   [inname, m] = __labels__ (inname, "u");
31   [outname, p] = __labels__ (outname, "y");
32   
33   w = __freq2str__ (sys.w);
34
35   disp ("");
36
37   for k = 1 : m
38     disp (["Frequency response '", sysname, "' from input '", inname{k}, "' to output ..."]);
39     disp ("");
40     __disp_resp__ (sys.H(:,k,:), w, outname);
41   endfor
42
43   display (sys.lti);  # display sampling time
44
45   if (tsam == -2)
46     disp ("Static gain.");
47   elseif (tsam == 0)
48     disp ("Continuous-time frequency response.");
49   else
50     disp ("Discrete-time frequency response.");
51   endif
52
53 endfunction
54
55
56 function __disp_resp__ (H, w, outname)
57
58   p = rows (H);       # number of outputs
59   len = size (H, 3);  # number of frequencies
60
61   H = mat2cell (H, ones (1, p), 1, len)(:);
62   H = cellfun (@__resp2str__, H, outname, "uniformoutput", false);
63   
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);
69   
70   start = 0;
71   stop = col_max;
72   while (start < width(end))
73     idx = find (width > start & width <= stop);
74     disp ([w, H{idx}]);
75     disp ("");
76     start = width(idx(end));
77     stop = start + col_max;
78   endwhile
79
80   ## FIXME: Handle case where tsize(2) is not enough
81   ##        to display frequencies and one output.
82
83 endfunction
84
85
86 function str = __freq2str__ (w, title = "w [rad/s]")
87
88   len = rows (w);
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);
93   str = [space, str];
94   
95 endfunction
96
97
98 function str = __resp2str__ (H, outname)
99
100   H = H(:);
101   len = length (H);
102   real_str = __vec2str__ (real (H));
103   im = imag (H);
104   if (any (im))
105     imag_str = __vec2str__ (abs (im), "i");
106     sign_str = repmat (" + ", len, 1);
107     neg = im < 0;
108     sign_str(neg, 2) = "-";
109     str = [real_str, sign_str, imag_str];
110   else
111     str = real_str;
112   endif
113   line = repmat ("-", 1, max (columns (str), columns (outname)));
114   str = strvcat (outname, line, str);
115   space = repmat ("    ", len+2, 1);
116   str = [space, str];
117
118 endfunction
119
120
121 function str = __vec2str__ (vec, post)
122
123   vec = vec(:);
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);
130   else
131     str = arrayfun (@(x) sprintf ("%.4f", x), vec, "uniformoutput", false);
132   endif
133   str = strjust (char (str), "right");
134   if (nargin > 1)
135     str = [str, repmat(post, length (vec), 1)];
136   endif
137
138 endfunction