1// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2// Copyright (C) INRIA -
3//
4// Copyright (C) 2012 - 2016 - Scilab Enterprises
5//
6// This file is hereby licensed under the terms of the GNU GPL v2.0,
7// pursuant to article 5.3.4 of the CeCILL v.2.1.
8// This file was originally licensed under the terms of the CeCILL v2.1,
9// and continues to be available under such terms.
10// For more information, see the COPYING file which you should have received
11// along with this program.
12
13function [nh]=h2norm(g,tol)
14    //
15    //                 /+00
16    //     2           |          *
17    //  |g| =1/(2*%pi).|trace[g(jw).g(jw)]dw
18    //     2           |
19    //                 /-00
20    if argn(2)<1 then
21        error(msprintf(gettext("%s: Wrong number of input argument(s): At least %d expected.\n"),..
22        "h2norm",1))
23    end
24
25    if type(g)==1,if norm(g)==0,nh=0,return,end,end,
26    if and(typeof(g)<>["rational","state-space"]) then
27        msg = gettext("%s: Wrong type for input argument #%d: Linear state space or a transfer function expected.\n")
28        error(msprintf(msg, "h2norm", 1))
29    end
30    if g.dt<>"c" & g.dt<>[] then
31        msg = gettext("%s: Wrong type for argument #%d: In continuous or undefined time domain expected.\n")
32        error(msprintf(msg,"h2norm",1))
33    end
34
35    [lhs,rhs]=argn(0),
36    if rhs==1 then
37        tol=1000*%eps,
38    else
39        if type(tol)<>1|size(tol,"*")<>1 then
40            msg = gettext("%s: Wrong type for input argument: Scalar expected.\n")
41            error(msprintf(msg, "h2norm", 2))
42        end
43        if ~isreal(tol)|tol<=0 then
44            msg = gettext( "%s: Input argument #%d must be strictly positive.\n")
45            error(msprintf(msg, "h2norm", 2))
46        end
47    end;
48    select typeof(g)
49    case "state-space" then
50        if norm(g.D)>0 then
51            msg = gettext("%s: Wrong value for input argument #%d: Proper system expected.\n")
52            error(msprintf(msg, "h2norm", 1)),
53        end;
54        sp = spec(g.A),
55        if max(real(sp))>=-tol then
56            msg = gettext("%s: Wrong value for input argument #%d: Stable system expected.\n")
57            error(msprintf(msg, "h2norm", 1)),
58        end,
59        w=obs_gram(g.A,g.C,"c"),
60        nh=abs(sqrt(sum(diag(g.B'*w*g.B)))),return,
61    case "rational" then
62        num = g.num;
63        den = g.den;
64        s = poly(0,varn(den))
65        [t1,t2] = size(num)
66        for i = 1:t1,
67            for j = 1:t2,
68                n = num(i,j)
69                d = den(i,j),
70                if coeff(n)==0 then
71                    nh(i,j) = 0
72                else
73                    if degree(n) >= degree(d) then
74                        msg = gettext("%s: Wrong value for input argument #%d: Proper system expected.\n")
75                        error(msprintf(msg, "h2norm", 1)),
76                    end
77                    pol = roots(d),
78                    if max(real(pol))>-tol then
79                        msg = gettext("%s: Wrong value for input argument #%d: Stable system expected.\n")
80                        error(msprintf(msg, "h2norm", 1))
81                    end,
82                    nt = horner(n,-s)
83                    dt = horner(d,-s)
84                    nh(i,j) = residu(n*nt,d,dt)
85                end,
86            end
87        end
88        nh=sqrt(sum(nh)),return,
89    end
90endfunction
91