├── BasicFunctions ├── ReadMe.txt ├── fold.m ├── hat.m ├── shift.m ├── simps.m └── unitstep_dt.m ├── GS Algorithm ├── Digital_Lens.m ├── GSalgorithm.m └── ReadMe.txt ├── Phase Profiles ├── Dammam_binary.m ├── Gaussian_Spot_3D_new.m ├── Multilevel.m ├── ReadMe.txt ├── dammam_binary_symmetry.m └── grating.m ├── ReadMe.txt ├── SLM_Propagation_1D.m └── SLM_Propagation_2D.m /BasicFunctions/ReadMe.txt: -------------------------------------------------------------------------------- 1 | MATLAB functions which are called by the beam propagation code. 2 | 3 | hat.m 4 | This function implements the Hankel Transform 5 | 6 | -------------------------------------------------------------------------------- /BasicFunctions/fold.m: -------------------------------------------------------------------------------- 1 | function [ y,ny ] = fold( x,nx ) 2 | %FOLD Summary of this function goes here 3 | % Detailed explanation goes here 4 | ny = fliplr(-nx); 5 | y = fliplr(x); 6 | 7 | end 8 | -------------------------------------------------------------------------------- /BasicFunctions/hat.m: -------------------------------------------------------------------------------- 1 | %[H,I]=hat(h,r,k,n;I) 2 | %-------------------- 3 | % 4 | %Hankel transform of order n. 5 | % 6 | %Input: 7 | % h Signal h(r) 8 | % r Radial positions [m] {0:numel(h)-1} 9 | % k Spatial frequencies [rad/m] {pi/numel(h)*(0:numel(h)-1)} 10 | % n Transform order {0} 11 | % or 12 | % I Integration kernel {default} 13 | % 14 | %Output: 15 | % H Spectrum H(k) 16 | % I Integration kernel 17 | % 18 | % 19 | % If the integration kernel is missing, it is 20 | % recomputed from the Bessel functions (slow). 21 | % 22 | 23 | function [H,I]=hat(h,r,k,n) 24 | if sum(size(h) > 1) > 1 25 | error('Signal must be a vector.'); 26 | end 27 | if nargin < 2 | isempty(r) 28 | r=0:numel(h)-1; 29 | else 30 | [r,w]=sort(r(:).'); 31 | h=h(w); 32 | end 33 | if nargin < 3 | isempty(k) 34 | k=pi/numel(h)*(0:numel(h)-1); 35 | end 36 | if nargin < 4 | isempty(n) 37 | n=0; 38 | end 39 | if numel(n) > 1 40 | if exist('w','var') 41 | I=n(:,w); 42 | else 43 | I=n; 44 | end 45 | else 46 | I=besselj(n,k(:)*r); 47 | end 48 | H=reshape(I*frdr(h,r),size(k)); 49 | -------------------------------------------------------------------------------- /BasicFunctions/shift.m: -------------------------------------------------------------------------------- 1 | function [ y,ny ] = shift( x,nx,n0 ) 2 | %SHIFT Summary of this function goes here 3 | % y[n] = x[n-n0] 4 | ny = nx + n0; 5 | y = x; 6 | end 7 | -------------------------------------------------------------------------------- /BasicFunctions/simps.m: -------------------------------------------------------------------------------- 1 | function z = simps(x,y,dim) 2 | %SIMPS Simpson's numerical integration. 3 | % The Simpson's rule for integration uses parabolic arcs instead of the 4 | % straight lines used in the trapezoidal rule. 5 | % 6 | % Z = SIMPS(Y) computes an approximation of the integral of Y via the 7 | % Simpson's method (with unit spacing). To compute the integral for 8 | % spacing different from one, multiply Z by the spacing increment. 9 | % 10 | % For vectors, SIMPS(Y) is the integral of Y. For matrices, SIMPS(Y) is a 11 | % row vector with the integral over each column. For N-D arrays, SIMPS(Y) 12 | % works across the first non-singleton dimension. 13 | % 14 | % Z = SIMPS(X,Y) computes the integral of Y with respect to X using the 15 | % Simpson's rule. X and Y must be vectors of the same length, or X must 16 | % be a column vector and Y an array whose first non-singleton dimension 17 | % is length(X). SIMPS operates along this dimension. 18 | % 19 | % Z = SIMPS(X,Y,DIM) or SIMPS(Y,DIM) integrates across dimension DIM of 20 | % Y. The length of X must be the same as size(Y,DIM). 21 | % 22 | % Examples: 23 | % -------- 24 | % % The integration of sin(x) on [0,pi] is 2 25 | % % Let us compare TRAPZ and SIMPS 26 | % x = linspace(0,pi,6); 27 | % y = sin(x); 28 | % trapz(x,y) % returns 1.9338 29 | % simps(x,y) % returns 2.0071 30 | % 31 | % If Y = [0 1 2 32 | % 3 4 5 33 | % 6 7 8] 34 | % then simps(Y,1) is [6 8 10] and simps(Y,2) is [2; 8; 14] 35 | % 36 | 37 | %-- Make sure x and y are column vectors, or y is a matrix. 38 | perm = []; nshifts = 0; 39 | if nargin == 3 % simps(x,y,dim) 40 | perm = [dim:max(ndims(y),dim) 1:dim-1]; 41 | yp = permute(y,perm); 42 | [m,n] = size(yp); 43 | elseif nargin==2 && isscalar(y) % simps(y,dim) 44 | dim = y; y = x; 45 | perm = [dim:max(ndims(y),dim) 1:dim-1]; 46 | yp = permute(y,perm); 47 | [m,n] = size(yp); 48 | x = 1:m; 49 | else % simps(y) or simps(x,y) 50 | if nargin < 2, y = x; end 51 | [yp,nshifts] = shiftdim(y); 52 | [m,n] = size(yp); 53 | if nargin < 2, x = 1:m; end 54 | end 55 | x = x(:); 56 | if length(x) ~= m 57 | if isempty(perm) % dim argument not given 58 | error('MATLAB:simps:LengthXmismatchY',... 59 | 'LENGTH(X) must equal the length of the first non-singleton dimension of Y.'); 60 | else 61 | error('MATLAB:simps:LengthXmismatchY',... 62 | 'LENGTH(X) must equal the length of the DIM''th dimension of Y.'); 63 | end 64 | end 65 | 66 | %-- The output size for [] is a special case when DIM is not given. 67 | if isempty(perm) && isequal(y,[]) 68 | z = zeros(1,class(y)); 69 | return 70 | end 71 | 72 | %-- Use TRAPZ if m<3 73 | if m<3 74 | if exist('dim','var') 75 | z = trapz(x,y,dim); 76 | else 77 | z = trapz(x,y); 78 | end 79 | return 80 | end 81 | 82 | %-- Simpson's rule 83 | y = yp; 84 | clear yp 85 | 86 | dx = repmat(diff(x,1,1),1,n); 87 | dx1 = dx(1:end-1,:); 88 | dx2 = dx(2:end,:); 89 | 90 | alpha = (dx1+dx2)./dx1/6; 91 | a0 = alpha.*(2*dx1-dx2); 92 | a1 = alpha.*(dx1+dx2).^2./dx2; 93 | a2 = alpha.*dx1./dx2.*(2*dx2-dx1); 94 | 95 | z = sum(a0(1:2:end,:).*y(1:2:m-2,:) +... 96 | a1(1:2:end,:).*y(2:2:m-1,:) +... 97 | a2(1:2:end,:).*y(3:2:m,:),1); 98 | 99 | if rem(m,2) == 0 % Adjusting if length(x) is even 100 | state0 = warning('query','MATLAB:nearlySingularMatrix'); 101 | state0 = state0.state; 102 | warning('off','MATLAB:nearlySingularMatrix') 103 | C = vander(x(end-2:end))\y(end-2:end,:); 104 | z = z + C(1,:).*(x(end,:).^3-x(end-1,:).^3)/3 +... 105 | C(2,:).*(x(end,:).^2-x(end-1,:).^2)/2 +... 106 | C(3,:).*dx(end,:); 107 | warning(state0,'MATLAB:nearlySingularMatrix') 108 | end 109 | 110 | %-- Resizing 111 | siz = size(y); siz(1) = 1; 112 | z = reshape(z,[ones(1,nshifts),siz]); 113 | if ~isempty(perm), z = ipermute(z,perm); end 114 | -------------------------------------------------------------------------------- /BasicFunctions/unitstep_dt.m: -------------------------------------------------------------------------------- 1 | function [ x,n ] = unitstep_dt( n1,n0,n2 ) 2 | %UNITSTEP_DT Summary of this function goes here 3 | % Detailed explanation goes here 4 | n = n1:n2; %the time vector 5 | n0 = find(n == n0); %finds the index of the step starting point 6 | x = zeros(1,length(n)); 7 | x(n0:end) = 1; 8 | end 9 | -------------------------------------------------------------------------------- /GS Algorithm/Digital_Lens.m: -------------------------------------------------------------------------------- 1 | function frenellens=Digital_Lens(f2,delta) 2 | %unit:mm 3 | %f2 is the focal length of Fourier Transform lens 4 | %delta is the axial shift, +:away 5 | %Initializing Hologram Matrices 6 | H=1272;%%Horizontal pixels 7 | V=1024;%%Vertical pixels 8 | x=-H/2:1:(H/2-1); 9 | y=-V/2:1:(V/2-1); 10 | x=x*12.5e-3;%(in mm) Scales the hologram in the V direction 11 | y=y*12.5e-3;%(in mm) Scales the hologram in the H direction 12 | [X,Y]=meshgrid(x, y); 13 | r=sqrt(X.^2 + Y.^2); 14 | nx=0; 15 | ny=0; 16 | gy=ny/(V*12.5e-3); 17 | gx=nx/(H*12.5e-3); 18 | lambda=0.532e-3; % units mm 19 | k=2*pi/lambda; 20 | %ff=10; %units mm 21 | %T=pi/lambda/ff*(X.^2+Y.^2); 22 | %f=450; 23 | %T=-k*(X.^2+Y.^2)/2*(1/f); 24 | % T=-k*(X.^2+Y.^2)/2*(1/f2-1/(f2+delta)); 25 | T = -k/2 * (X.^2 + Y.^2) * (1/(f2-delta) - 1/f2); 26 | HOL=mod(T+2*pi*(X*gx+Y*gy),2*pi); 27 | SLM=HOL-min(HOL(:)); 28 | SLM=SLM/max(SLM(:))*255; 29 | % fig=figure; 30 | % set(fig,'Position',[120 0 1272 1080],'MenuBar','none','ToolBar','none','resize','off');%fullscreen SLM 31 | % set(gca,'position',[0 0 1 1],'Visible','off') 32 | % imagesc(SLM) 33 | % imwrite(uint8(SLM),'frenel_lens.bmp') 34 | % axis image 35 | % axis off 36 | % colormap gray 37 | frenellens=uint8(SLM); 38 | 39 | 40 | -------------------------------------------------------------------------------- /GS Algorithm/GSalgorithm.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KillianRice/SLM-code/087aa654976e8ef68a40e0ca0b9599913868a338/GS Algorithm/GSalgorithm.m -------------------------------------------------------------------------------- /GS Algorithm/ReadMe.txt: -------------------------------------------------------------------------------- 1 | A MATLAB implementation of the Gerchberg-Saxton Algorithm 2 | -------------------------------------------------------------------------------- /Phase Profiles/Dammam_binary.m: -------------------------------------------------------------------------------- 1 | %Binary phase grating with translational symmetry producing odd number of 2 | %spots: 13 3 | clear all; 4 | close all; 5 | 6 | N = 2048; %Size of image will still be 1024 x 1272 7 | x_axis = 1:N; 8 | T = 256; 9 | %% Transition points obtained after optimization 10 | x_n = [0.1289 0.3233 0.5862 0.6139 0.7290 0.7919 0.9074]; % transitions 11 | x_n_T = round(x_n*T) + 1; % rounded off to integer 12 | phi_n = [0,pi,0,pi,0,pi,0,pi]; 13 | n = 1:T; 14 | 15 | % 1st 16 | [y1,n] = unitstep_dt(n(1),1,n(end)); 17 | [y2,n] = unitstep_dt(n(1),x_n_T(1)+1,n(end)); 18 | F0 = phi_n(1)*(y1-y2); 19 | 20 | % 2nd 21 | [y1,n] = unitstep_dt(n(1),x_n_T(1),n(end)); 22 | [y2,n] = unitstep_dt(n(1),x_n_T(2),n(end)); 23 | F1 = phi_n(2)*(y1-y2); 24 | 25 | % 3rd 26 | [y1,n] = unitstep_dt(n(1),x_n_T(2),n(end)); 27 | [y2,n] = unitstep_dt(n(1),x_n_T(3),n(end)); 28 | F2 = phi_n(3)*(y1-y2); 29 | 30 | % 4th 31 | [y1,n] = unitstep_dt(n(1),x_n_T(3),n(end)); 32 | [y2,n] = unitstep_dt(n(1),x_n_T(4),n(end)); 33 | F3 = phi_n(4)*(y1-y2); 34 | 35 | % 5th 36 | [y1,n] = unitstep_dt(n(1),x_n_T(4),n(end)); 37 | [y2,n] = unitstep_dt(n(1),x_n_T(5),n(end)); 38 | F4 = phi_n(5)*(y1-y2); 39 | 40 | % 6th 41 | [y1,n] = unitstep_dt(n(1),x_n_T(5),n(end)); 42 | [y2,n] = unitstep_dt(n(1),x_n_T(6),n(end)); 43 | F5 = phi_n(6)*(y1-y2); 44 | 45 | % 7th 46 | [y1,n] = unitstep_dt(n(1),x_n_T(6),n(end)); 47 | [y2,n] = unitstep_dt(n(1),x_n_T(7),n(end)); 48 | F6 = phi_n(7)*(y1-y2); 49 | 50 | % 8th 51 | [y1,n] = unitstep_dt(n(1),x_n_T(7),n(end)); 52 | F7 = phi_n(8)*y1; 53 | %% Finaly phase profile 54 | F = F0 + F1 + F2 + F3+ F4+ F5+F6+F7; 55 | figure('Name','Single period','NumberTitle','off'); 56 | plot(n/T,F/pi); 57 | ylabel('\phi (units of \pi)'); 58 | axis([0 inf -0.5 1.5]); 59 | 60 | rep = N/T; % Number of repitions 61 | func = repmat(F,1,rep); % Repeating one period 62 | 63 | figure('Name','Damman Grating Phase Profile (1D)','NumberTitle','off'); 64 | plot(x_axis,func/pi); 65 | axis([0 N -0.5 1.5]); 66 | xlabel('Pixels'); 67 | ylabel('\phi (units of \pi)'); 68 | clearvars -except func N x_axis 69 | 70 | %% extending to 2D 71 | 72 | for iter_col = 1:1272 73 | for iter_row = 1:1024 74 | Phi_x(iter_row,iter_col) = func(iter_col); %high freq 75 | end 76 | end 77 | for iter_row = 1:1024 78 | for iter_col = 1:1272 79 | Phi_y(iter_row,iter_col) = func(iter_row); 80 | end 81 | end 82 | figure('Name','Damman Grating Phase Profile (2D)','NumberTitle','off'); 83 | Phi = mod(Phi_x+Phi_y,2*pi); 84 | imagesc(Phi/pi,[0 2]); 85 | colormap gray; 86 | h = colorbar; 87 | ylabel(h,'\phi (units of \pi)'); 88 | axis off; 89 | %% Saving the 2D file as .bmp (8 bit) 90 | 91 | Phi = 255/(2*pi) * Phi; %Converting from [0,2pi] to [0,255] 92 | Phi = uint8(Phi); 93 | imwrite(Phi,'Dammann_2D_phase_13.bmp','bmp'); 94 | 95 | -------------------------------------------------------------------------------- /Phase Profiles/Gaussian_Spot_3D_new.m: -------------------------------------------------------------------------------- 1 | %% The purpose of this piece of code is to generate a phase profile using prism and lens phase (in the image plane). 2 | close all; 3 | clear all; 4 | clc; 5 | 6 | %% System Parameters 7 | w_beam = 0.535e-3; % beam waist of the generated mode 8 | lambda = 532e-9; % illumination wavelength 9 | k = 2*pi/lambda; % Wave number 10 | dx = 12.5e-6; % Pixel Pitch of Hamamatsu SLM (X13138-04) aka sampling freq 11 | D = pwd; % Working Directory of this file 12 | f = 1000e-3; % focal length : 1000 mm lens 13 | 14 | %% Options 15 | saveFile = 1; % options are: '1' or '0' 16 | 17 | %% Creating a 2-D Mesh 18 | Ncols = 1272; 19 | Nrows = 1024; 20 | 21 | xx = 1:Ncols; 22 | yy = 1:Nrows; 23 | [x,y] = meshgrid(xx,yy); 24 | 25 | 26 | %% Grating profile 27 | % (must be even number for periodicity in discrete time) 28 | T_x = 50; % period of the grating in terms of pixels X axis 29 | T_y = 50; % period of the grating in terms of pixels Y axis 30 | 31 | [n_x,Sp_x] = my_square(Ncols,T_x); 32 | [n_y,Sp_y] = my_square(Nrows,T_y); 33 | 34 | % Uncomment to add another frequency grating 35 | % (must be even number for periodicity in discrete time) 36 | Tx_highfreq = 100; % period of the grating in terms of pixels in X axis 37 | Ty_highfreq = 100; % period of the grating in terms of pixels in Y axis 38 | [n_x,Spx_highfreq] = my_square(Ncols,Tx_highfreq); 39 | [n_y,Spy_highfreq] = my_square(Ncols,Ty_highfreq); 40 | Ax_highfreq = 0; 41 | Ay_highfreq = 0; 42 | Spx_highfreq = Spx_highfreq * Ax_highfreq; 43 | Spy_highfreq = Spy_highfreq * Ay_highfreq; 44 | 45 | % Amplitude (remember, max value is pi) 46 | A_x = pi; 47 | A_y = 0; 48 | Sp_x = Sp_x * A_x; 49 | Sp_y = Sp_y * A_y; 50 | 51 | %% Generating Phase Patterns ( all phase is in range [0,2 pi) ) 52 | 53 | % Grating profile 54 | Phi_grating_x = zeros(1024,1272); 55 | Phi_grating_highfreq_x= zeros(1024,1272); 56 | Phi_grating_y = zeros(1024,1272); 57 | Phi_grating_highfreq_y = zeros(1024,1272); 58 | 59 | % multiplying the square wave with the matrix to create a matrix with a 60 | % square wave 61 | for iter_col = 1:1272 62 | for iter_row = 1:1024 63 | Phi_grating_x(iter_row,iter_col) = Sp_x(iter_col); %high freq x 64 | Phi_grating_highfreq_x(iter_row,iter_col) = Spx_highfreq(iter_col); %low freq x 65 | end 66 | end 67 | for iter_row = 1:1024 68 | for iter_col = 1:1272 69 | Phi_grating_y(iter_row,iter_col) = Sp_y(iter_row); % high freq y 70 | Phi_grating_highfreq_y(iter_row,iter_col) = Spy_highfreq(iter_col); %low freq y 71 | end 72 | end 73 | 74 | % Adding phase profiles moduli 2 pi 75 | Phi_grating = mod(Phi_grating_x + Phi_grating_y + Phi_grating_highfreq_x + Phi_grating_highfreq_y,2*pi); 76 | figure('Name','Grating Phase Profile','NumberTitle','off'); 77 | imshow(Phi_grating,[0 2*pi]); 78 | colorbar; 79 | 80 | % Shift in longitudinal direction 81 | Phi_lens = mod(-k*(dx)^2/(2*f)*((x-Ncols/2).^2+(y-Nrows/2).^2),2*pi); 82 | figure('Name','Fresnel-Lens Phase Profile (ColorMap)','NumberTitle','off'); 83 | imshow(Phi_lens,[0,2*pi]); 84 | colorbar; 85 | 86 | % Shift in the transverse direction (blazed grating or prism phase) 87 | a = 1e-3; 88 | Phi_prism = mod(-a*k*x*dx,2*pi); 89 | figure('Name','Prism Phase Profile (ColorMap)','NumberTitle','off'); 90 | imshow(Phi_prism,[0,2*pi]); 91 | 92 | 93 | % Sinusoidal Phase Grating 94 | 95 | c = 2*pi; % contrast 96 | period_px = 100; % period in terms of pixels 97 | f_0 = 1/(dx*period_px); % spatial frequency of the grating 98 | phi_sin = c/2 * sin(2*pi*f_0*x*dx)+c/2; 99 | figure('Name','Sinusoidal grating Phase Profile (ColorMap)','NumberTitle','off'); 100 | imagesc(phi_sin/pi); 101 | colormap gray; 102 | h = colorbar; 103 | ylabel(h,'\phi (units of \pi)'); 104 | axis off; 105 | 106 | 107 | %% Final Kinoform: (for computation) 108 | % Note: All computation is done in double precision. This is important. If 109 | % I do the computation in 8 bit numbers, significant errors are introduced. 110 | % Results were compared. 111 | Phi_total = mod(Phi_prism + Phi_lens + Phi_grating+ phi_sin,2*pi); 112 | figure('Name','Total Phase Total_Phase_Profile_computation','NumberTitle','off'); 113 | imshow(Phi_total,[0,2*pi]); 114 | 115 | Phi_total = 255/(2*pi) * Phi_total; %Converting from [0,2pi] to [0,255] 116 | 117 | %% Final kinoform: (for displaying onto SLM) 118 | % Wavefront Correction: 119 | S = fullfile(D,'Kinoforms','CAL_LSH0802080_532nm.bmp'); 120 | wvf_cor_532 = imread(S); 121 | wvf_cor_532 = double(wvf_cor_532); 122 | Phi_slm = mod(Phi_total + wvf_cor_532,256); 123 | % Note: According to the Inspection sheet of the SLM. For 532 nm, at signal 210, 124 | % we get 2 pi phase modulation. Thats how the SLM twisted nematic crystals 125 | % are operating at 532 nm at room temperature. 126 | Phi_slm = Phi_slm*210/255; 127 | % Appending zeros (because SLM ignores the last 8 columns) 128 | Z = zeros(1024,8); 129 | Phi_slm = cat(2,Phi_slm,Z); 130 | figure('Name','Total_Phase_Profile_SLM','NumberTitle','off'); 131 | imshow(Phi_slm,[0 255]); 132 | 133 | %% Saving File (using 8-bit numerics) 134 | 135 | % Saving file 136 | if saveFile == 1 137 | Phi_slm = uint8(Phi_slm); 138 | Phi_total = uint8(Phi_total); 139 | imwrite(Phi_slm,'Total_Phase_Profile_SLM.bmp','bmp'); 140 | imwrite(Phi_total,'Total_Phase_Profile_computation.bmp','bmp'); 141 | end -------------------------------------------------------------------------------- /Phase Profiles/Multilevel.m: -------------------------------------------------------------------------------- 1 | % A multilevel phase grating 2 | clear all; 3 | close all; 4 | 5 | N = 2048*2; % Size of the 1D grating 6 | x = 1:N; 7 | T = 128; % Period of the Dammam grating in pixels (must be even and a factor of N) 8 | L = 8; % The number of discrete phase levels 9 | n = 1:T/2; % The axis for one period 10 | 11 | % Transition points 12 | phi = [0,0.25,0.5,1.5]; % phi(n) in the research paper 13 | x_n = [0.13,0.235,0.337]; 14 | x_n_T = round(x_n*T) + 1; 15 | % The phase profile for one single period 16 | % F = 3 (# of Transition coordinates) or (degrees of freedom) or (This sets the number of spots = 2F-1) 17 | 18 | % F = 0 19 | [y1,n] = unitstep_dt(n(1),1,n(end)); 20 | [y2,n] = unitstep_dt(n(1),x_n_T(1)+1,n(end)); 21 | F0 = (2*pi/L)*phi(1)*(y1-y2); 22 | 23 | % F = 1 24 | [y1,n] = unitstep_dt(n(1),x_n_T(1),n(end)); 25 | [y2,n] = unitstep_dt(n(1),x_n_T(2),n(end)); 26 | F1 = (2*pi/L)*phi(2)*(y1-y2); 27 | 28 | % F = 2 29 | [y1,n] = unitstep_dt(n(1),x_n_T(2),n(end)); 30 | [y2,n] = unitstep_dt(n(1),x_n_T(3),n(end)); 31 | F2 = (2*pi/L)*phi(3)*(y1-y2); 32 | 33 | % F = 3 34 | [y1,n] = unitstep_dt(n(1),x_n_T(3),n(end)); 35 | F3 = (2*pi/L)*phi(4)*y1; 36 | 37 | % Computing the other half and getting the full period 38 | F = F0+F1+F2+F3; 39 | 40 | 41 | [F_rev,n_rev] = fold(F,n); % A function from my DSP course which reflects a function 42 | F = [F_rev,F]; % concatenating the two reflections 43 | rep = N/T; % Number of repitions 44 | func = repmat(F,1,rep); % Repeating one period 45 | 46 | figure('Name','Multilevel phase grating (1D)','NumberTitle','off'); 47 | plot(x,func); 48 | xlabel('Pixels'); 49 | ylabel('Phase (units of \pi)'); 50 | clear F F0 F1 F2 F3 F_rev L n n_rev phi rep T x y1 y2 51 | -------------------------------------------------------------------------------- /Phase Profiles/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Generate 2D phase profiles for the SLM 2 | -------------------------------------------------------------------------------- /Phase Profiles/dammam_binary_symmetry.m: -------------------------------------------------------------------------------- 1 | %Binary phase grating with translational symmetry producing even number of 2 | %spots: 8 3 | close all; 4 | clear all; 5 | 6 | N = 2048; %Size of image will still be 1024 x 1272 7 | x_axis = 1:N; 8 | T = 512; 9 | %% Transition points obtained after optimization 10 | x_n = [0.1812,0.2956,0.3282,0.4392, 0.5000,0.6812,0.7956,0.8282,0.9392]; % transitions 11 | x_n_T = round(x_n*T) + 1; % rounded off to integer 12 | phi_n = [0 pi 0 pi 0 pi 0 pi 0 pi]; 13 | n = 1:T; 14 | 15 | % 1st 16 | [y1,n] = unitstep_dt(n(1),1,n(end)); 17 | [y2,n] = unitstep_dt(n(1),x_n_T(1)+1,n(end)); 18 | F0 = phi_n(1)*(y1-y2); 19 | 20 | % 2nd 21 | [y1,n] = unitstep_dt(n(1),x_n_T(1),n(end)); 22 | [y2,n] = unitstep_dt(n(1),x_n_T(2),n(end)); 23 | F1 = phi_n(2)*(y1-y2); 24 | 25 | % 3rd 26 | [y1,n] = unitstep_dt(n(1),x_n_T(2),n(end)); 27 | [y2,n] = unitstep_dt(n(1),x_n_T(3),n(end)); 28 | F2 = phi_n(3)*(y1-y2); 29 | 30 | % 4th 31 | [y1,n] = unitstep_dt(n(1),x_n_T(3),n(end)); 32 | [y2,n] = unitstep_dt(n(1),x_n_T(4),n(end)); 33 | F3 = phi_n(4)*(y1-y2); 34 | 35 | % 5th 36 | [y1,n] = unitstep_dt(n(1),x_n_T(4),n(end)); 37 | [y2,n] = unitstep_dt(n(1),x_n_T(5),n(end)); 38 | F4 = phi_n(5)*(y1-y2); 39 | 40 | % 6th 41 | [y1,n] = unitstep_dt(n(1),x_n_T(5),n(end)); 42 | [y2,n] = unitstep_dt(n(1),x_n_T(6),n(end)); 43 | F5 = phi_n(6)*(y1-y2); 44 | 45 | % 7th 46 | [y1,n] = unitstep_dt(n(1),x_n_T(6),n(end)); 47 | [y2,n] = unitstep_dt(n(1),x_n_T(7),n(end)); 48 | F6 = phi_n(7)*(y1-y2); 49 | 50 | % 8th 51 | [y1,n] = unitstep_dt(n(1),x_n_T(7),n(end)); 52 | [y2,n] = unitstep_dt(n(1),x_n_T(8),n(end)); 53 | F7 = phi_n(8)*(y1-y2); 54 | 55 | % 9th 56 | [y1,n] = unitstep_dt(n(1),x_n_T(8),n(end)); 57 | [y2,n] = unitstep_dt(n(1),x_n_T(9),n(end)); 58 | F8 = phi_n(9)*(y1-y2); 59 | 60 | % 10th 61 | [y1,n] = unitstep_dt(n(1),x_n_T(9),n(end)); 62 | F9 = phi_n(10)*y1; 63 | %% Finaly phase profile 64 | F = F0 + F1 + F2 + F3+ F4+ F5+F6+F7+F8+F9; 65 | figure('Name','Single period','NumberTitle','off'); 66 | plot(n/T,F/pi); 67 | ylabel('\phi (units of \pi)'); 68 | axis([0 inf -0.5 1.5]); 69 | 70 | rep = N/T; % Number of repitions 71 | func = repmat(F,1,rep); % Repeating one period 72 | 73 | figure('Name','Damman Grating Phase Profile (1D)','NumberTitle','off'); 74 | plot(x_axis,func/pi); 75 | axis([0 N -0.5 1.5]); 76 | xlabel('Pixels'); 77 | ylabel('\phi (units of \pi)'); 78 | clearvars -except func N x_axis 79 | %% extending to 2D 80 | for iter_col = 1:1272 81 | for iter_row = 1:1024 82 | Phi_x(iter_row,iter_col) = func(iter_col); %high freq 83 | end 84 | end 85 | for iter_row = 1:1024 86 | for iter_col = 1:1272 87 | Phi_y(iter_row,iter_col) = func(iter_row); 88 | end 89 | end 90 | 91 | figure('Name','Damman Grating Phase Profile (2D)','NumberTitle','off'); 92 | Phi = mod(Phi_x+Phi_y,2*pi); 93 | imagesc(Phi/pi,[0 2]); 94 | colormap gray; 95 | h = colorbar; 96 | ylabel(h,'\phi (units of \pi)'); 97 | axis off; 98 | %% Saving the 2D file as .bmp (8 bit) 99 | Phi = 255/(2*pi) * Phi; %Converting from [0,2pi] to [0,255] 100 | Phi = uint8(Phi); 101 | imwrite(Phi,'Dammann_2D_phase_8.bmp','bmp'); 102 | -------------------------------------------------------------------------------- /Phase Profiles/grating.m: -------------------------------------------------------------------------------- 1 | %����������������������������������������������������add grating 2 | function X001=grating(Tx,Xcontrast,Ty,Ycontrast) 3 | % Tx=2; 4 | % Xcontrast=0; 5 | % Ty=2; 6 | % Ycontrast=0;%pi; 7 | m=1024; 8 | n=1272; 9 | phasex0=mod(mod([ceil(-n/2):ceil(n/2)-1],Tx)*Xcontrast/(Tx-1),2*pi); 10 | phasey0=mod(mod([ceil(-m/2):ceil(m/2)-1],Ty)*Ycontrast/(Ty-1),2*pi); 11 | [X0,Y0]=meshgrid(phasex0,phasey0); 12 | phase0=mod(X0+Y0,2*pi); 13 | X001=phase0*128/pi; 14 | X001=uint8(X001); 15 | % imwrite(uint8((phase0)*128/pi),'grating.bmp') 16 | -------------------------------------------------------------------------------- /ReadMe.txt: -------------------------------------------------------------------------------- 1 | Beam propagation 2 | -------------------------------------------------------------------------------- /SLM_Propagation_1D.m: -------------------------------------------------------------------------------- 1 | close all; 2 | clear all; 3 | clc; 4 | 5 | %% Setting up system parameters 6 | 7 | s_factor = 1; % Set this to 1 for default (actual) values. Set this to larger than 1 for scaling the fft2. This ... 8 | % ...will introduce increasing errors as you increase its value largr. 9 | Lambda = 532e-9; %Wavelength 10 | k = 2*pi/Lambda; %Wave number 11 | f_fourier = (400)*1e-3; %Focal length of the Fourier Transforming Lens 12 | dx = s_factor*12.5e-6; %spatial period of sampling (pixels) 13 | F_s = 1/dx; %spatial freq of sampling (pixels) 14 | N = 1024; 15 | x = [-N/2:1:N/2-1]; 16 | 17 | 18 | %% Input phase profile here: (example of sinusoidal grating shown) 19 | % other 1D phase profiles of length 1024 can be included 20 | c = 2*pi; % contrast 21 | period_px = 50; % period in terms of pixels 22 | f_0 = 1/(dx*period_px); % spatial frequency of the grating 23 | phi_sin = c/2 * sin(2*pi*f_0*x*dx)+c/2; 24 | figure('Name','Sinusoidal grating Phase Profile (ColorMap)','NumberTitle','off'); 25 | imagesc(phi_sin/pi); 26 | colormap gray; 27 | h = colorbar; 28 | ylabel(h,'\phi (units of \pi)'); 29 | axis off; 30 | 31 | 32 | xx = dx*(-N/2:1:N/2-1); % The x axis 33 | x = exp(i*phi_sin); % The electric field 34 | 35 | % Plotting the Phase profile in 1D 36 | figure('NumberTitle','off','Name','1D Computation: Fourier Transform'); 37 | s(1) = subplot(2,2,1); 38 | plot(xx,angle(x)/pi); 39 | ylabel('Phase \Phi (units of \pi)'); 40 | xlabel('X axis'); 41 | s(2) = subplot(2,2,2); 42 | plot(xx,abs(x)); 43 | ylabel('Amplitude'); 44 | xlabel('X axis'); 45 | title(s(1),'Phase profile SLM plane'); 46 | title(s(2),'Amplitude profile SLM plane (normalized)'); 47 | %% Computing the Fresnel Diffraction Integral 48 | FFT = fft(x,N); 49 | y = fftshift(FFT); 50 | 51 | % Setting the axis 52 | w_freq = linspace(-pi,pi-(2*pi)/N,N); 53 | freq_proper = F_s*w_freq/(2*pi); %Spatial frequency 54 | clear x; 55 | x = freq_proper*Lambda*f_fourier; % converting from spatial frequency to coordinates 56 | 57 | % Plotting the Image plane Field 58 | s(4) = subplot(2,2,4); 59 | plot(s_factor*x/(1e-3),abs(y)); 60 | ylabel('Amplitude'); 61 | xlabel('X axis (mm)'); 62 | s(3) = subplot(2,2,3); 63 | plot(s_factor*x/(1e-3),angle(y)); 64 | ylabel('Phase \Phi (units of \pi)'); 65 | xlabel('X axis (mm)'); 66 | title(s(3),'Phase profile Image plane'); 67 | title(s(4),'Amplitude profile Image plane'); 68 | 69 | -------------------------------------------------------------------------------- /SLM_Propagation_2D.m: -------------------------------------------------------------------------------- 1 | % This piece of code can take a phase profile and give you the resulting 2 | % electric field in the fourier plane. 3 | 4 | close all; 5 | clear all; 6 | clc; 7 | %% Setting up system parameters 8 | 9 | s_factor = 1; % Set this to 1 for default (actual) values. Set this to larger than 1 for scaling the fft2. This ... 10 | % ...will introduce increasing errors as you increase its value largr. 11 | 12 | D = pwd; % Working Directory of this file 13 | Lambda = 532e-9; %Wavelength 14 | k = 2*pi/Lambda; %Wave number 15 | f_fourier = 77e-3; %Focal length of the Fourier Transforming Lens 16 | T_s = s_factor*12.5e-6; %spatial period of sampling (pixels) 17 | F_s = 1/T_s; %spatial freq of sampling (pixels) 18 | 19 | %% Reading image file and converting to proper phase scaling 20 | 21 | S = fullfile(D,'Dammann_2D_phase_8.bmp'); %input the phase profile 22 | Phi = imread(S); 23 | % Phi = Phi(:,:,1); 24 | Phi = 2*pi/255 * double(Phi); 25 | Phi = Phi(:,1:1272); 26 | figure('NumberTitle','off','Name','2D Computation: Fourier Transform'); 27 | s(2) = subplot(2,2,2); 28 | imshow(Phi,[0 2*pi]); 29 | title(s(2),'Input Phase profile'); 30 | 31 | %% Generating 2D Eletric Field 32 | Ncols = 1272; % X axis 33 | Nrows = 1024; % Y axis 34 | 35 | xx = T_s*(-Ncols/2:1:Ncols/2-1); 36 | yy = T_s*(-Nrows/2:1:Nrows/2-1); 37 | 38 | u_amp = 1; 39 | u = exp(i*Phi); 40 | 41 | s(1) = subplot(2,2,1); 42 | imagesc(xx,yy,u_amp); 43 | xlabel('X_{SLM} axis'); 44 | ylabel('Y_{SLM} axis'); 45 | title(s(1),'Amplitude profile SLM plane (normalized)'); 46 | 47 | 48 | %% The Fourier transform (2D) 49 | % The electric field in the Fourier plane 50 | v = fft2(u); 51 | v = fftshift(v); 52 | 53 | % Axes 54 | w_freq_cols = linspace(-pi,pi-(2*pi)/Ncols,Ncols); 55 | freq_proper_cols = F_s*w_freq_cols/(2*pi); % Spatial frequency 56 | x_cols = freq_proper_cols*Lambda*f_fourier; % converting from spatial frequency to coordinates 57 | 58 | w_freq_rows = linspace(-pi,pi-(2*pi)/Nrows,Nrows); 59 | freq_proper_rows = F_s*w_freq_rows/(2*pi); % Spatial frequency 60 | x_rows = freq_proper_rows*Lambda*f_fourier; % converting from spatial frequency to coordinates 61 | 62 | % Display image 63 | 64 | s(3) = subplot(2,2,3); 65 | imagesc(s_factor*x_cols/(1e-3),s_factor*x_rows/(1e-3),abs(v).^2); 66 | title(s(3),'Amplitude profile Image plane'); 67 | xlabel('X_{image} (mm)'); 68 | ylabel('Y_{image} (mm)'); 69 | 70 | 71 | 72 | --------------------------------------------------------------------------------