# Decimation filters: IIR vs. FIR

I just read this article on decimation filters. In particular, in section 2.3.3 it is strongly suggested that FIR filters are advantageous, because you only need to calculate every $$M$$th sample, where $$M$$ is the decimation factor.

So in the case of the FIR, for each decimated sample you have to perform approximately $$M$$ multiplications and additions (this is already taking into account the reduction by calculating only every $$M$$th sample).

However, the same is true for a 1-tap IIR lowpass filter and, in addition such an IIR requires much less memory, namely only $$2$$ numbers, whereas the FIR requires $$2M$$ numbers (delay line and coefficients).

## I will give an application example

... where I believe the IIR is favorable: I have a 5 MHz bitstream and I wish to downsample to 10 Hz, so $$M=500000$$. For the 1-tap IIR, with minimal cost, I just extract every $$M$$th postfilter sample. For the FIR, I would either require a huge array for the coefficients and delays, or would need to decimate in several stages and still look at a much larger resource cost.

What am I missing here that explains the FIR filter's popularity?

## More details as requested:

The signal being digitized is a temperature sensor reading, which is slowly changing DC. I want to allow for up to 5 Hz nyquist frequency, so that some unlikely low frequency oscillations do not alias into the "DC" reading. However, the passband flatness and phase between DC at 5 Hz is irrelevant. I also want to maintain a couple of Hz of data rate to recognize temperature steps with sufficient responsiveness.

My 1-tap IIR filter looks like: $$y[n] = y[n-1]+(x[n] - y[n-1]) >>16$$ where 32-bit word size is used. The resulting analog resolution is approximately 16 bit.

I am wondering, if by using FIR-based decimation, I could markedly improve the resolution without exploding computational cost. E.g. I could decimate by 8, in 6 stages for $$M=8^6=262144$$. But would that give a useful advantage in resolution ?

• For starters, you can use polyphase decimation with FIR filters. This allows you to reduce by "M" where M is your decimation factor, the number of calculations required. Further, FIR filters can easily be made phase-linear, which can be required in some applications, like telecoms.
– Ben
Jul 14, 2022 at 21:13
• @Ben the numbers mentioned in my question for the number of calculations are already taking this reduction into account. Good point about the phase though. The FIR allows better control over the passband properties. In my application, this just isn't so relevant. So it looks like IIR can be worthwile in some applications.. Jul 14, 2022 at 21:37
• @tobalt it definitely can – audio applications abound – it's just that in decimation, it's not that common to actually use an IIR, for the reasons mentioned by Ben, and also, general problems with instability due to numerical precision that quickly becomes very awkward. I can't really imagine a 1/500000-band, 1/1000000 transition width IIR filter working reliably (unless its passband is basically round, not even remotely flat), but I've never even tried to do that practically. Jul 14, 2022 at 21:54
• good answer from Dan. in a professional context, i've done reconstruction filters which have sorta the same function of decimation filters. it's always simpler with polyphase FIR. but when i was in skool, we did "multirate DSP" where we would decimate by a factor of 2 or 4 multiple times with simple 1st or 2nd order IIR until the last stage. at the last decimation stage, we used an FIR filter that compensated for all of the rolloff that the half-dozen previous stages imposed on the final passband. IIR is okay for each stage of multistage decimation. but for one single stage, maybe not. Jul 15, 2022 at 2:28
• @MarcusMüller so is the poor passband shape the chief disadvantage of the IIR ? I have added a small paragraph with further detail. Jul 15, 2022 at 7:51

To decimate a 5 MHz sampled signal to a sampling rate of 10 Hz properly we need to define the expected passband and the performance needed over that passband in terms of passband distortion and dynamic range or signal to noise ratio that needs to be maintained. We also need to know the noise floor or spectral characteristics of the 5 MHz signal from DC to 2.5 MHz (this may be the quantization noise given by the number of bits used, or there may be additional signal occupying that spectrum).

Once defined, we can then design the decimation filter, which is an anti-alias filter required prior to down-sampling. This filter can be either an IIR or an FIR, but in my experience once actual performance is considered the FIR can be accomplished with less resources and less challenges related to precision issues especially for fixed point implementations.

Consider that the ideal decimation filter is to pass with minimum distortion the passband from DC to some upper limit $$f_b$$ that is less than Nyquist (Nyquist here is at 5 Hz), and suppress the alias bands which will begin at 10 Hz - $$f_b$$. I doubt any significant rejection of this first alias can be achieved with a single tap IIR running at 5 MHz unless we have no concern for the passband shape, and then all the additional alias bands that will be at $$10N \pm f_b$$ Hz throughout the rest of the original spectrum (with $$N$$ as positive integers). Even if we don't have a known interference in these alias regions, such a decimation filterwill have significant SNR degradation due to noise accumulation from the 250,000 alias's within the primary Nyquist band (at the higher 5 MHz rate) that will accumulate in band if not properly filtered out! ($$10log_{10}(250,000) = 54$$ dB or 9 bits if there was no filter).

A realizable solution is a multistage decimation since even a single stage FIR filter with such transition bandwidth requirements would be prohibitive in comparison. If actual performance requirements needed can be given, we can further detail the trade to see if there is indeed a counter example. A CIC (Cascade-Integrator-Comb) with a subsequent shaping filter could also be a very efficient solution if the passband needed is actually significantly smaller than Nyquist for the final 10 Hz rate, or alternatively CIC down to an intermediate frequency such as 100 Hz and then a higher performance decimation to go from 100 Hz to 10 Hz where the inverse Sinc shaping to compensate for the CIC could be included.

Update:

The OP added further details suggesting the desired result is the best estimate of the short term mean value. (Estimate of temperature). To determine the best filter we do need to understand the spectral characteristics of the 5 MHz sampled signal (assuming all prior details to ensure anti-aliasing of signals that may exist above ~ 10 MHz and beyond. (Related to this and not to do with the filter is ensuring that DC-offset errors which would typically be introduced in the sampling process have been properly calibrated out noting that such offset itself would typically change with temperature). However short of having this information, if we make an assumption of white noise and that the signal is stationary enough (over the averaging interval suggested by the decimated sampling rate), it is known that the optimum filter is a simple moving average under that noise condition. A CIC filter conveniently is such a moving average filter! This means if we were to decimate the waveform by a factor of 500,000, a CIC implemented as a moving average over 500,000 samples would result in the best SNR result for a signal occupying the resulting Nyquist bandwidth. Notably the resulting noise (with a white noise input) would also be white and opportunity remains to further average the result if it is determined that the signal occupies much less bandwidth and is sufficiently stationary over the considered averaging interval. Below I compare the frequency response of the OP's IIR to that for a 500,000 sample moving average filter, and below that I show what that implementation looks like as a functional block diagram.

What we see in this plot is how for the purpose of decimating to a 10 Hz rate, the suggested IIR filter is insufficient in rejecting aliases that would fold back in to degrade the estimate of the mean (DC component). These aliases would be at every integer multiple of the final output rate or 10 Hz. Notice, beautifully, how the CIC provides infinite rejection (limited in the plot due to resolution of the plot only) at each of these alias locations! Certainly this suggests for the IIR approach that a larger $$\alpha$$ is needed (current $$2^{16}$$), but even once reduced to match the general roll-off shape of the CIC, we won't get the added benefits of the nulls placed at the harmonics of the sampling rate that the CIC provides.

The implementation of this first order CIC is quite simple! As shown below, the CIC decimator to go from 5 MHz to 10 Hz consists of an accumulator, followed by down-sampling or selecting every 500,000th sample from the accumulator output, and then taking the successive difference of the resulting 10 Hz samples. The accumulator precision is such that it can only roll-over once in any 500,000 sample interval (and specifically that it does roll-over on an overflow). If a 32 bit accumulator was used, this would specify that the maximum input precision be $$32-log_2(500,000)+ 1 = 14$$ bits. (Such that a worst case continuous full-scale input would not overflow more than once in the 500,000 sample averaging interval.)

It would appear that the CIC is itself an IIR filter, given the feedback structure, but it in fact has a finite impulse response and is indeed an FIR filter. It is just an efficient implementation of a moving tap FIR filter (with 500,000 unity gain coefficients!), followed by a down sampler to select every 500,000th sample- the result would be identical.

As mentioned, further details are needed for the actual design- specifically the spectrum and the statistics of the signal. For instance, if the signal was stationary (it isn't!) and the spectrum indicated all other noise is white, then the optimum filter to estimate the mean is a simple average. For non-stationary signals (such as temperature versus time) under the condition of additive white noise only, the optimum filter is a moving average constrained over time to the bias instability floor (using terms from gyroscope sensors). What is great to know is the CIC filter IS exactly a simple moving average filter as the decimator filter and commonly used in decimator implementations due to its grand simplicity! The optimum time duration (and therefore optimum decimation) is easily determined from a plot of the Allan Deviation, where the temperature measurement would be treated as a "frequency input" (the Allan Deviation is primarily used to measure the frequency stability of oscillators). The Allan Deviation provides a plot of rms error versus time duration $$\tau$$, and if averaging of the signal would provide any benefit, the error would decrease as $$\tau$$ increases, ultimately to a minimum suggesting the best averaging time.

Example ADEV plot from https://www.phidgets.com/docs/Allan_Deviation_Primer showing a minimum for their example data at around 100 seconds. This means for this particular data, the estimate of the mean would be improved by increasing the averaging time up to 100 seconds, but any longer than that and the result would not get better, and ultimately as we go longer than 200 seconds or so, the estimate would degrade. (Typically this plot is shown with the vertical axis on a log scale as well). If the ADEV is decreasing at a rate of $$1/\sqrt{\tau}$$, the noise in that region can be considered white. We see this on this chart with the ADEV dropping by a factor of 10 when $$\tau$$ goes from 0.1 to 10 (a factor of 100), and thus the noise would be white over a frequency range from approximately from at least 10 Hz to the floor at 1/100th of a Hz.

For the users application this may suggest a final sampling rate that is larger or smaller than 5 Hz. A review of the spectrum (to see if narrow band interferences need to also be filtered out) and then once removed, a review of the Allan Deviation of the resulting waveform under maximum temperature change conditions to determine the optimum $$\tau$$ would then lead to the best sampling rate and filter.

An additional note related to measuring the mean value at DC is the limitation that will be given due to $$1/f$$ noise. The review of the spectrum under condition of stable temperature would indicate if this is of concern, and if so lead to other sensing architectures that would avoid measuring DC.

• Thank you very much Dan. Same as you have trouble arriving at a precise statement without more details in my question, I have trouble parsing your information into a useful recommendation for me. Therefore, I have added some more detail to the question. Maybe this sufficient, so you can eliminate some options and conclude if the FIR is worth it. Jul 15, 2022 at 7:49
• @tobalt thanks for the more info and clarifying your application. Your intention then is to get the best estimate of the mean with a signal that will ultimately be non-stationary over a certain time duration. I added some more details and suggestions of what you may be able to further do toward a solution of what would be optimum. I think we're still far from a precise statement but I do suggest next steps that would be worth while if you are interested to pursue them. Jul 16, 2022 at 13:09
• @tobalt if you can do a long capture of your 5MHz sampled signal with the sensor and then with the sensor replaced with a resistor at the sensor's nominal impedance together with all other analog components attached at the ADC input), I can show you the example ADEV and filtering options. We could start with a 1 second capture to define initial resampling/ filtering factors and then with that in place review a much longer capture under maximum temperature change conditions. Jul 16, 2022 at 13:34
• thanks, but this won't be necessary, I am well aware of the Allen deviation and have to preach it to users of the "just integrate longer" kind. 😉 I can just check it directly on the raw bitstream, but my gut feeling is that the minimum is at a few Hz anyway. So you suggest basically a long "box" integrator/counter to sum up e.g. 500k bits. Interesting, because I think I tried that already and it came out worse than my IIR filter above. I guess this suggests then that the Allen deviation minimum is actually much below 5 Hz. Jul 16, 2022 at 14:10
• @tobalt more importantly I suggest as a first step inspecting your broadband (5MHz sampled rate) spectrum, ultimately we would want to understand what that noise looks like that would be aliased in. The only purpose of this filter is anti-aliasing so what is needed is completely dependent on what is there. There is many opportunities for spurious noise from your electronics etc so you really need to see what the ADC is seeing. (And the min for ADEV is both dependent on the characteristics of your noise but also the max rate of change of your signal- both unknown to me but my guess is sub-Hz) Jul 16, 2022 at 14:21