Thread Subject: Implementation of FFT and IFFT in Matlab

Subject: Implementation of FFT and IFFT in Matlab

From: Saurabh

Date: 18 Mar, 2010 10:37:03

Message: 1 of 13

Hello

I have a rectangular function defined as follows
tstop = 10E-3;
t=-0.1:dt:0.1;
p_i=(1/2)*(sign(t)-sign(t-tstop));
input = figure;
plot(t,p_i)
rge = 0.05;
axis([-rge rge 0 1]);
xlabel('time')
ylabel('amplitude')
title('Input step function')

Then I take a Centered FFT

[pw,frequencyRange] = centeredFFT(p_i,1/dt);
positiveFFT = figure;
stem(frequencyRange,abs(pw));
% set(positiveFFT,'Position',[500,500,500,300])
xlabel('Freq (Hz)')
ylabel('Amplitude')
title('Using the positiveFFT function')
grid
axis([0 500 0 0.05]);
xlabel('frequency')
ylabel('amplitude')
title('FFT of the input step function')

where the function centeredFFT is

function [X,freq]=centeredFFT(x,Fs)
%this is a custom function that helps in plotting the two-sided spectrum
%x is the signal that is to be transformed
%Fs is the sampling rate

N=length(x);

%this part of the code generates that frequency axis
if mod(N,2)==0
    k=-N/2:N/2-1; % N even
else
    k=-(N-1)/2:(N-1)/2; % N odd
end
T=N/Fs;
freq=k/T; %the frequency axis

%takes the fft of the signal, and adjusts the amplitude accordingly
X=fft(x)/N; % normalize the data
X=fftshift(X); %shifts the fft data so that it is centered

Now I want to take IFFT of the function but i do not get back the same input signal

orignal_signal = ifft(YfreqDomain);
time = ifft(frequencyRange);
reterived = figure
plot( time,orignal_signal)
in

Could any one guide in simple terms where I am wrong ?

I also tried this

orignal_signal = ifft(ifftshift(YfreqDomain));
time = ifft(ifftshift(frequencyRange));
reterived = figure
plot( time,orignal_signal)

but could not reterive back original signal

Subject: Implementation of FFT and IFFT in Matlab

From: Rune Allnor

Date: 18 Mar, 2010 10:40:32

Message: 2 of 13

On 18 Mar, 11:37, "Saurabh " <saura...@student.ethz.ch> wrote:

> but could not reterive back original signal

Try this:

N = 100;
x = randn(N,1);
X = fft(x);
y = real(ifft(X));
d = max(abs(y-x))

d should now contain a very small number (on the order
of 1e-15), which means that y == x, except for numerical
round-off.

Rune

Subject: Implementation of FFT and IFFT in Matlab

From: Wayne King

Date: 18 Mar, 2010 10:54:03

Message: 3 of 13

"Saurabh " <saurabhs@student.ethz.ch> wrote in message <hnsvof$2f0$1@fred.mathworks.com>...
> Hello
>
> I have a rectangular function defined as follows
> tstop = 10E-3;
> t=-0.1:dt:0.1;
> p_i=(1/2)*(sign(t)-sign(t-tstop));
> input = figure;
> plot(t,p_i)
> rge = 0.05;
> axis([-rge rge 0 1]);
> xlabel('time')
> ylabel('amplitude')
> title('Input step function')
>
> Then I take a Centered FFT
>
> [pw,frequencyRange] = centeredFFT(p_i,1/dt);
> positiveFFT = figure;
> stem(frequencyRange,abs(pw));
> % set(positiveFFT,'Position',[500,500,500,300])
> xlabel('Freq (Hz)')
> ylabel('Amplitude')
> title('Using the positiveFFT function')
> grid
> axis([0 500 0 0.05]);
> xlabel('frequency')
> ylabel('amplitude')
> title('FFT of the input step function')
>
> where the function centeredFFT is
>
> function [X,freq]=centeredFFT(x,Fs)
> %this is a custom function that helps in plotting the two-sided spectrum
> %x is the signal that is to be transformed
> %Fs is the sampling rate
>
> N=length(x);
>
> %this part of the code generates that frequency axis
> if mod(N,2)==0
> k=-N/2:N/2-1; % N even
> else
> k=-(N-1)/2:(N-1)/2; % N odd
> end
> T=N/Fs;
> freq=k/T; %the frequency axis
>
> %takes the fft of the signal, and adjusts the amplitude accordingly
> X=fft(x)/N; % normalize the data
> X=fftshift(X); %shifts the fft data so that it is centered
>
> Now I want to take IFFT of the function but i do not get back the same input signal
>
> orignal_signal = ifft(YfreqDomain);
> time = ifft(frequencyRange);
> reterived = figure
> plot( time,orignal_signal)
> in
>
> Could any one guide in simple terms where I am wrong ?
>
> I also tried this
>
> orignal_signal = ifft(ifftshift(YfreqDomain));
> time = ifft(ifftshift(frequencyRange));
> reterived = figure
> plot( time,orignal_signal)
>
> but could not reterive back original signal

Hi, if you want the signal back, why not just

% you never give us dt by the way
tstop = 10E-3;
dt=1e-3;
t=-0.1:dt:0.1;
p_i=(1/2)*(sign(t)-sign(t-tstop));
 Pdft = fft(p_i);
p_m=ifft(Pdft,'symmetric');
norm(p_m-p_i)

Wayne

Subject: Implementation of FFT and IFFT in Matlab

From: Saurabh

Date: 18 Mar, 2010 11:20:05

Message: 4 of 13

well sorry for missing dt which is 1E-6..... I am new to matlab

but the thing is that if I just use the code

Pdft = fft(p_i);
plot (abs(Pdft))
     or
Pdft = fft(p_i);
stem(abs(Pdft))

I do not get what is expected so I have not used your remaining code since Fourier transform is not right . Because my input signal is a box card function and mathematically i know what its output is going to be that is a sinc function and the way i am using centeredFFT i do get a sync function .

I need to use FFT and IFFT in my program so that is why i am looking to how to use them correctly hence making a simple test program that i put up where I have a input boxcar and by taking its Fourier Transform I do get a sync function now to see how ifft works i should get back box car function . But i do not .

the suggestion which you gave does not convince me since Pdft is not a sync function and x axis give me no information about the frequency .

Could you elaborate your answer ?

and suggest me possibly How could I recover back Box car after doing ifft with the sync function

Regards



"Wayne King" <wmkingty@gmail.com> wrote in message <hnt0ob$gvm$1@fred.mathworks.com>...
> "Saurabh " <saurabhs@student.ethz.ch> wrote in message <hnsvof$2f0$1@fred.mathworks.com>...
> > Hello
> >
> > I have a rectangular function defined as follows
> > tstop = 10E-3;
> > t=-0.1:dt:0.1;
> > p_i=(1/2)*(sign(t)-sign(t-tstop));
> > input = figure;
> > plot(t,p_i)
> > rge = 0.05;
> > axis([-rge rge 0 1]);
> > xlabel('time')
> > ylabel('amplitude')
> > title('Input step function')
> >
> > Then I take a Centered FFT
> >
> > [pw,frequencyRange] = centeredFFT(p_i,1/dt);
> > positiveFFT = figure;
> > stem(frequencyRange,abs(pw));
> > % set(positiveFFT,'Position',[500,500,500,300])
> > xlabel('Freq (Hz)')
> > ylabel('Amplitude')
> > title('Using the positiveFFT function')
> > grid
> > axis([0 500 0 0.05]);
> > xlabel('frequency')
> > ylabel('amplitude')
> > title('FFT of the input step function')
> >
> > where the function centeredFFT is
> >
> > function [X,freq]=centeredFFT(x,Fs)
> > %this is a custom function that helps in plotting the two-sided spectrum
> > %x is the signal that is to be transformed
> > %Fs is the sampling rate
> >
> > N=length(x);
> >
> > %this part of the code generates that frequency axis
> > if mod(N,2)==0
> > k=-N/2:N/2-1; % N even
> > else
> > k=-(N-1)/2:(N-1)/2; % N odd
> > end
> > T=N/Fs;
> > freq=k/T; %the frequency axis
> >
> > %takes the fft of the signal, and adjusts the amplitude accordingly
> > X=fft(x)/N; % normalize the data
> > X=fftshift(X); %shifts the fft data so that it is centered
> >
> > Now I want to take IFFT of the function but i do not get back the same input signal
> >
> > orignal_signal = ifft(YfreqDomain);
> > time = ifft(frequencyRange);
> > reterived = figure
> > plot( time,orignal_signal)
> > in
> >
> > Could any one guide in simple terms where I am wrong ?
> >
> > I also tried this
> >
> > orignal_signal = ifft(ifftshift(YfreqDomain));
> > time = ifft(ifftshift(frequencyRange));
> > reterived = figure
> > plot( time,orignal_signal)
> >
> > but could not reterive back original signal
>
> Hi, if you want the signal back, why not just
>
> % you never give us dt by the way
> tstop = 10E-3;
> dt=1e-3;
> t=-0.1:dt:0.1;
> p_i=(1/2)*(sign(t)-sign(t-tstop));
> Pdft = fft(p_i);
> p_m=ifft(Pdft,'symmetric');
> norm(p_m-p_i)
>
> Wayne

Subject: Implementation of FFT and IFFT in Matlab

From: Matt J

Date: 18 Mar, 2010 11:37:04

Message: 5 of 13

A few comments about things that could be contributing to the problem:



> %takes the fft of the signal, and adjusts the amplitude accordingly
> X=fft(x)/N; % normalize the data
===============

This doesn't seem like an appropriate normalization. If you'e trying to approximate the continuous Fourier transform, the appropriate normalization would be

X=fft(x)*Fs; %Equation 1

Also, if you invert Equation 1, you get

x=ifft(X)/Fs; %Equation 2

which shows that later, when you take the ifft, you have to undo the normalization you did in Equation 1 to get the original signal back again.



> X=fftshift(X); %shifts the fft data so that it is centered
>
> Now I want to take IFFT of the function but i do not get back the same input signal
>
> orignal_signal = ifft(YfreqDomain);
> time = ifft(frequencyRange);
=================

I'm baffled as to why you think you can or need to use the IFFT to generate the time axis. You already created a time axis, t, early in the code.

Subject: Implementation of FFT and IFFT in Matlab

From: Wayne King

Date: 18 Mar, 2010 11:37:05

Message: 6 of 13

"Saurabh " <saurabhs@student.ethz.ch> wrote in message <hnt295$al5$1@fred.mathworks.com>...
> well sorry for missing dt which is 1E-6..... I am new to matlab
>
> but the thing is that if I just use the code
>
> Pdft = fft(p_i);
> plot (abs(Pdft))
> or
> Pdft = fft(p_i);
> stem(abs(Pdft))
>
> I do not get what is expected so I have not used your remaining code since Fourier transform is not right . Because my input signal is a box card function and mathematically i know what its output is going to be that is a sinc function and the way i am using centeredFFT i do get a sync function .
>
> I need to use FFT and IFFT in my program so that is why i am looking to how to use them correctly hence making a simple test program that i put up where I have a input boxcar and by taking its Fourier Transform I do get a sync function now to see how ifft works i should get back box car function . But i do not .
>
> the suggestion which you gave does not convince me since Pdft is not a sync function and x axis give me no information about the frequency .
>
> Could you elaborate your answer ?
>
> and suggest me possibly How could I recover back Box car after doing ifft with the sync function
>
> Regards
>
>
>
> "Wayne King" <wmkingty@gmail.com> wrote in message <hnt0ob$gvm$1@fred.mathworks.com>...
> > "Saurabh " <saurabhs@student.ethz.ch> wrote in message <hnsvof$2f0$1@fred.mathworks.com>...
> > > Hello
> > >
> > > I have a rectangular function defined as follows
> > > tstop = 10E-3;
> > > t=-0.1:dt:0.1;
> > > p_i=(1/2)*(sign(t)-sign(t-tstop));
> > > input = figure;
> > > plot(t,p_i)
> > > rge = 0.05;
> > > axis([-rge rge 0 1]);
> > > xlabel('time')
> > > ylabel('amplitude')
> > > title('Input step function')
> > >
> > > Then I take a Centered FFT
> > >
> > > [pw,frequencyRange] = centeredFFT(p_i,1/dt);
> > > positiveFFT = figure;
> > > stem(frequencyRange,abs(pw));
> > > % set(positiveFFT,'Position',[500,500,500,300])
> > > xlabel('Freq (Hz)')
> > > ylabel('Amplitude')
> > > title('Using the positiveFFT function')
> > > grid
> > > axis([0 500 0 0.05]);
> > > xlabel('frequency')
> > > ylabel('amplitude')
> > > title('FFT of the input step function')
> > >
> > > where the function centeredFFT is
> > >
> > > function [X,freq]=centeredFFT(x,Fs)
> > > %this is a custom function that helps in plotting the two-sided spectrum
> > > %x is the signal that is to be transformed
> > > %Fs is the sampling rate
> > >
> > > N=length(x);
> > >
> > > %this part of the code generates that frequency axis
> > > if mod(N,2)==0
> > > k=-N/2:N/2-1; % N even
> > > else
> > > k=-(N-1)/2:(N-1)/2; % N odd
> > > end
> > > T=N/Fs;
> > > freq=k/T; %the frequency axis
> > >
> > > %takes the fft of the signal, and adjusts the amplitude accordingly
> > > X=fft(x)/N; % normalize the data
> > > X=fftshift(X); %shifts the fft data so that it is centered
> > >
> > > Now I want to take IFFT of the function but i do not get back the same input signal
> > >
> > > orignal_signal = ifft(YfreqDomain);
> > > time = ifft(frequencyRange);
> > > reterived = figure
> > > plot( time,orignal_signal)
> > > in
> > >
> > > Could any one guide in simple terms where I am wrong ?
> > >
> > > I also tried this
> > >
> > > orignal_signal = ifft(ifftshift(YfreqDomain));
> > > time = ifft(ifftshift(frequencyRange));
> > > reterived = figure
> > > plot( time,orignal_signal)
> > >
> > > but could not reterive back original signal
> >
> > Hi, if you want the signal back, why not just
> >
> > % you never give us dt by the way
> > tstop = 10E-3;
> > dt=1e-3;
> > t=-0.1:dt:0.1;
> > p_i=(1/2)*(sign(t)-sign(t-tstop));
> > Pdft = fft(p_i);
> > p_m=ifft(Pdft,'symmetric');
> > norm(p_m-p_i)
> >
> > Wayne

Hi, you have to keep in mind that the Fourier transform of a sequence is periodic, so when you say with:

Pdft = fft(p_i);
plot(abs(Pdft));

You're not seeing sinc-like behavior, that is not correct. You are seeing the sinc-like behavior, you're seeing one period of it. When you use fftshift, you're still seeing one period, you're just looking at the period from a different starting point to ending point. But, it's still the same. Visualize for yourself the periodic extensions--no difference.

Wayne

Wayne

Subject: Implementation of FFT and IFFT in Matlab

From: Saurabh

Date: 18 Mar, 2010 11:38:04

Message: 7 of 13

Hi Rune

Thanks for your reply.

But i wonder if i take a some function lets say sine just for case to make myself convinced ..
fo = 4; %frequency of the sine wave
Fs = 100; %sampling rate
Ts = 1/Fs; %sampling time interval
t = 0:Ts:1-Ts; %sampling period
n = length(t); %number of samples
y = 2*sin(2*pi*fo*t); %the sine curve


and if you do its Fourier transform using fft

YfreqDomain = fft(y); %take the fft of our sin wave, y(t)

stem(abs(YfreqDomain));

This doesn’t quite look like what we predicted above. If you notice, there are a couple of things that are missing.
The x-axis gives us no information on the frequency. How can we tell that the peaks are in the right place?
The amplitude is all the way up to 100
The spectrum is not centered around zero

All above goes courtesy to a blog by blinkdagger

I am stuck in making some sort of same routine for ifft so that now when I do ifft i could reterive back the starting function ?

Regards
Rune Allnor <allnor@tele.ntnu.no> wrote in message <cc7fb6b1-ff78-460e-bef2-685856aa44b5@d27g2000yqf.googlegroups.com>...
> On 18 Mar, 11:37, "Saurabh " <saura...@student.ethz.ch> wrote:
>
> > but could not reterive back original signal
>
> Try this:
>
> N = 100;
> x = randn(N,1);
> X = fft(x);
> y = real(ifft(X));
> d = max(abs(y-x))
>
> d should now contain a very small number (on the order
> of 1e-15), which means that y == x, except for numerical
> round-off.
>
> Rune

Subject: Implementation of FFT and IFFT in Matlab

From: Matt J

Date: 18 Mar, 2010 12:12:04

Message: 8 of 13

"Saurabh " <saurabhs@student.ethz.ch> wrote in message <hnt3as$qm0$1@fred.mathworks.com>...
> Hi Rune
>
> Thanks for your reply.
>
> But i wonder if i take a some function lets say sine just for case to make myself convinced ..
> fo = 4; %frequency of the sine wave
> Fs = 100; %sampling rate
> Ts = 1/Fs; %sampling time interval
> t = 0:Ts:1-Ts; %sampling period
> n = length(t); %number of samples
> y = 2*sin(2*pi*fo*t); %the sine curve
>
>
> and if you do its Fourier transform using fft
>
> YfreqDomain = fft(y); %take the fft of our sin wave, y(t)
==============


Again, this should be normalized by Fs,

YfreqDomain = fft(y)/Fs; %Also, shift it for plotting purposes

Also you need to set up your frequency axis, and apply fftshift to center the plot

N=n;
faxis=( -ceil((N-1)/2):N-1-ceil((N-1)/2) )/N/Ts;
stem(faxis,fftshift(abs(YfreqDomain)));

Subject: Implementation of FFT and IFFT in Matlab

From: Saurabh

Date: 18 Mar, 2010 12:47:05

Message: 9 of 13

Hello Matt

Thanks for your reply .

I run this test code finally to convince myself

fo = 4; %frequency of the sine wave
Fs = 100; %sampling rate
Ts = 1/Fs; %sampling time interval
t = 0:Ts:1-Ts; %sampling period
n = length(t); %number of samples
y = 2*sin(2*pi*fo*t); %the sine curve
figure(1)
plot(t,y)
YfreqDomain = fft(y)./n; %take the fft of our sin wave, y(t)
N=n;
faxis=( -ceil((N-1)/2):N-1-ceil((N-1)/2) )/N/Ts;
figure(2)
stem(faxis,fftshift(abs(YfreqDomain)));

y_ret = ifft(YfreqDomain).*n;
figure(3)
plot(t,y_ret)

The points I wish to make

1. you suggested to replace N which was length of signal by Fs but i found in the code above no difference.
Hence i think normalizing by length of signal makes more meaning

2. Now i can reterive back the signal well It would be instructive if you can point me the difference between DTFT and DFT with some simple example which is implemented in matlab to apreciate the difference

Regards

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hnt5ak$sr4$1@fred.mathworks.com>...
> "Saurabh " <saurabhs@student.ethz.ch> wrote in message <hnt3as$qm0$1@fred.mathworks.com>...
> > Hi Rune
> >
> > Thanks for your reply.
> >
> > But i wonder if i take a some function lets say sine just for case to make myself convinced ..
> > fo = 4; %frequency of the sine wave
> > Fs = 100; %sampling rate
> > Ts = 1/Fs; %sampling time interval
> > t = 0:Ts:1-Ts; %sampling period
> > n = length(t); %number of samples
> > y = 2*sin(2*pi*fo*t); %the sine curve
> >
> >
> > and if you do its Fourier transform using fft
> >
> > YfreqDomain = fft(y); %take the fft of our sin wave, y(t)
> ==============
>
>
> Again, this should be normalized by Fs,
>
> YfreqDomain = fft(y)/Fs; %Also, shift it for plotting purposes
>
> Also you need to set up your frequency axis, and apply fftshift to center the plot
>
> N=n;
> faxis=( -ceil((N-1)/2):N-1-ceil((N-1)/2) )/N/Ts;
> stem(faxis,fftshift(abs(YfreqDomain)));

Subject: Implementation of FFT and IFFT in Matlab

From: Wayne King

Date: 18 Mar, 2010 13:57:05

Message: 10 of 13

"Saurabh " <saurabhs@student.ethz.ch> wrote in message <hnt7c9$1od$1@fred.mathworks.com>...
> Hello Matt
>
> Thanks for your reply .
>
> I run this test code finally to convince myself
>
> fo = 4; %frequency of the sine wave
> Fs = 100; %sampling rate
> Ts = 1/Fs; %sampling time interval
> t = 0:Ts:1-Ts; %sampling period
> n = length(t); %number of samples
> y = 2*sin(2*pi*fo*t); %the sine curve
> figure(1)
> plot(t,y)
> YfreqDomain = fft(y)./n; %take the fft of our sin wave, y(t)
> N=n;
> faxis=( -ceil((N-1)/2):N-1-ceil((N-1)/2) )/N/Ts;
> figure(2)
> stem(faxis,fftshift(abs(YfreqDomain)));
>
> y_ret = ifft(YfreqDomain).*n;
> figure(3)
> plot(t,y_ret)
>
> The points I wish to make
>
> 1. you suggested to replace N which was length of signal by Fs but i found in the code above no difference.
> Hence i think normalizing by length of signal makes more meaning
>
> 2. Now i can reterive back the signal well It would be instructive if you can point me the difference between DTFT and DFT with some simple example which is implemented in matlab to apreciate the difference
>
> Regards
>
> "Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hnt5ak$sr4$1@fred.mathworks.com>...
> > "Saurabh " <saurabhs@student.ethz.ch> wrote in message <hnt3as$qm0$1@fred.mathworks.com>...
> > > Hi Rune
> > >
> > > Thanks for your reply.
> > >
> > > But i wonder if i take a some function lets say sine just for case to make myself convinced ..
> > > fo = 4; %frequency of the sine wave
> > > Fs = 100; %sampling rate
> > > Ts = 1/Fs; %sampling time interval
> > > t = 0:Ts:1-Ts; %sampling period
> > > n = length(t); %number of samples
> > > y = 2*sin(2*pi*fo*t); %the sine curve
> > >
> > >
> > > and if you do its Fourier transform using fft
> > >
> > > YfreqDomain = fft(y); %take the fft of our sin wave, y(t)
> > ==============
> >
> >
> > Again, this should be normalized by Fs,
> >
> > YfreqDomain = fft(y)/Fs; %Also, shift it for plotting purposes
> >
> > Also you need to set up your frequency axis, and apply fftshift to center the plot
> >
> > N=n;
> > faxis=( -ceil((N-1)/2):N-1-ceil((N-1)/2) )/N/Ts;
> > stem(faxis,fftshift(abs(YfreqDomain)));

Hi Saurabh,

You don't need to use fftshift() if you don't want to.

fo = 4; %frequency of the sine wave
Fs = 100; %sampling rate
Ts = 1/Fs; %sampling time interval
t = 0:Ts:1-Ts; %sampling period
n = length(t); %number of samples
y = 2*sin(2*pi*fo*t); %the sine curve
freq = 0:(Fs/length(y)):Fs/2;
YfreqDomain = fft(y);
stem(freq,2/length(y)*abs(YfreqDomain(1:length(y)/2+1)))
figure;
 y_ret=ifft(YfreqDomain,'symmetric');
plot(t,y_ret);


In MATLAB you are always computing the DFT, the DTFT is a continuous function of frequency. You are always evaluating the Fourier transform of a sequence on a discrete grid.

Wayne

Subject: Implementation of FFT and IFFT in Matlab

From: Matt J

Date: 18 Mar, 2010 14:29:06

Message: 11 of 13

"Saurabh " <saurabhs@student.ethz.ch> wrote in message <hnt7c9$1od$1@fred.mathworks.com>...

>
> 1. you suggested to replace N which was length of signal by Fs but i found in the code above no difference.
> Hence i think normalizing by length of signal makes more meaning
=============

No, that's only because in this particular example, you had Fs=N. But that only occurs when your sampling period is 1. If instead, you had

Fs=100;
Ts=1/Fs;
t = 0:Ts:2; %sampling period

then you would not have obtained the desired scaling with fft(y)/length(t)

> 2. Now i can reterive back the signal well It would be instructive if you can point me the difference between DTFT and DFT with some simple example which is implemented in matlab to apreciate the difference
=============

As Wayne said, the DTFT is a continuous function of frequency. The DFT is a discrete sampling of the DTFT at frequency intervals 1/N/Ts=Fs/N.

Subject: Implementation of FFT and IFFT in Matlab

From: Greg Heath

Date: 18 Mar, 2010 23:09:40

Message: 12 of 13

On Mar 18, 7:37 am, "Matt J " <mattjacREM...@THISieee.spam> wrote:
> A few comments about things that could be contributing to the problem:
>
> > %takes the fft of the signal, and adjusts the amplitude accordingly
> > X=fft(x)/N; % normalize the data
>
> ===============
>
> This doesn't seem like an appropriate normalization.

Its very understandable:

It gives the amplitude of 1 at frequency f0 when x = exp(i*2*pi*f0*t)

>If you'e trying to approximate the continuous Fourier transform, the
> appropriate normalization would be
>
> X=fft(x)*Fs; %Equation 1
>
> Also, if you invert Equation 1, you get
>
> x=ifft(X)/Fs; %Equation 2

Incorrect.

dt = 1/Fs
N = length(x)
T = N*dt
t = dt*(0:N-1); % t = 0:dt:T-dt;
and
df = Fs/N % Fs = 1/T
f = df*(0:N-1); % f =0:df:Fs-df;

Then

X = dt*fft(x); % Approximate CTFT

and

x = N*(df*ifft(X));


>
> which shows that later, when you take the ifft, you have to undo the
> normalization you did in Equation 1 to get the original signal back again.
>
> > X=fftshift(X); %shifts the fft data so that it is centered
>
> > Now I want to take IFFT of the function but i do not get back the same
> input signal
>
> > orignal_signal = ifft(YfreqDomain);
> > time = ifft(frequencyRange);
>
> =================
>
> I'm baffled as to why you think you can or need to use the IFFT to generate the time axis. You already created a time axis, t, early in the code.

Yes, the time and frequency axes are relatively independent of
ifft and fft. If you choose ANY sequence y with length N, then

Y = fft(y);

which can correspond to ANY positive dt OR any positive df provided

df*dt = 1/N.

Hope this helps.

Greg

Subject: Implementation of FFT and IFFT in Matlab

From: Greg Heath

Date: 19 Mar, 2010 00:04:16

Message: 13 of 13

On Mar 18, 7:38 am, "Saurabh " <saura...@student.ethz.ch> wrote:
> Hi Rune
>
> Thanks for your reply.
>
> But i wonder if i take a some function lets say sine just for case to make myself convinced ..
> fo = 4; %frequency of the sine wave
> Fs = 100; %sampling rate
> Ts = 1/Fs; %sampling time interval
> t = 0:Ts:1-Ts; %sampling period
> n = length(t); %number of samples

Be more careful. That approach has gotten people into trouble:
In general, there is no guarantee that (1-Ts)/Ts is an integer.
I prefer using the notation

dt = 1/Fs
T = N*dt
df = 1/T %( or Fs/N)

Then

t = dt*(0:N-1);
t = 0:dt:T-dt;

and

f = df*(0:N-1);
f= 0:df:Fs-df;

> y = 2*sin(2*pi*fo*t); %the sine curve
>
> and if you do its Fourier transform using fft
>
> YfreqDomain = fft(y); %take the fft of our sin wave, y(t)
>
> stem(abs(YfreqDomain));
>
> This doesn’t quite look like what we predicted above.

That is because you are looking at 0 <= f <= Fs -df. To yield a
centered point of view with an understandable scaling,

X = fft(x)/N;
Xc = fftshift(X);
absXc = abs(Xc);
fc = f - df*floor(N/2);
figure, hold on
plot(fc,absXc)
plot(fc,absXc,'o')


> If you notice, there are a couple of things that are missing.
> The x-axis gives us no information on the frequency.

You have to provide that with

stem(fc,absXc)

>How can we tell that the peaks are in the right place?

In simple cases loke this one, just read it off the plot.
In general, N is large and there can be many peaks
with different levels. The function MAX will only find the first
of the peaks that have the largest value.

To find all isolated peaks you can use

find(absXc(i) > max(absXc(i-1),absXc(i+1)))

> The amplitude is all the way up to 100

Because you didn't normalize with N

> The spectrum is not centered around zero

Because you didn't use Xc and fc

> All above goes courtesy to a blog by blinkdagger
>
> Next time search within the newsgroup
>
> I am stuck in making some sort of same routine for ifft so
> that now when I do ifft i could reterive back the starting function ?

Since X and Xc are complex but x is real,

x = N*real(ifft(X));
 or

x = N*real(ifft( ifftshift(Xc) ));

Hope this helps.

Greg

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com