correct conversation from polar to cartesian coordinates for a surface profile

Hello everyone,
I have following problem. Within a bigger project I use the function . It describes the profile of an optical element and plotted this function should look like the gray image below. I wanted to recreate this image using cartesian coordinates (code below) and ended up with what you can see in the colored image. When you look along the positive x-axis in my plot you can see a discontinuity, which should not happen due to the 2pi-priodicity, such that as it is in the gray image.
How can I solve this problem? Thanks ahead ;)
The code i tried to use:
clc; close all; clear all;
% Variables
lam = 633e-9; % wavelength
f = 2; % focal length
a = pi/(lam*f); % Tuning constant
cutoff = 2*pi; % cutoff modulo operation
num_points = 1000; % number of pixel
% x,y-Grid
x_values = linspace(-5e-3, 5e-3, num_points); % for DOE with 1cm diameter
y_values = linspace(-5e-3, 5e-3, num_points);
[x, y] = meshgrid(x_values, y_values);
profile = a * (x.^2 + y.^2) .* atan2(y, x); % general phase profile in cartesian coords of Phi(r,phi)=a*r^2*phi
DOE_phaseprofile = mod(profile,cutoff); % DOE phaseprofile
% Plots
figure()
imagesc(x_values, y_values, DOE_phaseprofile);
colorbar;
xlabel('x (m)');
ylabel('y (m)');
viscircles([0 0],5e-3, 'LineWidth',0.8);
axis equal;
title('DOE1 PhaseProfil');

Réponses (1)

[edit: clean up copy-paste error, remove a line of code I added that was superfluous]
[edit: Reverse the axis labels. I rotated the plot 90 deg, but forgot to flip the axis labels.]
% Variables
lam = 633e-9; % wavelength
f = 2; % focal length
a = pi/(lam*f); % Tuning constant
cutoff = 2*pi; % cutoff modulo operation
num_points = 1000; % number of pixel
% x,y-Grid
x_values = linspace(-5e-3, 5e-3, num_points); % for DOE with 1cm diameter
y_values = linspace(-5e-3, 5e-3, num_points);
[x, y] = meshgrid(x_values, y_values);
profile = a * (x.^2 + y.^2) .* atan2(y, x); % general phase profile in cartesian coords of Phi(r,phi)=a*r^2*phi
DOE_phaseprofile = mod(profile,cutoff); % DOE phaseprofile
DOE_phaseprofile(y>0)=2*pi-DOE_phaseprofile(y>0);
% Plots
figure()
imagesc(y_values, x_values, DOE_phaseprofile');
colorbar;
xlabel('y (m)');
ylabel('x (m)');
viscircles([0 0],5e-3, 'LineWidth',0.8);
axis equal;
title('DOE1 PhaseProfil');
Try it. Good luck.

4 commentaires

@Jessica Winkler, I do not know why changing the phase when y>0 fixed the problem, but it did. Flipping the phase for y<0 works equally well (see below). Below I also added the colormap command, to render the image in gray, as you originally requested.
% Variables
lam = 633e-9; % wavelength
f = 2; % focal length
a = pi/(lam*f); % Tuning constant
cutoff = 2*pi; % cutoff modulo operation
num_points = 1000; % number of pixel
% x,y-Grid
x_values = linspace(-5e-3, 5e-3, num_points); % for DOE with 1cm diameter
y_values = linspace(-5e-3, 5e-3, num_points);
[x, y] = meshgrid(x_values, y_values);
profile = a * (x.^2 + y.^2) .* atan2(y, x); % general phase profile in cartesian coords of Phi(r,phi)=a*r^2*phi
DOE_phaseprofile = mod(profile,cutoff); % DOE phaseprofile
DOE_phaseprofile(y<0)=2*pi-DOE_phaseprofile(y<0);
% Plots
figure()
imagesc(y_values, x_values, DOE_phaseprofile');
colormap gray;
colorbar;
xlabel('y (m)');
ylabel('x (m)');
viscircles([0 0],5e-3, 'LineWidth',0.8);
axis equal;
title('DOE1 PhaseProfile');
Your original image, which you wanted to reproduce, had a discontinuity aazbove the teardrop, i.e. along a line from the center to the 12 oclock position. YOu said you wanted to eliminate the discontinuity, so that's what my fix does. If you only want to eliminate the discontinuity below the teardrop, then you could do what is below.
One other thing I do below is make the number of points slightly different along X and Y. I do this when I make figures like this, so that I cannot get my x and Y axes flipped from where they should be, without knowing it.
% Variables
lam = 633e-9; % wavelength
f = 2; % focal length
a = pi/(lam*f); % Tuning constant
cutoff = 2*pi; % cutoff modulo operation
num_pointX = 1000; % number of X pixel
num_pointY = 960; % number of Y pixel
% x,y-Grid
x_values = linspace(-5e-3, 5e-3, num_pointX); % for DOE with 1cm diameter
y_values = linspace(-5e-3, 5e-3, num_pointY);
[x, y] = meshgrid(x_values, y_values);
profile = a * (x.^2 + y.^2) .* atan2(y, x)+pi; % general phase profile in cartesian coords of Phi(r,phi)=a*r^2*phi
DOE_phaseprofile = mod(profile,cutoff); % DOE phaseprofile
%DOE_phaseprofile(y<0)=2*pi-DOE_phaseprofile(y<0);
% Plots
figure()
imagesc(y_values, x_values, DOE_phaseprofile');
colormap gray;
colorbar;
xlabel('y (m)');
ylabel('x (m)');
viscircles([0 0],5e-3, 'LineWidth',0.8);
axis equal;
title('DOE1 PhaseProfile');
I like this solution better than my previous solution, for two reasons: 1. This solution does not treat the negative y values differently - which has no good physical justification. This solution just adds a constant 180 degrees of phase everywhere. 2. This solution gives an image like the original image you posted, including the discontinuity from the center to 12 oclock.
Good luck.

Wow, thank you, awesome. Yes, there is an actual discontinuity along the negative x-Axis, while there is none within the teardrop. The only thing I can’t do is having a different number of pixels in x and y, because in reality 2 of those profiles (the original and it’s negative) will be overlapped and rotated towards each other. (Combined, this results in the phase profile of a sector-lens with 2 different focal lengths that depend on their mutual rotation angle theta).

I'm glad my answer was useful, and good luck with your work.

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by