1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 1985-2010 - INRIA - Serge Steer
3 // This file must be used under the terms of the CeCILL.
4 // This source file is licensed as described in the file COPYING, which
5 // you should have received as part of this distribution. The terms
6 // are also available at
7 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
9 function []=bode(varargin)
14 h1=syslin("c",(s^2+2*0.9*10*s+100)/(s^2+2*0.3*10.1*s+102.01))
15 num=22801+4406.18*s+382.37*s^2+21.02*s^3+s^4;
16 den=22952.25+4117.77*s+490.63*s^2+33.06*s^3+s^4
17 h2=syslin("c",num/den);
19 bode([h1;h2],0.01,100,["h1";"h2"])
23 if type(varargin($))==10 then
24 comments=varargin($),rhs=rhs-1;
28 fname="bode";//for error messages
30 discr=%f //for shannon limit
31 if or(typeof(varargin(1))==["state-space" "rational"]) then
32 //sys,fmin,fmax [,pas] or sys,frq
33 refdim=1 //for error message
34 discr=varargin(1).dt<>"c";
36 [frq,repf]=repfreq(varargin(1),1d-3,1d3)
37 elseif rhs==2 then //sys,frq
38 if size(varargin(2),2)<2 then
39 error(msprintf(_("%s: Wrong size for input argument #%d: A row vector with length>%d expected.\n"),..
42 [frq,repf]=repfreq(varargin(1:rhs))
43 elseif or(rhs==(3:4)) then //sys,fmin,fmax [,pas]
44 [frq,repf]=repfreq(varargin(1:rhs))
46 error(msprintf(_("%s: Wrong number of input arguments: %d to %d expected.\n"),fname,1,5))
48 [phi,d]=phasemag(repf)
49 if rhs>=3 then fmax=varargin(3),end
50 elseif type(varargin(1))==1 then
51 //frq,db,phi [,comments] or frq, repf [,comments]
54 case 2 then //frq,repf
57 error(msprintf(_("%s: Wrong size for input argument #%d: A row vector with length>%d expected.\n"),..
60 if size(frq,2)<>size(varargin(2),2) then
61 error(msprintf(_("%s: Incompatible input arguments #%d and #%d: Same column dimensions expected.\n"),..
64 [phi,d]=phasemag(varargin(2))
65 case 3 then //frq,db,phi
66 [frq,d,phi]=varargin(1:rhs)
67 if size(frq,2)<>size(d,2) then
68 error(msprintf(_("%s: Incompatible input arguments #%d and #%d: Same column dimensions expected.\n"),..
71 if size(frq,2)<>size(phi,2) then
72 error(msprintf(_("%s: Incompatible input arguments #%d and #%d: Same column dimensions expected.\n"),..
76 error(msprintf(_("%s: Wrong number of input arguments: %d to %d expected.\n"),fname,2,4))
79 error(msprintf(_("%s: Wrong type for input argument #%d: Linear dynamical system or row vector of floats expected.\n"),fname,1))
81 frq=frq';d=d',phi=phi'
87 if size(comments,"*")<>mn then
88 error(msprintf(_("%s: Incompatible input arguments #%d and #%d: Same number of elements expected.\n"),...
95 immediate_drawing=fig.immediate_drawing;
96 fig.immediate_drawing="off";
100 wrect=axes.axes_bounds;
104 axes.axes_bounds=[wrect(1)+0,wrect(2)+0,wrect(3)*1.0,wrect(4)*hx*0.95]
105 axes.data_bounds = [min(frq),min(d);max(frq),max(d)];
106 axes.log_flags = "lnn" ;
107 axes.grid=color("lightgrey")*ones(1,3);
108 axes.axes_visible="on";
109 axes.clip_state = "clipgrf";
110 if size(d,2)>1&size(frq,2)==1 then
111 xpolys(frq(:,ones(1,mn)),d,1:mn)
118 for i=1:size(e.children,"*")
119 datatipInitStruct(e.children(i),"formatfunction","formatBodeMagTip")
122 if discr&fmax<>[]&max(frq)<fmax then
123 xpoly(max(frq)*[1;1],axes.y_ticks.locations([1 $]));e=gce();
126 xtitle("",_("Frequency (Hz)"),_("Magnitude (dB)"));
131 axes.axes_bounds=[wrect(1)+0,wrect(2)+wrect(4)*hx,wrect(3)*1.0,wrect(4)*hx*0.95];
132 axes.data_bounds = [min(frq),min(phi);max(frq),max(phi)];
133 axes.log_flags = "lnn" ;
134 axes.grid=color("lightgrey")*ones(1,3);
135 axes.axes_visible="on";
136 axes.clip_state = "clipgrf";
137 if size(phi,2)>1&size(frq,2)==1 then
138 xpolys(frq(:,ones(1,mn)),phi,1:mn)
144 for i=1:size(ephi.children,"*")
145 datatipInitStruct(ephi.children(i),"formatfunction","formatBodePhaseTip")
148 if discr&fmax<>[]&max(frq)<fmax then
149 xpoly(max(frq)*[1;1],axes.y_ticks.locations([1 $]));e=gce();
152 xtitle("",_("Frequency (Hz)"),_("Phase (degree)"));
155 c=captions(ephi.children,comments,"lower_caption")
156 c.background=get(gcf(),"background")
158 fig.immediate_drawing=immediate_drawing;
159 // return to the previous scale
160 set( "current_axes", sciCurAxes ) ;
164 function str=formatBodeMagTip(curve,pt,index)
165 //this function is called by the datatips mechanism to format the tip
166 //string for the magnitude bode curves
167 str=msprintf("%.4g"+_("Hz")+"\n%.4g"+_("dB"), pt(1),pt(2))
170 function str=formatBodePhaseTip(curve,pt,index)
171 //this function is called by the datatip mechanism to format the tip
172 //string for the bode phase curves
173 str=msprintf("%.4g"+_("Hz")+"\n %.4g"+"°", pt(1),pt(2))