Aperture-Generation using MATLAB

Greetings!

For the second activity of Applied Physics 187, we were tasked to generate different grayscale shapes using scilab, octave or MATLAB. I chose MATLAB because I was more familiar with it. In this blog, I’ll explain how my code works, and the shapes they produced.

Initial Parameters

initialparameters

This first piece of code outputs two things: (1) the matrix A filled with 0’s of size (nx, ny) that shall act as the container for the shapes to be produced and (2) the 2D cartesian coordinates from -1 to 1 represented by the X and Y matrices. The vectors x and y represent the x and y coordinate axes. From this, ‘meshgrid’ generates the matrices X and Y, which contain the x and y coordinates for the 2D region (i.e. points off-axis). X and make operations over x and y easier. In essence, andwill determine the conditions for the shapes, while A will be the “canvas” for the shapes. Note that for each point in or Y, there is a corresponding point in A.

Displaying

The values of A are changed depending on the values of X and depending on the desired shape. The next portions describe how to obtain the desired shape in code. The images are then displayed and saved using the following code, where I used ‘export_fig’, a function made and shared by Yair Altman on MATLAB’s File Exchange.

addpath

Plotting

Centered Circle

circlecode

The first shape is a centered circular aperture. A matrix r is made, which contains the radial distance of the points from (0,0). The next line finds points in A with corresponding r <0.7, and changes their values from 0 to 1. The result is a circle centered at the origin with radius 0.7.

circle

Centered Square

squarecode

The second shape is a square centered at the origin. I first defined the “apothem”, which is defined as the shortest distance from the center to the side of a regular polygon. It’s kind of like the radius of a circle. Next, I found points whose and Y coordinates are both less than the apothem, and changed their values in A to 1.

Also, I would like to acknowledge and thank Martin Bartolome for teaching me the usage of the & symbol, which is very useful in shortening codes.

square

Sine wave along x

sinexcode

In the previous shapes, the binary shapes were produced by making the values of A only 0’s and 1’s. The next shape, the sine wave, is not binary, but has a gradient of values. To do this, the values of X (since we want the sine to  vary along this direction) were inputted into a sine function. The nice thing about using meshgrid to get and Y is that performing operations is very similar to how we do it by hand, as in this case. The image below is the sine wave viewed from above (looks like a corrugated sheet of metal). The constant multiplied to X controls the frequency and period of the sine wave.

sinex

Grating along the x-direction

gratingxcode

This shape is very similar to the previous one, but instead of using a sine wave, I used a square wave. The variable m controls how many bars there are.

gratingx

Annulus

annulus code

The annulus, or donut, is very similar to the circle. The first step is to make a circle with radius R_out. After that, a circle of radius R_in is deleted (by making the values 0). The result is as follows.

annulus

Centered circle with a gaussian gradient

Gausscode

This shape is similar to the centered circle, but instead of having a value of 1, the values of the circle must have a Gaussian distribution. I accomplished this by making bell, a 2D Gaussian bell curve centered at the origin that has the same size as A, which will act as a Gaussian filter. I “filtered” A by performing item-by-item multiplication of and bell. The parameters amp, center and sigma control the height, location and spread of the gaussian bell curve.

gauss

Ellipse (with rotating capabilities)

ellipsecode

I enjoyed working out this shape, just because I had to derive the method to rotate the coordinate axes (because I miss Math). Anyway, the variables and correspond to the maximum length of the ellipse along the x and y axis, respectively. The x and y axes can be rotated by an angle alpha in the CCW direction using the rotation matrix of cosines and sines, the rotated axes being denoted by Xprime and Yprime. The following images show the ellipse rotated 0, 45 and 90 degrees in the CCW direction.

Ellipses

Cross

crosscode

Since I had fun rotating the ellipse earlier, I ended up rotating the cross as well. The variable halfwidth, as its name implies, determines half the width of an arm of the cross.  The cross is very similar to the square, but instead of finding points that whose and Y coordinates are within a certain region, the cross is done by finding points whose X OR Y coordinate is within a certain region. The OR can be done by using | instead of &.

cross0cross

 

 

 

 

 

 

Checkerboard

 

checkerboardcode

This next shape was not required, but I did it because I found this activity extremely enjoyable, since I love coding. It’s a checkerboard, which is based on the grating. This was accomplished by making two perpendicular gratings (one along x and the other along y), and simply multiplying them term-by-term. The only portions that will remain are the intersections of the two gratings.

checkerboard

For this activity, I’d grade myself a 9/10. For while I enjoyed the activity and finished it relatively early (even having the time to make other shapes), I still made the blog very late.    All in all, I hope this activity will still be done for the next batches.

 

2 thoughts on “Aperture-Generation using MATLAB

  1. Thank you very much for sharing your knowledge. How to generate concentric circles? Binary circular gratings?

Leave a comment