# Xcos tutorial – Simple demo

This is the first tutorial in a series, explaining some of the Xcos demonstration models. One of the main disadvantages of Scilab/Xcos, which is also a characteristic of open source software, is the low detail and quality of the documentation. With this series of articles we’ll go through the Xcos demonstrations block diagram models and explain the content.

To access Xcos demo models you have 3 possibilities:

• click on the Scilab Demonstrations icon in the toolbar
• click Help (?) and then Scilab Demonstrations
• run the demo_gui() function in the Scilab console

From the Demonstrations window, in the left panel, click on Xcos. In the right panel select Standard demos. A third panel will open with all the Xcos Standard demo models. Select Simple Demo and the Xcos block diagram model will open.

The purpose of this Xcos demo model is to teach the user how to:

• generate an event signal with a specified time step
• modify an event signal
• generate a periodic square wave
• generate a sine wave

Observation: There are two main types of signals associated with Xcos blocks: event signals and data signals.

The properties of the event signals are the following:

• the ports are colored in red
• they can not be saved in the workspace
• the blocks which handle event signals are in the Event handling library (see Palette browser)
• they can be seen as function calls or triggers (as in Matlab Simulink)

The properties of data signals are the following:

• the ports are colored in black
• they can be saved in the workspace
• they represent numerical values (scalars or vectors)

The Clock block generates an event signal (time signal) with the following parameters:

• Period: 0.1
• Init time: 0.0

The Period is the time step, which can be viewed also as the refresh or update rate. The Init time parameter specifies the start of the event. The unit for both Period and Init time is seconds. The output can be used to trigger a Scope block or a signal generator block.

Since the period of out event signal is Tclock = 0.1 s, the frequency will be:

$f_{clock} = \frac{1}{T_{clock}}=10 \text{ Hz}$

The Frequency division block has an input event port and an output event port. The parameters of the block are:

• Phase (0 to division factor -1): 0
• Division factor: 30

The Division factor is used to change the frequency of the input event. The period of the output event Tout will be calculated function of the period of the input event Tin and the division factor DF:

$T_{out}=T_{in} \cdot DF$

This gives the period of the output event equal to:

$T_{out}=30 \cdot 0.1 = 3 \text{ s}$

The Square wave generator block has an event input port and a parameter defining the amplitude A of the signal. The output signal will switch between A and -A every time a new event is occurring. In our case the square signal will switch from 1 to -1 every 3 seconds.

The Sine wave generator doesn’t have an input event port. Instead, the output signal is defined by three parameters:

• Magnitude: 1
• Frequency (rad/s): 1
• Phase (rad): 0

The Magnitude is actually the amplitude of the signal. Since it’s in rad/s, the Frequency is actually the angular speed ω. In order to calculate the frequency of the signal, we can use the formula:

$f_{sine}=\frac{\omega}{2 \pi}=\frac{1}{2 \pi}=0.1591549 \text{ Hz}$

To find the period of the sine wave we divide 1 to the frequency:

$T_{sine}=\frac{1}{f_{sine}}=\frac{1}{0.1591549}=6.2831853 \text{ s}$

For a better understanding of the demo model, let’s make the following changes of the block parameters and analyze the output.

 Block Parameter Value Clock Period 0.01 Frequency division Division factor 50 Square wave generator Amplitude 0.5 Sine wave generator Frequency (rad/s) 5

With this parameter setting, the square signal should switch between 0.5 and -0.5 every 0.5 seconds. The sine wave will have its frequency increased five times.

Let’s recap what we’ve done during the tutorial:

• generated an event signal with the Clock block
• modify the event signal with the Frequency division block
• generate a periodic square signal with the Square wave generator block
• generate a sine signal with the Sine wave generator block

For any questions, observations and queries regarding this article, use the comment form below.

Don’t forget to Like, Share and Subscribe!