# Phase spectrum of the DTFT of a rect pulse using FFT

I'm trying to use the FFT to plot the phase spectrum of the DTFT of a sampled rect impulse function. Here's the Matlab code I've written.

fs=10; % sampling frequency Hz
Ts=1/fs;
T=4; % length of time vector in seconds
t=-T/2:Ts:T/2-Ts; %vector of 40 time samples -2:0.1:1.9
y=rectpuls(t); %unit length rect impulse
z=fft(y,10000).*Ts;
%fft multiplied by Ts to get rid of the 1/Ts factor in the spectral replicas
%of the DTFT of the signal
f=linspace(0, (length(z)-1)/length(z),length(z));
figure
plot(f,abs(z));
grid on

%plot of the phase of z as it is, so with the time sequence
%considered to be shifted by length(y)/2 samples by fft.
figure
plot(f,unwrap(angle(z)),'r');
grid on

%I try to get rid of the linear phase delay component to get the phase
%spectrum of the DTFT of the signal in its frequency period.
%I divide each fft sample by its own exponential delay factor.
delay=length(y)/(2*length(z));
expd=zeros(1,length(z));
for index=1:1:length(z)
expd(index)=exp(-1i*2*pi*(index-1)*delay);
end
z2=z./expd;
figure
plot(f,unwrap(angle(z2))./pi,'r');
grid on


Now the problem is that this phase plot is wrong, the DTFT is the sum of real sinc functions so its phase spectrum con only assume k*pi values. There's still a linear component in the phase plot and the slope is positive so the exponentials that I'm using are over compensanting the delay. I have found out that the problem is that the fft function introduces a linear phase delay as if the sequence was shifted by 19.5 samples instead of 20 samples as it should be in my example. Infact if I set in my code delay=19.5/10000 I get the correct phase plot (or at least one that makes sense :D):

So what's going on here, is it a precision problem? Are there mistakes in my Matlab code and/or my dsp theory formulas? Thank you!!

Unfortunately I can't link more than two images in the post :/

• Welcome to DSP.SE! I don't understand why you say its phase spectrum con only assume k*pi values as I can't think of a non-trivial phase spectrum for which that will ever be true. The trivial example is the phase spectrum of a constant.
– Peter K.
Oct 12, 2015 at 12:40
• Thanks for your comment. This is what I mean: If x(t)=rect(t/T) we know that its Fourier Transform is Tsinc(fT). I want to plot the DTFT of this signal sampled with a sampling period Ts. I know that such DTFT is the sum of Tsinc((f-kfs)T) functions for k assuming all the infinite integer values. Such spectrum is the sum of real functions so at each frequency it can only have 0 or pi phase. I'm trying to use the FFT as a way of sampling such DTFT. Oct 12, 2015 at 15:03
• Since the sequence is considered to be time shifted by the fft function (the first sample is associated with t=0 and not t=-T/2 as in the actual sequence) each fft k-th sample is multiplied by exp(-i*2*piklengthofdelay/fftlength). I try to get rid of this exp factor to get the actual phase of the DTFT. Oct 12, 2015 at 15:12
• Ah! Thanks for the clarification. Yes, the FFT assumes that the time indices start at 0 and go to $N-1$ (for a signal of length $N$).
– Peter K.
Oct 12, 2015 at 15:59