Monday, June 21, 2010

A2 - Scilab Basics

Here are my results for my first attempt at creating synthetic images in Scilab. These are useful for testing algorithms or simulating optical phenomena [1]. Go ahead and browse through them. I included the codes. Scilab is surprisingly easy to use, especially with its provisions for matrix calculations which simplifies coding and computation.

Centered Square
Aperture



nx = 500; ny = 500; //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
A = zeros (nx,ny);
xx = abs(X);
A (find(xx<0.5) style="color: rgb(255, 0, 0);">//chooses values along x
B = zeros (nx,ny); yy = abs(Y); B (find(yy<0.5) style="color: rgb(255, 0, 0);">//chooses values along y
C = A.*B;
//getting the intersection of the two arrays
imwrite(C, 'D:\Ayas Documents\1st sem 10-11\AP 186\square.jpg');


Sinusoid Along the x-Direction (Corrugated Roof)



nx = 500; ny = 500; //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= sin(X.*12); //applying the sin function; higher constant, smaller period
imwrite((r+1)/2, 'D:\Ayas Documents\1st sem 10-11\AP 186\corrugatedroof.jpg'); //r is scaled to positive values


Grating Along the x-Direction



nx = 500; ny = 500; //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= sin(X.*12); //applying the sine function
A = zeros (nx,ny);
A (find(abs(r)<0.5) style="color: rgb(255, 0, 0);">//applying a threshold for white slits
imwrite(A, 'D:\Ayas Documents\1st sem 10-11\AP 186\grating.jpg');


Annulus



nx = 500; ny = 500; //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) style="color: rgb(255, 0, 0);">//creating the bigger circle

A (find(r<0.4) style="color: rgb(255, 0, 0);">//creating the smaller circle
imwrite(A, 'D:\Ayas Documents\1st sem 10-11\AP 186\annulus.jpg');


Circular aperture with Graded Transparency (Gaussian Transparency)



nx = 500; ny = 500; //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 = exp(-r.^2.*%pi);//applying the Gaussian function with a = 1
imwrite(A, 'D:\Ayas Documents\1st sem 10-11\AP 186\gaussian.jpg');


I am pretty proud of my code and the results I obtained. It took me about an hour to refresh my matrix math, but after that it took me only a few minutes to figure out each problem. I would give myself a grade of 10.5 for this activity. The 0.5 for doing my best to work things out on my own with minimal consultations.

[1] Soriano, M. A2 - Scilab Basics 2010.pdf

No comments:

Post a Comment