# DTFT of any finite sequence in matlab

I think freqz in a MATLAB toolbox, is the way to obtain DTFT of sequence. freqz can calculate frequency response of:

H(z)=(Num)/(Den)

We can easily compute the z-transform of any finite sequence x(n) like this:

H(z)=x(0)z^0 + x(1)z^1 + ...

We know in above expression that the Den, is 1.

Recalling that: freeqz(num,den,n) gives the step response in n point. By x be vector of x(n),

[x1freqz, x1freqzw]=freqz(x,1,3000,'whole');


must gives us the DTFT.

1)Is it(above statement) correct? what happening if we shift our polynomial?? why?

The second way is to calculate DTFT formula completely, like this:

[X, W]=me_dtft(x1',pi,3000);
figure
title('my')
% plot(W/pi,20*log10(abs(X)));
plot(W/pi,abs(X))
ax = gca;
% ax.YLim = [-40 70];
ylabel('Magnitude (dB)')

function [X, w]=me_dtft(x,whalfrange, nsample)
w= linspace(-whalfrange,whalfrange,nsample);
t=0:1:size(x,2)-1;

X=zeros(1,size(w,2));
for i=1:1:size(w,2)
X(i)=x*exp(-t*1i*w(i))';
end

end


2)I confused, is the range of parameter t in above code, important?

3)Is this implementation correct? Why?

I think there must be dissonance, since the picture: Telling us something wrong! These transform are taken from pure sine wave(its code in in the picture), from right you can see the fft , freqz by the manner explained top and(left) the DTFT as explained earlier.

Edit after "Jason R"'s comment: Ok, also I removed logarithmic scale, since it make me confuse. After that, intuitively thy are alike, as you can see in next image, but why they are not exactly the same(refer to last image by logarithmic scale?)?

freqz:

[x1freqz, x1freqzw]=freqz(fliplr(XX'),1,3000,'whole');

figure
title('freqz')
% plot((x1freqzw/pi)-1,20*log10(abs(fftshift(x1freqz))))
plot((x1freqzw/pi)-1,abs(fftshift(x1freqz)))
ax = gca;
% ax.YLim = [-40 70];
ax.XTick = -1:.5:2;
ylabel('Magnitude')


Sine sample:

Fs=1000; Ts=1/Fs;
time=0:Ts:1;

Freqs=500;
Xs=zeros(length(Freqs),length(time));

for i=1:length(Freqs)
Xs(i,:)= cos(2*pi*Freqs(i)*time);
end

XX=Xs;
XX=XX./ max(abs(XX));

figure;plot(time, XX); axis(([0 time(end) -1 1]));
xlabel('Time (sec)'); ylabel('Singal Amp.');
title('A sample audio signal');
sound(XX,Fs)

• You should post a complete script that generates the plots that you gave. – Jason R Mar 3 '17 at 12:13
• One difference is that I see that in your code you are plotting the DTFT from -fs/2 to +fs/2 while the FFT goes from 0 to fs in comparison. (But you can use "fftshitt") Without seeing all your code I suspect the difference in the two plots is rounding error which would be reduced by including more points and or going to double precision floats. Note that the DTFT will be a Sinc function of which the FFT will be samples of, unless you create a sine wave with a frequency that is an integer sub multiple of the sampling rate. Also know that you can get samples of the DTFT by zero-padding the FFT. – Dan Boschen Mar 3 '17 at 12:25
• @DanBoschen in fft figure, 1000 means fs/2? – mohammadsdtmnd Mar 3 '17 at 12:40
• No 1000 means fs, your waveform is at fs/2 – Dan Boschen Mar 3 '17 at 12:46
• @DanBoschen Can you explain more why they(my DTFT and freqz, I mean) are not exactly the same? If I padding the fft, sinc will appear? – mohammadsdtmnd Mar 3 '17 at 12:48

The linspace function as used is going from -fs/2 to +fs/2 for 3000 samples so the +fs/2 is getting counted twice. In contrast the FFT and freqz, which goes from 0 to N-1 do not duplicate the two end points (in this case they go from DC to 1 bin less than fs where fs is the sampling rate). Therefore the sample locations are not exactly the same leading to the difference observable in the two freqz methods.

Further you can get samples of the DTFT by zero padding as another option: fft(x, 3000).

Instead of linspace which will work just fine if you choose the proper start and stop, I like to do:

t = [0: length(x)-1]*1/fs

• Yes to answer your second question I believe your approach is correct. Once you fix the sample locations the results should match – Dan Boschen Mar 3 '17 at 16:16
• That's worked pretty nice, but my second question was:2)I confused, is the range of parameter t in above code, important? – mohammadsdtmnd Mar 3 '17 at 20:41
• Ah I see--- Well the range of t is the number of samples. You overrode the default 512 samples in freqz to be 3000 for example, which gave you more samples of the same DTFT. That said, the range doesn't change the answer for the samples you pick, but gives you more samples in your results. For instance, even in your case where you had two different answers because the samples were at slightly different t values, in both cases you were seeing the same DTFT, just slightly different locations on the same (continuous) curve. (The DTFT is a continuous function of frequency). – Dan Boschen Mar 3 '17 at 23:41
• I think you explained w parameter, not t, isn't that? t must override time signal indexes, but index can drive separately from t in the exponential. we can use different t as used in the index of our signal, in better scheme we can shift that range before computing exponential, what's it's impact? – mohammadsdtmnd Mar 4 '17 at 9:32
• In your formula t must be the same length as x. If you increase the number of samples in t, you must have more samples in x which means more frequency precision in your DTFT (the Sinc pattern that you see in frequency will have nulls closer together). When the number of samples in x, t and frequency all match, you are computing the DFT (although the Long way compared to the FFT--- which is why a zero padded FFT would be a much more efficient approach for you to compute samples of the DTFT, or just use freqz as you have) – Dan Boschen Mar 4 '17 at 12:18