understanding fourier transform example in matlab - mathematics stack exchange

4
sign up log in tour help × 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. 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; % Sampling frequency T = 1/Fs; % Sample time L = 1000; % Length of signal t = (0:L1)*T; % Time vector % Sum of a 50 Hz sinusoid and a 120 Hz sinusoid x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t); y = x + 2*randn(size(t)); % Sinusoids plus noise plot(Fs*t(1:50),y(1:50)) title('Signal Corrupted with ZeroMean Random Noise') 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); % Next power of 2 from length of y Y = fft(y,NFFT)/L; f = Fs/2*linspace(0,1,NFFT/2+1); % Plot singlesided amplitude spectrum. plot(f,2*abs(Y(1:NFFT/2+1))) title('SingleSided Amplitude Spectrum of y(t)') xlabel('Frequency (Hz)') ylabel('|Y(f)|') , which gives: Understanding Fourier transform example in Matlab - Mathematics Sta... http://math.stackexchange.com/questions/636847/understanding-fourier... 1 of 4 4.3.2015 12:00

Upload: admicicmail

Post on 23-Dec-2015

17 views

Category:

Documents


4 download

DESCRIPTION

FT

TRANSCRIPT

Page 1: Understanding Fourier Transform Example in Matlab - Mathematics Stack Exchange

sign up log in tour help

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

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;                    % Sampling frequency

T = 1/Fs;                     % Sample time

L = 1000;                     % Length of signal

t = (0:L‐1)*T;                % Time vector

% Sum of a 50 Hz sinusoid and a 120 Hz sinusoid

x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t); 

y = x + 2*randn(size(t));     % Sinusoids plus noise

plot(Fs*t(1:50),y(1:50))

title('Signal Corrupted with Zero‐Mean Random Noise')

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); % Next power of 2 from length of y

Y = fft(y,NFFT)/L;

f = Fs/2*linspace(0,1,NFFT/2+1);

% Plot single‐sided amplitude spectrum.

plot(f,2*abs(Y(1:NFFT/2+1))) 

title('Single‐Sided Amplitude Spectrum of y(t)')

xlabel('Frequency (Hz)')

ylabel('|Y(f)|')

, which gives:

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

1 of 4 4.3.2015 12:00

Page 2: Understanding Fourier Transform Example in Matlab - Mathematics Stack Exchange

This is the code which confuses me:

NFFT = 2^nextpow2(L); % Next power of 2 from length of y

Y = fft(y,NFFT)/L;

f = Fs/2*linspace(0,1,NFFT/2+1);

% Plot single‐sided amplitude spectrum.

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: multiplying by two etc. etc. Why not just do: . Why are we dividing by in

this line: . What are we doing in this line: ??plot(f,2*abs(Y(1:NFFT/2+1))) plot(f, abs(Y(1:NFFT/2+1))) L

Y = fft(y,NFFT)/L; f = Fs/2*linspace(0,1,NFFT/2+1);

The definitions for the Matlab functions are the following:

p = nextpow2(A) returns the smallest power of two that is greater than or equal to the 

absolute value of A. (That is, p that satisfies 2^p >= abs(A)). 

abs(X) returns an array Y such that each element of Y is the absolute value of the 

corresponding element of X.

Y = fft(X,n) returns the n‐point DFT. fft(X) is equivalent to fft(X, n) where n is the 

size of X in the first nonsingleton dimension. If the length of X is less than n, X is 

padded with trailing zeros to length n. If the length of X is greater than n, the 

sequence X is truncated. When X is a matrix, the length of the columns are adjusted in 

the same manner.

y = linspace(a,b,n) generates a row vector y of n points linearly spaced between and 

including a and b. For n < 2, linspace returns b.

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

jjepsuomi1,779 8 29

3 Answers

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

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

2 of 4 4.3.2015 12:00

Page 3: Understanding Fourier Transform Example in Matlab - Mathematics Stack Exchange

Y = fft(y,NFFT)/L; % The MATLAB example which is actually wrong

The right scaling needed to adhere to would be dividing the Fouriertransform by the sampling frequency:

Parseval's theorem

Y = fft(y,NFFT)/Fs; % The Correct Scaling

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'stheorem in the simple case of calculating the FFT with the same number of points as the signallength:

x = rand(10000,1); % For any vector (of any length)

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 (anydifference is due to numerical computation errors) as stated by the Parseval's theorem fordiscrete Fourier transform (DFT):

So who is the culprit?! The discrepancy happens only when fft is calculated with a length otherthan the original input vector's length, which is specified via the second argument of the fftfunction. But still, the fft function is not doing anything wrong. It simply pads the input vectorwith 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 butwe shouldn't expect it to hold for the original signal (because of dependency on N in Parseval'stheorem).

So what can we do now in this case where the signal and it's DFT are of different lengths. Wecan 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; % NFFT is the length of FFT; 

% df is the above because the last point in fft corresponds 

% to Fs [Hz] in the continuous Fourier transform 

% so each fft step is equivalent to Fs/NFFT [Hz]

B = sum( abs(y).^2 )*dt ‐ sum( abs(fft(y, NFFT)/ScalingFactor).^2 )*df

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

ScalingFactor = Fs; % The output of fft has to be divided by this

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 and to Rick Rosson for here this

edited Dec 21 '14 at 11:28 answered Oct 27 '14 at 20:53

OmidS36 3

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

Did you find this question interesting? Try our newsletterSign up for our newsletter and get our top new questionsdelivered to your inbox ( ).see an example

Read up on the differences between the (i.e., discrete fourier transform), the (a fastversion of ), the *Fourier Series and the (i.e. the time-continuouscase).

DFT FFTDFT Fourier Transform

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

3 of 4 4.3.2015 12:00

Page 4: Understanding Fourier Transform Example in Matlab - Mathematics Stack Exchange

Basically, 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 ofthe 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 .

fft

Y

The DFT also loses all information about the time scale, since the input is just a vector of realor complex-valued samples. To nevertheless display actual frequencies in Hz, the code needs torecover those - that's what the code is about. It produces the labels for the -axis,nothing more.

linspace

A basic invariant of the DFT (and also other kinds of fourier transforms) is parseval's theorem( ). Scaling the amplitudes by a factor oftwo ensure that this theorem holds for the plotted value - the scaling factor compensates forignoring the right half of the result.

http://en.wikipedia.org/wiki/Parseval%27s_theorem

edited Jan 13 '14 at 12:38 answered Jan 13 '14 at 12:31

fgp14.6k 2 14 27

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

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

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

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

1 –

my friend we users of this site are not concentrated accept our answers,we are concentrated to gain knowledge,me forexample,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,orsome 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-transforms-matlab.html

answered Jan 13 '14 at 12:14

dato datuashvili4,459 5 20 52

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

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

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

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

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

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

4 of 4 4.3.2015 12:00