Fast Fourier Trasform ad MATLAB Implemetatio by aju Huag for Dr. Duca L. MacFarlae
Sigals I the fields of commuicatios, sigal processig, ad i electrical egieerig moregeerally, a sigalisay time varyig or spatial varyig quatity This variable(quatity chages i time Speech or audio sigal: A soud amplitude that varies i time Temperature readigs at differet hours of a day Stock price chages over days Etc. Sigals ca be classified by cotiues time sigal ad discrete time sigal: A discrete sigal or discrete time sigal is a time series, perhaps a sigal that has bee sampled ldfrom a cotiuous time sigal A digital sigal is a discrete time sigal that takes o oly a discrete set of values Cotiuous Time Sigal Discrete Time Sigal.5.5 f(t f[] -.5 -.5-3 4 Time (sec - 3 4
Periodic Sigal periodic sigal ad o periodic sigal: Periodic Sigal o-periodic Sigal f(t f[] - 3 4 Time (sec Period T: The miimum iterval o which a sigal repeats Fudametal frequecy: f =/T - 3 4 Harmoic frequecies: kf Ay periodic sigal ca be approimated by a sum of may siusoids at harmoic frequecies of the sigal(kf f with appropriate amplitude ad phase Istead of usig siusoid sigals, mathematically, we ca use the comple epoetial fuctios with both positive ad egative harmoic frequecies Euler formula: ep( j t si( t j cos( t 3
Time Frequecy Aalysis A sigal has oe or more frequecies i it, ad ca be viewed from two differet stadpoits: Time domai ad Frequecy domai Time Domia (Baded re Sog Frequecy Domai Amplitude Power - 4 6 8 Sample umber 4 4 6 8 Frequecy (Hz Time domaifigure: how asigalchages g over time Frequecy domai figure: how much of the sigal lies withi each give frequecy bad over a rage of frequecies hy frequecy domai aalysis? To decompose a comple sigal ito simpler parts to facilitate aalysis Differetial ad differece equatios ad covolutio operatios i the time domai become algebraic operatios i the frequecy domai Fast Algorithm (FFT 4
Fourier Trasform e ca go betwee the time domai ad the frequecy domai by usig a tool called Fourier trasform A Fourier trasform coverts a sigal i the time domai to the frequecy domai(spectrum A iverse Fourier trasform coverts the frequecy domai compoets back ito the origial time domai sigal Cotiuous Time Fourier Trasform: F ( j f ( t e j t dt f ( t Discrete Time Fourier Trasform(DTFT: X ( e j [ ] e j F ( j e j t d j j [ ] X ( e e d 5
Fourier Represetatio For Four Types of Sigals The sigal with differet time domai characteristics has differet frequecy domai characteristics Cotiues time periodic sigal > discrete o periodic spectrum Cotiues time o periodic sigal > cotiues o periodic spectrum 3 Discrete o periodic sigal > > cotiues periodicspectrum 4 Discrete periodic sigal > discrete periodic spectrum The last trasformatio betwee time domai ad frequecy is most useful The reaso that discrete is associated with both time domai ad frequecy domai is because computers ca oly take fiite discrete time sigals 6
( ( k Periodic Sequece A periodic sequece with period is defied as: For eample: k e j k, where k is iteger (it is called Twiddle Factor k ( k k ( Properties: Periodic k k * ( k k ( Symmetric ( Orthogoal k k r other For a periodic sequece ( with period, oly samples are idepedet. So that sample i oe period is eough to represet the whole sequece 7
Amplitude Discrete Fourier Series(DFS Periodic sigals may be epaded ito a series of sie ad cosie fuctios X ( k X ( k ( ( k X ( k k X ( k DFS ( IDFS ( ( ( X ( k is still a periodic sequece with period i frequecy domai The Fourier series forthe discrete time periodic wave show below:.5 Sequece (i time domai 3 4 time X. -. Fourier Coeffiets -.4 4 3 4 8
Fiite Legth Sequece Real lift sigal is geerally a fiitei legth sequece ( ( others If we periodic eted it by the period, the ( ( r ( r ( 9
Relatioship Betwee Fiite Legth Sequece ad Periodic Sequece A periodic sequece is the periodic etesio of a fiite legth sequece ( ( r (( m A fiite legth sequece is the pricipal value iterval of the periodic sequece ( ( R ( here R ( others So that: ( ( R ( IDFS [ X ( k ] R ( X ( k X ( k R ( k DFS[ ( ] R (
Discrete Fourier Trasform(DFT Usig the Fourier series represetatio we have Discrete Fourier Trasform(DFT for fiite legth sigal DFT ca covert time domai discrete sigal ito frequecydomai discrete spectrum Assume that we have a sigal { [ ]}. The the DFT of the sigal is a sequece X [ k ] for k,, X [ k ] [ ] e jk The Iverse Discrete Fourier Trasform(IDFT: [ ] k X [ k ] e jk / /,,,,. ote that because MATLAB caot use a zero or egative idices, the ide starts from i MATLAB
DFT Eample The DFT is widely used i the fields of spectral aalysis, acoustics, medical imagig, ad telecommuicatios. For eample: [ ] [ 4 6 ], 4, ( 3 j k,,,3 X [ k ] [ ] e [ ]( j X [ ] 4 ( 6 X [ ] ( 4 j 6 j 3 3 X [ ] ( 4 ( 6 9 X [ 3] ( 4 j 6 j 3 j k j Amplitude X[k] 6 5 4 3 Time domai sigal -.5.5.5 3 Time 8 6 4 Frequecy domai sigal.5.5.5 3 Frequecy
Fast Fourier Trasform(FFT The Fast Fourier Trasform does ot refer to a ew or differet type of Fourier trasform. It refers to a very efficiet algorithm for computig the DFT The time take to evaluate a DFT o a computer depeds pricipally o the umber of multiplicatios ivolved. DFT eeds multiplicatios. FFT oly eeds log ( The cetral isight which leads to this algorithm is the realizatio that a discrete Fourier trasform of a sequece of poits ca be writte i terms of two discrete Fourier trasforms of legth / Thus if is a power of two, it is possible to recursively apply this decompositio util we are left with discrete Fourier trasforms of sigle poits 3
Fast Fourier Trasform(cot. Fast Fourier Trasform(cot. Re writig / ] [ ] [ jk e k X as ] [ ] [ k k X It is easy to realize that the same values of are calculated may times as the k It is easy to realize that the same values of are calculated may times as the computatio proceeds Usig the symmetric property of the twiddle factor, we ca save lots of computatios ( ( ( ( ] [ ] [ ( r r k X r k kr odd k eve k k ( ( ( ( ( ( k X k X r r r r k r kr k r kr r r ( ( k X k X k Thus the poit DFT ca be obtaied from two / poit trasforms, oe o eve iput data, ad oe o odd iput data. 4
Itroductio for MATLAB MATLAB is a umerical computig eviromet developed by Mathorks. MATLAB allows matri maipulatios, plottig of fuctios ad data, ad implemetatio of algorithms Gettig help You ca get help by typig the commads help or lookfor at the >> prompt, e.g. >> help fft Arithmetic operators Symbol Operatio Eample + Additio 3. + 9 Subtractio 6. 5 * Multiplicatio * 3 / Divisio 5 / ^ Power 3^ 5
Data Represetatios i MATLAB Variables: Variablesaredefiedastheassigmetoperator =.Thesytaof variable assigmet is variable ame = a value (or a epressio For eample, >> = 5 = 5 >> y = [3*7, pi/3]; % pi is i MATLAB Vectors/Matrices: MATLAB ca create ad maipulate arrays of (ectors (vectors, (matrices, or more dimesios row vectors: a = [,, 3, 4] is a X4 matri colum vectors: b = [5; 6; 7; 8; 9] is a 5X matri, e.g. >> A = [ 3; 7 8 9; 4 5 6] A = 3 7 8 9 4 5 6 6
Mathematical Fuctios i MATLAB MATLAB offers may predefied mathematical fuctios for techical computig, e.g. cos( Cosie abs( Absolute value si( Sie agle( Phase agle ep( Epoetial coj( Comple cojugate sqrt( Square root log( atural logarithm Colo operator (: Suppose we wat to eter a vector cosistig of poits (,.,.,.3,,5. e ca use the commad >> = :.:5; Most of the work you will do i MATLAB will be stored i files called scripts, orm files, cotaiig sequeces of MATLAB commads to be eecuted over ad over agai 7
Basic plottig i MATLAB MATLAB has a ecellet set of graphic tools. Plottig a give data set or the results of computatio is possible with very few commads The MATLAB commad to plot a graph is plot(,y, e.g. >> = :pi/:*pi; p.8 >> y = si(;.6 >> plot(,y.4 Sie fuctio MATLAB eables you to add ais Labels ad titles, e.g. Sie of. -. -.4 >> label('=:\pi'; -.6 >> ylabel('sie of '; -.8 >> tile('sie fuctio' - 3 4 5 6 7 =: 8
Amplitude Power.5 -.5 Sie ave Sigal -..4.6.8 Time (s 8 6 4 Power Spectrum of a Sie ave 3 4 5 6 7 8 Frequecy (Hz Eample : Sie ave Fs = 5; % Samplig frequecy t = :/Fs:; % Time vector of secod f = 5; % Create a sie wave of f Hz. = si(*pi*t*f; i*t*f fft = 4; % Legth of FFT % Take fft, paddig with zeros so that legth(x is equal to fft X = fft(,fft; % FFT is symmetric, throw away secod half X = X(:fft/; % Take the magitude of fft of m = abs(x; % Frequecy vector f = (:fft/-*fs/fft; % Geerate the plot, title ad labels. figure(; plot(t,; title('sie ave Sigal'; label('time (s'; ylabel('amplitude'; l('a lit ' figure(; plot(f,m; title('power Spectrum of a Sie ave'; label('frequecy (Hz'; ylabel('power'; Frequecy (Hz ylabel( Power ; 9
Eample : Cosie ave Amplitude Power.5 -.5 Cosie ave Sigal -..4.6.8 Time (s 8 6 4 Power Spectrum of a Cosie ave 3 4 5 6 7 8 Frequecy (Hz Fs = 5; % Samplig frequecy t = :/Fs:; % Time vector of secod f = 5; % Create a sie wave of f Hz. = cos(*pi*t*f; fft = 4; % Legth of FFT % Take fft, paddig with zeros so that legth(x is equal to fft X = fft(,fft; % FFT is symmetric, throw away secod half X = X(:fft/; % Take the magitude of fft of m = abs(x; % Frequecy vector f = (:fft/-*fs/fft; % Geerate the plot, title ad labels. figure(; plot(t,; title('sie ave Sigal'; label('time (s'; ylabel('amplitude'; figure(; plot(f,m; title('power Spectrum of a Sie ave'; label('frequecy (Hz'; ylabel('power';
Eample 3: Cosie ave with Phase Shift Amplitude Power.5 -.5 Cosie ave Sigal with Phase Shift -..4.6.8 Time (s Power Spectrum of a Cosie ave Sigal with Phase Shift 8 6 4 3 4 5 6 7 8 Frequecy (Hz Fs = 5; % Samplig frequecy t = :/Fs:; % Time vector of secod f = 5; % Create a sie wave of f Hz. pha = /3*pi; % phase shift = cos(*pi*t*f + pha; fft = 4; % Legth of FFT % Take fft, paddig with zeros so that legth(x is equal to fft X = fft(,fft; % FFT is symmetric, throw away secod half X = X(:fft/; % Take the magitude of fft of m = abs(x; % Frequecy vector f = (:fft/-*fs/fft; / % Geerate the plot, title ad labels. figure(; plot(t,; title('sie ave Sigal'; label('time (s';; ylabel('amplitude'; figure(; plot(f,m; title('power Spectrum of a Sie ave'; label('frequecy (Hz'; ylabel('power';
Eample 4: Squareave Amplitude Power.5 -.5 Square ave Sigal -..4.6.8 Time (s 8 6 4 Power Spectrum of a Square ave 4 6 8 Frequecy (Hz Fs = 5; % Samplig frequecy t = :/Fs:; % Time vector of secod f = 5; % Create a sie wave of f Hz. = square(*pi*t*f; fft = 4; % Legth of FFT % Take fft, paddig with zeros so that legth(x is equal to fft X = fft(,fft; % FFT is symmetric, throw away secod half X = X(:fft/; % Take the magitude of fft of m = abs(x; % Frequecy vector f = (:fft/-*fs/fft; % Geerate the plot, title ad labels. figure(; plot(t,; title('square ave Sigal'; label('time (s'; ylabel('amplitude'; figure(; plot(f,m; title('power Spectrum of a Square ave'; label('frequecy (Hz'; ylabel('power';
Eample 5: SquarePulse Amplitude Power.8.6.4. Square Pulse Sigal -.5.5 Time (s 3 5 5 5 Power Spectrum of a Square Pulse 4 6 8 Frequecy (Hz Fs = 5; % Samplig frequecy t = -.5:/Fs:.5; % Time vector of secod w =.; % width of rectagle = rectpuls(t, w; % Geerate Square Pulse fft = 5; % Legth of FFT % Take fft, paddig with zeros so that legth(x is equal to fft X = fft(,fft; % FFT is symmetric, throw away secod half X = X(:fft/; % Take the magitude of fft of m = abs(x; % Frequecy vector f = (:fft/-*fs/fft; % Geerate the plot, title ad labels. figure(; plot(t,; title('square Pulse Sigal'; label('time (s'; ylabel('amplitude'; figure(; plot(f,m; title('power Spectrum of a Square Pulse'; label('frequecy (Hz'; ylabel('power'; 3
Amplitude r Power 4 3 Gaussia Pulse Sigal -.5.5 Time (s 6 5 4 3 Eample 6: Gaussia Pulse Power Spectrum of a Gaussia Pulse 5 5 5 3 Frequecy (Hz Fs = 6; % Samplig frequecy t = -.5:/Fs:.5; = /(sqrt(*pi*.*(ep(-t.^/(*.; fft = 4; % Legth of FFT % Take fft, paddig with zeros so that legth(x is equal to fft X = fft(,fft; % FFT is symmetric, throw away secod half X = X(:fft/; % Take the magitude of fft of m = abs(x; % This is a evely spaced frequecy vector f = (:fft/-*fs/fft; % Geerate the plot, title ad labels. figure(; plot(t,; title('gaussia Pulse Sigal'; label('time (s'; ylabel('amplitude'; figure(; plot(f,m; title('power Spectrum of a Gaussia Pulse'; label('frequecy (Hz'; ylabel('power'; 4
Eample 7: Epoetial Decay Amplitude Powe er.5.5 7 6 5 4 3 Epoetial Decay Sigal..4.6.8 Time (s Power Spectrum of Epoetial Decay Sigal 4 6 8 Frequecy (Hz Fs = 5; % Samplig frequecy t = :/Fs:; % Time vector of secod = *ep(-5*t; fft = 4; % Legth of FFT % Take fft, paddig with zeros so that legth(x is equal to fft X = fft(,fft; % FFT is symmetric, throw away secod half X = X(:fft/; % Take the magitude of fft of m = abs(x; % This is a evely spaced frequecy vector f = (:fft/-*fs/fft; % Geerate the plot, title ad labels. figure(; plot(t,; title('epoetial Decay Sigal'; label('time (s'; ylabel('amplitude'; figure(; plot(f,m; title('power Spectrum of Epoetial Decay Sigal'; label('frequecy (Hz'; ylabel('power'; 5
Eample 8: Chirp Sigal Amplitude Pow wer.5 -.5 Chirp Sigal -..4.6.8 Time (s 5 5 5 Power Spectrum of Chirp Sigal 4 6 8 Frequecy (Hz Fs = ; % Samplig frequecy t = :/Fs:; % Time vector of secod = chirp(t,,,fs/6; fft = 4; % Legth of FFT % Take fft, paddig with zeros so that legth(x is equal to fft X = fft(,fft; % FFT is symmetric, throw away secod half X = X(:fft/; % Take the magitude of fft of m = abs(x; % This is a evely spaced frequecy vector f = (:fft/-*fs/fft; % Geerate the plot, title ad labels. figure(; plot(t,; title('chirp Sigal'; label('time (s'; ylabel('amplitude'; figure(; plot(f,m; title('power Spectrum of Chirp Sigal'; label('frequecy (Hz'; ylabel('power'; 6