Why am I getting complex values in a function after IFFT function?

I have taken a simple function (the code below) for illustration to understand my real MATLAB code. I was taught that when we do an FFT of any function (denoted by y_ft), we get the function in a frequency domain that has complex values (e.g.,:a+ib). After taking the IFFT (variable: y_ifft), I should get the function value with only real numbers (just a's). But in the below code, my y_ifft has still complex values. Why is that? Since the Fourier analysis concepts are new learning to me, I am missing some concept here. Answers are appreciable.
f0=4; % frquency of a sine wave
Fs=100; % sampling rate
dt=1/Fs; %sampling interval
t=0:dt:1-dt; % time axis
n=length(t); %number of samples
y=2*sin(2*pi*f0*t); %sine function
figure()
plot(t,y);
y_ft=fft(y);
y_ifft=ifft(y);
figure()
stem(abs(y_ft),'*');

 채택된 답변

Star Strider
Star Strider 2018년 1월 13일
‘I am missing some concept here.’
No. You simply made a typographical error in your code. You are getting complex values because you are not taking the inverse Fourier transform of your Fourier-transformed variable, but the Fourier transform of your original signal:
Your code as posted:
y_ifft=ifft(y);
Probably what you intend:
y_ifft=ifft(y_ft);
That produced pure real numbers whan I ran it.

댓글 수: 9

Thanks for your answer. My bad 'typo error'. Anyway, I will remove the question as it makes less sense now.
My pleasure.
It certainly makes sense. Such observations are important.
If my Answer helped you solve your problem, please Accept it!
DON'T REMOVE THE QUESTION.
Hello Star strider,
In the above code, it is type error and that gives me complex values. But in my original code, where I take the fft of a function- u(M x N) and while taking ifft, the result I get still contains complex values. Is that how fft algorithm works? or I am making a mistake somewhere?. Please share your views.
I am guessing here, because I do not have your ‘original code’. It is quite possible that taking the inverse Fourier transform of the Fourier transform of a more complicated expression results in very small imaginary values (on the order of eps, or ‘machine epsilon’) that result from the inherent inaccuracies of floating-point computation.
Thanks for your response. Yes you are indeed right. After doing the ifft of a fourier transformed function, I get values like (1.1070e-14 + 1.6292235e-25i) where the real part is in the order of e-14 and imaginary part is in the order e-25, which is really small. But earlier I got both the real and imaginary values in the same order. Then would that be wrong?.
It depends on what ‘the same order’ is. If very small, again on the order of eps, the true value could simply be zero. If much larger, then I have no idea without seeing your code. I suspect however that you would not be taking the inverse Fourier transform of what you believe you are.
I will bear your comments in mind next time I get the same results. Thanks for your help. I would highly appreciate if you can answer my another question.
https://nl.mathworks.com/matlabcentral/answers/379794-how-to-define-the-spatial-frequency-in-matlab
As always, my pleasure.
I saw your other Question. I am not familiar enough with the physics you describe to figure out the best way to approach it. (My background is biomedical engineering, emphasizing instrumentation, signal processing, and control theory.) There are people amongst the Contributors here, and in MathWorks, who will likely read it and advise you.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Fourier Analysis and Filtering에 대해 자세히 알아보기

태그

질문:

2018년 1월 13일

댓글:

2018년 1월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by