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.
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.
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.
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.