next up previous contents
Next: Operating with arrays as Up: Some general features about Previous: Access to the components   Contents

Plotting a vector or some part of a vector. Use of several windows

By clicking the button named `Demos' in Scilab screen, we can see an astonishing variety of graphs from primitives to animations or combinations of 2D and 3D graphs. In Clinical Neurophysiology, the most habitual representation of data is analogous to the output of a polygraph or oscilloscope. Other commonly employed graphs are `raster' or `superimpose'. In the raster mode, once time reaches the right end of the screen it appears another trace under the first one and so on until the screen is full. In the superimpose mode different traces (triggered or not) are displayed in the same vertical position.

We are going to reproduce this kind of graphic. We will begin by generating a vector similar to an EEG. It is a modulated sinusoid at 10 Hz (which is formed by the superposition of two nearby sinusoids). It is sampled at 0.02 s (i.e., at 50 Hz) and covers 5 seconds (251 points).


-->t = 0:0.02:5;
 
-->signal = sin(2 * %pi * 9.7 * t) + sin(2 * %pi * 10.3 * t);

Figure 2.1: Options of `plot2d'
\begin{figure}
\epsfbox{figures/plot1.eps}
\par\par
\end{figure}
The top trace of the figure has been drawn with the next command


-->plot2d(t,signal);

It is possible that it appears differently in your screen. The relation between width and height can be modified by adjusting your graphics window with the mouse. The command `plot2d' is more powerful than `plot'. Notice that the graph plots properly the values of the x-axis because we plot `signal' against `t'.

If we try to modify vertical axis we have to use the options of `plot2d'. A very extensive documentation can be found by typing `help plot2d'. But now we are interested in scale amplitude in such a way that vertical axis goes from -7 to 7. We can adjust y-axis by using the next command (the result is shown at the middle trace of figure 2.1)


-->plot2d(t,signal,1,"051"," ",[0,-7,5,7]);

We included four optional parameters. The fourth one (``051'') is an especially interesting string. It is a three character string. The first character control the display of captions (`0' indicates that captions will not be shown), the second one controls the computation of the frame (`5' means that we are going to change the scale) and the third one controls the frame (`1' implies that the frame will be shown). The change in the scale is determined by the sixth parameter which defines the rectangle ( rect=[xmin,ymin,xmax,ymax] ) at which the curve will be represented.

We are trying to represent something similar to an EEG and usually the record has no axis. With the information contained in the above paragraph it seems not very difficult to eliminate the frame and the scales. We substitute `051' by `050'.


-->plot2d(t,signal,1,"050"," ",[0,-7,5,7]);

The result is shown at the bottom. The screen goes from 0 to 5 and from -7 to 7 (for the x-axis and y-axis respectively) and the curve is shown without scales or frames as we wanted. We got something very similar to a trace of EEG. But how could we manage to show different traces at the same `screen' or `sheet'?

We have a very useful command that enables to plot several graphs in one window: `xsetech' whose first parameter is a vector containing the characteristics of a rectangle inside the window. Everything we plot after its use will be represented inside this rectangle. And this rectangle is scaled in relation to the window that contains it.

We are going to plot four traces with progressively higher amplitudes. We try to simulate a bipolar EEG from anterior to posterior (something similar to the usual Fp1-F3, F3-C3, C3-P3, P3-O1) and to create a little more of realism we add a random noise with `rand'


// signal considered above
t = 0:0.02:5;
signalbase = sin(2 * %pi * 9.7 * t) + sin(2 * %pi * 10.3 * t);

// the four traces that we are going to plot
signal1 = signalbase * .1+ rand(signalbase) * 2;
signal2 = signalbase * .3+ rand(signalbase) * 2;
signal3 = signalbase * .6+ rand(signalbase) * 2;
signal4 = signalbase *  1+ rand(signalbase) * 2;

// the plot of the traces
xsetech ([0,0,1,1/4])                           // defines first subwindow
plot2d(t,signal1,1,"050"," ",[0,-7,5,7]);       // plot first trace
xsetech ([0,1/4,1,1/4])                         // defines second subwindow
plot2d(t,signal2,1,"050"," ",[0,-7,5,7]);       // plot second trace
xsetech ([0,2/4,1,1/4])                         // defines third subwindow
plot2d(t,signal3,1,"050"," ",[0,-7,5,7]);       // plot third trace
xsetech ([0,3/4,1,1/4])                         // defines forth subwindow
plot2d(t,signal4,1,"050"," ",[0,-7,5,7]);       // plot forth trace

It is a long but very repetitive code (to tell the truth it does not include the prompt because it has been written in a file and executed by using `exec'; try `help exec' if you want to know more about this interesting command). The result is shown in figure 2.2.

Figure 2.2: Simulation of an EEG
\begin{figure}
\epsfbox{figures/eegsym.eps}
\end{figure}

Notice that although the plot of each trace has been chosen to avoid superposition of the subwindows, we could have superimposed a part of a subwindow with any part of another one. It is also important to note that we represented the whole signal of each trace (a `four channel recording') but we could have plotted consecutive parts of the same vector in different vertical positions and we would have got a `raster' plot.

Until now we have been using only one window but Scilab can use several graphical windows at the same time. Try to execute the following code


-->xset('window',1)
 
-->xset('wpos',0,0)
 
-->xset('wdim',400,100)
 
-->t=1:400;
 
-->signal = sin (2 * %pi * 0.1 * t);
 
-->plot2d(t,signal)

-->xset('window',2)
 
-->xset('wpos',0,200)
 
-->xset('wdim',150,100)
 
-->plot2d(t(1:50),signal(1:50))
 
-->xset('window',3)            
 
-->xset('wpos',200,200)        
 
-->xset('wdim',150,100)             
 
-->plot2d(t(51:100),signal(51:100))

`xsetech' acts on any graphic windows, so we can access any part of them to trace subplots. It is easy to create several windows and plot on them different curves. If you type `help xset' you can see a lot of aspects that you can define. You can return to the initial settings with `xset(default)'. To erase the content of a window you can use `xclear()' or `xbasc()'. The difference between them is that if you erase the content with `xclear()' you can recover the plot with `xbasr()' but if you erase the content with xbasc() you can not recover it. To close the graphics window you can use `xdel()' and to put the window on the top of the screen use `xselect()' after `xset('window',n)' (being n the number of the window). In summary, saying as if we were the manual

xset:
set values of the graphics context
xselect:
raise the current graphics window
xclear:
clear a graphics window
xbasr:
redraw a graphics window
xbasc:
clear a graphics window and erase the associated recorded graphics
xdel:
delete a graphics window

We think that this example clearly shows the flexibility of Scilab to adapt to different contexts and, among them, to emulate the graphics performed by our recording equipment.


next up previous contents
Next: Operating with arrays as Up: Some general features about Previous: Access to the components   Contents
je 2006-10-13