This code can be used to plot the Bode, Black, Nichols and Nyquist diagrams of a transfer function from an XCOS diagram. It is not necessary to open the XCOS file before running the script.
The post processing “post_xcos_simulate” functionality is used for this operation.
function post_xcos_simulate(%cpr, scs_m, needcompile)
// Do Nothing
endfunction
//We load the XCOS file to be simulated
importXcosDiagram("C:\\...path...\Exemple diagramme de Bode depuis XCOS/Essai_BODE_plot.zcos")
// Clear any pre_xcos_simulation;
clear pre_xcos_simulate;
function post_xcos_simulate(%cpr, scs_m, needcompile)
// Retrieve all objects
objs = scs_m.objs;
clrBlock = [];
//Looking for CLR block
for i=1:size(objs)
if objs(i).gui == "CLR" then
clrBlock = objs(i);
break;
end
end
// Retrieve exprs
s = poly(0,'s');
expr = clrBlock.graphics.exprs;
num = evstr(expr(1));
den = evstr(expr(2));
h = syslin('c', num/den);
// Open new figure then plot Bode, Black, Nyquist
clf();//Clear all previous curves
f = gcf();// Create a figure
f.figure_size=[1200,1200];
// f window background color
f.background=color('lightsteelblue');
f.figure_name='BODE BLACK NICHOLS NYQUIST DIAGRAMS FROM XCOS FILE'
subplot(221);
bode(h, 0.01, 100,"Mon filtre");
subplot(222);
black(h,0.01,10,"Mon filtre");
nicholschart(colors=color('gray')*[1 1]);
subplot(223);
nyquist(h,0.01,10,"Mon filtre");
hallchart(colors=color('blue')*[1 1])
endfunction
//We run the simulation of the Bode and other diagrams
xcos_simulate(scs_m, 4);
XCOS diagram is the following one:
The plot is depicted below:
Of course, it is possible to split the code in order to keep only one kind of diagram.
Example:
importXcosDiagram("C:\\...path...\Essai_BODE_plot.zcos")
// Clear any pre_xcos_simulation;
clear pre_xcos_simulate;
function post_xcos_simulate(%cpr, scs_m, needcompile)
// Retrieve all objects
objs = scs_m.objs;
clrBlock = [];
//Looking for CLR block
for i=1:size(objs)
if objs(i).gui == "CLR" then
clrBlock = objs(i);
break;
end
end
// Retrieve exprs
s = poly(0,'s');
expr = clrBlock.graphics.exprs;
num = evstr(expr(1));
den = evstr(expr(2));
h = syslin('c', num/den);
// Open new figure then plot Bode
scf(max(winsid())+1);
bode(h, 0.01, 100,"Mon filtre");
endfunction
//Bode simulation running
xcos_simulate(scs_m, 4);