Simple (Arduino) digital butterworth filter : why do I see oscillations?

I want to filter deviations from an ADC over time (low pass), not very critical. So I first tried exponential averaging, something like y= (7*y+x)/8. That gave useful results, but there is an obvious rounding issue (no increase as long as x smaller than y+8, decrease if x smaller than y). Adding +4 doesn't really solve the problem.

So I implemented a fixed-point lowpass Butterworth third order filter, with various cutoff frequencies, based on coefficients extracted from http://www-users.cs.york.ac.uk/~fisher/mkfilter/trad.html (unfortunately down since a few months).

The code looks as follows for 0.033 sampling cutoff

byte dtTI, // filter input (0 newest)
dtTIFast; // filter output, =y, =8 bit decimals
// dtTIFast[2,4,6]= previous filter outputs
dtTIFast=dtTIFast; dtTIFast=dtTIFast; dtTIFast=dtTIFast;
dtTIFast=dtTIFast; dtTIFast=dtTIFast; dtTIFast=dtTIFast;
unsigned long modifier = 30UL*64*(dtTI + dtTI + 3*dtTI + 3*dtTI);
modifier += 84739UL*(dtTIFast/4+64*dtTIFast);
modifier -= 73839UL*(dtTIFast/4+64*dtTIFast);
modifier += 21628UL*(dtTIFast/4+64*dtTIFast);
dtTIFast=modifier>>21;  dtTIFast=modifier>>13;  // No need to add 2^12, 0.0004deg

The output seemed to give correct results at first, but if I iterate the filter 50 times with the same input(s), I see it keeps oscillating instead of converging to a steady value, and not centered around the x input. For other values (for example 0.001 cutoff), its way worse !

Can you tell me where I'm wrong ? I checked that the sum of my coefficient is 32768, and it looks like there can't be any overflow. I use 14bits y values now, and the real values of coeffs are 2.586028659, -2.253398256 0.660048953 and 0,000915081. Should I need more precision ?

Thanks for your help

• Have you analyzed the "real valued" impulse response of your filter? This is an IIR, and that name means something! – Marcus Müller Apr 14 '16 at 10:04
• Also, with an 1/30 low pass filter, and assuming you have enough memory for coefficients and values, you might just go the route of a FIR, and do a decimation=30 (or just 15) polyphased implementation of your FIR, reducing complexity to 1 (or 2) MACs per input sample. – Marcus Müller Apr 14 '16 at 10:06
• One guess could be that your states (the dtTIFast array) is truncated to 16 bits....You could gain alot in clarity and also efficiency if you stored all your variables and coefficients in shorts (short arrays) instead of chars and constants. – niaren Apr 14 '16 at 11:40
• The FIR suggestion seems slightly dubious to me. Is it really possible to take a VERY long LP FIR filter and reduce its implementation to 1 or 2 MACs per sample without introducing alising distortion? – niaren Apr 14 '16 at 11:52
• My states are indeed truncated to 16 bits. Now that means physically 1/2560th of a Celsius degree ... The reason or using arrays of chars/bytes is that it's the way those are sent over air (RF packets). Regarding memory, I use several "daily" memories (1 byte/hour) and arrays such as dtTI are actually holding the 16 last (byte) values. About the "real valued" response, I did some Excel simulations, but with a "noisy" input, so the oscillation character was not visible there. – Tochinet Apr 14 '16 at 12:05