omersk3
omersk3

Reputation: 23

Why is my discrete time Fourier transform incorrect?

I created a code that tries to compute the DTFT of a function.

This is my code:

figure
n = linspace(0,2*pi,1500); %example
x = cos(n); %example
l = length(n);
syms k w
Xk = sum(x(1:l).*exp((-1i.*k.*2/l.*pi.*(0:l-1))));
Xk = matlabFunction(Xk);
d = linspace(-5,5,1000);
stem(d,Xk(d))

In order to check my code, as you can see, I tried to compute the discrete time Fourier transform of cos(n) by sampling it and comparing it to the continuous time Fourier transform of cos(x), but unfortunately I don't get the same result.

Here is what I get by running this code:

click here

and when I increase n, although the values in -1,1 are increasing like we expect, the non-zero values are also increasing in contrast to the expectation.

(The Fourier transform of cos(x) is deltaFunction(x-1)+deltaFunction(x+1))

Thanks for any help. I know there are functions in MATLAB that do so but I try to create one by myself.

Upvotes: 2

Views: 879

Answers (2)

Cris Luengo
Cris Luengo

Reputation: 60444

What you are seeing is the result of using non-integer values of k.

The DTFT:

The DTFT assumes an infinite input signal. You don't have this, you have only one period of the cosine. Making the assumption that the rest of the signal is all zeros, you introduce many high frequencies in the transition from 1 to 0 at t=0, and t=2π. You also introduce some lower frequencies. These frequencies are the ones that you see in your plot.

If you want to compute the DTFT of cos(t) with t not limited to one period, then you need an infinite sum in your computation, you cannot do this of course.

If you want to assume that your signal is periodic, then you are computing the DFT.

The DFT:

You have a series of samples that you define as being at n = 0:l-1 in your transform, with l taking the function of N in the equation as shown on Wikipedia:

DFT equation

In this equation, both n and k are integers. k is typically also defined in the range 0:l-1, but since it is periodic, it is OK to define it in the range -ceil(l/2):ceil(l/2)-1, for example.

Setting d to a set of integer values in your code, I see the expected result:

d = -10:10;
stem(d,abs(Xk(d)))

output

Upvotes: 3

IKavanagh
IKavanagh

Reputation: 6187

Firstly, the result you have shown is correct for the DTFT of a cosine windowed between 0 and 2π. I have a feeling you are interested in computing the DFT of a cosine from -∞ to +∞ which Cris Luengo has demonstrated.

I think it would be useful to talk through the Fourier transform of a cosine, windowed cosine and the DTFT briefly.


The Fourier transform of a cosine is

Fourier transform of cosine

where the cosine is defined for t = -∞ to +∞, which can be computed by the DFT. But the Fourier transform of a windowed cosine

Windowed cosine

is

Fourier transform of windowed cosine

where N is number of periods of the window (1 above). Plotting this in MATLAB produces

Plot of Fourier transform of windowed cosine

So, in MATLAB if you want to compute the DTFT of a cosine your input should be a sampled cosine from t = -∞ to +∞ and your result should be similar to

Plot of DTFT of cosine

because the DTFT is periodic with period 2π but the DFT is not. The DTFT of a cosine is

DTFT of cosine

Upvotes: 2

Related Questions