Activity 2: Scilab Basics
Zzzzzz.... (-_-)
After several decades... (well, it seemed like decades to me)
FINALLY! I was able to install it in my computer..
From my previous classes, using Scilab was actually highly recommended for the fact that it is free (like Python) unlike Matlab. What is even more surprising is that its programming syntax is similar to Matlab (which I use more often) which made it easier for me to follow.
The aim of the activity was to simulate optical phenomena and test algorithms in imaging systems with the use of synthetic images (which are computer-generated images).
There were several materials most commonly used for simulating optical phenomena like apertures and gratings. In this activity, we were asked to create synthetic images of the following structures or materials:
----- centered circular aperture
----- centered square aperture
----- sinusoid along the x-direction (corrugated roof)
----- annulus
----- circular aperture with graded transparency (gaussian transparency)
So from the initial code provided for the centered circular aperture, I tweeked it to create the rest of the synthetic images.
Centered Circular Aperture
The first image is the centered circular aperture. By changing specific parameters, we can create a specific size of image, with a specific diameter for the aperture. The code for this is...
//circular aperture
nx = 200; ny=200; //# of elements (x and y)
d = 0.4; //parameter for circle
x = linspace(-1,1,nx); //defines the range
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y); //creates two 2D arrays
r = sqrt(X.^2 + Y.^2); //element-by-element sum of squares
A = zeros(nx,ny); //zero-valued nx-by-ny array
A(find(r<d)) = 1; //sets circular aperture
imshow(A);
imwrite(A, 'circular_aperture.jpg');
//circular aperture
nx = 200; ny=200; //# of elements (x and y)
d = 0.4; //parameter for circle
x = linspace(-1,1,nx); //defines the range
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y); //creates two 2D arrays
r = sqrt(X.^2 + Y.^2); //element-by-element sum of squares
A = zeros(nx,ny); //zero-valued nx-by-ny array
A(find(r<d)) = 1; //sets circular aperture
imshow(A);
imwrite(A, 'circular_aperture.jpg');
![]() |
| Figure 2. Circular aperture with d (from left to right) values: 0.1, 0.2, 0.3, 0.4 |
Centered Square Aperture
The second image was a square aperture. It took along time for me to figure out how to code it the shortest way (easiest) but then I thought of the x and y values in the grid, and it finally clicked to me. So what shown below are the generated apertures with varying sides.
//square aperture
nx = 200; ny=200; //# of elements (x and y)
f = 0.7 //square side=f*nx
sq = zeros(nx,ny);
sq(nx/2 - nx*f/2:nx/2 + nx*f/2, nx/2 - nx*f/2:nx/2 + nx*f/2) = 1;
//sets square aperture
imshow(sq);
imwrite(sq, 'square_aperture.jpg');
//square aperture
nx = 200; ny=200; //# of elements (x and y)
f = 0.7 //square side=f*nx
sq = zeros(nx,ny);
sq(nx/2 - nx*f/2:nx/2 + nx*f/2, nx/2 - nx*f/2:nx/2 + nx*f/2) = 1;
//sets square aperture
imshow(sq);
imwrite(sq, 'square_aperture.jpg');
![]() |
| Figure 4. Square aperture with varying sides (left to right) with s: 0.1, 0.2, 0.3, 0.4 |
Sinusoid along the x and y direction (corrugated roof)
The third image is quite similar to the creation of the grid (the ndgrid) however we take into account that what we wish is a sinusoid instead of consecutive numbers. To further visualize this (instead of 2D), I also provided a 3D view of the sine wave.
//corrugated roof
nx = 200; //# of elements (size)
nx = 200; //# of elements (size)
f=3; //factor for # of sines
x = linspace(-1*f,1*f,nx);
sine = sin(%pi*x); //creates sine function
sin_wave = ndgrid(sine, sine)';
imshow(sin_wave);
//mesh(sin_wave);
imwrite(sin_wave, 'sinusoid_1_03.jpg');
imwrite(sin_wave', 'sinusoid_2_03.jpg');
![]() |
| Figure 5. Sinusoid along the y (left) and x (right) direction with f=3 |
![]() |
| Figure 6. Mesh (3D) plot of the sinusoid wave with f=3 |
![]() |
| Figure 7. Sinusoid along the x (left) and y (right) direction with f=10 |
![]() |
| Figure 8. Sinusoid along the x (left) and y (right) direction with f=50 |
Grating along the x and y direction
This is quite similar to the previous image except that instead of having a gradient in the transition from black to white, it is simply black and white. Therefore I just added a simple parameter to consider all values greater than 0 to be 0 and all values greater than 0 to be 1. This can also be made with multiple frequencies as well.
//grating
nx = 200; //# of elements (size)
f = 6; //factor for # of gratings
t = linspace(-1*f,1*f,nx);
sine = sin(%pi*t);
sin_wave = ndgrid(sine, sine)';
sin_wave(find(sin_wave>0))=1; //sets gratings
sin_wave(find(sin_wave<0)) = 0;
imshow(sin_wave);
imshow(sin_wave');
imwrite(sin_wave, 'grating_f06_1.jpg'); //for x&y direction
imwrite(sin_wave', 'grating_f06_2.jpg');
![]() |
| Figure 9. Grating along the x (left) and y (right) direction with f=6 |
![]() |
| Figure 10. Grating along the x (left) and y (right) direction with f=10 |
![]() |
| Figure 11. Sinusoid along the x (left) and y (right) direction with f=50 |
Annulus
This image is just a tweek of the image generation for for the circular aperture, just that I have to limit up to which radius (range of radii) would give a value of 1. By changing certain parameters, I can change the size of the annulus.
//annulus
nx = 200; ny=200; //# of elements (x and y)
d_in = 0.4; d_out = 0.7; //parameters of circles (in&out)
x = linspace(-1,1,nx); //defines the range
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y); //creates two 2D arrays
r = sqrt(X.^2 + Y.^2); //element-by-element sum of squares
ann = zeros(nx,ny); //zero-valued nx-by-ny array
ann(find(r<d_out & r>d_in)) = 1; //sets the limits,
imshow(ann); //for inner and outer circle
imwrite(ann, 'annulus_0.4&0.7_.jpg');
![]() |
| Figure 12. Annulus with radius factors of 0.4 (inner) and 0.7 (outer) |
![]() |
| Figure 13. Annulus with radius factors of 0.3 (inner) and 0.8 (outer) |
Circular aperture with graded transparency (gaussian transparency)
This is a tweeking also the circular aperture except that instead of having a plain which (or blank) center, there is a gradient (with gaussian function). So a simple product of the circular aperture and the gaussian function would give this gaussian transperency.
//gaussian transparency
a=1;b=0;c=1;d=0.6; //parameter for gaussian,circle
a=1;b=0;c=1;d=0.6; //parameter for gaussian,circle
nx = 200; ny=200; //# of elements (x and y)
x = linspace(-1,1,nx); //defines the range
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y);
r = sqrt(X.^2 + Y.^2); //element-by-element sum of squares
A = zeros(nx,ny); //zero-valued nx-by-ny array
A(find(r<d)) = 1; //sets circular aperture
gauss = a*exp(-(r-b).^2/(2*c^2)); //gaussian function(2D)
gaussA = A.*gauss; //sets the gaussian aperture
imshow(gaussA);
imwrite(gaussA, 'gaussian_aperture.jpg');
![]() |
| Figure 14. Gaussian transparency with aperture radius factor of 0.6 |
![]() |
| Figure 15. Gaussian transparency with aperture radius factor of 0.9 |
I would like to thank Gino Borja for the copy of my installer and Tin Roque for our exchange which enlightened me a lot. :P
Reference/s:
[1] AP 186 Activity 2: Scilab Basics. Dr. Maricor Soriano
[2] Scilab. www.scilab.org
Reference/s:
[1] AP 186 Activity 2: Scilab Basics. Dr. Maricor Soriano
[2] Scilab. www.scilab.org
















Comments
Post a Comment