Tuesday, June 22, 2010

Entry 8: Digital Aperture, Scilab, Python and Diffraction

The activity's objectives are generating digital versions of physical apertures and familiarize with the capabilities of Scilab for image generation and manipulation.

When I was doing this activity, an idea came to me. I remembered that the Fraunhofer diffraction of an aperture is the fourier transform of the transmittance function. In context, the transmittance function in real world experiments is simply the same as the things we're making (only digital versions).

But before going too much about fourier transform, the task was to make 5 patterns, namely; square, sinusoid along the x-axis, a grating in the x-axis, an annulus and an aperture with gaussian transmittance.

Dr. Soriano gave us a gist of how things should be done. She wrote for us a code to generate a circular pattern.

Fig 1: Circular aperture
//circle
nx = 500; ny = 500;
x = linspace(-1,1,nx);
y = linspace(-1,1,ny);[X,Y] = ndgrid(x,y);
r = sqrt(X.^2 + Y.^2);
A = zeros(nx,ny);
A(find(r<0.5))=1;
imshow(A,[]);
imwrite(A, 'circle.png')


 So the other apertures I created are the following (Fig 2.):

Square Aperture (Upper Left)
Sinusoidal Filter (Upper Right)
Grating (Center)
Annulus (Lower Left)
Gaussian Filter (Lower Right)


Fig 2: Digital apertures and filters



After generating the images above, I tried to find their resulting fourier transforms. The resulting image after taking the fourier transform of the original image would have a physical equivalent of the aperture's diffraction pattern at large distances (Fraunhofer Diffraction).

The resulting patterns are the following (Fig. 3):

Diffraction Pattern of a Square Aperture (Upper Left)
Diffraction Pattern of a Sinusoidal Filter (Upper Right)
Diffraction Pattern of a Grating (Center)
Diffraction Pattern of a Annulus (Lower Left)
Diffraction Pattern of a Gaussian Filter (Lower Right)


Fig. 3: Fraunhofer Diffraction Patterns for each aperture/filter



For this activity, I would rate myself a 12. I think, I have attained the objectives of this activity satisfactorily. I have finished all the required outputs and I extended the study to cover its applications (simulation of Fraunhofer Diffraction for each aperture/filter). I also wrote the corresponding Python codes for the generation of the same images (Appendix 2).


Appendix 1 (Scilab Codes):

//square
nx = 500; ny = 500;
x = linspace(-1,1,nx);
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y);
del = 0.05;
xr = (X>-del). * (X-del). * (Y<0.1))=0;>0.2))=0;
imshow(A,[]);
imwrite(A, 'annulus.png')

//gaussian
nx = 500; ny = 500;
x = linspace(-10,10,nx);
y = linspace(-10,10,ny);
[X,Y] = ndgrid(x,y);
r = sqrt(X.^2 + Y.^2);
r(find(r>=10))= 0;
A = exp(-(r.^2)/2);
A(find(A==1))= 0;
imshow(A,[]);
imwrite(A, 'gaussian.png')


Appendix 2 (Python Codes):

#circle
nx = 500; ny = 500; r = 0.5
X, Y = meshgrid(linspace(-1,1,nx),linspace(-1,1,ny))
A = zeros([nx, ny])
circ = sqrt(X**2+Y**2)
A[where(circ < r)] = 1 

imshow(A) 
savefig('circle.png') 

#square nx = 500; ny = 500; cx = nx/2; cy = ny/2; r = 10 
square = zeros([nx,ny]) 
square[cx-r:cx+r, cy-r:cy+r] = 1 
imshow(square) 
savefig('square.png') 

#sinusoid nx = 500; ny = 500 
sx = sin(linspace(-6*pi, 6*pi, nx)) 
X,Y = meshgrid(sx,linspace(-1,1,ny)) 
imshow(X) 
savefig('sinusoid.png') 

#grating nx = 500; ny = 500 inc = 20 
A = zeros([nx, ny]) 
A[:,arange(0,nx,inc)] = 1 
imshow(A) 
savefig('grating.png') 

#annulus nx = 500; ny = 500; r1 = 0.1; r2 = 0.2 
X, Y = meshgrid(linspace(-1,1,nx),linspace(-1,1,ny)) 
A = zeros([nx, ny]) 
circ = sqrt(X**2+Y**2) 
A[where(circ < r2)] = 1 
A[where(circ < r1)] = 0 
imshow(A) 
savefig('annulus.png') 

#gaussian nx = 500; ny = 500; r = 5 
X, Y = meshgrid(linspace(-10,10,nx),linspace(-10,10,ny)) 
A = zeros([nx, ny]) 
circ = sqrt(X**2+Y**2) 
A[where(circ < r)] = 1 
Z = exp(-circ**2/2)*A 
imshow(Z) 
savefig('gaussian.png')

1 comment:

  1. Thank you for the aperture codes and the explanation. But can you please share fft code as how are you plotting the results. And also if we don't have a aperture at the centre rather it is shifted then we get a diffraction pattern with maximum at the centre via fft. So how can we shift that in the result.

    ReplyDelete