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

Saturday, June 19, 2010

A1 - Digital Scanning


Did you ever have a hard copy of a graph, with large intervals but without any tick-marks? And you wanted to know the true values that the plot was indicating?

Well, I did, and these are the results I obtained after reconstructing the plot using simple ratio and proportion, knowing the pixel locations of the points on the graph. This method is very simple. After using GIMP (sort of like a free Photoshop :D) to scan the x and y axes of the graph and recording the pixel locations in Microsoft Excel, we can get a relationship of the number of pixels per fixed interval of, say, 10 units. Given this and the pixel location of the origin, we can obtain an equation to convert the pixel values to real values! Once we have computed for the real values, we could then replot the graph in Excel.

In the picture, the blue and green dots correspond to the plot of real values computed in Excel, while the black plots beneath them correspond to the original scanned image of the graph. We can see that it corresponds well and that there are practically no deviations from the original plots. I also took the average difference of the pixel locations for several intervals, giving less room for error. We can conclude that the reconstruction of the scanned graph is accurate and can be used for further analysis.

For this Activity, I would give myself a grade of 12 because besides understanding the lesson and reproducing the required one plot, I went ahead and reproduced the other plot in the graph for completeness.

This activity amazed me because I never knew that it was so simple to digitize old records (the graph I picked was published in 1923).

Hope this helps! :D