]> Creatis software - CreaPhase.git/blob - octave_packages/control-2.3.52/@tf/__minreal__.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / control-2.3.52 / @tf / __minreal__.m
1 ## Copyright (C) 2009   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 ## Minimal realization of TF models.
20
21 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
22 ## Created: October 2009
23 ## Version: 0.1
24
25 function sys = __minreal__ (sys, tol)
26
27   ## TODO: Once ZPK models are implemented, convert TF to ZPK,
28   ##       do cancellations over there and convert back to TF
29
30   sqrt_eps = sqrt (eps);                        # treshold for zero
31
32   [p, m] = size (sys);
33
34   for ny = 1 : p
35     for nu = 1 : m
36       sisosys = __sys_prune__ (sys, ny, nu);
37
38       [zer, gain] = zero (sisosys);
39       pol = pole (sisosys);
40
41       for k = length (zer) : -1 : 1             # reversed because of deleted zeros
42         [~, idx] = min (abs (zer(k) - pol));    # find best match
43
44         if (tol == "def")
45           if (abs (zer(k)) < sqrt_eps)          # catch case zer(k) = 0
46             t = 1000 * eps;
47           else
48             t = 1000 * abs (zer(k)) * sqrt_eps;
49           endif
50         else
51           t = tol;
52         endif
53
54         if (abs (zer(k) - pol(idx)) < t)
55           zer(k) = [];
56           pol(idx) = [];
57         endif
58       endfor
59
60       num = real (gain * poly (zer));
61       den = real (poly (pol));
62
63       num_idx = find (abs (num) < sqrt_eps);    # suppress numerical noise
64       den_idx = find (abs (den) < sqrt_eps);    # in polynomial coefficients
65
66       num(num_idx) = 0;
67       den(den_idx) = 0;
68
69       sys.num{ny, nu} = tfpoly (num);
70       sys.den{ny, nu} = tfpoly (den);
71     endfor
72   endfor
73
74 endfunction