MATLAB phase of 2D rectangular pulse's Fourier transform I'm trying to plot the graph of the phase of the Fourier transform of a 2D rectangular pulse. I've been able to evaluate the FFT but I'm not sure if the phase is correct because there are some tilts I can't explain.


What are those diagonal lines that appear in the phase? 
How can I eliminate them?




Here there is the code:

clc
close all
clear
npoints=512;
perc=1;
dt=6*1E-7/(npoints*perc);  % Tempo di campionamento
df=1/(npoints*dt); % Frequenza di campionamento

t(1)=0;
f(1)=0;
for k=2:npoints/2
    t(k)=(k-1)*dt;
    t(npoints-k+2)=-t(k);
    f(k)=(k-1)*df;
    f(npoints-k+2)=-f(k);
end
t(npoints/2+1)=t(npoints/2+2)-dt;
f(npoints/2+1)=f(npoints/2+2)-df;

ts=ifftshift(t);
fs=fftshift(f);

figure
[X,Y]=meshgrid(ts,ts);

D = npoints/2;        % to indicate origin at the center of the function
a = 5;          % change it to enlarge or reduce the pulse
y = repmat(1:npoints,npoints,1);
x = y';
rect = zeros(npoints);
rect(D-a:D+a-1,D-a:D+a-1) = ones(2*a);
rect=(rect);
surf(X,Y,rect);
shading interp
axis tight
title ('Rect 3D');
rect=ifftshift(rect);
figure, surf(X,Y,rect);
shading interp
axis tight
title ('Rect 3D shifted');

R = fft2((rect));
R = fftshift(R);
[X,Y]=meshgrid(fs,fs);
figure;
surf(X,Y,abs(R)); 
shading interp
axis tight
title('Fourier Transform of Rectangular function');
%plot real part
figure;
surf(X,Y,real(R)); 
shading interp
axis tight
title('Real part');

Rm=abs(R);
imm=imag(R);
re=real(R);
re(abs(re) &lt; 1e-8) = 0;
imm(abs(imm) &lt; 1e-8) = 0;
R=re+imm;
phase=angle(R);

%plot phase
[X,Y]=meshgrid(fs,fs);
figure
surf(X,Y,phase);
shading flat
axis tight
title ('Phase of the rect');
The issue is that the phase value found is between $-\pi$ and $\pi$ (or $0$ and $2\pi$) but it needs to be "unwrapped" to be continuous.

In 1D, the unwrap function will help. Your mileage may vary when applying it to a 2D Fourier transform.

Also, there is a bug in your code that forces the R variable to be real valued:

R=re+imm;
phase=angle(R);


is not going to get you the right phase at all.