Sei sulla pagina 1di 19

Conceptualmente hablando FFT es bastante simple. En la prctica, la FFT es difcil!

As que no voy a entrar en todos los detalles de este al oritmo y voy a permanecer centrado en su uso y aplicaciones. Esta serie de mensa!es va a terminar con una biblioteca con todas las funciones que usted ser capa" de utili"ar para todo tipo de aplicaciones, sin preocuparse de las matemticas. #a trama en un osciloscopio muestra una onda que est hecha de muestras que se caracteri"an por sus intensidades $ordenadas% y su tiempo de muestreo $abscisa%. Estamos en el dominio del tiempo.

Cuanto ms simple sea la se&al $por e!emplo, una onda sinusoidal simple%, el ms simple es la caracteri"aci'n. #a transformada de Fourier propone para descomponer cualquier se&al en una suma de seno y coseno. (ara cada punto de datos del espectro de potencia de FFT corresponde una ma nitud $ordenadas% y una frecuencia $abscisa%. Ahora estamos en el dominio de la frecuencia.

Cuanto ms comple!a es la se&al $por e!emplo, la se&al ) ruido ) arm'nicos ) )% ...

..... ms comple!a es la caracteri"aci'n.

* +adivinen qu,- El al oritmo de la FFT se puede e!ecutar en modo inverso, de modo que a partir de una FFT puede reconstruir una se&al de la vida real. (odemos estudiar ms adelante c'mo esta propiedad puede ser utili"ado para la de eliminaci'n de ruido de las se&ales. FFT se aplica a vectores que contienen las muestras n, donde n debe ser una potencia de .. E!ecutar el al oritmo de estos datos puede conducir a resultados inesperados. #a ra"'n es que el vector trunca la informaci'n de la se&al y pueden contener ondas incompletamente descritas $por e!emplo,

las ondas de ba!a frecuencia%. Este efecto secundario puede ser corre ida por medio de pesa!e de la se&al, dando menos importancia a los datos principales y colas. Esta es la funci'n de ventanas. #a funci'n de pesa!e en la ventanas depende del tipo de se&al a anali"ar/ Transients 0hose duration is shorter than the len th of the 0indo0 / 1ectan ular $2o3 car% Transients 0hose duration is lon er than the len th of the 0indo0 / E3ponential, 4ann 5eneral6purpose applications / 4ann 7pectral analysis $frequency6response measurements% / 4ann $for random e3citation%, 1ectan ular $for pseudorandom e3citation% 7eparation of t0o tones 0ith frequencies very close to each other but 0ith 0idely differin amplitudes / 8aiser62essel 7eparation of t0o tones 0ith frequencies very close to each other but 0ith almost equal amplitudes / 1ectan ular Accurate sin le6tone amplitude measurements / Flat top 7ine 0ave or combination of sine 0aves / 4ann 7ine 0ave and amplitude accuracy is important / Flat top 9arro0band random si nal $vibration data% / 4ann 2roadband random $0hite noise% / :niform Closely spaced sine 0aves / :niform, 4ammin E3citation si nals $hammer blo0% / Force 1esponse si nals / E3ponential :n;no0n content / 4ann <nce the 0indo0in is e3ecuted, you can run the FFT. The result of this al orithm lies in to vectors containin the real and ima inary computed values. *ou need then to apply some math to convert them into a vector of intensities. =indo0 type 1ectan le $bo3 car%

=indo0 type 4ammin /

=indo0 type Flat top

All plots e3ported from my >7( tool bo3 ?(anorama@

4ere is a recap of the pieces of code that 0e need in order to convert a 0ave into a frequency spectrum/ 7tore data in a vector $>ouble data type% =ei h this data accordin to one function $default is 1ectan le, also ;no0n as bo3 car, in other 0ords, no 0ei hin !% E3ecute the FFT al orithm Convert comple3 values in usable data =ithout ;no0in too much of the details from the various al orithms, you 0ill very quic;ly face a dilemma/ 0ould you prefer speed or precision- (recision 0ill require lar e vectors of data, 0hile speed 0ill require multiple vectors/ e. . instead of computin 0ei hed values for each ne0 set of

data, 0e could compute 0ei hin factors once and apply them repeatedly. 7ame thou hts for the bit reversal at the top of the FFT al orithm. The proposed e3ample do not use pre6processin of data, to the benefit of the number of samples, and to the clarity of the code. Firstly, some constants shall be declared in the header $.h% file

01 02 03 04 0$ 02 06 07 08 10 11

// Custom constants #define FORWARD 0x01 #define REVERSE 0x00 // Windo in! t"#e #define W%&'()*'REC(A&+,E 0x00// -ectan!.e /0ox ca-1 #define W%&'()*'3A44%&+ 0x01// 5ammin! #define W%&'()*'3A&& 0x02// 5ann #define W%&'()*'(R%A&+,E 0x03// t-ian!.e /0a-t.ett1 #define W%&'()*'0,AC94A& 0x04// :.ac;mann #define W%&'()*'F,('(O* 0x0$// f.at to# #define W%&'()*'WE,C3 0x02// e.c5

Then data shall be stored in one of the t0o fi3ed si"e vectors 1 const uint7't sam#.es < 24= 2 dou:.e >Rea.?sam#.es@= 3 dou:.e >%ma!?sam#.es@= And this is the 0ei hin routine >oid *.ainFF(AA indo in!/dou:.e B>DataC uint7't sam#.esC uint7't indo ("#eC uint7't di-1 D // (5e ei!5in! function is s"met-ic= 5a.f t5e ei!5s a-e -eco-ded dou:.e sam#.es4inusOne < /dou:.e/sam#.es1 E 1F01= fo- /uint7't i < 0= i G /sam#.es HH 11= iII1 D dou:.e index4inusOne < dou:.e/i1= dou:.e -atio < /index4inusOne / sam#.es4inusOne1= dou:.e ei!5in!Facto- < 1F0= // com#ute and -eco-d ei!5tin! factos itc5 / indo ("#e1 D case W%&'()*'REC(A&+,EA // -ectan!.e /:ox ca-1 ei!5in!Facto- < 1F0= :-ea;= case W%&'()*'3A44%&+A // 5ammin!

01 02 03 04 0$ 02 06 07 08 10 11 12 13

14 1$ 12 16 17 18

ei!5in!Facto- < 0F$4 E /0F42 B cos/2F0 B #i B -atio11= :-ea;= case W%&'()*'3A&&A // 5ann ei!5in!Facto- < 0F$4 B /1F0 E cos/2F0 B #i B -atio11=

:-ea;= case W%&'()*'(R%A&+,EA // t-ian!.e /0a-t.ett1 ei!5in!Facto- < 1F0 E //2F0 B a:s/index4inusOne E 20 /sam#.es4inusOne / 2F0111 / sam#.es4inusOne1= 21 :-ea;= 22 case W%&'()*'0,AC94A&A // :.ac;mann ei!5in!Facto- < 0F42323 E /0F486$$ B /cos/2F0 B #i 23 B -atio111 I /0F06822 B /cos/4F0 B #i B -atio111= 24 :-ea;= 2$ case W%&'()*'F,('(O*A // f.at to# ei!5in!Facto- < 0F2710238 E /0F$207862 B cos/2F0 B 22 #i B -atio11 I /0F1870388 B cos/4F0 B #i B -atio11= 26 :-ea;= 27 case W%&'()*'WE,C3A // e.c5 ei!5in!Facto- < 1F0 E sJ//index4inusOne E 28 sam#.es4inusOne / 2F01 / /sam#.es4inusOne / 2F011= 30 :-ea;= 31 K 32 if /di- << FORWARD1 D 33 >Data?i@ B< ei!5in!Facto-= 34 >Data?sam#.es E /i I 11@ B< ei!5in!Facto-= 3$ K 32 e.se D 36 >Data?i@ /< ei!5in!Facto-= 37 >Data?sam#.es E /i I 11@ /< ei!5in!Facto-= 38 K 40 K 41 K 9otes/ There is a little tric; here. As the 0ei hin function is symetrical, 0hy bother computin them all- 4alf of them are computed and applied symetrically to the vector of data The dir parameter stands for direction/ remember, FFT is reversible, so that 0e can apply it in FORWARD or REVERSE mode FFT stands for fast Fourier Transform. The DFT $>irect Fourier Transform% applies to vectors containin any number of si nal samples. 2ut it is sssssssssllllllllllllllllllllllloooooooooooooo00000000 due to the repeated number of operation. Computin a DFT of n points ta;es nA. arithmetical operations, 0hile an FFT can compute the same result in only n lo .$n% operations. The implemented al orithm is the ?CooleyBTu;ey@

al orithm 0hich the far most popular. The ?only@ limitation is that the number of samples in the si nal must be a power of two. 4o0ever, if the number of samples is less than that, it is possible to replace missin data 0ith C values 0ithout affectin the final result/ this is the zero padding. 01 02 03 04 0$ 02 06 07 08 10 11 12 13 14 1$ 12 16 17 18 20 21 22 23 24 2$ 22 26 27 28 30 31 32 33 34 3$ 32 36 37 >oid *.ainFF(AAcom#ute/dou:.e B>RC dou:.e B>%C uint7't sam#.esC uint7't di-1 D // (5is com#utes an inE#.ace com#.exEtoEcom#.ex FF( // di- < 1 !i>es fo- a-d t-ansfo-mC di- < 0 !i>es -e>e-se t-ansfo-m // Re>e-se :its uint7't L < 0= fo- /uint7't i < 0= i G /sam#.es E 11= iII1 D if /i G L1 D s a#/M>R?i@C M>R?L@1= s a#/M>%?i@C M>%?L@1= K uint7't ; < /sam#.es HH 11= 5i.e /; G< L1 D L E< ;= ; HH< 1= K L I< ;= K // Com#ute t5e FF( dou:.e c1 < E1F0= dou:.e c2 < 0F0= uint7't .2 < 1= fo- /uint7't . < 0= . G ex#onent/sam#.es1= .II1 D uint7't .1 < .2= .2 GG< 1= dou:.e u1 < 1F0= dou:.e u2 < 0F0= fo- /L < 0= L G .1= LII1 D fo- /uint7't i < L= i G sam#.es= i I< .21 D uint7't i1 < i I .1= dou:.e t1 < u1 B >R?i1@ E u2 B >%?i1@= dou:.e t2 < u1 B >%?i1@ I u2 B >R?i1@= >R?i1@ < >R?i@ E t1= >%?i1@ < >%?i@ E t2= >R?i@ I< t1= >%?i@ I< t2= K dou:.e N < /u1 B c11 E /u2 B c21= u2 < /u1 B c21 I /u2 B c11=

38 40 41 42 43 44 4$ 42 46 47 48 $0 $1 $2 K 9otes/

u1 < N= K c2 < sJ-t//1F0 E c11 / 2F01= if /di- << FORWARD1 c2 < Ec2= c1 < sJ-t//1F0 I c11 / 2F01= K // Sca.in! fo- fo- a-d t-ansfo-m if /di- << FORWARD1 D fo- /uint7't i < 0= i G sam#.es= iII1 D >R?i@ /< sam#.es= >%?i@ /< sam#.es= K K

The bit reversal routine could be performed once for all FFTDs usin vectors of the same si"e, but to the cost of an e3tra vector 0hich our limited memory cannot afford. The dir parameter stands for direction/ remember, FFT is reversible, so that 0e can apply it in FORWARD or REVERSE mode 1esultin Ema inary and 1eal data are output in the input data vectors <nce the FFT al orithm is e3ecuted, 0e et comple3 values, made of imaginary values and real values. =e need to apply some maths in order to convert them in magnitude values. This is quite simply done than;s to the follo0in routine 0hich converts the comple3 $so as to say rectan ular% values into a polar value/ >oid *.ainFF(AAcom#.ex(o4a!nitude/dou:.e B>RC dou:.e B>%C uint7't sam#.es1 D 2 // >4 is 5a.f t5e siNe of >R and >% 3 fo- /uint7't i < 0= i G sam#.es= iII1 D 4 >R?i@ < sJ-t/sJ/>R?i@1 I sJ/>%?i@11= $ K 2 K 1ead this document about conversion $p F% 1 Computin the phase information is quite easy, but E do not really care about it and it requires the arctangent() tri onometric 0hich does not come standard in Arduino lan ua e. 4o0ever if you need it, you 0ill have to use the mat library% and compute the pahse values usin the follo0in formula/ phaseGFFT$A%HIarctan ent$JEma GFFT$A%HJ K J1ealGFFT$A%HJ%.

7o far so ood, 0e ot our ma nitude spectrum. =e may no0 analy"e it !ust li;e any other spectrum in order to et meanin ful information. En our case, 0e may 0ant to identify the main frequencies from the spectrum. Firstly 0e 0ill run a pea; pic;in al orithm in order to locate ma!or pea;s, and then ;eep the most intense one. Those 0ho 0ant to perform spectral comparison 0ill have to ;eep all si nificant pea;s.

There are lots of pea; pic;in al orithm! 2ut the one 0e need has to be small and fast. This one is pretty trivial but it 0or;s reat. 7econdly, because of the poor pea; shapes $lac; of lar e vectors of data% 0e need to interpolate the pea; ape3 in order to et an accurate frequency. E am usin a non iterative quadratic interpolation 0hich ives ood results too. 2oth routines are mer ed in one function 01 dou:.e maLo-*ea;/dou:.e B>DC uint7't sam#.es1 D 02 dou:.e max) < 0= 03 uint7't %ndexOf4ax) < 0= 04 fo- /uint7't i < 1= i G /sam#.es E 11= iII1 D 0$ if //>D?iE1@ G >D?i@1 MM />D?i@ H >D?iI1@11 D 02 if />D?i@ H max)1 D 06 max) < >D?i@= 07 %ndexOf4ax) < i= 08 K 10 K 11 K dou:.e de.ta < 0F$ B //>D?%ndexOf4ax)E1@ E 12 >D?%ndexOf4ax)I1@1 / />D?%ndexOf4ax)E1@ E /2F0 B >D?%ndexOf4ax)@1 I >D?%ndexOf4ax)I1@11= dou:.e inte-#o.atedO < /%ndexOf4ax) I de.ta1 / 13 sam#.in!Du-ation= 14 -etu-n/inte-#o.atedO1= 1$ K

(lainFFT is a simple but effective library 0hich contains all the previously described functions for runnin FFT on vectors of data. 4ere is the e3ample code from the library files 0hich demonstrates the $pretty easy% use of the FFT. The content of the vectors of interest is printed on completion of each FFT sta e 01 #inc.ude P*.ainFF(F5P 02 03 *.ainFF( FF( < *.ainFF(/1= // C-eate FF( o:Lect // (5ese >a.ues can :e c5an!ed in o-de- to e>a.uate t5e 04 functions 0$ const uint12't sam#.es < 24= 02 dou:.e si!na.F-eJuenc" < 1000= 06 dou:.e sam#.in!F-eJuenc" < $000= 07 uint7't si!na.%ntensit" < 100= 08 // (5ese a-e in#ut and out#ut >ecto-s 10 dou:.e >Rea.?sam#.es@= 11 dou:.e >%ma!?sam#.es@= 12 uint7't -unOnce < 0x00=

13 14 1$ 12 16 17 18 20 21 22 23 24 2$ 22 26 27 28 30 31 32 33 34 3$ 32 36 37 38

#define SC,'%&DEO 0x00 #define SC,'(%4E 0x01 #define SC,'FREQRE&C) 0x02 >oid setu#/1D Se-ia.F:e!in/11$2001= Se-ia.F#-int.n/PRead"P1= K >oid .oo#/1 D if /-unOnce << 0x001 D -unOnce < 0x01= // 0ui.d -a data dou:.e c"c.es < ///sam#.esE11 B si!na.F-eJuenc"1 / sam#.in!F-eJuenc"1= fo- /uint7't i < 0= i G sam#.es= iII1 D >Rea.?i@ < uint7't//si!na.%ntensit" B /sin//i B /2F2731 B c"c.es11 / sam#.es1 I 1F011 / 2F01= K #-intVecto-/>Rea.C sam#.esC SC,'(%4E1= FF(F indo in!/>Rea.C sam#.esC FF('W%&'()*'3A44%&+C FF('FORWARD1= // Wei!5 data #-intVecto-/>Rea.C sam#.esC SC,'(%4E1= FF(Fcom#ute/>Rea.C >%ma!C sam#.esC FF('FORWARD1= // Com#ute FF( #-intVecto-/>Rea.C sam#.esC SC,'%&DEO1= #-intVecto-/>%ma!C sam#.esC SC,'%&DEO1= FF(Fcom#.ex(o4a!nitude/>Rea.C >%ma!C sam#.es1= // Com#ute ma!nitudes #-intVecto-/>Rea.C /sam#.es HH 11C SC,'FREQRE&C)1= dou:.e x < FF(FmaLo-*ea;/>Rea.C sam#.esC sam#.in!F-eJuenc"1= Se-ia.F#-int.n/xC 21= K K

40 41 42 43 44 >oid #-intVecto-/dou:.e B>DC uint7't nC uint7't sca.e("#e1 D 4$ dou:.e time%nte->a. < /1F0 / sam#.in!F-eJuenc"1= 42 fo- /uint12't i < 0= i G n= iII1 D 46 // *-int a:scissa >a.ue 47 s itc5 /sca.e("#e1 D 48 case SC,'%&DEOA $0 Se-ia.F#-int/iC DEC1=

$1 $2 $3 $4 $$ $2 21=

:-ea;= case SC,'(%4EA Se-ia.F#-int//i B time%nte->a.1C 21= :-ea;= case SC,'FREQRE&C)A Se-ia.F#-int//i / /time%nte->a. B /sam#.esE1111C

$6 :-ea;= $7 K $8 Se-ia.F#-int/P P1= 20 // *-int o-dinate >a.ue 21 Se-ia.F#-int/>D?i@C 21= 22 Se-ia.F#-int.n/1= 23 K 24 Se-ia.F#-int.n/1= 2$ K Attention! The distributed code is sli htly different from the posted code due to last minute chan es and optimi"ations 7amples value must be a po0er of . La;e sure you have enou h memory before incresin the si"e of vectors Mectors must contain unsi ned inte ers $C to N.O%, you can easily chan e that to NF bits inte ers $si ned or not% to the cost of doublin the vectors si"e... 1emember the 9yquist theorem 0hen chan in the samplin and the si nal frequencies (lease chec; t i" page if you are interested in the code. Feel free to pass your comments and su estions. Those 0ho e3erci"ed the library and tried to chan e the default value from the customi"able variables $and most probably the si"e of both data vectors% probably faced some loc;ups, inifinite loops or unpredicted operations. En most cases, the ans0er to these problems lies in the limited si"e of the arduino memoryP #etQs be positive and study this sub!ect. >ependin upon the type of Arduino platform, you may use one of these microprocessors/ Arduino >iecimila/ ATLe aNFR $data sheet% Arduino >uemilanove/ ATLe aSFR $data sheet% Arduino :no/ ATme aS.R $data sheet% For other platforms, follo0 this thread For older platforms, follo0 this thread Each of these microprocessors has three separate memories/ F#A74 memory/ this is a non volatile memory space 0here the s;etch is stored 71AL $7tatic 1andom Access Lemory%/ this is the memory sapce 0here the s;etch creates and manipulates variables at run time EE(1<L $Electrically6Erasable (ro rammable 1ead6<nly Lemory%/ this is a non volatile memory space 0here data can be readK0rite at run time. This memory has limitations/ the

number of readK0rite cycle is estimated to be no more than NCC.CCC, and it ta;es real time to et access to it #etQs o bac; to our problem. The si"e of the 71AL $0here the vectors are created at run time% depends upon the microprocessor/ ATLe aNFR/ #$%& bytes ATLe aSFR/ %$&' bytes That is not muchP and every byte counts! 9o0 letQs consider the vectors. Firstly, 0e have to decide about the type of data to be recorded/ (oolean or c ar or un"igned c ar or (yte or uint')t/ N byte each int or un"igned int or uint#*)t or int#*)t/ . bytes each long or un"igned long or uint+%)t or int+%)t/ T bytes each float or dou(le/ T bytes each

For more information on data types, follo0 this thread This means that the follo0in vectors 0ill occupy/ 1 :"te >O?32@= // 32B7<2$2 :"tes 2 int >O?32@= // 32B12<$12 :"tes 3 f.oat >O?32@= // 32B32<1024 :"tes 7o that it is a ood idea to chec; the available memory space prior to runnin s;etches. This ho0 you can do this/ 01 int memo-"(est/1 D // (5is function i.. -etu-n t5e num:e- of :"tes cu--ent." f-ee 02 in SRA4 03 int :"teCounte- < 0= // initia.iNe a counte04 :"te B:"teA--a"= // c-eate a #ointe- to a :"te a--a" // Rse t5e ma..oc function to -e#eated." attem#t a..ocatin! a 0$ ce-tain num:e- of :"tes to memo-" 5i.e / /:"teA--a" < /:"teB1 ma..oc /:"teCounte- B 02 siNeof/:"te111 S< &R,, 1 D :"teCounte-II= // %f a..ocation as successfu.C t5en u# t5e 06 count fo- t5e next t-" 07 f-ee/:"teA--a"1= // F-ee memo-" afte- a..ocatin! it 08 K f-ee/:"teA--a"1= // A.so f-ee memo-" afte- t5e function 10 finis5es -etu-n :"teCounte-= // Send :ac; t5e num:e- num:e- of :"tes 11 5ic5 5a>e :een successfu.." a..ocated 12 K This code has been 0ritten by 1ob Faludi

E received some questions related to the use of the FFT library. This e3ample illustrate ho0 to interface the FFT function to an acquisition en ine, such as the optimi"ed one from the (lainA>C library.

01 /B 02 03 04 0$ 02

Exam#.e of use of t5e ADC and FF( .i:-a-ies Co#"-i!5t /C1 2010 Didie- ,on!ue>i..e

(5is #-o!-am is f-ee soft a-eA "ou can -edist-i:ute it and/o- modif" it unde- t5e te-ms of t5e +&R +ene-a. *u:.ic ,icense as 06 #u:.is5ed :" t5e F-ee Soft a-e FoundationC eit5e- >e-sion 3 of t5e 07 ,icenseC o08 /at "ou- o#tion1 an" .ate- >e-sionF 10 (5is #-o!-am is dist-i:uted in t5e 5o#e t5at it i.. :e 11 usefu.C :ut W%(3OR( A&) WARRA&()= it5out e>en t5e im#.ied a--ant" 12 of 4ERC3A&(A0%,%() o- F%(&ESS FOR A *AR(%CR,AR *RR*OSEF See 13 t5e 14 +&R +ene-a. *u:.ic ,icense fo- mo-e detai.sF 1$ )ou s5ou.d 5a>e -ecei>ed a co#" of t5e +&R +ene-a. *u:.ic 12 ,icense a.on! it5 t5is #-o!-amF %f notC see G5tt#A F!nuFo-!<PP 16 .icenses<PPHF 17 18 B/ 20 21 22 23 /B *-intin! function o#tions B/ 24 #define SC,'%&DEO 0x00 2$ #define SC,'(%4E 0x01 22 #define SC,'FREQRE&C) 0x02 26 27 #inc.ude G#.ainadcF5H 28 #inc.ude G#.ainfftF5H 30 *.ainADC ADC < *.ainADC/1= /B C-eate ADC o:Lect B/ 31 *.ainFF( FF( < *.ainFF(/1= /B C-eate FF( o:Lect B/ 32 33 /B Rse- defined >a-ia:.es B/ 34 const uint12't sam#.es < 127= 3$ uint12't f-eJuenc" < 20000= 32 uint7't c5anne. < 0=

36 37 38 40 41 42 43 44 4$ 42 46 47 48 $0 $1 $2 $3 $4 $$ $2 $6 $7 $8 20 21

/B Data >ecto-s B/ uint7't >Data?sam#.es@= dou:.e >Rea.?sam#.es@= dou:.e >%ma!?sam#.es@= >oid setu#/1D /B %nitia.iNe se-ia. comm #o-t B/ Se-ia.F:e!in/11$2001= // /B Set acJuisition #a-amete-s B/ ADCFsetAcJuisition*a-amete-s/c5anne.C sam#.esC f-eJuenc"1= K >oid .oo#/1 D /B AcJui-e data and sto-e t5em in a >ecto- of :"tes B/ ADCFacJui-eData/>Data1= /B Con>e-t 7 :its unsi!ned data in 32 :its f.oats B/ fo- /uint12't i < 0= i G sam#.es= iII1 D >Rea.?i@ < dou:.e/>Data?i@1= K /B Wei!5 data B/ FF(F indo in!/>Rea.C sam#.esC FF('W%&'()*'3A44%&+C FF('FORWARD1= /B Com#ute FF(A >Rea. and >%ma! >ecto-s contain t5e sou-ce data and i.. contain t5e -esu.t data on com#.etion of executin! t5e functionB/ FF(Fcom#ute/>Rea.C >%ma!C sam#.esC FF('FORWARD1= /B Com#ute ma!nitudesA t5e -esu.tin! data can :e -ead f-om t5e >Rea. >ecto- B/ FF(Fcom#.ex(o4a!nitude/>Rea.C >%ma!C sam#.es1= /B R#.oad f-eJuenc" s#ect-um B/ #-intVecto-/>Rea.C /sam#.es HH 11C SC,'FREQRE&C)1= /B *ause B/ de.a"/$0001= K

22 23 24 2$ 22 26 27 28 >oid #-intVecto-/dou:.e B>DC uint7't nC uint7't sca.e("#e1 D 60 /B 4u.it#u-#ose #-intin! function B/ 61 dou:.e time%nte->a. < /1F0 / f-eJuenc"1= 62 fo- /uint12't i < 0= i G n= iII1 D 63 /B *-int a:scissa >a.ue B/ 64 s itc5 /sca.e("#e1 D 6$ case SC,'%&DEOA Se-ia.F#-int/iC DEC1= :-ea;=

62 66 67 68 70 71 72 73 74 7$ 72

case SC,'(%4EA Se-ia.F#-int//i B time%nte->a.1C 21= :-ea;= case SC,'FREQRE&C)A Se-ia.F#-int//i / /time%nte->a. B /sam#.esE1111C 21= :-ea;= K Se-ia.F#-int/P P1= /B *-int o-dinate >a.ue B/ Se-ia.F#-int/>D?i@C 21= Se-ia.F#-int.n/1= K Se-ia.F#-int.n/1= K G/#.ainfftF5HG/#.ainadcF5HG/5tt#AH

4ere are a fe0 comments on the use of (lainFFT. First of all/ (lainFFT performs in place calculation, 0hich means that the results are recorded 0ithin the vectors containin the source data. ?:sable@ results are available on completion of the Comple3To1eal$% e3ecution. 2efore e3ecution, v1eal and vEma vectors contain the transformed results from the FFT. After e3ecution, the v1eal vector containsP t0o times the frequency spectrum in a perfectly symetric 0ay. The (lainFFTUCN.ino s;ecth e3ample sho0s 1 /B Com#ute ma!nitudes B/ 2 FF(FCom#.ex(oRea./>Rea.C >%ma!C sam#.esC FF('SC,'()*'A4*,%(RDE1= =hile in fact 0e could save processin time 0ith 1 /B Com#ute ma!nitudes B/ FF(FCom#.ex(oRea./>Rea.C >%ma!C sam#.es HH 1C 2 FF('SC,'()*'A4*,%(RDE1= As lon as, in any 0ay the s;etch uses 1 /B *-int f-eJuenc" s#ect-um B/ 2 *-intVecto-/>Rea.C /sam#.es HH 11C SC,'FREQRE&C)1= for printin the spectrum. CVF>. The samplin frequency $#etQs call this variable Fs% determines the frequency ran e of the spectrum 0hile the number of points acquired durin si nal acquisition $#etQs call this variable 9% determines the resolution frequency. As a consequence of that// 6 To Encrease the frequency ran e, increase the si nal samplin frequency 6 To increase the frequency resolution for a iven frequency ran e, increase the number of points acquired at the same samplin frequency. The result from the FFT can be vie0ed as a collection of 9K. ?bins@. To each bin corresponds a frequency ran e $or band0idth%, centred on a frequency value, as reported in the v1eal vector. The 0idth of each bin $#etQs call this variable >f% is equal to FsK9. The last bin is centred on the ma3 frequency $#etQs call this variable Fma3% at $FsK.%6$FsK9%. The first bin is a little bit special/ Et is centred on C4", so as to say on the >C component of the si nal. 7o that the raphical bin representation should be half the si"e of the other bins! All these parameters can be pictured in the follo0in 0ay

0in 1 0in 2 0in 3 0in &/2 F F F F // F F F F F F // F F F F F F // F F F A F A F A F // F A F T T T T T T T T T EE T T T EEEEEEE T T EEEEEEEEEEEEEEEEEEEEE T EEEEEEEEEEEEEEEEEEEEEEEEEEE EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

&"Juist f-eJuenc"/Fs/21 4axF F-eJuenc" /Fmax1 Df B 2 Df B 1 Df B 0C 03N /DC1

En the real 0orld, the amount of si nal resultin from/ 6 >f W C )K6 >f K., in other 0ords Ch", is read from v1eal GCH 6 >f W N )K6 >f K. is read from v1eal GNH 6 >f W . )K6 >f K. is read from v1eal G.H And so on, up to 6 >f W 9K. )K6 >f K., so as to say Fma3, is read from v1eal G9K.H Each bin contains the po0er of one or more si nals 0hich frequencies fit in the band band0ith. From this point, it is obvious that the narro0er the bin, the better the selectivity of the filter. En some, not to say most, circumstances, the spectrum representation does not e3actly loo;s li;e pure histo rams. For a iven si nal characteri"ed by a pure sinusoidal function, the most abundant po0er is read from the bin 0hich corresponds to the si nal frequency, but the ad!acent bins may contain some po0er abundances too. This is due to the frequency lea;a e. =e may use one from the ne3t options to overcome this phenomenon/ 6 Encrease the number of samples $9% in order to improve the selectivity 6 Apply appropriate windowing 6 Apply spectrum pea; interpolation (ea; interpolation helps improvin the most accurate pea; ape3 location than;s to some linear $e. . Vuadratic fit% or non6linear $e. . 5aussian fit%. =hile this interpolation applies to a limited number of consecutive data points, 0e may apply simplified formula for computin ape3 locations. (ea; interpolation is almost mandatory 0hen all si nificant pea;s from a frequency spectrum must be correlated $e. . Findin harmonics%. The follo0in pictures illustrate various use cases of (lainFFT for plottin frequency spectrum. En all cases, a 4ann 0indo0 0as applied as 0ell as 5aussian pea; ape3 interpolation. 4ere is the frequency spectrum from a sinusoidal si nal acquired 0ith a samplin rate of N;4" over .XF samples/

4ere is the frequency spectrum from a sinusoidal si nal acquired 0ith a samplin rate of N;4" over S.OFR samples/

4ere is the frequency spectrum from a sinusoidal si nal acquired 0ith a samplin rate of XCC4" over .XF samples/

,ote/ The pea; loo;s broader than in picture N, but this feelin is related to the narro0er scale! This is probably on over6simplified e3planation, but it is sufficient for usin (lainFFT in the most appropriate manner. Feel free to add your comments and su estions.

Potrebbero piacerti anche