Thursday, June 13, 2013

A3 - Scilab Basics


This activity capitalizes on Scilab's relative ease in operating with matrices.

Using the code :

nx = 100; ny = 100; //defines the number ofelements 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 xand y coordinates
r= sqrt(X.^2 + Y.^2); //note element-per-elementsquaring of X and Y
A = zeros (nx,ny);
A (find(r<0.7) ) = 1;
f = scf();
grayplot(x,y,A);
f.color_map = graycolormap(32);

given by Ma'm Jing to create a circular aperture we were tasked to study it and use it to create 
different images. 

                               
                                                            Fig 1. Centered circular aperture

The first image that was easiest to recreate was the annulus which is like two 
concentric circles centered at the middle. I merely gave the region, with radii between 0.4 and 0.7,  in    the range [-1,1] in both axes, a value of 1 using the code shown below.

nx = 100; ny = 100; //defines the number ofelements 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 xand y coordinates
r= sqrt(X.^2 + Y.^2); //note element-per-elementsquaring of X and Y
A = zeros (nx,ny);
A (find(r<0.7 &  r >0.4) ) = 1;// portion between circles of radii 0.4 and 0.7 is given a value of 1
f = scf();
grayplot(x,y,A);
f.color_map = graycolormap(32);
this results in the image shown below. 
Fig 2. Annulus with black center
Another annulus can be created by switching the values of A making the default value 1 instead of an array of 0's using the below code,
nx = 100 ; ny =1 00; //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-elementsquaring of X and Y
A = ones (nx,ny);// made 1
A (find(r< 0.7 & r >0.4)) = 0;// portion between circles of radii 0.4 and 0.7 is gibven a value of 0
f = scf ();
grayplot(x,y,A);
f.color_map = graycolormap(32);

This gives an aperture with a white center as shown below. 
                                    
    Fig 3. Annulus with white center
The next image that was easiest to create is the square centered at the middle. Using the code shown below where I defined the side of the square to be r with a length of 0.4 I was able to accurately derive such a square.

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 xand y coordinates
r= 0.4; // side of the square
A = zeros (nx,ny);
A (find (abs(X)<r & abs (Y)<r))=1;
f = scf();
grayplot(x,y,A);
f.color_map = graycolormap(32);

                                                Fig 4. White square aperture centered in the middle



The corrugated roof or the sinusoid along the x axis was the next image I created. I used the 
da = gda() default function and x_label.text  to label the x  in order to show that the sinusoid is indeed oriented along the x-axis. I also made the x-axis range from -1 to 2.


nx = 100 ; ny =100; //defines the number of elements along x and y
x = linspace(-1,2,nx);//defines the range. Note that x ranges from -1 to 2 and y ranges from -1 to 1
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y);//creates two 2-D arrays of x and y coordinates
da=gda()
da.x_label.text="x";// label for x axis
A = sin(X.*%pi*7); f = scf (); grayplot(x,y,A); f.color_map = graycolormap(32);

The image is shown here, 
                                         Fig 5. Sinusoid along the x-axis forming a corrugated roof
The next image I created was the circular aperture with graded transparency (gaussian transparency). Using the definition of the Gaussian

nx = 100; ny = 100; //defines the number ofelements 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 xand y coordinates
r= sqrt(X.^2 + Y.^2); //note element-per-elementsquaring of X and Y
A = zeros (nx,ny);
A = (exp(-r) ) ;// using definition of Guassian 
f = scf();
grayplot(x,y,A);
f.color_map = graycolormap(32);
                                                 Fig 6.  Gaussian transparent circular aperture
The last image I was able to create was the grating along the x-axis. I used the for loop to set all 
columns of A which are multiples of 10 to have a value of 1 yielding 10 gratings for the 100 elements in the x axis. 

nx = 100; ny = 100; //defines the number ofelements along x and y
x = linspace(-1,2,nx); //defines the range
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y); //creates two 2-D arrays of xand y coordinates
da=gda()
da.x_label.text="x";// label for x axis

; // for x and y axes labels
A = zeros (nx,ny);
for r = 1:10:nx
    A(:,r)= 1
    end

f = scf();
grayplot(x,y,A);
f.color_map = graycolormap(32);

This gave me the image with 10 gray gratings across the x-axis. I used the x.label.text 
to properly identify the x from the y axis. I also made the range of x from -1 to 2 to differentiate it from
 the y axis.  This is seen in the image below 
                                                                      Fig 6. Gratings across the x-axis

Honestly, I think I deserve a perfect 10/10 because  I did everything that was asked and I properly labelled all pictures ( they can stand alone) and adequately explained my rationale. 



5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. I know right? PCD represent! I love 10/10s #justsaying

      Delete
  2. Thank you so much abc123! <3 10/10 for sure.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete