Вы находитесь на странице: 1из 4

Understanding Fourier transform example in Matlab - Mathematics Sta... http://math.stackexchange.com/questions/636847/understanding-fourier...

1 of 4

sign up

Mathematics Stack Exchange is a question and answer site for people studying math at any level and
professionals in related fields. It's 100% free, no registration required.

log in

tour

help

Understanding Fourier transform example in Matlab


I'm studying about Fourier series and transform and I get confused with the following Matlab example of Fourier transformation:
Fs=1000;%Samplingfrequency
T=1/Fs;%Sampletime
L=1000;%Lengthofsignal
t=(0:L1)*T;%Timevector
%Sumofa50Hzsinusoidanda120Hzsinusoid
x=0.7*sin(2*pi*50*t)+sin(2*pi*120*t);
y=x+2*randn(size(t));%Sinusoidsplusnoise
plot(Fs*t(1:50),y(1:50))
title('SignalCorruptedwithZeroMeanRandomNoise')
xlabel('time(milliseconds)')

This gives:

I have no problem with this part, but the Fourier transform is where I get lost:
NFFT=2^nextpow2(L);%Nextpowerof2fromlengthofy
Y=fft(y,NFFT)/L;
f=Fs/2*linspace(0,1,NFFT/2+1);
%Plotsinglesidedamplitudespectrum.
plot(f,2*abs(Y(1:NFFT/2+1)))
title('SingleSidedAmplitudeSpectrumofy(t)')
xlabel('Frequency(Hz)')
ylabel('|Y(f)|')

, which gives:

4.3.2015 12:00

Understanding Fourier transform example in Matlab - Mathematics Sta... http://math.stackexchange.com/questions/636847/understanding-fourier...

2 of 4

This is the code which confuses me:


NFFT=2^nextpow2(L);%Nextpowerof2fromlengthofy
Y=fft(y,NFFT)/L;
f=Fs/2*linspace(0,1,NFFT/2+1);
%Plotsinglesidedamplitudespectrum.
plot(f,2*abs(Y(1:NFFT/2+1)))

I don't understand all the confusing multiplications and divisions done here....can someone explain why we are for example here:
plot(f,2*abs(Y(1:NFFT/2+1))) multiplying by two etc. etc. Why not just do: plot(f,abs(Y(1:NFFT/2+1))) . Why are we dividing by L in
this line: Y=fft(y,NFFT)/L; . What are we doing in this line: f=Fs/2*linspace(0,1,NFFT/2+1); ??
The definitions for the Matlab functions are the following:
p=nextpow2(A)returnsthesmallestpoweroftwothatisgreaterthanorequaltothe
absolutevalueofA.(Thatis,pthatsatisfies2^p>=abs(A)).
abs(X)returnsanarrayYsuchthateachelementofYistheabsolutevalueofthe
correspondingelementofX.
Y=fft(X,n)returnsthenpointDFT.fft(X)isequivalenttofft(X,n)wherenisthe
sizeofXinthefirstnonsingletondimension.IfthelengthofXislessthann,Xis
paddedwithtrailingzerostolengthn.IfthelengthofXisgreaterthann,the
sequenceXistruncated.WhenXisamatrix,thelengthofthecolumnsareadjustedin
thesamemanner.
y=linspace(a,b,n)generatesarowvectoryofnpointslinearlyspacedbetweenand
includingaandb.Forn<2,linspacereturnsb.

In other words I would just want someone to better explain the four given code lines to me. What are we doing an why :)
Thank you for any help!
P.S.
here is the reference:
http://www.mathworks.se/help/matlab/ref/fft.html
(fourier-analysis) (fourier-series) (matlab)

edited Jan 13 '14 at 12:14

asked Jan 13 '14 at 11:55


jjepsuomi
1,779

29

3 Answers
I think this question deserves a more satisfactory answer. The truth is that the MATLAB
example is actually wrong in dividing the fft by the signal length in the time domain (which is
L):

4.3.2015 12:00

Understanding Fourier transform example in Matlab - Mathematics Sta... http://math.stackexchange.com/questions/636847/understanding-fourier...

3 of 4

Y=fft(y,NFFT)/L;%TheMATLABexamplewhichisactuallywrong

The right scaling needed to adhere to Parseval's theorem would be dividing the Fourier
transform by the sampling frequency:
Y=fft(y,NFFT)/Fs;%TheCorrectScaling

Incidentally, these two are the same in this MATLAB example! (L = Fs = 1000)

It's important to see why the contradiction to Parseval's theorem is caused in the first place.
The MATLAB fft function is not to be blamed here! Actually, it adheres to the Parseval's
theorem in the simple case of calculating the FFT with the same number of points as the signal
length:
x=rand(10000,1);%Foranyvector(ofanylength)
A=sum(abs(x).^2)sum(abs(fft(x)).^2)/length(x);

For any vector (of any length), A in the above code would be extremely close to 0 (any
difference is due to numerical computation errors) as stated by the Parseval's theorem for
discrete Fourier transform (DFT):

So who is the culprit?! The discrepancy happens only when fft is calculated with a length other
than the original input vector's length, which is specified via the second argument of the fft
function. But still, the fft function is not doing anything wrong. It simply pads the input vector
with enough zeros (or truncates it if the vector length is more than the demanded fft length)
and then performs the fft. The Parseval's theorem holds for the padded (truncated) signal but
we shouldn't expect it to hold for the original signal (because of dependency on N in Parseval's
theorem).
So what can we do now in this case where the signal and it's DFT are of different lengths. We
can emulate the continuous case of Parseval's theorem:

We can approximate the integral by a sum. The differential elements should be:
dt=1/Fs;
df=Fs/NFFT;%NFFTisthelengthofFFT;
%dfistheabovebecausethelastpointinfftcorresponds
%toFs[Hz]inthecontinuousFouriertransform
%soeachfftstepisequivalenttoFs/NFFT[Hz]
B=sum(abs(y).^2)*dtsum(abs(fft(y,NFFT)/ScalingFactor).^2)*df

Assuming that we want Parseval's theorem to hold, we need to set the ScalingFactor so that B
would be zero. Setting it as Fs would exactly do that:
ScalingFactor=Fs;%Theoutputofffthastobedividedbythis

The output of the fft function has to be divided by the sampling frequency of the input vector
(Fs) (or equivalently multiplied by dt = 1/Fs) for the Parseval's theorem to hold!

Credits go to Elige Grant for writing about this way of scaling here and to Rick Rosson for this
edited Dec 21 '14 at 11:28

answered Oct 27 '14 at 20:53


OmidS
36

Excellent :) thank you :) jjepsuomi Oct 27 '14 at 20:55

Did you find this question interesting? Try our newsletter


Sign up for our newsletter and get our top new questions
delivered to your inbox (see an example).

Read up on the differences between the DFT (i.e., discrete fourier transform), the FFT (a fast
version of DFT), the *Fourier Series and the Fourier Transform (i.e. the time-continuous
case).

4.3.2015 12:00

Understanding Fourier transform example in Matlab - Mathematics Sta... http://math.stackexchange.com/questions/636847/understanding-fourier...

4 of 4

Basically, fft computes the DFT. The DFT is simply an invertible linear map from
to itself,
i.e. you may imagine it as a change of base. For real-valued inputs, the right half of the result of
the DFT is always the conjugated mirror-image of the left part, i.e. for a -point DFT you have
that

. This is why the code ignores the right half of

The DFT also loses all information about the time scale, since the input is just a vector of real
or complex-valued samples. To nevertheless display actual frequencies in Hz, the code needs to
recover those - that's what the linspace code is about. It produces the labels for the -axis,
nothing more.
A basic invariant of the DFT (and also other kinds of fourier transforms) is parseval's theorem
(http://en.wikipedia.org/wiki/Parseval%27s_theorem). Scaling the amplitudes by a factor of
two ensure that this theorem holds for the plotted value - the scaling factor compensates for
ignoring the right half of the result.
edited Jan 13 '14 at 12:38

answered Jan 13 '14 at 12:31


fgp
14.6k

14

27

+1 for your help dato datuashvili Jan 13 '14 at 12:32

+1 Thank you for helping :) jjepsuomi Jan 13 '14 at 12:38

@jjepsuomi that is good point when you are helping to other :) dato datuashvili Jan 13 '14 at 12:41

please dont forget to accept any of our answer for future help dato datuashvili Jan 13 '14 at 12:47

my friend we users of this site are not concentrated accept our answers,we are concentrated to gain knowledge,me for
example,you can accept any answer,i am not care about it :)good lcuk dato datuashvili Jan 13 '14 at 12:54

because sometimes for fast fourier multiplication,scientist use length which is power of two,or
some length
,this is idea of fast fourier transform, http://www.mathworks.com
/matlabcentral/newsreader/view_thread/243154
http://www.mathworks.com/company/newsletters/articles/faster-finite-fourier-transformsmatlab.html
answered Jan 13 '14 at 12:14
dato datuashvili
4,459

+1 Thank you for your help! :) jjepsuomi Jan 13 '14 at 12:19

you are welcome,good lucks dato datuashvili Jan 13 '14 at 12:20

for additional DSP question,dsp.stackexchange.com dato datuashvili Jan 13 '14 at 12:21

+1 Thank you, I will ask my future questions about Fourier on that site :) jjepsuomi Jan 13 '14 at 12:23

you can use both of this,good lucks dato datuashvili Jan 13 '14 at 12:24

20

52

4.3.2015 12:00

Вам также может понравиться