How can i use the ifft() function properly?

조회 수: 23 (최근 30일)
Andres Diago Matta
Andres Diago Matta 2021년 9월 11일
편집: Andres Diago Matta 2021년 9월 11일
I have a siganl xt, defined between -2 and 1, so I use fft(xt, 20*length(xt)) to get the signal xt in frecuency, but when i use ifft, i cannot plot again the signal properly, because it is not in the original limits between -2 and 1.
clear
close all
ts=0.01;
fs=1/ts;
t=-10:ts:10;
xt=((t>=-2)&(t<=1)).*((2/(3))*(t+2));
plot(t,xt, 'b','LineWidth',2), grid;
title("\fontsize{15} x(t)"), xlim([-4 4]),
xlabel("\fontsize{15} t"), ylabel("\fontsize{15} x(t)");
xf=fft(xt, 20*length(xt));
xf=fftshift(xf);
f=linspace(-0.5*fs, 0.5*fs,length(xf));
plot(f,abs(xf)/fs,'b','LineWidth',2), grid, title("\fontsize{15} fft(x(t))");
xlim([-3 3]), xlabel("f"), ylabel("| x(f) |"), ylim([0 3])
xt_ifft = fftshift(ifft(ifftshift(xf)));
xt_ifft=abs(xt_ifft);
t1=linspace(-5, 5,length(xt_ifft));
plot(t1, xt_ifft ,'b','LineWidth',2), grid ;
title(" x(t) from ifft ");
from ifft i get
but i need it between -2 and 1
  댓글 수: 2
Julius Muschaweck
Julius Muschaweck 2021년 9월 11일
why do you call fft with (20*length(xt), which will just add a lot of zeros at the end of your xt array?
Andres Diago Matta
Andres Diago Matta 2021년 9월 11일
I call fft with (20*length(xt), because this way the function fft in matlab performe a better calculation, so i can do a better test of the signal, when i use filters or i add noise to the signal

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

채택된 답변

David Goodmanson
David Goodmanson 2021년 9월 11일
Hi Andres,
Shifting a function in time by t0 produces a factor of exp(2 pi i f t0) in the frequency domain. In this case it's best to let that phase factor keep track of any time shift.
ts=0.01;
fs=1/ts;
t=-10:ts:10;
xt=((t>=-2)&(t<=1)).*((2/(3))*(t+2));
xtnew = ifft(fft(xt));
figure(1)
plot(t,xt,t,xtnew+.1) % add .1, otherwise you have an overlay
Of course this has to work since ifft(fft(z)) always gives back what you started with.
If you want to pad the time function before doing the fft (for example to get finer spacing in the frequency domain), then you can still let fft and ifft keep track of the shifts. ffts and iffts are oriented toward the first element of their respective arrays. You just need a new time array of the correct spacing, and that starts at -10 just like the old one:
xf = fft(xt, 20*length(xt)); % fft pads out xt function
xtnew = ifft(xf);
tnew = (0:length(xf)-1)*ts -10; % tnew(1) = -10
figure(2)
plot(t,xt,tnew,(ifft(xf))+.1)
xlim([-10 10])
  댓글 수: 1
Andres Diago Matta
Andres Diago Matta 2021년 9월 11일
편집: Andres Diago Matta 2021년 9월 11일
Hi David, thanks a lot, that's just what i was looking for.
So, there´s no need to use fftshift before fft?, because that don´t allow the phase factor to keep a track to time shifts rigth?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matched Filter and Ambiguity Function에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by