5e408e085f37298f74a3cf5cecf83d170c7cd98a
[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 // <-- ENGLISH IMPOSED -->
10
11 function r=checkbode(h,leg)
12   fig=gcf();
13   r=%t
14   ap=fig.children(1);
15   am=fig.children(2);
16   if am.log_flags<>'lnn' then r=%f;return,end
17   if or(am.axes_visible<>["on","on","on"]) then r=%f;return,end
18   if ap.log_flags<>'lnn' then  r=%f;return,end
19   if or(ap.axes_visible<>["on","on","on"]) then r=%f;return,end
20
21   n=size(am.children($).children,'*')
22   if n<>size(h,1) then pause,end
23   for k=1:n;
24     pm=am.children($).children(n+1-k);
25     pp=ap.children($).children(n+1-k);
26     if or(pm.data(:,1)<>pp.data(:,1)) then ;r=%f;return,end
27     [frq,rf]=repfreq(h(k,1),pm.data(:,1));[phi,d]=phasemag(rf);
28     if norm(d(:)-pm.data(:,2))> 1d-15 then r=%f;return,end
29   end
30   if argn(2)==2 then 
31     if size(ap.children,'*')<2 then r=%f;return,end
32     if ap.children(1).type<>'Legend' then r=%f;return,end
33     if size(ap.children(1).links,'*')<>n then r=%f;return,end
34     if or(ap.children(1).text<>leg(:)) then r=%f;return,end
35   end
36 endfunction
37
38 s=poly(0,'s')
39 n=1+s;d=1+2*s;
40 h=syslin('c',n,d)
41 sl=tf2ss(h);
42 sld=dscr(sl,0.01);
43 hd=ss2tf(sld);
44
45 [w,rf]=repfreq(h,0.01,100);
46
47 //continuous time
48 clf();bode(h);
49 if ~checkbode(h) then pause,end
50 clf();bode(h,'h');
51 if ~checkbode(h,'h') then pause,end
52
53 clf();bode(h,0.01,100);
54 if ~checkbode(h) then pause,end
55 clf();bode(h,0.01,100,'h');
56 if ~checkbode(h,'h') then pause,end
57
58 clf();bode(h,0.01,100,0.01)
59 if ~checkbode(h) then pause,end
60 clf();bode(sl,0.01,100);
61 if ~checkbode(sl) then pause,end
62 clf();bode(sl,0.01,100,0.01)
63
64 //continuous time SIMO
65 h=[h;h+1];sl=[sl;sl+1];
66 clf();bode(h);
67 if ~checkbode(h) then pause,end
68 clf();bode(h,['h';'h+1']);
69 if ~checkbode(h,['h';'h+1']) then pause,end
70
71 clf();bode(h,0.01,100);
72 if ~checkbode(h) then pause,end
73 clf();bode(h,0.01,100,['h';'h+1']);
74 if ~checkbode(h,['h';'h+1']) then pause,end
75
76 clf();bode(h,0.01,100,0.01)
77 if ~checkbode(h) then pause,end
78 clf();bode(sl,0.01,100);
79 if ~checkbode(sl) then pause,end
80 clf();bode(sl,0.01,100,0.01)
81
82
83 //discrete case
84 clf();bode(hd);
85 if ~checkbode(hd) then pause,end
86 clf();bode(hd,'h');
87 if ~checkbode(hd,'h') then pause,end
88
89 clf();bode(hd,0.01,100);
90 if ~checkbode(hd) then pause,end
91 clf();bode(hd,0.01,100,'h');
92 if ~checkbode(hd,'h') then pause,end
93
94 clf();bode(hd,0.01,100,0.01)
95 if ~checkbode(hd) then pause,end
96 clf();bode(sld,0.01,100);
97 if ~checkbode(sld) then pause,end
98 clf();bode(sl,0.01,100,0.01)
99
100 //discrete case SIMO
101 hd=[hd;hd+1];sld=[sld;sld+1];
102 clf();bode(hd);
103 if ~checkbode(hd) then pause,end
104 clf();bode(hd,['h';'h+1']);
105 if ~checkbode(hd,['h';'h+1']) then pause,end
106
107 clf();bode(hd,0.01,100);
108 if ~checkbode(hd) then pause,end
109 clf();bode(hd,0.01,100,['h';'h+1']);
110 if ~checkbode(hd,['h';'h+1']) then pause,end
111
112 clf();bode(hd,0.01,100,0.01)
113 if ~checkbode(hd) then pause,end
114 clf();bode(sld,0.01,100);
115 if ~checkbode(sld) then pause,end
116 clf();bode(sl,0.01,100,0.01)
117
118 //bode given by precmputed freqency response
119 h=syslin('c',n,d)
120 clf();bode(h,0.01,100);
121 fig=gcf();
122 dp=fig.children(1).children($).children.data;
123 dm=fig.children(2).children($).children.data;
124 [w,rf]=repfreq(h,0.01,100);
125  
126 clf(); bode(w,rf)
127 fig=gcf();
128 if norm(dp-fig.children(1).children($).children.data)>1d-13 then pause,end
129 if norm(dm-fig.children(2).children($).children.data)>1d-13  then pause,end
130
131 clf(); bode(w,20*log(abs(rf))/log(10),(180/%pi)*atan(imag(rf),real(rf)))
132 fig=gcf();
133 if norm(dp-fig.children(1).children($).children.data)>1d-13 then pause,end
134 if norm(dm-fig.children(2).children($).children.data)>1d-13  then pause,end