From: Frank on
Hi guys!

Can you help me with this ifft scaling problem. I put here my simple code to calculate the fft of a time signal and then ifft back into time domain. If you can please check that the code is consistent and the frequency axis and time axis are properly calculated!
PROBLEM:
What I get from ifft is my time function (full of zeros at the end, that I would like to remove before plotting in the time domain) and also the ifft is not properly scaled, as I get 10 times the amplitude of my initial time signal.

close all

dx = 0.01;
xmax = 4;
x = 0:dx:xmax;
y = sin(2*pi*8*x); %+ sin(2*pi*20*x);

N = 2^12;
ty = fft(y,N)/length(x);

fs = 1/dx;
f = (0:(N-1)/2)/N*fs;

aty = real(ifft(ty,N)*N);
nx = length(aty);
xty = 0:1/fs:(nx-1)*1/fs;

figure
plot(f,abs(ty(1:N/2)));
axis('tight');

figure
plot(x,y,'b')
hold on
plot(xty,aty,'r')
axis('tight')

Thank you in advance

Frank
From: Wayne King on
"Frank " <francesco.manni01(a)fastwebnet.it> wrote in message <hn3qim$ste$1(a)fred.mathworks.com>...
> Hi guys!
>
> Can you help me with this ifft scaling problem. I put here my simple code to calculate the fft of a time signal and then ifft back into time domain. If you can please check that the code is consistent and the frequency axis and time axis are properly calculated!
> PROBLEM:
> What I get from ifft is my time function (full of zeros at the end, that I would like to remove before plotting in the time domain) and also the ifft is not properly scaled, as I get 10 times the amplitude of my initial time signal.
>
> close all
>
> dx = 0.01;
> xmax = 4;
> x = 0:dx:xmax;
> y = sin(2*pi*8*x); %+ sin(2*pi*20*x);
>
> N = 2^12;
> ty = fft(y,N)/length(x);
>
> fs = 1/dx;
> f = (0:(N-1)/2)/N*fs;
>
> aty = real(ifft(ty,N)*N);
> nx = length(aty);
> xty = 0:1/fs:(nx-1)*1/fs;
>
> figure
> plot(f,abs(ty(1:N/2)));
> axis('tight');
>
> figure
> plot(x,y,'b')
> hold on
> plot(xty,aty,'r')
> axis('tight')
>
> Thank you in advance
>
> Frank

Hi Frank, your scaling problem comes from the fact that you are dividing the DFT of your input x by the length of x, which is 401, and then you are multiplying the inverse Fourier transform by 2^12, so 2^12/401 accounts for your scaling by approximately 10. Why do you need to pad out so far?

dx = 0.01;
xmax = 4;
x = 0:dx:xmax;
y = sin(2*pi*8*x); %+ sin(2*pi*20*x);YDFT=fft(y);
YDFT = fft(y);
y1 = ifft(YDFT,'symmetric');
plot(x,y,'k'); hold on; plot(x,y1,'b')

Wayne
From: Matt J on
"Frank " <francesco.manni01(a)fastwebnet.it> wrote in message <hn3qim$ste$1(a)fred.mathworks.com>...

> PROBLEM:
> What I get from ifft is my time function (full of zeros at the end, that I would like to remove before plotting in the time domain)
======================

Not sure why that's a difficult problem. Just throw away the zeros and the corresponding points on your axes.



and also the ifft is not properly scaled, as I get 10 times the amplitude of my initial time signal.
>
> close all
>
> dx = 0.01;
> xmax = 4;
> x = 0:dx:xmax;
> y = sin(2*pi*8*x); %+ sin(2*pi*20*x);
>
> N = 2^12;
> ty = fft(y,N)/length(x);
======

should be

ty = fft(y,N)*dx;


>
> fs = 1/dx;
> f = (0:(N-1)/2)/N*fs;
>
> aty = real(ifft(ty,N)*N);
==================

Should be

aty = abs(ifft(ty,N))/dx;
From: Frank on
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <hn3sik$5d1$1(a)fred.mathworks.com>...
> "Frank " <francesco.manni01(a)fastwebnet.it> wrote in message <hn3qim$ste$1(a)fred.mathworks.com>...
>
> > PROBLEM:
> > What I get from ifft is my time function (full of zeros at the end, that I would like to remove before plotting in the time domain)
> ======================
>
> Not sure why that's a difficult problem. Just throw away the zeros and the corresponding points on your axes.
>
>
>
> and also the ifft is not properly scaled, as I get 10 times the amplitude of my initial time signal.
> >
> > close all
> >
> > dx = 0.01;
> > xmax = 4;
> > x = 0:dx:xmax;
> > y = sin(2*pi*8*x); %+ sin(2*pi*20*x);
> >
> > N = 2^12;
> > ty = fft(y,N)/length(x);
> ======
>
> should be
>
> ty = fft(y,N)*dx;
>
>
> >
> > fs = 1/dx;
> > f = (0:(N-1)/2)/N*fs;
> >
> > aty = real(ifft(ty,N)*N);
> ==================
>
> Should be
>
> aty = abs(ifft(ty,N))/dx;

Thank you guys for your help! I have indeed another associated question to ask you. When I had a dc component, let's say I take y = sin(...) + 5 and let the code run, I get an ifft that has a dc component different from 5... Why?
From: Wayne King on
"Frank " <francesco.manni01(a)fastwebnet.it> wrote in message <hn3suf$s80$1(a)fred.mathworks.com>...
> "Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <hn3sik$5d1$1(a)fred.mathworks.com>...
> > "Frank " <francesco.manni01(a)fastwebnet.it> wrote in message <hn3qim$ste$1(a)fred.mathworks.com>...
> >
> > > PROBLEM:
> > > What I get from ifft is my time function (full of zeros at the end, that I would like to remove before plotting in the time domain)
> > ======================
> >
> > Not sure why that's a difficult problem. Just throw away the zeros and the corresponding points on your axes.
> >
> >
> >
> > and also the ifft is not properly scaled, as I get 10 times the amplitude of my initial time signal.
> > >
> > > close all
> > >
> > > dx = 0.01;
> > > xmax = 4;
> > > x = 0:dx:xmax;
> > > y = sin(2*pi*8*x); %+ sin(2*pi*20*x);
> > >
> > > N = 2^12;
> > > ty = fft(y,N)/length(x);
> > ======
> >
> > should be
> >
> > ty = fft(y,N)*dx;
> >
> >
> > >
> > > fs = 1/dx;
> > > f = (0:(N-1)/2)/N*fs;
> > >
> > > aty = real(ifft(ty,N)*N);
> > ==================
> >
> > Should be
> >
> > aty = abs(ifft(ty,N))/dx;
>
> Thank you guys for your help! I have indeed another associated question to ask you. When I had a dc component, let's say I take y = sin(...) + 5 and let the code run, I get an ifft that has a dc component different from 5... Why?

Hi Frank, for y = sin()+5, you should get something with a mean value of 5. In other words, you get a sinewave that oscillates around 5, from [4,6]

dx = 0.01;
xmax = 4;
x = 0:dx:xmax;
y = 5+sin(2*pi*8*x);
YDFT = fft(y);
y1 = ifft(YDFT,'symmetric');
plot(x,y,'k'); hold on; plot(x,y1,'b')

Wayne