Tuesday, June 22, 2010

Not so Scilab Basics





A while ago, we were given a somehow a crash course on Scilab. We used the 4.1.2 version because the toolbox that we are going to use somehow only works for the 4.++ versions.

Given the readings, I tried to familiarize myself to the instructions and syntaxes on Scilab. It looks very mush like python but with several differences.

At the end of the readings, we were given a set of exercises to do to make us refresh our programming skills and familiarize the programming language that is Scilab.

Initially, we were given an example on how to generate a centered circle. The code looks like this:

nx = 100; ny = 100; //defines the number of
elements along x and y
x = linspace(-1,1,nx); //defines the range
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y); //creates two 2-D arrays of x
and y coordinates
r= sqrt(X.^2 + Y.^2); //note element-per-element
squaring of X and Y
A = zeros (nx,ny);
A (find(r<0.7)>
imshow (A, []);

where the words after "//" are comments. So that code basically generates an array that has a value of zeros per element then finds those indexes that satisfies r<0.7>

it looks like this:
Figure1. Circular Aperture


So we were asked to do different things.
We are to create synthetic images of:
a. centered square aperture
b. sinusoid along the x-direction (corrugated roof)
c. grating along the x-direction
d. annulus
e. circular aperture with graded transparency (gaussian transparency).

First off, I did the centered square aperture. It has this code:

// Square aperture
nx = 400; ny= 400;
x = linspace(-1,1,nx);
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y);
A = zeros(nx,ny);
A(find(abs(X)<>
imshow(A, []);

And it gave the following image:

Figure 2. Square Aperture


I must say, this example taught me the powers of the '&' sign. :) I got stuck here because I always ended up making an off-axis square. Booo. But when I used the '&' sign, voila!

The corrugated roof generated this code:

// Sine
nx = 400; ny = 400;
x = linspace(-1,1,nx);
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y);
A = sin(2*%pi*X.*5)
imwrite(A, 'C:\Users\Celestino\Pictures\Scilab pics\Sine.jpeg')
imshow(A, []);

And gave the following image:

Figure 3: Corrugated Roof

This figure showed the values of sin in the Y axis in terms of color. During the conception of the code, I got stuck because I dwelt in masking method that I forgot that it was simpler to plug in the equation of sine. Man, I really need to refresh my programming.


** Edit:
Sine fucntion have values ranging from -1 to 1. That's why there are little white lines in the images. Arvin Mabilangan told us that we have to set the range to be 0 to 1. So the edited code looks like this:

// Sine
nx = 400; ny = 400;
x = linspace(-1,1,nx);
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y);
A = (sin(2*%pi*X.*5)+1)/2
imwrite(A, 'C:\Users\Celestino\Pictures\Scilab pics\Sine.jpeg')
imshow(A, []);

and yields this image:

Figure 3.5: Corrugated Roof version 2

Next up is the grating along the x-direction. It yielded this code:

// Grating
nx = 400; ny = 400;
x = linspace(-1,1,nx);
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y);
a = sin(2*%pi*X.*5);
A = zeros(nx,ny);
A(find(a >= 0.1)) = 1
imwrite(A, 'C:\Users\Celestino\Pictures\Scilab pics\Grating.jpeg')
imshow(A, []);

And produced this image:
Figure 4: Grating

Nice thing about this code is that I used the previous corrugated roof and tweaked it a little to produce a grating. Basically, there is an array with zero value. What I did is that I set a threshold value for the sine function. This threshold if met will yield a value of 1. And that produced a grating.

The imshow function produces a non-uniform grating though when imwrite-ed, it gives a better and more accurate picture of the grating.


The annulus was coded like this:

nx = 400; ny = 400; // defines the number of elements along x and y
x = linspace(-1, 1, nx); // defines the range
y = linspace(-1, 1, ny);
[X, Y] = ndgrid(x,y); //creates a 2D array of x and y coordinates
r = sqrt(X.^2 + Y.^2); // note element per element squaring of X and Y
A = ones(nx,ny);
A(find(r<1))>
A(find(r<0.5))>
imwrite(A, 'C:\Users\Celestino\Pictures\Scilab pics\Annulus.jpeg')
imshow (A, []);

With an image that looks like this:

Figure 5: Annulus

This is relatively the easiest of the exercises given. Here, what I did is to tweak the example given to produce another circular aperture. After that, it is just a matter of adjusting the values necessary.

Lastly, for the circular aperture with graded transparency (gaussian transparency), I have made the code to be like this:

//Circular Aperture with graded transparency
nx = 400; ny = 400;
x = linspace(-1, 1, nx);
y = linspace(-1, 1, ny);
[X, Y] = ndgrid(x,y);
r = sqrt(X.^2 + Y.^2);
A = ones(nx,ny);
lambda = 10
A = exp(-(r)^2/lambda^2)
A(find(r<0.1))>
//A(find(r>0.25)) =exp(-(r.)^2/lambda^2
//A(find(r>0.25)) = 0
imwrite(A, 'C:\Users\Celestino\Pictures\Scilab pics\Gaussian2.jpeg')
imshow (A, []);

And yielded this image:

Figure 6: Circular Aperture with graded transparency

This one is hard. And I am not pretty sure I got this one right. What I did is to show a Gaussian circular decay and then masked a circular aperture. Adjustment for the desired image parameter is a lot of trial and error and the imshow does not help that much for giving an inaccurate image.

In this activity, I think I'll grade myself 8 out of 10. Because Gaaah! I suck at programing. And I don't know if I got the graded transparency right.

I'd like to thank Andy Polinar, Arvin Mabilangan, Gino Leynes and Maam Jing Soriano for helping me figure out how to code these exercises in Scilab.

Thanks for reading. Comment if there are any mistakes.

No comments:

Post a Comment