* Bug #12793 fixed (proposal 2) - Cacsd: improve the bode() plots
[scilab.git] / scilab / modules / cacsd / tests / unit_tests / bode.tst
1 // =============================================================================
2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) 2008 - INRIA - Serge Steer
4 //
5 //  This file is distributed under the same license as the Scilab package.
6 // =============================================================================
7 //
8 // <-- TEST WITH GRAPHIC -->
9 //
10 // <-- ENGLISH IMPOSED -->
11 //
12
13 function r=checkbode(h,leg)
14     fig=gcf();
15     r=%t;
16     ap=fig.children(1);
17     am=fig.children(2);
18     if am.log_flags<>"lnn" then r=%f;return,end
19     if or(am.axes_visible<>["on","on","on"]) then r=%f;return,end
20     if ap.log_flags<>"lnn" then  r=%f;return,end
21     if or(ap.axes_visible<>["on","on","on"]) then r=%f;return,end
22
23     n=size(am.children($).children,"*");
24     if n<>size(h,1) then pause,end
25     for k=1:n;
26         pm=am.children($).children(n+1-k);
27         pp=ap.children($).children(n+1-k);
28         if or(pm.data(:,1)<>pp.data(:,1)) then ;r=%f;return,end
29         [frq,rf]=repfreq(h(k,1),pm.data(:,1));[phi,d]=phasemag(rf);
30         if norm(d(:)-pm.data(:,2))> 1d-15 then r=%f;return,end
31     end
32     if argn(2)==2 then
33         if size(ap.children,"*")<2 then r=%f;return,end
34         if ap.children(1).type<>"Legend" then r=%f;return,end
35         if size(ap.children(1).links,"*")<>n then r=%f;return,end
36         if or(ap.children(1).text<>leg(:)) then r=%f;return,end
37     end
38 endfunction
39
40 s=poly(0,"s");
41 n=1+s;d=1+2*s;
42 h=syslin("c",n,d);
43 sl=tf2ss(h);
44 sld=dscr(sl,0.01);
45 hd=ss2tf(sld);
46
47 [w,rf]=repfreq(h,0.01,100);
48
49 // Continuous time
50 clf();bode(h);
51 if ~checkbode(h) then pause,end
52 clf();bode(h,"h");
53 if ~checkbode(h,"h") then pause,end
54
55 clf();bode(h,0.01,100);
56 if ~checkbode(h) then pause,end
57 clf();bode(h,0.01,100,"h");
58 if ~checkbode(h,"h") then pause,end
59
60 clf();bode(h,0.01,100,0.01);
61 if ~checkbode(h) then pause,end
62 clf();bode(sl,0.01,100);
63 if ~checkbode(sl) then pause,end
64 clf();bode(sl,0.01,100,0.01);
65
66 // Continuous time SIMO
67 h=[h;h+1];sl=[sl;sl+1];
68 clf();bode(h);
69 if ~checkbode(h) then pause,end
70 clf();bode(h,["h";"h+1"]);
71 if ~checkbode(h,["h";"h+1"]) then pause,end
72
73 clf();bode(h,0.01,100);
74 if ~checkbode(h) then pause,end
75 clf();bode(h,0.01,100,["h";"h+1"]);
76 if ~checkbode(h,["h";"h+1"]) then pause,end
77
78 clf();bode(h,0.01,100,0.01);
79 if ~checkbode(h) then pause,end
80 clf();bode(sl,0.01,100);
81 if ~checkbode(sl) then pause,end
82 clf();bode(sl,0.01,100,0.01);
83
84
85 // Discrete case
86 clf();bode(hd);
87 if ~checkbode(hd) then pause,end
88 clf();bode(hd,"h");
89 if ~checkbode(hd,"h") then pause,end
90
91 clf();bode(hd,0.01,100);
92 if ~checkbode(hd) then pause,end
93 clf();bode(hd,0.01,100,"h");
94 if ~checkbode(hd,"h") then pause,end
95
96 clf();bode(hd,0.01,100,0.01);
97 if ~checkbode(hd) then pause,end
98 clf();bode(sld,0.01,100);
99 if ~checkbode(sld) then pause,end
100 clf();bode(sl,0.01,100,0.01);
101
102 // Discrete case SIMO
103 hd=[hd;hd+1];sld=[sld;sld+1];
104 clf();bode(hd);
105 if ~checkbode(hd) then pause,end
106 clf();bode(hd,["h";"h+1"]);
107 if ~checkbode(hd,["h";"h+1"]) then pause,end
108
109 clf();bode(hd,0.01,100);
110 if ~checkbode(hd) then pause,end
111 clf();bode(hd,0.01,100,["h";"h+1"]);
112 if ~checkbode(hd,["h";"h+1"]) then pause,end
113
114 clf();bode(hd,0.01,100,0.01);
115 if ~checkbode(hd) then pause,end
116 clf();bode(sld,0.01,100);
117 if ~checkbode(sld) then pause,end
118 clf();bode(sl,0.01,100,0.01);
119
120 // bode given by precomputed frequency response
121 h=syslin("c",n,d);
122 clf();bode(h,0.01,100);
123 fig=gcf();
124 dp=fig.children(1).children($).children.data;
125 dm=fig.children(2).children($).children.data;
126 [w,rf]=repfreq(h,0.01,100);
127
128 clf(); bode(w,rf);
129 fig=gcf();
130 if norm(dp-fig.children(1).children($).children.data)>1d-13 then pause,end
131 if norm(dm-fig.children(2).children($).children.data)>1d-13 then pause,end
132
133 clf(); bode(w,20*log(abs(rf))/log(10),(180/%pi)*atan(imag(rf),real(rf)))
134 fig=gcf();
135 if norm(dp-fig.children(1).children($).children.data)>1d-13 then pause,end
136 if norm(dm-fig.children(2).children($).children.data)>1d-13 then pause,end
137
138 // Bode_asymp()
139 s = %s;
140 G = (s+10)/(s*(s+100)); // A rational matrix
141 sys = syslin("c", G); // A continuous-time linear system in transfer matrix representation.
142 f_min = .0001; f_max = 1600; // Frequencies in Hz
143
144 clf(); bode(sys, f_min, f_max, "rad"); // The optional argument "rad" converts Hz to rad/s
145 bode_asymp(sys, 10, 1000); // Plots asymptotes in the given range of frequency (in rad/s).