I have been able to create a 3D rectangular pulse and to evaluate the fft of it, but when it comes to the phase it looks like it’s wrong shifted. The phase should only admitt values 0,pi (or -pi) instead is multiplied by a linear shift. I guess it's because matlab is receiving the pulse not centered in the way it is wanted. Is there a way to fix it, to have a correct phase’s graph? Thanks!
close all
clear
clc
npoints=512;
perc=1;
dt=6*1E-7/(npoints*perc);
df=1/(npoints*dt);
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;
a = 100;
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');
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) < 1e-12) = 0;
imm(abs(imm) < 1e-12) = 0;
phase=atan2(imm, re);
[X,Y]=meshgrid(fs,fs);
figure
surf(X,Y,phase);
shading flat
axis tight
title ('Phase of the rect');




