You are on page 1of 26

http://iecprojects.blogspot.in/2010/07/ece-matlab-projects-image-processing.

html

FIR Filter Basics


1.1 What are "FIR filters"?
FIR filters are one of two primary types of digital filters used in Digital Signal Processing (DSP) applications, the other type being IIR.

1.2 What does "FIR" mean?


FIR means Finite Impulse Response . If you put in an impulse, that is, a single ! sample followed by many " samples, #eroes will come out after the ! sample has made its way through the delay line of the filter.

1.3 Why is the impulse response "finite"?


In the common case, the impulse response is finite because there is no feedbac$ in the FIR. % lac$ of feedbac$ guarantees that the impulse response will be finite. &herefore, the term finite impulse response is nearly synonymous with no feedbac$ . 'owe(er, if feedbac$ is employed yet the impulse response is finite, the filter still is a FIR. %n e)ample is the mo(ing a(erage filter, in which the *th prior sample is subtracted (fed bac$) each time a new sample comes in. &his filter has a finite impulse response e(en though it uses feedbac$+ after * samples of an impulse, the output will always be #ero.

1.4 How do I pronounce "FIR"?


Some people say the letters F,I,R- other people pronounce as if it were a type of tree. .e prefer the tree. (&he difference is whether you tal$ about an F,I,R filter or a FIR filter.)

1.

What is the alternati!e to FIR filters?

DSP filters can also be Infinite Impulse Response (IIR). (See dsp/uru0s IIR FAQ.) IIR filters use feedbac$, so when you input an impulse the output theoretically rings indefinitely.

1." How do FIR filters compare to IIR filters?


1ach has ad(antages and disad(antages. 2(erall, though, the ad(antages of FIR filters outweigh the disad(antages, so they are used much more than IIRs.

1.".1 What are the ad!anta#es of FIR Filters $compared to IIR filters%?
3ompared to IIR filters, FIR filters offer the following ad(antages+ &hey can easily be designed to be linear phase (and usually are). Put simply, linear,phase filters delay the input signal but don4t distort its phase. &hey are simple to implement. 2n most DSP microprocessors, the FIR calculation can be done by looping a single instruction.

&hey are suited to multi,rate applications. 5y multi,rate, we mean either decimation (reducing the sampling rate), interpolation (increasing the sampling rate), or both. .hether decimating or interpolating, the use of FIR filters allows some of the calculations to be omitted, thus pro(iding an important computational efficiency. In contrast, if IIR filters are used, each output must be indi(idually calculated, e(en if it that output will discarded (so the feedbac$ will be incorporated into the filter).

&hey ha(e desireable numeric properties. In practice, all DSP filters must be implemented using finite,precision arithmetic, that is, a limited number of bits. &he use of finite,precision arithmetic in IIR filters can cause significant problems due to the use of feedbac$, but FIR filters without feedbac$ can usually be implemented using fewer bits, and the designer has fewer practical problems to sol(e related to non,ideal arithmetic.

&hey can be implemented using fractional arithmetic. 6nli$e IIR filters, it is always possible to implement a FIR filter using coefficients with magnitude of less than !.". (&he o(erall gain of the FIR filter can be ad7usted at its output, if desired.) &his is an important consideration when using fi)ed,point DSP0s, because it ma$es the implementation much simpler.

1.".2 What are the disad!anta#es of FIR Filters $compared to IIR filters%?
3ompared to IIR filters, FIR filters sometimes ha(e the disad(antage that they re8uire more memory and9or calculation to achie(e a gi(en filter response characteristic. %lso, certain responses are not practical to implement with FIR filters.

1.& What terms are used in descri'in# FIR filters?


Impulse Response , &he impulse response of a FIR filter is actually 7ust the set of FIR coefficients. (If you put an impluse into a FIR filter which consists of a ! sample followed by many " samples, the output of the filter will be the set of coefficients, as the ! sample mo(es past each coefficient in turn to form the output.) Tap , % FIR tap is simply a coefficient9delay pair. &he number of FIR taps, (often designated as * ) is an indication of !) the amount of memory re8uired to implement the filter, :) the number of calculations re8uired, and ;) the amount of filtering the filter can do- in effect, more taps means more stopband attenuation, less ripple, narrower filters, etc. Multiply-Accumulate (MAC) , In a FIR conte)t, a <%3 is the operation of multiplying a coefficient by the corresponding delayed data sample and accumulating the result. FIRs usually re8uire one <%3 per tap. <ost DSP microprocessors implement the <%3 operation in a single instruction cycle.

Transition Band , &he band of fre8uencies between passband and stopband edges. &he narrower the transition band, the more taps are re8uired to implement the filter. (% small transition band results in a sharp filter.)

Delay Line , &he set of memory elements that implement the =>,! delay elements of the FIR calculation.

Circular Buffer , % special buffer which is circular becauseincrementing at the end causes it to wrap around to the beginning, or because decrementing from the beginning causes it to wrap around to the end. 3ircular buffers are often pro(ided by DSP microprocessors to implement the mo(ement of the samples through the FIR delay,line without ha(ing to literally mo(e the data in memory. .hen a new sample is added to the buffer, it automatically replaces the oldest one.

2.1 (inear )hase


2.1.1 What is the association 'etween FIR filters and "linear* phase"?
<ost FIRs are linear,phase filters- when a linear,phase filter is desired, a FIR is usually used.

2.1.2 What is a linear phase filter?


?inear Phase refers to the condition where the phase response of the filter is a linear (straight,line) function of fre8uency (e)cluding phase wraps at @9, !A" degrees). &his results in the delay through the filter being the same at all fre8uencies. &herefore, the filter does not cause phase distortion or delay distortion . &he lac$ of phase9delay distortion can be a critical ad(antage of FIR filters o(er IIR and analog filters in certain systems, for e)ample, in digital data modems.

2.1.3 What is the condition for linear phase?


FIR filters are usually designed to be linear,phase (but they don0t ha(e to be.) % FIR filter is linear,phase if (and only if) its coefficients are symmetrical around the center coefficient, that is, the first coefficient is the same as the last- the second is the same as the ne)t,to, last, etc. (% linear,phase FIR filter ha(ing an odd number of coefficients will ha(e a single coefficient in the center which has no mate.)

2.1.4 What is the delay of a linear*phase FIR?


&he formula is simple+ gi(en a FIR filter which has * taps, the delay is+ (* , !) 9 (: B Fs), where Fs is the sampling fre8uency. So, for e)ample, a :! tap linear,phase FIR filter operating at a ! $'# rate has delay+ (:! , !) 9 (: B ! $'#)C!" milliseconds.

2.1.4 What is the alternati!e to linear phase?


*on,linear phase, of course. -,) %ctually, the most popular alternati(e is minimum phase . <inimum,phase filters (which might better be called minimum delay filters) ha(e less delay than linear,phase filters with the same amplitude response, at the cost of a non,linear phase characteristic, a.$.a. phase distortion . % lowpass FIR filter has its largest,magnitude coefficients in the center of the impulse response. In comparison, the largest,magnitude coefficients of a minimum,phase filter are nearer to the beginning . (See dsp/uru0s tutorial How To Design Minimum-Phase FIR Filters for more details.)

2.2 Frequenc Res!onse 2.2.1 What is the + transform of a FIR filter?


For an *,tap FIR filter with coefficients h($), whose output is described by+ y(n)Ch("))(n) @ h(!))(n,!) @ h(:))(n,:) @ ... h(*,!))(n,*,!), the filter0s = transform is+ '(#)Ch(")#," @ h(!)#,! @ h(:)#,: @ ... h(*,!)#,(*,!) , or

2.2.2 What is the fre,uency response formula for a FIR filter?


&he (ariable # in '(#) is a continuous comple) (ariable, and we can describe it as+ #CrDe 7, where r is a magnitude and is the angle of #. If we let rC!, then '(#) around the unit circle becomes the filter0s fre8uency response '(7). &his means that substituting e7 for # in '(#) gi(es us an e)pression for the filter0s fre8uency response '(), which is+ '(7)Ch(")e,7" @ h(!)e,7! @ h(:)e,7: @ ... h(*,!)e,7(*,!) , or 6sing 1uler0s identity, e,7aCcos(a) , 7sin(a), we can write '() in rectangular form as+ '(7)Ch(")Ecos(") , 7sin(")F @ h(!)Ecos(!) , 7sin(!)F @ ... h(*,!)Ecos((*,!)) , 7sin((*,!))F , or

2.2.3 -an I calculate the fre,uency response of a FIR usin# the .iscrete Fourier /ransform $.F/%?
Ges. For an *,tap FIR, you can get * e(enly,spaced points of the fre8uency response by doing a DF& on the filter coefficients. 'owe(er, to get the fre8uency response of the filter at any arbitraryfre8uency (that is, at fre8uencies bet een the DF& outputs), you will need to use the formula abo(e.

2.2.4 What is the .- #ain of a FIR filter?


3onsider a D3 (#ero '#) input signal consisting of samples which each ha(e (alue !.". %fter the FIR0s delay line had filled with the !." samples, the output would be the sum of the coefficients. &herefore, the gain of a FIR filter at D3 is simply the sum of the coefficients. &his intuiti(e result can be chec$ed against the formula abo(e. If we set to #ero, the cosine term is always !, and the sine term is always #ero, so the fre8uency response becomes+

2.2.

How do I scale the #ain of a FIR filter?

Simply multiply all coefficients by the scale factor.

2." #umeric Pro!erties 2.3.1 0re FIR filters inherently sta'le?


Ges. Since they ha(e no feedbac$ elements, any bounded input results in a bounded output.

2.3.2 What ma1es the numerical properties of FIR filters "#ood"?


%gain, the $ey is the lac$ of feedbac$. &he numeric errors that occur when implementing FIR filters in computer arithmetic occur separately with each calculation- the FIR doesn0t remember its past numeric errors. In contrast, the feedbac$ aspect of IIR filters can cause numeric errors to compound with each calculation, as numeric errors are fed bac$. &he practical impact of this is that FIRs can generally be implemented using fewer bits of precision than IIRs. For e)ample, FIRs can usually be implemented with !H bits, but IIRs generally re8uire ;: bits, or e(en more.

2.4 Why are FIR filters #enerally preferred o!er IIR filters in multirate $decimatin# and interpolatin#% systems?
5ecause only a fraction of the calculations that would be re8uired to implement a decimating or interpolating FIR in a literal way actually needs to be done.

Since FIR filters do not use feedbac$, only those outputs which are actually going to be used ha(e to be calculated. &herefore, in the case of decimating FIRs (in which only ! of * outputs will be used), the other *,! outputs don0t ha(e to be calculated. Similarly, for interpolating filters (in which #eroes are inserted between the input samples to raise the sampling rate) you don0t actually ha(e to multiply the inserted #eroes with their corresponding FIR coefficients and sum the result- you 7ust omit the multiplication,additions that are associated with the #eroes (because they don0t change the result anyway.) In contrast, since IIR filters use feedbac$, e(ery input must be used, and e(ery input must be calculated because all inputs and outputs contribute to the feedbac$ in the filter.

2.

What special types of FIR filters are there?


Bo!car , 5o)car FIR filters are simply filters in which each coefficient is !.". &herefore, for an *,tap bo)car, the output is 7ust the sum of the past * samples. 5ecause bo)car FIRs can be implemented using only adders, they are of interest primarily in hardware implementations, where multipliers are e)pensi(e to implement. "ilbert Transformer , 'ilbert &ransformers shift the phase of a signal by I" degrees. &hey are used primarily for creating the imaginary part of a comple) signal, gi(en its real part. Differentiator , Differentiators ha(e an amplitude response which is a linear function of fre8uency. &hey are not (ery popular nowadays, but are sometimes used for F< demodulators. Lt#-Band , %lso called *y8uist filters, these filters are a special class of filters used primarily in multirate applications. &heir $ey selling point is that one of e(ery L coefficients is #ero,,a fact which can be e)ploited to reduce the number of multiply,accumulate operations re8uired to implement the filter. (&he famous half, band filter is actually an ?th,band filter, with ?C:.) Raised-Cosine , &his is a special $ind of filter that is sometimes used for digital data applications. (&he fre8uency response in the passband is a cosine shape which has been raised by a constant.) See dsp/uru0s Raise$-%osine FAQ for more information.

%side from regular and e)tra crispy there are+

3.1 What are the methods of desi#nin# FIR filters?


&he three most popular design methods are (in order)+ Par&s-Mc%lellan' &he Par$s,<c3lellan method (inaccurately called Reme# by <atlab) is probably the most widely used FIR filter design method. It is an iteration algorithm that accepts filter specifications in terms of passband and stopband fre8uencies, passband ripple, and stopband attenuation. &he fact that you can directly specify all the important filter parameters is what ma$es this method so

popular. &he P< method can design not only FIR filters but also FIR differentiators and FIR 'ilbert transformers . (in$owing'. In the windowing method, an initial impulse response is deri(ed by ta$ing the In(erse Discrete Fourier &ransform (IDF&) of the desired fre8uency response. &hen, the impulse response is refined by applying a data window to it.

Direct %alculation' &he impulse responses of certain types of FIR filters (e.g. Raised 3osine and .indowed Sinc) can be calculated directly from formulas.

3.2 How do I actually desi#n FIR filters?


.ith a FIR filter design program, of course. -,) %lthough it0s possible to design FIR filters using manual methods, it is a whole lot easier 7ust to use a FIR filter design program.

3.3 What FIR filter desi#n pro#rams are a!aila'le?


FIR filter design programs come in three broad categories+ Filter Design A!!lications' See dsp/uru0s Digital Filter Design )o*tware page for a list of filter design programs. $ear and dear to us #ere at dsp%uru is Io egian&s o n)co!eFIR product' (e belie)e *cope+IR offers an e!cellent combination of professional features, smoot# user interface, and affordable price' (e sell it for -ust ./00, it# a generous 12 day trial period' 3)en if you already use Matlab, *cope+IR&s 4point and s#oot4 capabilities can impro)e your +IR filter design producti)ity' Math Programs' <atlab and its Free %lones offer built,in FIR filter design functions. )ource co$e' 2ne of the best places on the net to find source code to design FIR filters is 3harles Poynton0s Filter Design )o*tware page.

4.1 What is the 'asic al#orithm for implementin# FIR filters?


Structurally, FIR filters consist of 7ust two things+ a sample delay line and a set of coefficients. &o implement the filter+ !. Put the input sample into the delay line. :. <ultiply each sample in the delay line by the corresponding coefficient and accumulate the result. ;. Shift the delay line by one sample to ma$e room for the ne)t input sample.

4.2 How do I implement FIR filters in -?


&here are lots of possibilities, including a few tric$s. &o illustrate, we ha(e pro(ided a set of FIR filter algorithms implemented in 3 called Fir%lgs.c in )co!eFIR+s $istri,ution *ile. Fir%lgs.c includes the following functions+

1. *ir-,asic+ Illustrates the basic FIR calculation described abo(e by implementing it


(ery literally. 2. *ir-circular+ Illustrates how circular buffers are used to implement FIRs.

3. *ir-shu**le+ Illustrates the shuffle down techni8ue used by some of &e)as


Instruments0 processors.

4. *ir-s!lit+ Splits the FIR calculation into two flat (non,circular) pieces to a(oid the
use circular buffer logic and shuffling.

5. *ir-$ou,le-.+ 6ses a double,si#ed delay line so that the FIR calculation can be done
using a flat buffer.

6. *ir-$ou,le-h+ Similar to firJdoubleJ#, this uses a double,si#ed coefficient so that


the FIR calculation can be done using a flat buffer.

4.3 How do I implement FIR filters in assem'ly?


FIR assembly algorithms are 8uite processor,specific, but the most common system uses a circular buffer mechanism pro(ided by the DSP processor. &he basic steps are+ !. 3onfigure the circular buffer- load the coefficient and delay,line pointers. &hen, for each input sample+ :. Store the incoming data in the delay line- increment the delay,line pointer. ;. 3lear the multiplier,accumulator. K. ?oop o(er all coefficients9delays- accumulate the (alues obtained by multiplying the coefficients by the delayed samples. L. Round or truncate the result as the FIR output. %lternati(ely, a shuffle down method is used in &e)as Instruments0 older fi)ed,point processors to implement circular buffers. &he processor literally mo(es each sample delay (alues by one slot during each multiply,accumulate ((ia the <%3D instruction). 1ach DSP microprocessor manufacturer pro(ides e)ample FIR assembly code in its data boo$s or its application handboo$s, so be sure to loo$ at those before you rein(ent the circular buffer .

4.4 How do I test my FIR implementation?


'ere are a few methods+ Im!ulse Test' % (ery simple and effecti(e test is to put an impulse into it (which is 7ust a ! sample followed by at lest * , ! #eroes.) Gou can also put in an impulse train , with the ! samples spaced at least * samples apart. If all the coefficients of the filter come out in the proper order, there is a good chance your filter is wor$ing correctly. (Gou might want to test with non,linear phase coefficients so you can see

the order they come out.) .e recommend you do this test whene(er you write a new FIR filter routine. )te! Test' Input * or more ! samples. &he output after * samples, should be the sum (D3 gain) of the FIR filter.

)ine Test' Input a sine wa(e at one or more fre8uencies and see if the output sine has the e)pected amplitude.

)we!t FM Test' +rom 3ric 5acobsen6 <y fa(orite test after an impulse train is to ta$e two identical instances of the filter under test, use them as I and M filters and put a comple) F< linear sweep through them from D3 to Fs9:. Gou can do an FF& on the result and see the complete fre8uency response of the filter, ma$e sure the phase is nice and continuous e(erywhere, and match the response to what you0d e)pect from the coefficient set, the precision, etc.

4.

What tric1s are useful in implementin# FIR filters?

FIR tric$s center on two things !) not calculating things that don0t need to be calculated, and :) fa$ing circular buffers in software.

4. .1 How do I s1ip needless calculations?


First, if your filter has #ero,(alued coefficients, you don0t actually ha(e to calculate those taps- you can lea(e them out. % common case of this is half,band filter, which ha(e the property that e(ery,other coefficient is #ero. Second, if your filter is symmetric (linear phase), you can pre,add the samples which will be multiplied by the same coefficient (alue, prior to doing the multiply. Since this techni8ue essentially trades an add for a multiply, it isn0t really useful in DSP microprocessors which can do a multiply in a single instruction cycle. 'owe(er, it is useful in %SI3 implementations (in which addition is usually much less e)pensi(e than multiplication)- also, some newer DSP processors now offer special hardware and instructions to ma$e use of this tric$.

4. .2 How do I fa1e circular 'uffers in software?


.hen hardware support for circular buffers isn0t a(ailable, you ha(e to fa$e them. %lso, since %*SI 3 has no construct to describe circular buffers, most 3 compilers can0t generate code to use them, e(en if the target processor has them. Gou can always implement a circular buffer by duplicating the logic of a circular buffer in software (and many ha(e), but the o(erhead can be prohibiti(e- the circular,fa$e might ta$e se(eral instructions to implement, compared to 7ust a single instruction to do the multiply,accumulate operation. &herefore you need to fa$e it. 'ere are se(eral basic techni8ues to fa$e circular buffers+

1. )!lit the calculation' Gou can split any FIR calculation into its pre,wrap and
post,wrap parts. 5y splitting the calculation into these two parts, you essentially can do the circular logic only once, rather than once per tap. (See firJdoubleJ# in Fir%lgs.c abo(e.) 2. Du!licate the $ela line' For a FIR with * taps, use a delay line of si#e :*. 3opy each sample to its proper location, as well as at location,plus,*. &herefore, the FIR calculation0s <%3 loop can be done on a flat buffer of * points, starting anywhere within the first set of * points. &he second set of * delayed samples pro(ides the wrap around comparable to a true circular buffer. (See firJdoubleJ# in Fir%lgs.c abo(e.)

3. Du!licate the coe**icients' &his is similar to the abo(e, e)cept that the duplication
occurs in terms of the coefficients, not the delay line. 3ompared to the pre(ious method, this has a calculation ad(antage of not ha(ing to store each incoming sample twice, and it also has a memory ad(antage when the same coefficient set will be used on multiple delay lines. (See firJdoubleJh in Fir%lgs.c abo(e.)

4. /se ,loc& !rocessing' In bloc$ processing, you use a delay line which is a multiple
of the number of taps. Gou therefore only ha(e to mo(e the data once per bloc$ to implement the delay,line mechanism. .hen the bloc$ si#e becomes large , the o(erhead of a mo(ing the delay line once per bloc$ becomes negligible.

0.IIR Filter Basics


In*inite Im!ulse Res!onse 1IIR2 *ilters are *un$amental elements o* Digital )ignal Processing. This is $s!3uru+s mini-FAQ o* 4Frequentl As&e$ Questions4 on the su,5ect o* IIR *ilters. 6. IIR Basics 1.1 What are IIR filters? What does "IIR" mean?
IIR filters are one of two primary types of digital filters used in Digital Signal Processing (DSP) applications (the other type being FIR). IIR means Infinite Impulse Response .

1.2 Why is the impulse response "infinite"?


&he impulse response is infinite because there is feedbac$ in the filter- if you put in an impulse (a single ! sample followed by many " samples), an infinite number of non,#ero (alues will come out (theoretically).

1.3 What is the alternati!e to IIR filters?


DSP filters can also be Finite Im!ulse Res!onse (FIR). FIR filters do not use feedbac$, so for a FIR filter with $ coefficients, the output always becomes #ero after putting in $ samples of an impulse response.

1.4 What are the ad!anta#es of IIR filters $compared to FIR filters%?
IIR filters can achie(e a gi(en filtering characteristic using less memory and calculations than a similar FIR filter.

1.

What are the disad!anta#es of IIR filters $compared to FIR filters%?


&hey are more susceptable to problems of finite,length arithmetic, such as noise generated by calculations, and limit cycles. (&his is a direct conse8uence of feedbac$+ when the output isn0t computed perfectly and is fed bac$, the imperfection can compound.) &hey are harder (slower) to implement using fi)ed,point arithmetic. &hey don0t offer the computational ad(antages of FIR filters for multirate (decimation and interpolation) applications.

2ultirate .3) is a fundamental techni,ue of .i#ital 3i#nal )rocessin#. Here4 dsp5uru pro!ides "Rarely 5i!en 0nswers" to "Fre,uently 0s1ed 6uestions" a'out the important and mysterious su'7ect of 2ultirate .i#ital 3i#nal )rocessin#.
Multirate Basics Decimation Inter!olation Resam!ling

1.1 What does multirate mean?


Multirate simply means multiple sampling rates . % multirate DSP system uses multiple sampling rates within the system. .hene(er a signal at one rate has to be used by a system that e)pects a different rate, the rate has to be increased or decreased, and some processing is re8uired to do so. &herefore <ultirate DSP really refers to the art or science of c#angingsampling rates.

1.2 Why should I do multirate .3)?


&he most immediate reason is when you need to pass data between two systems which use incompatible sampling rates. For e)ample, professional audio systems use KA $'#

rate, but consumer 3D players use KK.! $'#- when audio professionals transfer their recorded music to 3Ds, they need to do a rate con(ersion. 5ut the most common reason is that multirate DSP can greatly increase processing efficiency (e(en by orders of magnitudeN), which reduces DSP system cost. T#is ma7es t#e sub-ect of multirate D*8 )ital to all professional D*8 practitioners'

1.3 What are the cate#ories of multirate?


<ultirate consists of+ !. Decimation' &o decrease the sampling rate, :. Inter!olation' &o increase the sampling rate, or, ;. Resam!ling'&o combine decimation and interpolation in order to change the sampling rate by a fractional (alue that can be e)pressed as a ratio. For e)ample, to resample by a factor of !.L, you 7ust interpolate by a factor of ; then decimate by a factor of : (to change the sampling rate by a factor of ;9:C!.L.)

1.4 Where can I #et software to implement multirate?


Right here. 2ur multirateJalgs pac$age includes a decimation, interpolation, and resampling routines. Gou can download it from dsp/uru0sD)P Algorithm 7i,rar .

1.

What reference 'oo1s co!er multirate topics?


<any DSP boo$s omit the important sub7ect of multirate altogether. 5ut two introductory te)ts that briefly go into it are+ !. /n$erstan$ing Digital )ignal Processing 87 o9:; 2. Digital )ignal Processing an$ the Microcontroller 83ro9<; 5ut it0s a big sub7ect. &he two most popular industrial strength (ad(anced) boo$s that co(er multirate in depth are+ !. Multirate Digital )ignal Processing 8%ro<"; :. Multirate ) stems an$ Filter Ban&s 8=ai92;

2.6 Basics 2.1.1 What are "decimation" and "downsamplin#"?


?oosely spea$ing, decimation is the process of reducing the sampling rate. In practice, this usually implies lowpass,filtering a signal, then throwing away some of its samples. Downsampling is a more specific term which refers to 7ust the process of throwing away samples, without the lowpass filtering operation. &hroughout this F%M, though, we0ll 7ust use the term decimation loosely, sometimes to mean downsampling .

2.1.2 What is the "decimation factor"?


&he decimation factor is simply the ratio of the input rate to the output rate. It is usually symboli#ed by < , so input rate 9 output rateC<. &ip+ Gou can remember that < is the symbol for decimation factor by thin$ing of deci, M, ation . (1)ercise for the student+ which letter is used as the symbol for interpo, L,ation factorO)

2.1.3 Why decimate?


&he most immediate reason to decimate is simply to reduce the sampling rate at the output of one system so a system operating at a lower sampling rate can input the signal. 5ut a much more common moti(ation for decimation is to reduce the cost of processing+ the calculation and9or memory re8uired to implement a DSP system generally is proportional to the sampling rate, so the use of a lower sampling rate usually results in a cheaper implementation. &o that, Pim &homas adds+ %lmost anything you do to9with the signal can be done with fewer operations at a lower sample rate, and the wor$load is almost always reduced by more than a factor of <. For e)ample, if you double the sample rate, an e8ui(alent filter will re8uire four times as many operations to implement. &his is because both amount of data (per second) and the length of the filter increase by two, so con(olution goes up by four. &hus, if you can hal(e the sample rate, you can decrease the wor$ load by a factor of four. I guess you could say that if you reduce the sample rate by <, the wor$load for a filter goes down to (!9<)>:.

2.1.4 Is there a restriction on decimation factors I can use?


Ges. Decimation in(ol(es throwing away samples, so you can only decimate by integer factors- you cannot decimate by fractionalfactors. ('owe(er, you can do interpolation prior to decimation to achie(e an o(erall rational factor, for e)ample, K9L see Part >' Resam!ling.)

2.1.

Which si#nals can 'e downsampled?

% signal can be downsampled (without doing any filtering) whene(er it is o(ersampled , that is, when a sampling rate was used that was greater than the *y8uist criteria re8uired. Specifically, the signal0s highest fre8uency must be less than half the postdecimationsampling rate. (&his 7ust boils down to applying the *y8uist criteria to the input signal, relati(e to the new sampling rate.) In most cases, though, you0ll end up lowpass,filtering your signal prior to downsampling, in order to enforce the *y8uist criteria at thepost-decimation rate. For e)ample, suppose you ha(e a signal sampled at a rate of ;" $'#, whose highest fre8uency component is !" $'# (which is less than the *y8uist fre8uency of !L $'#). If you wish to reduce the sampling rate by a factor of three to !" $'#, you must ensure that you ha(e no components greater

than L $'#, which is the *y8uist fre8uency for the reduced rate. 'owe(er, since the original signal has components up to !" $'#, you must lowpass,filter the signal prior to downsampling to remo(e all components abo(e L $'# so that no aliasing will occur when downsampling. &his combined operation of filtering and downsampling is calleddecimation.

2.1." What happens if I !iolate the 8y,uist criteria in downsamplin# or decimatin#?


Gou get aliasing,,7ust as with other cases of (iolating the *y8uist criteria. (%liasing is a type of distortion which cannot be corrected once it occurs.)

2.2 Multistage 2.2.1 -an I decimate in multiple sta#es?


Ges, so long as the decimation factor, <, is not a prime number. For e)ample, to decimate by a factor of !L, you could decimate by L, then decimate by ;. &he more prime factors < has, the more choices you ha(e. For e)ample you could decimate by a factor of :K using+ one stage+ :K two stages+ H and K, or A and ; three stages+ K, ;, and : four stages+ ;, :, :, and :

2.2.2 -ool. 9ut why 'other with all that?


If you are simply downsampling (that is, throwing away samples without filtering), there0s no benefit. 5ut in the more common case of decimating (combining filtering and downsampling), the computational and memory re8uirements of the filters can usually be reduced by using multiple stages.

2.2.3 :;4 so how do I fi#ure out the optimum num'er of sta#es4 and the decimation factor at each sta#e?
&hat0s a tough one. &here isn0t a simple answer to this one+ the answer (aries depending on many things, so if you really want to find the optimum, you ha(e to e(aluate the resource re8uirements of each possibility. 'owe(er, here are a couple of rules of thumb which may help narrow down the choices+ 6sing two or three stages is usually optimal or near,optimal. Decimate in order from the largest to smallest factor. In other words, use the largest factor at the highest sampling rate. For e)ample, when decimating by a factor of H" in three stages, decimate by L, then by K, then by ;.

&he multirate ,oo& re*erences gi(e additional, more specific guidance.

2." Im!lementation 2.3.1 How do I implement decimation?


Decimation consists of the processes of lowpass filtering, followed by downsampling. &o implement the filtering part, you can use either FIR or IIR filters. &o implement the downsampling part (by a downsampling factor of < ) simply $eep e(ery Mt# sample, and throw away the M-/samples in between. For e)ample, to decimate by K, $eep e(ery fourth sample, and throw three out of e(ery four samples away.

2.3.2 /hat almost sounds too easy...


Beauty, e#9 -,)

2.3.3 If I<m #oin# to throw away most of the lowpass filter<s outputs4 why 'other to calculate them in the first place?
Gou may be onto something. In the case of FIR filters, any output is a function only of the past inputs (because there is no feedbac$). &herefore, you only ha(e to calculate outputs which will be used. For IIR filters, you still ha(e to do part or all of the filter calculation for each input, e(en when the corresponding output won0t be used. (Depending on the filter topology used, certain feed,forward parts of the calculation can be omitted.),. &he reason is that outputs youdo use are affected by the feedbac$ from the outputs you don&t use. T#e fact t#at only t#e outputs #ic# ill be used #a)e to be calculated e!plains decimating filters are almost al ays implemented using +IR filters: #y

2.> FIR Decimators 2.4.1 What com!utational sa!in#s do I #ain 'y usin# a FIR decimator?
Since you compute only one of e(ery < outputs, you sa(e <,! operations per output, or an o(erall sa(ings of (<,!)9<. &herefore, the larger the decimation factor is, the larger the sa(ings, percentage,wise. % simple way to thin$ of the amount of computation re8uired to implement a FIR decimator is that it is e8ual to the computation re8uired for a non,decimating *,tap filter operating at the outputrate.

2.4.2 How much memor sa!in#s do I #ain 'y usin# a FIR decimator?
*one. Gou still ha(e to store e(ery input sample in the FIR0s delay line, so the memory re8uirement is the same si#e as for a non,decimated FIR ha(ing the same number of taps.

2.4.3 How do I desi#n a FIR decimator?


Pust use your fa(orite FIR design method. &he design criteria are+ !. &he passband lower fre8uency is #ero- the passband upper fre8uency is whate(er information bandwidth you want to preser(e after decimating. &he passband ripple is whate(er your application can tolerate. 2. &he stopband lower fre8uency is half the output rate minus the passband upper fre8uency. &he stopband attenuation is set according to whate(er aliasing your application can stand. (*ote that there will always be aliasing in a decimator, but you 7ust reduce it to a negligible (alue with the decimating filter.) ;. %s with any FIR, the number of taps is whate(er is re8uired to meet the passband and stopband specifications.

2.4.4 How do I implement a FIR decimator?


% decimating FIR is actually the same as a regular FIR, e)cept that you shift < samples into the delay line for each output you calculate. <ore specifically+ !. Store < samples in the delay line. :. 3alculate the decimated output as the sum,of,products of the delay line (alues and the filter coefficients. ;. Shift the delay line by < places to ma$e room for the inputs of the ne)t decimation. %lso, 7ust as with ordinary FIRs, circular buffers can be used to eliminate the re8uirement to literally shift the data in the delay line.

2.4.

Where can I #et source code to implement a FIR decimator in -?

Iowegian0s )co!eFIR comes with a free set of multirate algorithms, including FIR decimation functions in 3. Pust $ownloa$ and install the ScopeFIR distribution file.

2.4." Where can I #et assem'ly code to implement a FIR decimator?


&he ma7or DSP (endors pro(ide e)amples of FIR decimators in their data boo$s and application notes- chec$ their web sites.

2.4.& How do I test a FIR decimator?


Gou can test a decimating FIR in most of the ways you might test an ordinary FIR+

!. % special case of a decimator is an ordinary FIR. .hen gi(en a (alue of ! for <, a decimator should act e)actly li$e an ordinary FIR. Gou can then do impulse, step, and sine tests on it 7ust li$e you can on an ordinary FIR. :. If you put in a sine whose fre8uency is within the decimator0s passband, the output should be distortion,free (once the filter reaches steady,state), and the fre8uency of the output should be the same as the fre8uency of the input, in terms of absolute '#. ;. Gou also can e)tend the impulse response test used for ordinary FIRs by using a fat impulse , consisting of < consecuti(e ! samples followed by a series of " samples. In that case, if the decimator has been implemented correctly, the output will not be the literal FIR filter coefficients, but will be the sum of e(ery subset of < coefficients. K. Gou can use a step response test. /i(en a unity,(alued step input, the output should be the sum of the FIR coefficients once the filter has reached steady state.

".6 Basics 3.1.1 What are "upsamplin#" and "interpolation"?


6psampling is the process of inserting #ero,(alued samples between original samples to increase the sampling rate. (&his is called #ero,stuffing .) 6psampling adds to the original signal undesired spectral images which are centered on multiples of the original sampling rate. Interpolation , in the DSP sense, is the process of upsampling followed by filtering. (&he filtering remo(es the undesired spectral images.) %s a linear process, the DSP sense of interpolation is somewhat different from the math sense of interpolation, but the result is conceptually similar+ to create in,between samples from the original samples. &he result is as if you had 7ust originally sampled your signal at the higher rate.

3.1.2 Why interpolate?


&he primary reason to interpolate is simply to increase the sampling rate at the output of one system so that another system operating at a higher sampling rate can input the signal.

3.1.3 What is the "interpolation factor"?


&he interpolation factor is simply the ratio of the output rate to the input rate. It is usually symboli#ed by ? , so output rate 9 input rateC?. &ip+ Gou can remember that ? is the symbol for interpolation factor by thin$ing of interpo,L,ation .

3.1.4 Is there a restriction on interpolation factors I can use?


Ges. Since interpolation relies on #ero,stuffing you can only interpolate by integer factorsyou cannot interpolate by fractional factors. ('owe(er, you can combine interpolation and

decimation to achie(e an o(erall rational factor, for e)ample, K9L- see Part >' Resam!ling.)

3.1.4 Which si#nals can 'e interpolated?


%ll. &here is no restriction.

3.1.

When interpolatin#4 do I always need to do filterin#?

Ges. 2therwise, you0re doing upsampling. -,)

3.1." :;4 you 1now what I mean...do I always need to do interpolation $upsamplin# followed 'y filterin#% or can I #et 'y with doin# 7ust upsamplin#?
6psampling adds undesired spectral images to the signal at multiples of the original sampling rate, so unless you remo(e those by filtering, the upsampled signal is not the same as the original+ it0s distorted. Some applications may be able to tolerate that, for e)ample, if the images get remo(ed later by an analog filter, but in most applications you will ha(e to remo(e the undesired images (ia digital filtering. &herefore, interpolation is far more common that upsampling alone.

".2 Multistage 3.2.1 -an I interpolate in multiple sta#es?


Ges, so long as the interpolation ratio, ?, is not a prime number. For e)ample, to interpolate by a factor of !L, you could interpolate by ; then interpolate by L. &he more factors ? has, the more choices you ha(e. For e)ample you could interpolate by !H in+ one stage+ !H two stages+ K and K three stages+ :, :, and K four stages+ :, :, :, and :

3.2.2 -ool. 9ut why 'other with all that?


Pust as with decimation, the computational and memory re8uirements of interpolation filtering can often be reduced by using multiple stages.

3.2.3 :;4 so how do I fi#ure out the optimum num'er of sta#es4 and the interpolation ratio at each sta#e?
&here isn0t a simple answer to this one+ the answer (aries depending on many things. 'owe(er, here are a couple of rules of thumb+

6sing two or three stages is usually optimal or near,optimal. Interpolate in order of the smallest to largest factors. For e)ample, when interpolating by a factor of H" in three stages, interpolate by ;, then by K, then by L. (6se the largest ratio on the highest rate.)

&he multirate ,oo& re*erences gi(e additional, more specific guidance.

"." Im!lementation 3.3.1 How do I implement interpolation?


Interpolation always consists of two processes+ !. Inserting ?,! #ero,(alued samples between each pair of input samples. &his operation is called #ero stuffing . :. ?owpass,filtering the result. &he result (assuming an ideal interpolation filter) is a signal at ? times the original sampling rate which has the same spectrum o(er the input *y8uist (" to Fs9:) range, and with #ero spectral content abo(e the original Fs9:.

3.3.2 How does that wor1?


!. &he #ero,stuffing creates a higher,rate signal whose spectrum is the same as the original o(er the original bandwidth, but has images of the original spectrum centered on multiples of the original sampling rate. :. &he lowpass filtering eliminates the images.

3.3.3 Why do interpolation 'y =ero*stuffin#? .oesn<t it ma1e more sense to create the additional samples 'y 7ust copyin# the ori#inal samples?
&his idea is appealing because, intuiti(ely, this stairstep output seems more similar to the original than the #ero,stuffed (ersion. 5ut in this case, intuition leads us down the garden path. &his process causes a #ero,order hold distortion in the original passband, and still creates undesired images (see below). %lthough these effects could be un,done by filtering, it turns out that #ero,stuffing approach is not only more correct , it actually reduces the amount of computation re8uired to implement a FIR interpolation filter. &herefore, interpolation is always done (ia #ero, stuffing.

".> FIR Inter!olators 3.4.1 How does =ero*stuffin# reduce computation of the interpolation filter?
&he output of a FIR filter is the sum each coefficient multiplied by each corresponding input sample. In the case of a FIR interpolation filter, some of the input samples are stuffed #eros. 1ach stuffed #ero gets multiplied by a coefficient and summed with the others. 'owe(er, this adding,and,summing processing has no effect when the data sample is #ero,, which we $now in ad(ance will be the case for ?,! out of each ? input samples of a FIR interpolation filter. So why bother to calculate these tapsO &he net result is that to interpolate by a factor of ?, you calculate ? outputs for each input using ? different sub,filters deri(ed from your original filter.

3.4.2 -an you #i!e me an e>ample of a FIR interpolator?


'ere0s an e)ample of a !:,tap FIR filter that implements interpolation by a factor of four. &he coefficients are h",h!!, and three data samples, )",): (with the newest, ):, on the left) ha(e made their way into the filter0s delay line+

h 0 x 2

h1

h2

h3

h4

h5

h6

h7

h8

h9

h10 h11

Result

x 1

x 0

x2h0+x1h4+ x0h8 x2h1+x1h5+ x0h9 x2h2+x1h6+ x0h10 x2h3+x1h7+x 0h11

x 2

x 1

x 0

x 2

x 1

x0

x 2

x1

x0

3.4.3 What can I #enerali=e from that e>ample?


&he table suggests the following general obser(ations about FIR interpolators+ Since the interpolation ratio is four (?CK), there are four sub,filters (whose coefficient sets are mar$ed here with matching colors.) &hese sub,filters are officially called polyphase filters .

For each input, we calculate ? outputs by doing ? basic FIR calculations, each using a different set of coefficients.

&he number of taps per polyphase filter is ;, or, e)pressed as a formula+ *polyC*total 9 ?.

&he coefficients of each polyphase filter can be determined by s$ipping e(ery ?th coefficient, starting at coefficients " through ?,!, to calculate corresponding outputs " through ?,!.

%lternati(ely, if you rearranged your coefficients in ad(ance in scrambled order li$e this+ h?@ h>@ h<@ h6@ h0@ h9@ h:, hH, h!", h;, hQ, h!! then you could 7ust step through them in order.

.e ha(e hinted here at the fact that * should be a multiple of ?. &his isn0t absolutely necessary, but if * isn0t a multiple of ?, the added complication of using a non, multiple of ? often isn0t worth it. So if the minimum number of taps that your filter specification re8uires doesn0t happen to be a multiple of ?, your best bet is usually to 7ust increase * to the ne)t multiple of ?. Gou can do this either by adding some #ero, (alued coefficients onto the end of the filter, or by re,designing the filter using the larger * (alue.

3.4.4 What com!utational sa!in#s do I #ain 'y usin# a FIR interpolator?


Since each output is calculated using only *9? coefficients (rather than * coefficients), you get an o(erall computational sa(ings of (* , *9?) per output . % simple way to thin$ of the amount of computation re8uired to implement a FIR interpolator is that it is e8ual to the computation re8uired for a non,interpolating *,tap filter operating at theinput rate. In effect, you ha(e to calculate ? filters using *9? taps each, so that0s * total taps calculated per input.

3.4.

How much memor sa!in#s do I #ain 'y usin# a FIR interpolator?

3ompared to the straight,forward implementation of interpolation by upsampling the signal by stuffing it with ?,! #eros , then filtering it, you sa(e memory by a factor of (?,!)9?. In other words, you don0t ha(e to store ?,! #ero,stuffed upsamples per actual input sample.

3.4." How do I desi#n a FIR interpolator?


Pust use your fa(orite FIR design method. &he design criteria are+ !. &5D

".0 Im!lementation 3. .1 How do I implement a FIR interpolator?


%n interpolating FIR is actually the same as a regular FIR, e)cept that, for each input, you calculate ? outputs per input using ? polyphase filters, each ha(ing *9? taps. <ore specifically+ !. Store a sample in the delay line. (&he si#e of the delay line is *9?.) :. For each of ? polyphase coefficient sets, calculate an output as the sum,of,products of the delay line (alues and the filter coefficients. ;. Shift the delay line by one to ma$e room for the ne)t input. %lso, 7ust as with ordinary FIRs, circular buffers can be used to eliminate the re8uirement to literally shift the data in the delay line.

3. .2 Where can I #et source code to implement a FIR interpolator in -?


Iowegian0s )co!eFIR comes with a free set of multirate algorithms, including FIR interpolation functions in 3. Pust $ownloa$ and install the ScopeFIR distribution file.

3. .3 Where can I #et assem'ly code to implement a FIR interpolator?


&he ma7or DSP (endors pro(ide e)amples of FIR interpolators in their data boo$s and application notes, so chec$ their web sites.

3. .4 How do I test a FIR interpolator?


Gou can test an interpolating FIR in most of the ways you might test an ordinary FIR+ !. % special case of an interpolator is an ordinary FIR. .hen gi(en a (alue of ! for ?, an interpolator should act e)actly li$e an ordinary FIR. Gou can then do impulse, step, and sine tests on it 7ust li$e you can on an ordinary FIR. :. If you put in a sine whose fre8uency is within the interpolator0s passband, the output should be distortion,free (once the filter reaches steady state), and the fre8uency of the output should be the same as the fre8uency of the input, in terms of absolute '#. ;. Gou can use a step response test. /i(en a unity,(alued step input, e(ery group of ? outputs should be the same as the sums of the coefficients of the ? indi(idual polyphase filters, once the filter has reached steady state.

>.6 Basics 4.1.1 What is "resamplin#"?


Resampling means combining interpolation and decimation to change the sampling rate by a rational factor.

4.1.2 Why resample?


Resampling is usually done to interface two systems which ha(e different sampling rates. If the ratio of two system0s rates happens to be an integer, decimation or interpolation can be used to change the sampling rate (depending on whether the rate is being decreased or increased)- otherwise, interpolation and decimation must be used toget#er to change the rate % practical and well,$nown e)ample results from the fact that professional audio e8uipment uses a sampling rate of KA $'#, but consumer audio e8uipment uses a rate of KK.! $'#. &herefore, to transfer music from a professional recording to a 3D, the sampling rate must be changed by a factor of+ (KK!"" 9 KA""") C (KK! 9 KA") C (!KQ 9 !H") &here are no common factors in !KQ and !H", so we must stop factoring at that point. &herefore, in this e)ample, we would interpolate by a factor of !KQ then decimate by a factor of !H".

4.1.3 What is the "resamplin# factor"?


&he interpolation factor is simply the ratio of the output rate to the input rate. /i(en that the interpolation factor is ? and the decimation factor is <, the resampling factor is ? 9 <. In the abo(e e)ample, the resampling factor is !KQ 9 !H" C ".I!AQL

4.1.4 Is there a restriction on the resamplin# factor I can use?


Ges. %s always, the *y8uist criteria must be met relati(e to the resulting output sampling rate, or aliasing will result. In other words, the output rate cannot be less than twice the highest fre8uency (of interest) of the input signal.

4.1.

When resamplin#4 do I always need to a filter?

Ges. Since resampling includes interpolation, you need an interpolation filter. 2therwise, the images created by the #ero,stuffing part of interpolation will remain, and the interpolated signal will not be the same as the original. ?i$ewise, since resampling includes decimation, you seemingly need a decimation filter. 2r do youO Since the interpolation filter is in,line with the decimation filter, you could 7ust combine the two filters by con(ol(ing their coefficients into a single filter to use for decimation. 5etter yet, since both are lowpass filters, 7ust use whiche(er filter has the lowest cutoff fre8uency as the interpolation filter.

4.1." How do I desi#n the resamplin# filter?


%s hinted at abo(e+ Determine the cutoff fre8uency of the decimation filter (as e)plained in Part 2' Decimation.) Determine the cutoff fre8uency of the interpolation filter (as e)plained in Part "' Inter!olation) 6se the lower of the two cutoff fre8uencies to design the resampling filter.

>.2 Multistage 4.2.1 -an I resample in multiple sta#es?


Ges, but there are a couple of restrictions+ If either the interpolation or decimation factors are prime numbers, you won0t be able to decompose those parts of the resampler into stages. Gou must preser(e the *y8uist criteria at each stage or else aliasing will result. &hat is, no stage can ha(e an output rate which is less than twice the highest fre8uency of interest.

4.2.2 -ool. 9ut why 'other with all that?


Pust as with interpolation and decimation, the computational and9or memory re8uirements of the resampling filtering can sometimes be greatly reduced by using multiple stages.

>." Im!lementation 4.3.1 How do I implement resamplin#?


&he straight,forward implementation of resampling is to do interpolation by a factor of ?, then decimation by a factor of <. (Gou must do it in that order- otherwise, the decimator would remo(e part of the desired signal,,which the interpolator could not restore.)

4.3.2 Is that strai#ht*forward implementation efficient?


*o. &he problem is that for resampling factors close to !.", the interpolation factor can be 8uite large. For e)ample, in the case described abo(e of changing from the sampling rate from KA $'# to KK.! $'#, the ratio is only ".I!AQL, yet the interpolation factor is !KQN %lso, you are filtering the signal twice+ once in the interpolator and once in the decimator. 'owe(er, one of the filters has a larger bandwidth than the other, so the larger,bandwidth filter is redundant.

4.3.3 3o what<s the cure?


Pust combine the computational and memory ad(antages that FIR interpolator and decimator implementations can pro(ide. (If you don0t already understand those, be sure to read and understandPart 2' Decimation, and Part "' Inter!olation before continuing.) First, let0s briefly re(iew what ma$es FIR interpolation and decimation efficient+ .hen interpolating by a factor of ?, you only ha(e to actually calculate !9? of the FIR taps per interpolator output. .hen decimating by a factor of <, you only ha(e to calculate one output for e(ery < decimator inputs. So, combining these ideas, we will calculate only the outputs we actually need, using only a subset of the interpolation coefficients to calculate each output. &hat ma$es it possible to efficiently implement e(en FIR resamplers which ha(e large interpolation and9or decimation factors. &he tric$y part is figuring out which polyphase filters to apply to which inputs, to calculate the desired outputs, as a function of ? and <. &here are (arious ways of doing that, but they0re all beyond our scope here.

4.3.4 Where can I #et source code to implement a FIR decimator in -?

Iowegian0s )co!eFIR comes with a free set of multirate algorithms, including FIR resampling functions in 3. Pust $ownloa$ and install the ScopeFIR distribution file.

4.3.

How do I test a FIR resampler?

!. &he most ob(ious method is to put in a sine whose fre8uency is within the resampler0s passband. If an undistorted sine comes out, that0s a good sign. *ote, howe(er, that there will typically be a ramp up at the beginning of the sine, due to the filter0s delay line filling with samples. &herefore, if you analy#e the spectral content of the sine, be sure to s$ip past the ramp,up portion. :. Depending on the resampling factor, resampling can be thought of as a general case of other types of multirate filtering. It can be+ o Interpolation+ &he interpolation factor, ?, is greater than one, and the decimation factor, <, is one. o Decimation+ &he interpolation factor, ?, is one, but the decimation factor, <, is greater than one.

2rdinary Filtering+ &he interpolation and decimation factors, ? and <, are both one.

&herefore, if you successfully test it with all these cases using the methods appropriate for each case, it probably is correct.

You might also like