The Beauty of SciLab

SciLab is the answer to numerical problems that needs a little bit more “umph”. The rich option would be MATLAB but this is the very reason why Scilab is beautiful… It is free and offers an array of tools for signal processing, dynamics, image processing, etc. It also has a community that shares their programs for others to use.

A few months back, I was trying to find an open source alternative to MATLAB. First, there was octave which I used for 3 months in a linux machine but the pc broke down. I then found out about SciLab through AP 185 and have been using it as an alternative to MATLAB ever since.

Syntax and Masking

When one wants to learn a new language, it is best to put it into practice! This applies to any programming language you wish to learn. This may include a lot of searching through google to check for the right command and other capabilities. Luckily, the syntax of SciLab is sort of based on MATLAB so any MATLAB user may find the commands quite similar.

I will now jump into Masking. Masking, as I understood, is a technique in order to create images from basic geometric conditions. This can be used to simulate apertures or you need a specific image. The first part of the activity is to create a white circle using the code provided by Ma’am Jing.


I adjusted the code a little bit by using the SIVP toolkit because the colormap command does not show 1 as white and 0 as black. Below is the code to generate a Circle aperture with varying sizes.

nx = 500; ny = 500;
x = linspace(-1,1,nx);
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y);

r = sqrt(X.^2 + Y.^2);
r_edge= 0.7;
A = zeros(nx,ny);
A (find(r<r_edge)) = 1;

imshow(A);

circle
A circle aperture

A rundown of commands used:

linspace – creates a 1xN matrix that ranges between two numbers with n divisions.

ndgrid – creates a grid system wherein all the cartesian coordinates between x and y are then saved in X and Y similar to meshgrid in MATLAB. This is the key into creating these types of images in SciLab. This can also be transformed to polar coordinates rho and theta.

sqrt – takes the square root of a variable.

zeros – creates an NxM matrix that only has zero float values in each element.

find – finds the values within the matrix that satisfies the conditional in its argument.

The size of the circle can change when r_edge has a new value. The value chosen was 0.7 which indicates that its diameter ranges from -0.7 to 0.7. The image shown above is actually 500×500 pixels but was only resized.

The example above is a basic lesson on masking. As you may have noticed, this technique can be used in many different ways. Because they are essentially matrices, matrix operations can be used such as addition, multiplication, subtraction, etc. For this activity matrix multiplication will be used more often.

Using matrix operations, one can create different types of images. below are some of the images created using a conditionals, matrix operations, or both.


Square Aperture

nx = 500; ny = 500;
x = linspace(-1,1,nx);
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y);

A = zeros(nx,ny);
A(find(abs(X)<0.4 & abs(Y)<0.4))=1;
imshow(A);

square
A square aperture

It can be seen from the code that a use of boolean operators can be used inside the find command. This is so that multiple conditions may be applied for the ndgrid.

Sine wave along x

f = 5 ;
A = sin(2*3.1416*f*Y);
imshow(A)

sinusoid
Sinusoidal gradient along x

A sine wave can also be produced. The frequency f defines the periodic appearance of the peaks. This shows how masking provides an analytical simulation of functions for use in other aspects.

Edged sine wave

A = zeros(nx,ny);
A(find(sin(2*3.1416*f*Y)>.3)) =1;
imshow(A)

hardroof
Sinusoidal edges along x

The difference of this sinusoidal pattern with the one earlier is that this used the find command in order to set a value for the condition applied in this case the sin function.

Annulus

A = zeros(nx,ny);
r1 = sqrt(X.^2+Y.^2);
//r2 = sqrt(X.^2+Y.^2);

A (find(r1>0.5 & r1<1))=1;
imshow(A);

annulus
An annulus

This is another example of the use of a boolean. You can even make concentric circles by adding more conditions to the boolean.

Gaussian

A = zeros(nx,ny);
r = sqrt(X.^2 + Y.^2);
Gauss = exp(r.^2)*exp(-2);

A(find(r<.9))=1;

Product = Gauss.*A;
imshow(Product)

gauss
A Gaussian pattern enclosed in a circle

The Gaussian pattern is an example of matrix multiplication. Basically, a gaussian function is first computed for all values of X and Y in the ndgrid. Then a circular aperture is used to limit the values computed by the gaussian by doing matrix multiplication indicated by the ” .* ” operation. The operation is to ensure that the multiplication is element-wise.

Ellipse

xwidth = .5; ywidth = .7;

A = zeros(nx,ny);

el = sqrt(X.^2/xwidth.^2 + Y.^2/ywidth.^2);

A(find(el<1))=1;
imshow(A);

ellipse
An elliptical aperture

Any type of pattern can be created so long as you use a geometric formula.

Cross

A = zeros(nx,ny);
width = 0.02;

A(find(abs(X)<width | abs(Y)<width))=1;
imshow(A)

cross
A cross

And the last patter to be created is a cross. Again, a boolean operator is used to add an “or” condition.


I have been using masking techniques ever since June as part of my research. I used it to simulate the zernike polynomials. An example is shown below. (The syntax is in MATLAB).

x = -1:0.01:1;
[X,Y] = meshgrid(x,x);
[theta,r] = cart2pol(X,Y);
idx = r<=1;
z = nan(size(X));

N=4
M=0

z(idx) = zernfun(N,M,r(idx),theta(idx));
figure(1)
pcolor(x,x,z), shading interp
colormap(‘jet’);
axis square, colorbar

primary-spherical
Z(4,0) polynomial – Primary Spherical Aberration

This is only to show one possibility that masking can be of use. All in all, we’ve learned that SciLab is a high-level numerical programming language full of different utilities for many different applications in academic and scientific endeavors.

I would like to acknowledge my seatmate Josh Abuel in simplifying some of my conditionals for a more easy-to-read code.

 

References:

  1. https://en.wikipedia.org/wiki/Scilab
  2. https://en.wikipedia.org/wiki/Mask_(computing)
  3. https://en.wikipedia.org/wiki/Normal_distribution