Tuesday, June 29, 2010

2. Scilab Basics

Scilab makes matrix multiplications very simple. The syntax is very much like the math. With matrices, synthetic images can be generated. A matrix of 1's and 0's form a black and white image (any binary set of values would do). Using more than two values will generate gray-scaled images.

In this activity, basic images were generated from Scilab matrices. I started from the code provided in class. This generates a circular aperture. I changed the number of elements (100 to 200) to improve the resolution of the image. The range was also increased to generate a larger image. This can be used as a mask for a matrix simulation of a light source.

All of the images generated are derived from this code.

[1] nx = 200; ny = 200; //defines the number of elements along x and y
[2] x = linspace(-2,2,nx); //defines the range [3] y = linspace(-2,2,ny);
[4] [X,Y] = ndgrid(x,y); //creates two 2-D arrays of x and y coordinates

[5] r= sqrt(X.^2 + Y.^2); //note element-per-element squaring of X and Y

[6] A = zeros (nx,ny);
[7] A (find(r<0.7) =1;

circular aperture

A small modification of the code will generate a square aperture and an annulus. The white areas represented by 1's. Changing lines [5], [6] and [7] to the following will yield the square and annular apertures.

To generate a square aperture
change [5],[6],[7] to A(find(abs(Y)<1>0.5)) = 1;

To generate an annulus
A(find(r<1>0.5)) = 1;


a square apperture and an annulus

One can use the circular aperture with a simulated light source. Assuming that the beam's distribution is Gaussian, we simply multiply the matrix of the aperture and that of the Gaussian source.

we simply add the following lines of code, omitting [8]
G = exp(-2*X.^2 - 2*Y.^2); //gaussian fxnB = G.*A;
imshow(G,[]);
imshow(B,[]);


Gaussian beam and beam with circular apperture

Gratings can also be made by modifying the square aperture. Vertical gratings were made with the following conditions,

n=.20
A(find(abs(Y)>n & abs(Y)n+.80 & abs(Y)n+1.60 & abs(Y)<2))>

vertical gratings generated by imposing conditions on A along the x-axis

Trying out a sinusoid along the x-axis will generate this image.


Image of a sinusoid along the x-axis

This is generated by changing the conditions of the square aperture

A= sin(X*10);// apply sine function
A = A/max(A); //normalization
imshow(abs (A), []); //avoid negative values

For this exercise, I give myself a score of 9/10. The objectives of the activity are all in here. Minus one point for being late.

Thanks to Theiszm for the Scilab tips. Throughout the duration of the past week I have been (1) attempting to link SIP and then SIVP to Scilab 5 and (2) install SIP and SIVP from binary files to on my Linux. The binary installation took me into the trail of OpenCV and Cmake, very foreign installation requirements for me. I then gave up and took on Ma'am Jing's advise to use Scilab 4.1.2 and SIP. The linking trick sure works. For the later versions of Scilab, the problem seems to be that it can't find the file libsip.dll. I hope the SIP and SIVP link will workout in the later versions.

No comments:

Post a Comment