Руководство по цифровому фильтрованию, включая основы, важные детали и советы по реализации, с примерами кода из реальных инженерных проектов.
Document text
FEATURES A Digital Filtering Primer FEATURE ARTICLE common ma xi m for designing embedded controllers is that a controller is no better than its feedback sensor. For examp l e, if you are trying to control the positio n o f someth i ng, a controller ' can do no bet t er than its sensor's ability to measure a position. You c a n have the hotte st m i croprocessor or DSP in t h e world, but if you can't acc u rat e ly s ense what you are tryi n g to cont r ol , you wi l l get po o r results .
Introduction to Doremi-DSP Fast-scaling Routine for Floating-point RiSe and DSP Processors You'll often find articles written about But, what if you are stuck us i n g a sensor t h at is noisy or a few bits short of resolut io n! Is there anything you can do! "Yes ! " The key is to u se the pro c essor t o enhance the data b e fore using it to · control the data. And, the best part is ther e are s i mple techniques that en a ble you t o do this even with a l ow - performance processor . If you need this kind of informa - tion , I invite you to j oin me on a jour n ey into th e w o rld of d i gital f iltering .
We ' ll take a look at how digital filte r s work, important deta i l s t o rem e mbe r when using digital f ilters , and implementation t i ps including sample code from re a l engineering projects . digital filters, how they work, and how to design them. But, how often do they approach the subject from a practical standpoint? Tom shares some design tips from the trenches. THE BASI C S The most common digital filterin g technique is to si mpl y tak e a r u n n ing average o f s ev e r a l samples o ~ , data .
The id e a is that ra th er th a n j ust re ad in g the transduce r each time yo u c l o se yo ur contr o l loop , you re ad it every t i m e y ou t a ke a pie ce o f da t a and a ve rag e it i nto t h e p r e vious val u e usi ng a w ei ghting fa ct or . I n t he p ro cess, t he
Page 1
520 518 516 ~ 514 ~ 0 512 U U 0 510 « 508 attenuation arc introduc e d by d i gital filtering , as with any type of f iltering . Table 2 shows the actual phase lags a s determined again from a simple spreadsheet model of the filter and response. IMPORTANT DETAIL TO REMEMBER Now that we have looked at how a digital filter works, we need to look at some details that are important to know, but that textbooks usually forget to mention . 506 504 10 15 20 25 30 Time data becomes less noisy since random errors tend to cancel . Mathematically, this is expressed as :
there was one bit labout 0.04 VI of white noise on th e signal, it usually returns 129 with occasional values of The time constant needs tuning. When you use a digital filt e r, the time constant becomes an add i tio n al item to tune. For example, if you use digital filtering to clean up data used in a PID servo loop, you w i ll need to tune the filter constant as well as the P, I, and D gains . Furthermore, s i nce the actual time constant of the f ilter is a function of both the filter constant K 1 . 10 and 12R.
If thc noisc is truly white la fairly good assumption I , we would find that the occurrenccs of the othcr values would alter the filtered value to he 129.25, a resolution you normally need a lO-bit ADC to obtain. where X/iI""d _ MW is the latest filtered value, Xfi/If ' m/"I . /, the previous filtered value, X ,.w , the value just read from the sensor, . lI1d K, the filter constant (this always has a value between 0 and 1 in which 0 represents no filterinl; and 1 involves total filt e ringl. To minimize the number of multiplica- tion operation5, this equation is usually implemented as: 100 3: 60 0 "- '" Q) II:
40 80 20 This technique gives a filter with much the same characteristics of a simple RC filter. Figure I, which shows some raw "noisy" data read in from a sensor and the filtered result, illustrates the effect of this equation . In looking at Figure I, you may notice another interesting thing about digital filtering . The filtered signal s are fractional values of the analog-to- digital converter's (ADe) codes, which means they are at a higher resolution than thc nonfiltered signal . In fact, using digital filtering often gives you the equivalent of one or two additional bits on your ADC! This phenomenon occurs because the filter averages out the white noise on your systcm .
0 0 To further illustrate this tech- nique, Figure 2 shows the same filtering scheme with three different filtcr constants on a simple step function. Notice that on this graph, I have drawn a line showing the one- time-constant response 11 - e - ' - 0.631. Using a spreadsheet to model the filter with a step function is an easy way to determine the time constant . Table 1 shows the time constants correspond- ing to the three filter constants used in Figure 2 . and the sample rate time, you need to consider filtering requirements as well as PID requirements.
Although it may appear from f i rst impressions that digital filtering can he more trouble than it is worth, the bottom line is that sometimes you can't get adequate stability without it . Sample TIme TIme C o nstant For example, suppose you hav e a voltage of 5 . 05 V on an ADC which was scaled from 0 to 10 V. If the signal was perfect, the ADC would always return a value of 129. However, if 0 . 01 0 . 01 0.01 0 . 4 05 0.255 0 . 205 Figure 3 shows the same filter const a nts applied to a simple sine wave . This graph clearly shows that a phase lag along with significant Table1- Thetime constant of the finer who s e response is shown in Figure 2 decreases as the filter constant increases _
Page 2
hardware filter when using a softwm filt e r , why not just forge t t h e s o ft w filter and do it al l i n hardwa re?" 0.5 Q) Ul c: 8 . 0 Ul Q) a: Th e r e ar e two reason s to n ot r e ly sol e ly on hardwa r e. F irs t , im p le- m e nting a hi g h-freq u en c y antialias ' filter requires on l y a s m all l and inexpensive) capacitor a nd r esi sto r . But , to imp l ement lower - fre q ue n cy filters, y ou need much larger (and m o r e e xpens i ve! capacitors . Hence, i t is usu a lly more cost effec t ive t o impleme n t lowe r- frequency filt e r s in s oftwar e . S e cond, frequent l y t h e s e lect i on pr o per time cons t a nt s fo r t h e s e filte r s is a matter of tuning .
F O rdi ff e r ent in s t a llations, yo u m ig ht w a n t d i fferen tim e constants . With a softwar e filte r, adjustinR a time consta n t i s n c mor e p ai nful than adjusti n g a gain . Bu t wi t h a hardware filter, yo u ' ve got t o get out th e soldering iron and c h a n ge c apac i - t o r s or re s istor s to make a time- c o n s t a nt chan g e . -0 . 5 -1 0 Figure ~Digitafly fi l tering a pure sine wave not on l y a f lenu a tes th e s ignal amp l il u de , but also shifts its phase (like any analog filter would) . With it , yo u h ave t o wor k h ard t o tun e th e s y s tem , hut a n a cc e pt a hl e s oluti o n is possible , data a t 2S H z.
H e r e th e probl e m st e m s from th e fact that, at 2S Hz , we are not s a mpling the whole wave . T h e filtering is s moothing misrepresenta- ti ve dat a p o int s into a fi c titiou s wav e f o rm . Filterin g i ntrodu c es a la g int o yo ur c ontrol s y s t c m . R e memb e rin g th e l ag i s es p e ciall y important if the sy s tem dyn a mics r e quire th e u se of le a d t er m s s u c h as d e ri va ti ve (o r r a t e) ga in . Yo u mu s t be c a re ful n ot t o nu ll if y th e a d va nt age of lcad term s by u s ing to o mu c h filtcring.
Ther e i s a de lic a t e b ala n ce th a t eve n th e m os t so ph is t ica t e d co ntr o l e n g i - IWl ' r ss tru gg l e w i t h , hilt a h a l a n ce b e twe e n th e tw o extr e m es d oes e xi s t . Th e b o tt o m lin e : anytim e y o u u se di g it a l filtering , you must have an a n a l og filt e r o n y our signal inputs to K Samp l eTime PhaseLag 10 0 . 01 72 . 0 ° 25 0 . 01 57 . 6 ° 45 0 . 01 43 . 2 ° Rem e mber to ini t ia li z e the f il ter .
A common mistake i n imp l e - m e nting a diRital filter is faili n g to properly initialize the runn i n g averag S o m e times this mistake a r ise s in the fo r m of simply forgetti n g to i n i t ial i z e th e a v e r a ge at all . Othe r ti m es, it t a k th e f o rm of initializ i ng to zero. The proper approac h is t o initial - iz e the average to a val u e n ea r t h e tru e v a lue so the filter doesn't have to dea l with what is, in effect, a b i g step function at poweru p . A c omm o n w a y to initialize th e averag e i s to read th e se n sor one time at powerup .
The When w r itin g th e s oft wa r c f o r P ar k er ' s o ri gi n a l c 1e c tr o h y dr osta ti c a c tuat o r ( EH A) , I was a bl e t o fi lte r both the p osi tion allli v e l oci t y term s with o ut killing th e e ff ec t o f th e acc e lerati o n gain . In th a t ca se, th e ac ce l e rati o n term w a s d o ubl y f ilt ere d , but s till abl e t o c o n t ribut e a sig nific a nt l e ading e ff e ct. It w as a di f fi c ult tunin g task (and I h a d h e l p fr o m a co ntrol s g u y ! , but w i th o u t it we co uld no t get ad e quat e r es p o n se f r om o u r co n- troller .
Ta b le 2 - lncreasing the si gn al attenuation by decreasing the filter constant also increases the phase lag (as shown in Figure 3). f ilter o ut hi g her fr e quency noi se. So , for in s tanc e, o n th e Parker EMClOO digital-programmable motion control - ler , I u se d an analog RC antialiasing filter a t 600 Hz for a s i g nal that I s ampl e d at 1000 Hz . A question sometimes raised at t h is po int i s, " If y o u alway s ne e d a Y o u s till n e ed a n a n a l og filt er. If you have a s i g n a l w ith no i se at a fr e qu e nc y hi g h e r than th e fr e qu e n cy at which you ar e s amplin g t h e d a t a, y o u can g c t a phcnom e non ca ll e d a li as in g.
With ali as in g , as yo u sa m p l e t he high e r-frequ e n cy d a ta, you c a n e nd up reading " b ea t fr e quen c i es , " w hi c h appe a r a s low e r-fr e qu e n cy si g n a l s . F o r e x a mpl c, s upp ose we h ave a n un s hi e ld ed p ress u r e senso r li n e th a t is picking up nois e from flu o r esce nt lights driven off a 60-H z A C line . Let 's further supp ose that w e a r e sa mplin g 100 Q) U l c: 60 0 0 . Ul Q) a: 40 80 20 0 0 0 . 2 0 . 4 0 . 6 0 . 8 T ime Figure 4 - Fiflering a step function using floating - point math in the filter ro u ti ne produces a nice, s m oot h ou t put .
Using integer math and keeping track of r emainders results in some bu m ps, bu t no D C o ffset . Usi ng i n te g er math b u dr o pping the remainders produces a D C offset in the output .
Page 3
void ft1t e r lt nt * fi1t e r ed . in t raw . un s i g n e d tn t f i I tCo n s t a n t. un s ign e d int * low ) f * c on v ert to long to a v oid o v e rfl o w o n multip ly *f along - I *filtered - raw ) ; a l on g - lalo ng * fi l tConst a nt); f * add remainder from l as t t im e *f along + - *low ; f * s tor e r e m a ind e r fo r n ex t t im e throu g h * f *low - O x OOOOffff & a l o ng ; f* s hift right for fra c tional filt er co n s tant *f along ~ along » 16 ; *fil tere d - ra w + a l o n g; reading is then used as the valu e which initializes the a verag e .
A purist may want to initialize th e sum to the average of tw o or thre e readinJ;s , but that is usuall y not necessary unle s s y o ur s ystem i s e x tremely noisy . Th e go a l is to g e t th e valu e nearly ri g ht to a v o id an ex trem e retipontieto a ti tql function ; thc i nitial v alue doe s n ' t have to be p e rf e ct , ju s t clos e. 2000 , and th e new value is 1900. K would e qual 65 , 53 6 divid e d by 4 or 16, 38 4 . H e nce , th e equ a tion i s; X " ,, ,_ , ,,, 1 - (( 1 6 .
1 M x ( 2 000 - 1 9 00)) » 16) + 1 9 00 - I ns Usin g fracti o n a l-int ege r m a th r a ther th a n fl oa ting-p o in t ma th c a n eas i ly r c du c c c o mputat io n t i mc by an o rder o f m ag n i tude. ~/ / . . U s e a n inteJ;er and a r e mainder , r a th er th a n a l o n g i nt eg er numb e r . Fu r a 12-bit AD C , y o u wi ll pr o b a bl y f i nd that us i ng o nly 1 6 bits of filter e d dat a w ill n o t giv e y o u enough re so lu t ion a nd will a c tu a lly intr o duce tru nca ti o n e rr o r s i n yo ur filt e red v a lue .
But, if y ou o pt for u s in g a 32-bit word f or you r filt e red d a ta wh e n runn i n g o n a 16 - o r 8-b it micr o pr o ce ss o r, y ou g r ea tl y incr ea s e th e pr oce ssin g time neede d t o d o th e m ult i pl y. IMPLEMENTATION TIPS N o w that we havc lookcd at h o w a digital filter works and some imp o r- tant deta i ls to remember, w e ne e d t o look a t some implementation t ip s. ' \ : : (CONNECTS TO R5 - 233) AOe - l' AID CONVERTER'I1& channoUl bll~.$ Ot . O& ADC - 8G AID CONVERTER' 8 channoUlO bll) . $12UO =k~It:~,:=f~er;:1~ ;WofU::1Og signals. RS - 4221RS·485 available (lengths to 4.0001 .
Call for into on other A I D configurations and 12 bit converters (terminal block and cable sold separately) . AOe - 8E TEMPERATURE INTERFACE' (8 ch) 13t . O& Includes term. block & 8 temp . sensors ( - 40 ' to 148' Fl. ~~~i'o n ,~~'J:'~~~~8~:~~;:"~~~A~b " equi:m~~ s~~~:b ~:Jc~ ~~~k; Im~~:c~".? . ~.~~.~:'.oo ~:r ~~~r~~t~ci~A(~r:~~.,"':~~=2)~~r;r:s Converts an R5 - 232 port Into 4 selectable RS . 422 ports . CO - 485 (R5 - 232 to RS o 422IAS - 485 con_ ~ .. 44.0& Don't use floating-point math.
Unless you have the very unusu al situation of having an e mb e dded c o ntroller with ample horsep o wer and resource s, th e last thing you want to do is usc floating-po i nt math with thi t i equation . Instead , us e fractional- integer math . The tr i c k i s t o hold o n to th e r e m ai n de r from th e pr e vi o u s pas s with th e fi l t er. IWith a s hift o p e rati o n , the r e m ai n der i s th e part th a t ge t s s hift e d away w h cn you dividc by 256 o r 65,5 3 6 as d escr ib e d a b o v e. I E ac h time yo u do t h e m ultip ly, a d d th e re ma i n- d e r fr o m the p r ev i o u s p a s s a nd th e n s t ore th e n ew r e m a i n de r . ~e8 t~~~~sSo.r . ~~ ~ l~ 1 ~=~~u:
~:d~ . uaklg -EXPANDA8LE expand your I nterface 10 control and monitor up to 512 relays. up 10 576 digital Input s . up to You want to r e pr e s e nt a FULL TECHNICAL SUPPORT prov i ded over the telephone by our slaff . Technical reference & disk n o nintcgcr 11lunbcr as S 0 111 C f rac tion al value of either 256 o r 65,536. F o r instance, if you hav e a 1 6-bit c o ntr o l- ler, the natural way to repr e sent the frac t ion \ 1 2 is with the numb e r 3 2,7 6 8 . To multiply , you multiply th e numb e r by the constant and shift it by 16 wh en you are all don e .
- HtGH RELIABILJTY engineered 10f ' cont i nuous 24 hour industrial applications with 10 years of proven performance In Ihe energy management fie l d. ~~~ . inc! a~eJ~~~:~~~ :r~r~=i~~t~:~I~~:r . -CONNECTS TO R5 - 232. RS - 422 or RS - 485 .- with IBM and compatible s . Mac and mosl computers. All standard baud rates and proto c ols (SO to 19.200 baud). our 800 number to orde r FREE INFORMAnON PACKET. Technical inform a ti o n (614) 464- ~4 70.
U s i ng a r e maind e r , r a th e r th a n a l onger w or d l e n g th , a l so of f c r s the adva nt age o f u s in g 1 6, n o t 3 2 , bit s f o r sub se qu e nt c a l c ulati o n s when y o u use th e f ilt ere d da t a in so m e thin g like a 24 HOUR ORDER LINE ( 800) 842-n14 ' Visa-Mastercard-Ameri ca n Ex tess-COO For example, supp o se th e filt e r constant is 0 . 25, our runnin g a ve r age is International & Domestic FAX (614) 464 - 9656 Use for information . technical supp or t & o r O Or s . ELECTRONIC ENERGY CONTROL . IN C. 380 South Fifth Slre el. Suile 6 0 4 Columbus. Ohi o 43215·5438 The Computer Applications Joumal Issue #52 November 1994 17 #109
Page 4