Asked by HADIMARGO
on 9 Aug 2019

hi.i want to plot fourier transform of this:

my code is this:

clc

clear all

t=-5:0.01:5;

y=exp(-0.5*abs(t));

Fs=1;

X=fft(y);

f=(-length(X)/2:length(X)/2-1)*(Fs/length(X))

subplot(211);

plot(t,y);

subplot(212);

plot(f,fftshift(abs(X)));

but it donot.

the wrong result is this:

Answer by David Goodmanson
on 9 Aug 2019

Edited by David Goodmanson
on 9 Aug 2019

Accepted Answer

HI Hadimargo,

A few modifications are necessary to make this work out. oo First, in the time domain the exponential function is not really small enough at the left and right boundary to give a fully accurate result. Points are inexpensive here, and changing the t array to, say, -40:.01:40 gives suitably small values at the boundaries. oo Second, for an fft with a given array spacing delta_t , you can't declare the sampling frequency to be anything you want. Fs equals 1/delta_t by definition, so in this case Fs = 100. oo Third, if you zoom in on your result, the max of the function is not at f=0 but is off by one array point. Since N is odd, the frequency array should be symmetric about zero:

f = ((-(N-1)/2:(N-1)/2)/N)*Fs

Also, the plot you are comparing to is a function of w and not f, so

w = 2*pi*((-(N-1)/2:(N-1)/2)/N)*Fs

oo Fourth, for the scaling of the result, you are using the fft as a approximation to a continuous integral, so the sum perfomed by the fft is multiplied by the time spacing delta_t. WIth those changes, then zooming in on the result as a function of w shows agreement with the comparison fourier transform plot.

HADIMARGO
on 9 Aug 2019

hey dude. look at this.

i write a code that calculate fourier transform with integral. and it gives me correct anser. so if u can tell me why my fft code didnot give correct answer.

my second and correct code:

clc

clear all

t=-40:.01:40;

k=0;

y=exp(-0.5*abs(t));

for f=-5:.01:5

k=k+1;

X(k)=trapz(t,exp(-j*2*pi*f*t).*y);

end

f=-5:.01:5;

subplot(211);

plot(t,y);

subplot(212);

plot(f,abs(X)) ;

look at fourier transform. it gave me 4 in w=0 and this is correct.

David Goodmanson
on 9 Aug 2019

The problem is that your first new code implements three of the changes I mentioned, but not the fourth. The fft result X is supposed to get multiiplied by delta_t. Once you do that, the result is 4 and not 400.

It is worthwhile to verify by a second method, as you did. Note that trapz includes delta_t automatically.

HADIMARGO
on 16 Aug 2019

such complete and perfect answers.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.