You are on page 1of 26

FastFourierTransformand

MATLABImplementation
by
WanjunHuang
for for
Dr.DuncanL.MacFarlane
1
Signals
In the fields of communications, signal processing, and in electrical engineering
more generally, a signal is any timevarying or spatialvarying quantity
This variable(quantity) changes in time
Speech or audio signal: A sound amplitude that varies in time
Temperature readings at different hours of a day
Stock price changes over days
Etc Etc.
Signalscanbeclassifiedbycontinuestimesignalanddiscretetime signal:
Adiscretesignalordiscretetimesignalisatimeseries,perhapsasignalthat
h b l d f ti ti i l hasbeensampledfromacontinuoustimesignal
Adigitalsignalisadiscretetimesignalthattakesononlyadiscretesetof
values
1
Continuous Time Signal
1
Discrete Time Signal
-0.5
0
0.5
f
(
t
)
-0.5
0
0.5
f
[
n
]
0 10 20 30 40
-1
Time (sec)
0 10 20 30 40
-1
n
2
PeriodicSignal
periodicsignalandnonperiodicsignal:
1
Periodic Signal
1
Non-Periodic Signal
0 10 20 30 40
-1
0
f
(
t
)
Time (sec)
0 10 20 30 40
-1
0
f
[
n
]
nn
PeriodT:Theminimumintervalonwhich
asignalrepeats
Fundamental frequency: f
0
=1/T Fundamentalfrequency:f
0
1/T
Harmonicfrequencies:kf
0
Anyperiodicsignalcanbeapproximated
byasumofmanysinusoidsatharmonicfrequenciesofthesignal(kf
0
)with y y q g ( f
0
)
appropriateamplitudeandphase
Insteadofusingsinusoidsignals,mathematically,wecanusethecomplex
exponentialfunctionswithbothpositiveandnegativeharmonicfrequencies
) cos( ) sin( ) exp( t j t t j e e e + =
Eulerformula:
3
TimeFrequencyAnalysis
Asignalhasoneormorefrequenciesinit,andcanbeviewedfrom
twodifferentstandpoints:TimedomainandFrequencydomain
Ti me Domi an (Banded Wren Song)
0
1
A
m
p
l
i
t
u
d
e
Ti me Domi an (Banded Wren Song)
1
2
P
o
w
e
r
Frequency Domai n
0 2 4 6 8
x 10
4
-1
Sample Number
A
0 200 400 600 800 1000 1200
0
Frequency (Hz)
Timedomain figure: how a signal changes over time
Why frequency domain analysis?
g g g
Frequencydomain figure: how much of the signal lies within each given
frequency band over a range of frequencies
Whyfrequencydomainanalysis?
Todecomposeacomplexsignalintosimplerpartstofacilitateanalysis
Differentialanddifferenceequationsandconvolutionoperationsinthe
timedomainbecomealgebraicoperationsinthefrequencydomain
FastAlgorithm(FFT)
4
Fourier Transform FourierTransform
We can go between the time domain and the frequency domain
by using a tool called Fourier transform
AFouriertransformconvertsasignalinthetimedomaintothe
frequencydomain(spectrum)
A i F i f h f d i
y g f
AninverseFouriertransformconvertsthefrequencydomain
componentsbackintotheoriginaltimedomainsignal
ContinuousTime Fourier Transform:
e e
t
e
d e j F t f
t j
}
=
+

) (
2
1
) (
}
=
+


dt e t f j F
t j e
e ) ( ) (
Continuous TimeFourierTransform:
DiscreteTime Fourier Transform(DTFT): Discrete TimeFourierTransform(DTFT):
}
=
t
e e
e
t
2
) (
2
1
] [ d e e X n x
n j j

=
+
=

n
n j j
e n x e X
e e
] [ ) (
5
Fourier Representation For Four Types of Signals FourierRepresentationForFourTypesofSignals
Thesignalwithdifferenttimedomaincharacteristicshasdifferent
frequencydomaincharacteristics
1 Continuestimeperiodicsignal>discretenonperiodic
spectrum
q y
p
2 Continuestimenonperiodicsignal>continuesnonperiodic
spectrum
3 Discrete nonperiodic signal > continues periodic spectrum 3 Discretenon periodicsignal >continuesperiodicspectrum
4 Discreteperiodicsignal>discreteperiodicspectrum
Thelasttransformationbetweentimedomainandfrequencyismost q y
useful
Thereasonthatdiscreteisassociatedwithbothtimedomainandfrequency
domainisbecausecomputerscanonlytakefinitediscretetimesignals p y g
6
Periodic Sequence PeriodicSequence
) (
~
) (
~
kN n x n x + =
, where k is integer
A periodic sequence with period N is defined as:
Periodic
, g
kn
N
j
kn
N
e W
t 2

= For example:
) ( ) ( N n k n N k kn
W W W
+ +
(it is called Twiddle Factor)
Properties: Periodic
) ( ) ( N n k
N
n N k
N
kn
N
W W W
+ +
= =
Symmetric
) ( ) ( *
) (
n N k
N
n k N
N
kn
N
kn
N
W W W W

= = =
Properties:
Orthogonal

=
=

=
other
rN n N
W
N
k
kn
N
0
1
0
ForaperiodicsequencewithperiodN,onlyN samples
areindependent.SothatN sampleinoneperiodisenoughto
represent the whole sequence
x(n)
representthewholesequence
7
Discrete Fourier Series(DFS) DiscreteFourierSeries(DFS)
Periodicsignalsmaybeexpandedintoaseriesofsineand
cosine functions

=
1
0
1
0
) (
~ 1
) (
~
) (
~
) (
~
N
kn
N
N
n
kn
N
W k X
N
n x
W n x k X
cosinefunctions
)) (
~
( ) (
~
)) (
~
( ) (
~
k X IDFS n x
n x DFS k X
=
=
= 0 n
N
is still a periodic sequence with period N in frequency
domain
) (
~
k X
The Fourier series for the discretetime periodic wave shown below:
1
Sequence x (in time domain)
0.2
Fourier Coeffients
0
0.5
A
m
p
l
i
t
u
d
e
0 4
-0.2
0
X
0 10 20 30 40
0
time
0 10 20 30 40
-0.4
8
Finite Length Sequence FiniteLengthSequence
) (n x
N n 1 0 s s
Real lift signal is generally a
fi i l h
If we periodic extend it by the period N then
+ =

rN n x n x ) ( ) (
~

=
0
) (
) (
n x
n x
others
N n 1 0 s s
finite length sequence
IfweperiodicextenditbytheperiodN,then
+
= r
rN n x n x ) ( ) (
) (n x
) (
~
n x

9
RelationshipBetweenFiniteLengthSequence
d P i di S andPeriodicSequence
A periodic sequence is the periodic extension of a finite length
A finite length sequence is the principal value interval of the periodic
sequence

= + =

= m
N
n x rN n x n x )) (( ) ( ) (
~
Afinitelengthsequenceistheprincipalvalueintervaloftheperiodic
sequence
) ( ) (
~
) ( n R n x n x
N
=

=
0
1
) (n R
N
others
N n 1 0 s s
Where

0 others
Sothat:
) ( )] (
~
[ ) ( ) (
~
) ( n R k X IDFS n R n x n x
N N
= =
) ( )] (
~
[ ) ( ) (
~
) (
) ( )] ( [ ) ( ) ( ) (
n R n x DFS k R k X k X
N N
N N
= =
10
Discrete Fourier Transform(DFT) DiscreteFourierTransform(DFT)
UsingtheFourierseriesrepresentationwehaveDiscrete
Fourier Transform(DFT) for finite length signal FourierTransform(DFT)forfinitelengthsignal
DFTcanconverttimedomaindiscretesignalintofrequency
domaindiscretespectrum
Assumethatwehaveasignal.ThentheDFTofthe
signalisasequencefor
1
0
]} [ {

=
N
n
n x
] [k X 1 , , 0 = N k

=

=

1
0
/ 2
] [ ] [
N
n
N jnk
e n x k X
t
TheInverseDiscreteFourierTransform(IDFT):

= =

=
1
0
/ 2
. 1 , , 2 , 0 , ] [
1
] [
N
k
N jnk
N n e k X
N
n x
t
Note that because MATLAB cannot use a zero or negative NotethatbecauseMATLABcannotuseazeroornegative
indices,theindexstartsfrom1inMATLAB
11
DFT Example DFTExample
TheDFTiswidelyusedinthefieldsofspectralanalysis,
acoustics medical imaging and telecommunications acoustics,medicalimaging,andtelecommunications.
4
5
6
e
Time domain signal
Forexample:
0
1
2
3
A
m
p
l
i
t
u
d
e
) 3 , 2 , 1 , 0 ( , 4 ], 6 1 4 2 [ ] [ = = = n N n x
nk
nk j
j n x e n x k X ) ( ] [ ] [ ] [
3
0
2
3
0

=

t
0 0.5 1 1.5 2 2.5 3
-1
Time
n n 0 0 = =
11 6 ) 1 ( 4 2 ] 0 [ = + + + = X
j j j X 2 3 6 1 ) 4 ( 2 ] 1 [ + = + + + =
10
12
Frequency domain signal
9 6 ) 1 ( ) 4 ( 2 ] 2 [ = + + = X
j j j X 2 3 6 1 ) 4 ( 2 ] 3 [ = + + =
4
6
8
10
|
X
[
k
]
|
0 0.5 1 1.5 2 2.5 3
0
2
Frequency
12
Fast Fourier Transform(FFT) FastFourierTransform(FFT)
The Fast Fourier Transform does not refer to a new or different
type of Fourier transform. It refers to a very efficient algorithm for yp y g
computing the DFT
The time taken to evaluate a DFT on a computer depends
principally on the number of multiplications involved. DFT needs principally on the number of multiplications involved. DFT needs
N
2
multiplications. FFT only needs Nlog
2
(N)
The central insight which leads to this algorithm is the
realization that a discrete Fourier transform of a sequence of N realization that a discrete Fourier transform of a sequence of N
points can be written in terms of two discrete Fourier transforms
of length N/2
Thus if N is a power of two it is possible to recursively apply Thus if N is a power of two, it is possible to recursively apply
this decomposition until we are left with discrete Fourier
transforms of single points
13
Fast Fourier Transform(cont.) FastFourierTransform(cont.)
Rewriting

=

=

1
0
/ 2
] [ ] [
N
n
N jnk
e n x k X
t
as

=

=
1
0
] [ ] [
N
n
nk
N
W n x k X
It is easy to realize that the same values of are calculated many times as the
nk
W Itiseasytorealizethatthesamevaluesofarecalculatedmanytimesasthe
computationproceeds
Usingthesymmetricpropertyofthetwiddlefactor,wecansavelotsofcomputations
N
W
1 1 1 N N N
) 1 2 ( ) 2 (
) ( ) ( ] [ ] [
1 2
) 1 2 (
1 2
2
1
0
1
0
1
0
W r x W r x
W n x W n x W n x k X
N
r k
N
N
kr
N
N
n odd
n
kn
N
N
n even
n
kn
N
N
n
nk
N

+ =

+ =

=
) ( ) (
) ( ) (
) 1 2 ( ) 2 (
1 2
0
2 2
1 2
0
2 1
0 0
k X W k X
W r x W W r x
W r x W r x
k
N
r
kr
N
k
N
N
r
kr
N
r
N
r
N

+ =

=
= =
) ( ) (
2 1
k X W k X
k
N
+ =
ThustheNpointDFTcanbeobtainedfromtwoN/2pointtransforms,oneoneven
inputdata,andoneonoddinputdata.
14
Introduction for MATLAB IntroductionforMATLAB
MATLAB is a numerical computing environment developed by
MathWorks. MATLAB allows matrix manipulations, plotting of p , p g
functions and data, and implementation of algorithms
Getting help Getting help
You can get help by typing the commands hel p or l ookf or at
the >> prompt, e.g.
>> hel p f f t >> hel p f f t
Arithmetic operators
Symbol Operation Example
+ Addition 3 1 + 9 + Addition 3.1 + 9
Subtraction 6.2 5
* Multiplication 2 * 3
/ Division 5 / 2 / Division 5 / 2
^ Power 3^2
15
Data Representations in MATLAB DataRepresentationsinMATLAB
Variables: Variables are defined as the assignment operator = . The syntax of
variable assignment is
i bl l ( i ) var i abl e name = a val ue ( or an expr essi on)
For example,
>> x = 5
x =
5
>> y = [ 3*7, pi / 3] ; %pi i s i n MATLAB
Vectors/Matrices MATLAB can create and manip late arra s of 1 ( ectors) 2
t
Vectors/Matrices: MATLAB can create and manipulate arrays of 1 (vectors), 2
(matrices), or more dimensions
row vectors: a = [1, 2, 3, 4] is a 1X4 matrix
column vectors: b = [5; 6; 7; 8; 9] is a 5X1 matrix, e.g.
>> A = [ 1 2 3; 7 8 9; 4 5 6]
A = 1 2 3
7 8 9
4 5 6 4 5 6
16
Mathematical Functions in MATLAB MathematicalFunctionsinMATLAB
MATLAB offers many predefined mathematical functions for
technical computing, e.g. p g, g
cos( x) Cosi ne abs( x) Absol ut e val ue
si n( x) Si ne angl e( x) Phase angl e
exp( x) Exponent i al conj ( x) Compl ex conj ugat e
Colon operator (:)
exp( x) Exponent i al conj ( x) Compl ex conj ugat e
sqr t ( x) Squar e r oot l og( x) Nat ur al l ogar i t hm
Colon operator (:)
Suppose we want to enter a vector x consisting of points
(0,0.1,0.2,0.3,,5). We can use the command
>> x = 0:0.1:5;;
Most of the work you will do in MATLAB will be stored in files called
scripts, or mfiles, containing sequences of MATLAB commands to be
executed over and over again
17
Basic plotting in MATLAB BasicplottinginMATLAB
MATLAB has an excellent set of graphic tools. Plotting a given data set or
the results of computation is possible with very few commands
The MATLAB command to plot a graph is plot(x,y), e.g.
>> x = 0: pi / 100: 2*pi ; 0.8
1
Sine function
p / p ;
>> y = si n( x) ;
>> pl ot ( x, y)
0.2
0.4
0.6
x
MATLAB enables you to add axis
Labels and titles, e.g.
\ i
-0.4
-0.2
0
S
i
n
e

o
f

x
>> xl abel ( ' x=0: 2\ pi ' ) ;
>> yl abel ( ' Si ne of x' ) ;
>> t i l e( ' Si ne f unct i on' )
0 1 2 3 4 5 6 7
-1
-0.8
-0.6
x=0:2t x 0:2t
18
Example 1: Sine Wave Example1:SineWave
0.5
1
Sine Wave Signal
Fs = 150; %Sampl i ng f r equency
t = 0: 1/ Fs: 1; %Ti me vect or of 1 second
f = 5; %Cr eat e a si ne wave of f Hz.
i ( 2* i *t *f )
-0.5
0
A
m
p
l
i
t
u
d
e
x = si n( 2*pi *t *f ) ;
nf f t = 1024; %Lengt h of FFT
%Take f f t , paddi ng wi t h zer os so t hat l engt h( X)
i s equal t o nf f t
X = f f t ( x, nf f t ) ;
%FFT i s symmet r i c, t hr ow away second hal f
0 0.2 0.4 0.6 0.8 1
-1
Time (s)
80
Power Spectrum of a Sine Wave
%FFT i s symmet r i c, t hr ow away second hal f
X = X( 1: nf f t / 2) ;
%Take t he magni t ude of f f t of x
mx = abs( X) ;
%Fr equency vect or
f = ( 0: nf f t / 2- 1) *Fs/ nf f t ;
40
60
80
P
o
w
e
r
%Gener at e t he pl ot , t i t l e and l abel s.
f i gur e( 1) ;
pl ot ( t , x) ;
t i t l e( ' Si ne Wave Si gnal ' ) ;
xl abel ( ' Ti me ( s) ' ) ;
l b l ( ' A l i t d ' )
0 10 20 30 40 50 60 70 80
0
20
Frequency (Hz)
P
yl abel ( ' Ampl i t ude' ) ;
f i gur e( 2) ;
pl ot ( f , mx) ;
t i t l e( ' Power Spect r umof a Si ne Wave' ) ;
xl abel ( ' Fr equency ( Hz) ' ) ;
yl abel ( ' Power ' ) ; Frequency (Hz) yl abel ( Power ) ;
19
Example 2: Cosine Wave Example2:CosineWave
Fs = 150; %Sampl i ng f r equency
t = 0: 1/ Fs: 1; %Ti me vect or of 1 second
f = 5; %Cr eat e a si ne wave of f Hz.
x = cos( 2*pi *t *f ) ;
0.5
1
Cosine Wave Signal
x = cos( 2*pi *t *f ) ;
nf f t = 1024; %Lengt h of FFT
%Take f f t , paddi ng wi t h zer os so t hat l engt h( X) i s
equal t o nf f t
X = f f t ( x, nf f t ) ;
%FFT i s symmet r i c, t hr ow away second hal f
-0.5
0
A
m
p
l
i
t
u
d
e
y , y
X = X( 1: nf f t / 2) ;
%Take t he magni t ude of f f t of x
mx = abs( X) ;
%Fr equency vect or
f = ( 0: nf f t / 2- 1) *Fs/ nf f t ;
0 0.2 0.4 0.6 0.8 1
-1
Time (s)
80
Power Spectrum of a Cosine Wave
%Gener at e t he pl ot , t i t l e and l abel s.
f i gur e( 1) ;
pl ot ( t , x) ;
t i t l e( ' Si ne Wave Si gnal ' ) ;
xl abel ( ' Ti me ( s) ' ) ;
yl abel ( ' Ampl i t ude' ) ;
40
60
80
P
o
w
e
r
yl abel ( Ampl i t ude ) ;
f i gur e( 2) ;
pl ot ( f , mx) ;
t i t l e( ' Power Spect r umof a Si ne Wave' ) ;
xl abel ( ' Fr equency ( Hz) ' ) ;
yl abel ( ' Power ' ) ;
0 10 20 30 40 50 60 70 80
0
20
Frequency (Hz)
P
y
Frequency (Hz)
20
Example 3: Cosine Wave with Phase Shift Example3:CosineWavewithPhaseShift
Fs = 150; %Sampl i ng f r equency
t = 0: 1/ Fs: 1; %Ti me vect or of 1 second
f = 5; %Cr eat e a si ne wave of f Hz.
pha = 1/ 3*pi ; %phase shi f t
0.5
1
Cosine Wave Signal with Phase Shift
pha = 1/ 3*pi ; %phase shi f t
x = cos( 2*pi *t *f + pha) ;
nf f t = 1024; %Lengt h of FFT
%Take f f t , paddi ng wi t h zer os so t hat l engt h( X) i s
equal t o nf f t
X = f f t ( x, nf f t ) ;
-0.5
0
A
m
p
l
i
t
u
d
e
( , )
%FFT i s symmet r i c, t hr ow away second hal f
X = X( 1: nf f t / 2) ;
%Take t he magni t ude of f f t of x
mx = abs( X) ;
%Fr equency vect or
/ /
0 0.2 0.4 0.6 0.8 1
-1
Time (s)
80
Power Spectrum of a Cosine Wave Signal with Phase Shift
f = ( 0: nf f t / 2- 1) *Fs/ nf f t ;
%Gener at e t he pl ot , t i t l e and l abel s.
f i gur e( 1) ;
pl ot ( t , x) ;
t i t l e( ' Si ne Wave Si gnal ' ) ;
xl abel ( ' Ti me ( s) ' ) ;
40
60
80
P
o
w
e
r
xl abel ( Ti me ( s) ) ;
yl abel ( ' Ampl i t ude' ) ;
f i gur e( 2) ;
pl ot ( f , mx) ;
t i t l e( ' Power Spect r umof a Si ne Wave' ) ;
xl abel ( ' Fr equency ( Hz) ' ) ;
0 10 20 30 40 50 60 70 80
0
20
Frequency (Hz)
P
q y
yl abel ( ' Power ' ) ;
Frequency (Hz)
21
Example 4: Square Wave Example4:SquareWave
0.5
1
Square Wave Signal
Fs = 150; %Sampl i ng f r equency
t = 0: 1/ Fs: 1; %Ti me vect or of 1 second
f = 5; %Cr eat e a si ne wave of f Hz.
x = squar e( 2*pi *t *f ) ;
-0.5
0
A
m
p
l
i
t
u
d
e
x = squar e( 2*pi *t *f ) ;
nf f t = 1024; %Lengt h of FFT
%Take f f t , paddi ng wi t h zer os so t hat l engt h( X) i s
equal t o nf f t
X = f f t ( x, nf f t ) ;
%FFT i s symmet r i c, t hr ow away second hal f
0 0.2 0.4 0.6 0.8 1
-1
Time (s)
100
Power Spectrum of a Square Wave
y , y
X = X( 1: nf f t / 2) ;
%Take t he magni t ude of f f t of x
mx = abs( X) ;
%Fr equency vect or
f = ( 0: nf f t / 2- 1) *Fs/ nf f t ;
40
60
80
100
P
o
w
e
r
%Gener at e t he pl ot , t i t l e and l abel s.
f i gur e( 1) ;
pl ot ( t , x) ;
t i t l e( ' Squar e Wave Si gnal ' ) ;
xl abel ( ' Ti me ( s) ' ) ;
yl abel ( ' Ampl i t ude' ) ;
0 20 40 60 80
0
20
40
Frequency (Hz)
yl abel ( Ampl i t ude ) ;
f i gur e( 2) ;
pl ot ( f , mx) ;
t i t l e( ' Power Spect r umof a Squar e Wave' ) ;
xl abel ( ' Fr equency ( Hz) ' ) ;
yl abel ( ' Power ' ) ; y
22
Example 5: Square Pulse Example5:SquarePulse
0.8
1
Square Pulse Signal
Fs = 150; %Sampl i ng f r equency
t = - 0. 5: 1/ Fs: 0. 5; %Ti me vect or of 1 second
w = . 2; %wi dt h of r ect angl e
x = r ect pul s( t w) ; %Gener at e Squar e Pul se
0.2
0.4
0.6
A
m
p
l
i
t
u
d
e
x = r ect pul s( t , w) ; %Gener at e Squar e Pul se
nf f t = 512; %Lengt h of FFT
%Take f f t , paddi ng wi t h zer os so t hat l engt h( X) i s
equal t o nf f t
X = f f t ( x, nf f t ) ;
%FFT i s symmet r i c, t hr ow away second hal f
-0.5 0 0.5
0
Time (s)
30
Power Spectrum of a Square Pulse
y , y
X = X( 1: nf f t / 2) ;
%Take t he magni t ude of f f t of x
mx = abs( X) ;
%Fr equency vect or
f = ( 0: nf f t / 2- 1) *Fs/ nf f t ;
15
20
25
P
o
w
e
r
%Gener at e t he pl ot , t i t l e and l abel s.
f i gur e( 1) ;
pl ot ( t , x) ;
t i t l e( ' Squar e Pul se Si gnal ' ) ;
xl abel ( ' Ti me ( s) ' ) ;
yl abel ( ' Ampl i t ude' ) ;
0 20 40 60 80
0
5
10
Frequency (Hz)
yl abel ( Ampl i t ude ) ;
f i gur e( 2) ;
pl ot ( f , mx) ;
t i t l e( ' Power Spect r umof a Squar e Pul se' ) ;
xl abel ( ' Fr equency ( Hz) ' ) ;
yl abel ( ' Power ' ) ; y
23
Example 6: Gaussian Pulse Example6:GaussianPulse
3
4
Gaussian Pulse Signal
Fs = 60; %Sampl i ng f r equency
t = - . 5: 1/ Fs: . 5;
x = 1/ ( sqr t ( 2*pi *0. 01) ) *( exp( - t . ^2/ ( 2*0. 01) ) ) ;
nf f t = 1024; %Lengt h of FFT
1
2
A
m
p
l
i
t
u
d
e
nf f t = 1024; %Lengt h of FFT
%Take f f t , paddi ng wi t h zer os so t hat
l engt h( X) i s equal t o nf f t
X = f f t ( x, nf f t ) ;
%FFT i s symmet r i c, t hr ow away second hal f
X = X( 1: nf f t / 2) ;
-0.5 0 0.5
0
Time (s)
( )
%Take t he magni t ude of f f t of x
mx = abs( X) ;
%Thi s i s an evenl y spaced f r equency vect or
f = ( 0: nf f t / 2- 1) *Fs/ nf f t ;
%Gener at e t he pl ot , t i t l e and l abel s.
40
50
60
Power Spectrum of a Gaussian Pulse
r
f i gur e( 1) ;
pl ot ( t , x) ;
t i t l e( ' Gaussi an Pul se Si gnal ' ) ;
xl abel ( ' Ti me ( s) ' ) ;
yl abel ( ' Ampl i t ude' ) ;
f i gur e( 2) ;
0
10
20
30
P
o
w
e
r f i gur e( 2) ;
pl ot ( f , mx) ;
t i t l e( ' Power Spect r umof a Gaussi an Pul se' ) ;
xl abel ( ' Fr equency ( Hz) ' ) ;
yl abel ( ' Power ' ) ;
0 5 10 15 20 25 30
0
Frequency (Hz)
24
Example 7: Exponential Decay Example7:ExponentialDecay
1.5
2
Exponential Decay Signal
Fs = 150; %Sampl i ng f r equency
t = 0: 1/ Fs: 1; %Ti me vect or of 1 second
x = 2*exp( - 5*t ) ;
nf f t 1024 %Lengt h of FFT
0.5
1
A
m
p
l
i
t
u
d
e
nf f t = 1024; %Lengt h of FFT
%Take f f t , paddi ng wi t h zer os so t hat
l engt h( X) i s equal t o nf f t
X = f f t ( x, nf f t ) ;
%FFT i s symmet r i c, t hr ow away second
hal f
0 0.2 0.4 0.6 0.8 1
0
Time (s)
P S t f E ti l D Si l
hal f
X = X( 1: nf f t / 2) ;
%Take t he magni t ude of f f t of x
mx = abs( X) ;
%Thi s i s an evenl y spaced f r equency
vect or
40
50
60
70
Power Spectrum of Exponential Decay Signal
e
r
f = ( 0: nf f t / 2- 1) *Fs/ nf f t ;
%Gener at e t he pl ot , t i t l e and l abel s.
f i gur e( 1) ;
pl ot ( t , x) ;
t i t l e( ' Exponent i al Decay Si gnal ' ) ;
xl abel ( ' Ti me ( s) ' ) ;
0 20 40 60 80
0
10
20
30
P
o
w
e
xl abel ( ' Ti me ( s) ' ) ;
yl abel ( ' Ampl i t ude' ) ;
f i gur e( 2) ;
pl ot ( f , mx) ;
t i t l e( ' Power Spect r umof Exponent i al
Decay Si gnal ' ) ;
0 20 40 60 80
Frequency (Hz)
y g ) ;
xl abel ( ' Fr equency ( Hz) ' ) ;
yl abel ( ' Power ' ) ;
25
Example 8: Chirp Signal Example8:ChirpSignal
0.5
1
Chirp Signal
Fs = 200; %Sampl i ng f r equency
t = 0: 1/ Fs: 1; %Ti me vect or of 1 second
x = chi r p( t , 0, 1, Fs/ 6) ;
nf f t = 1024; %Lengt h of FFT
-0.5
0
A
m
p
l
i
t
u
d
e
; g
%Take f f t , paddi ng wi t h zer os so t hat
l engt h( X) i s equal t o nf f t
X = f f t ( x, nf f t ) ;
%FFT i s symmet r i c, t hr ow away second hal f
X = X( 1: nf f t / 2) ;
0 0.2 0.4 0.6 0.8 1
-1
Time (s)
Power Spectrum of Chirp Signal
%Take t he magni t ude of f f t of x
mx = abs( X) ;
%Thi s i s an evenl y spaced f r equency
vect or
f = ( 0: nf f t / 2- 1) *Fs/ nf f t ;
%Gener at e t he pl ot t i t l e and l abel s
15
20
25
p p g
w
e
r
%Gener at e t he pl ot , t i t l e and l abel s.
f i gur e( 1) ;
pl ot ( t , x) ;
t i t l e( ' Chi r p Si gnal ' ) ;
xl abel ( ' Ti me ( s) ' ) ;
yl abel ( ' Ampl i t ude' ) ;
0 20 40 60 80 100
0
5
10
P
o
w
y p
f i gur e( 2) ;
pl ot ( f , mx) ;
t i t l e( ' Power Spect r umof Chi r p Si gnal ' ) ;
xl abel ( ' Fr equency ( Hz) ' ) ;
yl abel ( ' Power ' ) ;
Frequency (Hz)
26

You might also like