MATLAB Answers

why the Real and imaginary part of fourier transform (fft) are not zero for real odd function

49 views (last 30 days)
Dear all,
I got confused when I found that the real part of sin-modulated Gaussian function was not zero after fft. I used the same code to test cos-modulated Gaussian function, the imagniary part actually was almost 0 that matchs with my expectation. In the code, I use fftshift to make sure that the first point in fft corresponds to the t=0. I am not sure what's wrong with my code? Could you please give me some suggestions?
n=128;
t=[-n/2:n/2-1]*(10)/(n/2);
f=@(x)sin(2*x).*exp(-x.^2/30);
% f=@(x)cos(2*x).*exp(-x.^2/30);
xt=f(t);
xf=fft(fftshift(xt));
%
samp=1/(t(2)-t(1));
freq=[0:(n-1)]/n*samp;
% fft of sin-modulated Gaussian is not correct since real(xf)~=0, why?
% fft of cos-modulated Gaussian is correct since imag(xf)==0
figure;
subplot(1,3,1);
plot(freq,abs(xf));
subplot(1,3,2);
plot(freq,real(xf));
subplot(1,3,3);
plot(freq,imag(xf));

  0 Comments

Sign in to comment.

Accepted Answer

David Goodmanson
David Goodmanson on 15 Oct 2020
Hi Jiali,
with an even number of points the symmetry is not quite right. In the frequency domain, for eight points (without any fftshift), the array points correspond to frequencies
[0 1 2 3 ny -3 -2 -1]
there is the dc term, then all the other frequencies pair up, but there is one extra point, the nyquist point. Its frequency can be either +- 4 depending on how you look at it. 4 and -4 are equivalent, and transform over to the time domain as a wave oscillating at maximum possible frequency, alternating between +1 and -1 with the array elements. Furthermore, the nyquist point produces a real-valued wave, no imaginary part (the other frequecies, except for f=0, produce complex-valued waves);
Now reverse the roles of f and t. A nonzero value in f(t) at the nyquist point is going to give a real-valued oscillation between +1 and -1 in the frequency plot. In the sine case you are expecting imaginary values which you mostly get. But you also get real values due to the time-domain nyquist point, and that is what you see in the frequency plot. In the cosine case you are expecting real values which you get. You also get a real contribution from the time-domain nyquist point, but the important thing is no imaginary contribution. So cos works, sine doesn't work.
The easy way out is to go to an odd number of points. Then for example you have
[0 1 2 3 -3 -2 -1]
The frequencies pair up and there is no nyquist point. The code below uses 129 points and you should like the result. The t array is changed appropriately. Note the use of ifftshift, not fftshift. ifftshift drops the t=0 point down to the beginning of the array. fftshift takes the t=0 point back to the middle of the array. For an even number of points fftshift and ifftshift are identical.. For an odd number of points, they are inverses but not identical.
n = 129;
%t=[-n/2:n/2-1]*(10)/(n/2);
t=[-(n-1)/2:(n-1)/2]*(10)/(n/2); % changed
f=@(x)sin(2*x).*exp(-x.^2/30);
%f=@(x)cos(2*x).*exp(-x.^2/30);
xt=f(t);
xf=fft(ifftshift(xt)); % need ifftshift
%
samp=1/(t(2)-t(1));
freq=[0:(n-1)]/n*samp;
% fft of sin-modulated Gaussian is not correct since real(xf)~=0, why?
% fft of cos-modulated Gaussian is correct since imag(xf)==0
figure(1)
subplot(1,3,1);
plot(freq,abs(xf));
subplot(1,3,2);
plot(freq,real(xf));
subplot(1,3,3);
plot(freq,imag(xf));

  3 Comments

Jiali
Jiali on 16 Oct 2020
Dear David,
Your detailed answer totally solved my puzzles. I really apprieciate your nice help.
Best Regards,
Jiali
Jiali
Jiali on 16 Oct 2020
Dear David,
From your description, I think that Nyquist point doesn't exist for odd number of points. In the frequency domain, for nine points (without any fftshift), the array points correspond to frequencies [0 1 2 3 4 -4 -3 -2 -1] . Is my understanding correct?
Regards,
Jiali

Sign in to comment.

More Answers (0)

Products


Release

R2015a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!