How to execute fft and ifft

조회 수: 105 (최근 30일)
Maxilyn Tan
Maxilyn Tan 2015년 8월 11일
댓글: yusra Ch 2020년 3월 12일
I tried to execute this code but for some reason the graph of the ifft is not a perfect sine wave. Can someone please instruct me as to what I did wrong? I got the fft code from the internet and I want to get the ifft
Main Code
fo = 4; %frequency of the sinewave
Fs = 100; %sampling rate
Ts = 1/Fs; %sampling time interval
t = 0:Ts:1-Ts;
n = length(t); %number of samples
y = sin(2*pi*fo*t);
figure(1)
plot(t,y)
grid on
[YfreqD,freqRng] = positiveFFT(y,Fs);
figure(2)
stem(freqRng,abs(YfreqD));
B = ifft(YfreqD)*length(y);
t1 = (0:(1-Ts)/(length(B)-1):1-Ts);
figure(3)
plot(t1,B)
grid on
Positive fft code
function[X,freq] = positiveFFT(x,Fs)
N = length(x);
k = 0:N-1;
T = N/Fs;
freq = k/T; %create the frequency range
X = fft(x)/N; %normalize the data
cutOff = ceil(N/2);
X = X(1:cutOff);
freq = freq(1:cutOff);

채택된 답변

David Sanchez
David Sanchez 2015년 8월 11일
Hi thereb, the sine wave is right there, but the problem is that after FFTing the your y data, YfreqD contains half the samples than the original. When calculating the iFFT, that length is kept and plot(t1,B) plots half the original data.
You can try plot like this to see the points you are plotting in each case:
figure(1)
plot(t,y,t,y,'r*')
figure(3)
plot(t1,B,t1,B,'r*')
You can add a higher sampling rate at the beginning of your code and your figure(3) will appear like a less "straight" sine wave.
fo = 4; %frequency of the sinewave
Fs = 200; %sampling rate
Ts = 1/Fs; %sampling time interval
t = 0:Ts:1-Ts;
n = length(t); %number of samples
y = sin(2*pi*fo*t);
figure(1)
plot(t,y,t,y,'r*')
grid on
[YfreqD,freqRng] = positiveFFT(y,Fs);
figure(2)
stem(freqRng,abs(YfreqD));
B = ifft(YfreqD)*length(y);
t1 = (0:(1-Ts)/(length(B)-1):1-Ts);
figure(3)
plot(t1,B,t1,B,'r*')
grid on
  댓글 수: 2
Maxilyn Tan
Maxilyn Tan 2015년 8월 19일
I see what you mean, thank you very much!
yusra Ch
yusra Ch 2020년 3월 12일
Could you please help me with this
My idea is to generate two diffrent sinus signal with diffrent frequencies f1 and f2. One generated I want to do the sum of them (let's call it total) . Next, I want t filter the total signal and recuperate the signal that has frequency equal to f1.
I have created wave1 with f1=30Hz and wave2 with f2=60Hz. I did their sum "total".
The next step is to recuperate wave1 from the "total". I did the FFT of the total. I got two peaks one in 30Hz and the other in 60Hz. Next, I have filtered out (Low pass filter) the wave2. Now that I got the peak corresponding to wave1 (30Hz). My question is how to do the ifft in a right way to get the wave1 back. My code gives my a signal diffrent to wave1 (see the pictures) I dont know where is the problem
>>t=0:1/1000:0.1;
>> f1=30;
>> T1=1/f1;
>> f2=60;
>> T2=1/f2;
>> wave1=sin(2*pi*t/T1);
>> wave2=sin(2*pi*t/T2);
>> total=wave1+wave2;
>> xdft3=fft(total);
>> frq3=0:1000/length(total):1000/2;
>> xdftPos3=xdft3(1:length(total)/2+1);
>> xdftNorm3=xdftPos3/max(xdftPos3);
>> figure
>> plot(frq3,abs(xdftNorm3))
>> XX=(find(frq3>31)); % Low pass filter to recuperate the wave1
>> xdft4=xdft3;
>> xdft4(XX)=0;
>> invers3=ifft(xdft4);
>> figure
>> plot(t,invrs3)

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

추가 답변 (1개)

Shaohui Yong
Shaohui Yong 2017년 7월 24일
This is really helpful, thanks!
  댓글 수: 1
Venkat Ta
Venkat Ta 2017년 10월 31일
Thanks. The code is fine, is there any possible chance to get no complex values of B sine wave same like as input y ?
suppose if I plot like as plot(t,y,'b',t1,real(B),'r--') with Fs=200; there is so much differences between input Y and ifft B.
Is it good choice to take real(B)?

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

카테고리

Help CenterFile Exchange에서 Fourier Analysis and Filtering에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by