Tuesday, June 22, 2010

Scilab: Easy and Free

Scilab is an important freeware in visualizing and computing in physics. It is a high level programming language. It can convert multiple lines in python and C into single lines which eliminates errors in programming.

One important aspect in the usage of Scilab is the manipulation and computation of matrices. To practice this, we can create different types of patterns. This can also be used for simulations in optical apertures.

Figure 1: Circle with radius 0.7

The first image, which is a practice image, is a circle. It is created with the following code.

1 //Cirlce

2 nx = 100; ny = 100; radius = 0.7;

3 x = linspace(-1,1,nx);

4 y = linspace(-1,1,ny);

5 [X,Y] = ndgrid(x,y);

6 r= sqrt(X.^2 + Y.^2);

7 A = zeros (nx,ny);

8 A (find(r

9 imshow (A, []);

10 imwrite (A, 'C:\Users\Amelie\Desktop\Circle.jpg');

In line 2, we define the number of elements per axis. We set this now as 100 per axis. Lines 3 and 4 create linear spaces from -1 to 1 with nx or ny number of elements in between. Line 5 now creates the grid that is 2 x 2 in size but 100 x 100 in pixels. Line 6 calculates the distance of a pixel from the origin. Line 7 initializes the grid where the values of the grey scale are assigned. Line 8 now finds the elements which are less than a specified radius away from the origin and assigns a grey scale value of 1. This creates a filled circle of color white. Lines 9 and 10 shows and save the image to the desktop of the computer.

Figure 2: Centered Square Aperture

This next image is a centered square aperture. Here a filled square of white is created and placed at the center of the image. This is done by the following code.

1 //Centered Square Aperture

2 nx = 200; ny = 200; hs = 0.5;

3 x = linspace(-1,1,nx);

4 y = linspace(-1,1,ny);

5 [X,Y] = ndgrid(x,y);

6 A = ones(nx,ny);

7 A(find(abs(X)>hs)) = 0;

8 A(find(abs(Y)>hs)) = 0;

9 imshow(A, []);

10 imwrite (A, 'C:\Users\Amelie\Desktop\Centered Square Aperture.jpg');

Lines 2 to 4 are similar to that of the circular aperture and serve the same purpose. Line 5 creates a grid similar to line 5 of the circular aperture but this time fills the grid with one rather than zero. The use of this is evident in the next two lines. Lines 7 and 8 now changes the values of the frame of the square aperture into 0 by finding the values of x and y that are larger than the half side (hs). If the grid was filled with zeroes and a square aperture of ones were created, an extra two lines would be needed since changing the greater than sign to less than sign would create a cross rather than a square. We can also change the side of the square by changing the value of hs into half that of the wanted size of the side.

Figure 3: Sinusoid along X-direction

This next image is slightly more complicated. The image is that of a sinusoid along the x direction creating a “yero” like effect. We now examine its source code.

1 //Sinusoid along the X-direction

2 nx = 200; ny = 200; period = 4*%pi;

3 x = linspace(-1,1,nx);

4 y = linspace(-1,1,ny);

5 [X,Y] = ndgrid(x,y);

6 A = (sin(period*Y) + 1)/2;

7 imshow(A, []);

8 imwrite (A, 'C:\Users\Amelie\Desktop\Sinusoid along the X-direction.jpg');


Again, lines 2 to 5 are similar to the previous figures. Line 6 creates a grid of the same size as that of the [X, Y] but places values that create a sinusoid along x direction. This is where it is counter intuitive in the programming code. In the matrix, we assign x as the first element of the grid and y to the second. But in showing the image, we can see that the base of the image is the y element while the side of the image is the y element. Now, since the image shows intensities of gray scale, negative values are a no-no. So we scale the sinusoidal values by adding 1 and dividing everything by 2. This shifts the output values of the sine to 0 to 1. Now, if we wish to change the period of sinusoid, we can change the value of the variable period.


Figure 4: Gratings


This next image is very familiar to optical experiments. The grating is a series of slits that are parallel to each other. Let us now examine the code.


1 //Grating

2 nx = 100; ny = 100; hw = 3; distance = 7;

3 x = linspace(0,100,nx);

4 y = linspace(0,100,ny);

5 [X,Y] = ndgrid(x,y);

6 A = zeros(nx,ny);

7 A(find(modulo(Y,distance)<=hw)) = 1;

8 A(find(modulo(Y,distance)>= (distance - hw))) = 1;

9 imshow(A, []);

10 imwrite (A, 'C:\Users\Amelie\Desktop\Grating.jpg');

Now, lines 7 and 8 create the gratings or slits by assigning 1 to the values that satisfy the modulo condition. The modulo condition states that the if the remainder of the division of Y or y coordinate by the distance defined as the distance between slits is less than the half width or greater than the distance minus the half width, a value of 1 will be assigned. This is used to allow the user to vary the components of the grating. We can now create slits of different distances from each other as well as different thicknesses. The sample above has a distance of 7 units and thickness of 6 units.

Now, another approach is shown by Mr. Androphil Polinar is his blog processedworld.blogspot.com. He used a technique used in analog to digital conversion. This is where a sinusoid is used while applying a threshold where the values switch from 1 and 0. Granted this is less complicated, it proposes a few problems in manipulating the aperture. Though by varying the period we can vary the distances, the slit width is difficult to manipulate. Theoretically, it can be varied by the threshold values. But it is difficult to ascertain the exact threshold value to get the exact width of the grating needed. This is solved by the code above.

Figure 5: Annulus

The next image is a variation of the circular aperture. This is the annulus. It resembles a donut where a filled white circle has a smaller filled black circle inside. Let’s now see how this is coded.

1 //Annulus

2 nx = 100; ny = 100; outr = 0.7; inr = 0.2;

3 x = linspace(-1,1,nx);

4 y = linspace(-1,1,ny);

5 [X,Y] = ndgrid(x,y);

6 r= sqrt(X.^2 + Y.^2);

7 A = zeros (nx,ny);

8 A (find(r<0.7)

9 A (find(r<0.2)

10 imshow (A, []);

11 imwrite (A, 'C:\Users\Amelie\Desktop\Annulus.jpg');

We can compare this code to that of the circle. We can see an addition of only one line, line 9. The idea is, we create a circular aperture and we reassign the values of a smaller circle back to zero. This creates an annulus.


Figure 6: Circular Aperture with Graded Transparency

This next image is a circular aperture with graded transparency. Basically, it is a circular aperture but instead of the circle to be a filled white value, it is assigned a Gaussian distribution or fading transparency. Let now look at the code.

1 //Circular Aperture with Graded Transparency

2 nx = 100; ny = 100; radius = 0.3;

3 x = linspace(-1,1,nx);

4 y = linspace(-1,1,ny);

5 [X,Y] = ndgrid(x,y);

6 r= sqrt(X.^2 + Y.^2);

7 A = zeros (nx,ny);

8 A = exp(-r);

9 A(find(r>radius)) = 0;

10 imshow (A, []);

11 imwrite (A, 'C:\Users\Amelie\Desktop\Circular Aperture with Graded Transparency.jpg');

This code is comparable to the code for circular aperture. The idea is instead of initializing a grid of zeros, we create a grid with values of exponentials with negative r dependence where r is the distance of a pixel from the origin. Then, in line 9, we reassign everything outside the circle to zero. We then get our circular aperture with graded transparency.

These are simple examples of using scilab to control matrices as well as simple apertures. From here we can use Fourier transform to see the diffraction pattern caused by this aperture and see the effect of varying variables to the diffraction pattern. Later on, we will figure out more methods in using scilab in our everyday experiments.

For this experiment I give myself a score of 11. I was able to complete the activity as well as personalize my code by allowing it to be easily variable. This will be very useful in looking at the effect of different parameters in the figure as well as the corresponding processes resulting from this activity.


Thursday, June 17, 2010

Digital Scanning: From Hard Copy to Soft Copy

Most papers and journals that we can gather are already in pdf form or printed form. When studying these papers, we take a look at the graphs and analyze the trends. We run into a problem when we need the actual values of the graph. This is where digital scanning and processing comes in.

The idea of digital scanning and processing is that if the graph printed is accurate and scaled, we can find the values of the data points we need in the graph just by using ratio and proportion. We can relate the pixels of the image to a specific scale on the actual graph, thus relating the pixel location to physical quantities represented by the graph.

Here we start by finding an old graph without the specific values of the data points. The graph I obtained comes from the Journal of Microbiology. By scanning this graph, we can now process the image to calculate the relationship of the pixel location to physical quantities.

Figure 1 Original Scanned Graph


Figure 2 Cropped Image of the Graph

First, we start by counting the pixels of the first tick mark to the last tick mark in the x and y axes. From the image and the use of Paint software, we find that the first tick mark in the x axis is at 18th pixel and the last tick mark is in the 474th pixel. These correspond to the 23 years and 100 years tick marks respectively. For the y axis, the first tick mark is at 799th pixel corresponding to -1 R value and 0th tick mark corresponding to the 6 R value.

From here, we compute for the unit/pixel constant.

77 years / (474 pixels -18 pixels) = 0.168859649 years / pixel (1)

7 R values / 799 pixels = 0.008760951 R values / pixel (2)

So given a data points x and y axis pixel count we can compute for the actual years and R values they represent.

X-axis

(data point(x) – 18 pixels) * constantx-axis + 23 years = # of years (3)

Y-axis

(799 pixels – data point(y)) * constanty-axis - 1 R value = R value (4)

For the x-axis, the subtraction of 18 pixels accounts for the offset of the origin and the addition of 23 years accounts for the starting year of the graph. While for the y-axis, the difference of 799 pixels and the data point y value accounts for the fact that the 0th pixel along the y axis start from the top of the graph to the bottom. But subtracting the data point y value to the maximum number of pixels along the y-axis (799) we get the height of the data point from the base of the graph. The subtraction of negative 1 accounts for the fact that the graph starts at negative 1. The constants used are the values solved in equations 1 and 2.

Now, to find the values of the data points, we get the pixel locations of the data points and apply equations 3 and 4 to get the actual years and R values of the graphs.

Years

R

Years

R

18

200

23

4.24781

59

90

29.92325

5.211514

178

230

50.01754

3.984981

267

440

65.04605

2.145181

326

490

75.00877

1.707134

474

615

100

0.612015

(a) (b)

Table 1 Data Points of Ash or Triangular markers (a) is in pixels (b) in actual values

Years

R

Years

R

18

267

23

3.660826

59

420

29.92325

2.320401

178

410

50.01754

2.40801

267

627

65.04605

0.506884

326

550

75.00877

1.181477

474

630

100

0.480601

(a) (b)

Table 2 Data Points of Organic Matter or Circular markers (a) is in pixels (b) in actual values

Years

R

Years

R

18

276

23

3.581977

59

591

29.92325

0.822278

178

553

50.01754

1.155194

267

723

65.04605

-0.33417

326

672

75.00877

0.112641

474

673

100

0.10388

(a) (b)

Table 3 Data Points of Water or Square markers (a) is in pixels (b) in actual values

Years

R

Years

R

18

259

23

3.730914

59

460

29.92325

1.969962

178

440

50.01754

2.145181

267

610

65.04605

0.65582

326

585

75.00877

0.874844

474

643

100

0.366708

(a) (b)

Table 4 Data Points of Water or X markers (a) is in pixels (b) in actual values

From the solved actual values of the data points, we can now graph the data and compare to the original graph.

Figure 3 Reconstructed graph with superimposed original graph

We can see that the data points very closely match each other. Small deviations can be seen in position. This can be attributed to 2 things. One, the data points in the graph are large, hence the exact position of the data point can vary largely. Two, since the graph was initially photocopied a slight distortion can be seen. This distortion will correspond to a change in the actual value and the corresponding pixel location.

I then conclude that this method is effective in very closely approximating the values in a graph. It is only limited to the quality of the initial graph in terms of resolution, clarity of printing and precision. It is recommended that the graph to be scanned be on a separate sheet of paper rather than bound in a book or a journal.

For this exercise, I give myself a score of 10. I was able to complete the activity as well as repeat the process for the multiple data series available in the graph. I was also able to present the data in a complete form. Hence, I believe I deserve a perfect score.

I would also like to thank Arvin Mabilangan for the support and initial help in the understanding the methodology.