how to transform frequency domain into time domain

Ambbie picture Ambbie · Dec 26, 2012 · Viewed 11.4k times · Source

I had created a 3 three different frequency signal and filter out the signal i don't want. But when i using ifft in matlab, it shows a wrong graph.How to transform my frequency domain spectrum back into my 3 second time domain graph? Below my code is as below:

clc
clear all
Fs = 8192;
T = 1/Fs;                                             
%create tones with different frequency
t=0:T:1;
t2=1:T:2;
t3=2:T:3;

y1 = sin(2*pi*220*t); 
y2 = sin(2*pi*300*t2);
y3 = sin(2*pi*440*t3);                          

at=y1+y2+y3;


figure;
plot(t,y1,t2,y2,t3,y3),title('Tones with noise');

[b,a]=butter(2,[2*290/Fs,2*350/Fs],'stop');
e=filter(b,a,at);
et=(ifft(abs(e)));
figure,
plot(et)

Answer

bla picture bla · Dec 26, 2012

As it is now, et is in the frequency domain, because of the fft. You don't need to fft. just plot(e) and you'll get the time domain filtered waveform. Yo can check the filter performance in the freq. domain by fft though, just

plot(abs(fftshift(fft(fftshift(e)))));
xlim([4000 5000])

Edit:

Your code as it is written on the question has the following bug: at has exactly 1 second of info in it (or 8192 elements). If you plot(at) you'll see the sum of frequencies alright, but they all happen in the same time. This is how to fix it:

clear all
Fs = 8192; % or multiply by 3 if needed
T = 1/Fs;                                             
%create tones with different frequency
t=0:T:3;

y1 = sin(2*pi*220*t).*(t<1); 
y2 = sin(2*pi*300*t).*(t<2 & t>=1);
y3 = sin(2*pi*440*t).*(t>=2);                          

at=y1+y2+y3;

[b,a]=butter(2,[2*290/Fs,2*350/Fs],'stop');
e=filter(b,a,at);
figure,
plot(t,e)

enter image description here

dt=t(2)-t(1);
N=length(at);
df=1/(N*dt);        % the frequency resolution (df=1/max_T)
if mod(N,2)==0
    f_vector= df*((1:N)-1-N/2);     % frequency vector for EVEN length vectors: f =[-f_max,-f_max+df,...,0,...,f_max-df]
else
    f_vector= df*((1:N)-0.5-N/2);   % frequency vector for ODD length vectors f =[-f_max,-f_max+fw,...,0,...,f_max]
end
freq_vec=f_vector;
fft_vec=fftshift(fft(e));

plot(freq_vec,abs(fft_vec))
xlim([0 1000])

enter image description here