FIR Filter Basics
FIR Filter Basics
FIR Filter Basics
html
1.
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.".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.
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.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.
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.
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.
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.
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.
FIR tric$s center on two things !) not calculating things that don0t need to be calculated, and :) fa$ing circular buffers in software.
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.
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.
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
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.
2.1.
% 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.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 ;.
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.
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.
!. % 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.
decimation to achie(e an o(erall rational factor, for e)ample, K9L- see Part >' Resam!ling.)
3.1.
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.
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.)
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.
h 0 x 2
h1
h2
h3
h4
h5
h6
h7
h8
h9
h10 h11
Result
x 1
x 0
x 2
x 1
x 0
x 2
x 1
x0
x 2
x1
x0
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.
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.
4.1.
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.
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.
!. &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.