# Calculating filter coefficients of a Moving average with error correction

I have an oversampling ADC where I need to correct Gain/offset errors 'during' each conversion. This also links to my previous question - Moving average and linearization of two piecewise linear systems

For simplicity, it is a First order Incremental Sigma Delta with 1000 sampling clock cycles for one conversion, meaning that I have to average 1000 samples at the end of each conversion for filtering. A straightforward approach is to use an accumulator (1/z-1). However, using this approach I can correct the errors only after conversion.

After plotting the input and output of ADC with a DC sweep, I get the characteristic similar to Blue line shown below:

If I remove the offset (b) and gain(m2) errors, I would be left with violet line which is my objective.

One straightforward way of correcting this would be at the end of each conversion cycle, which is correcting at the 'end' of conversion. The approach what I am looking for is to correct it 'during' the filtering.

My filter is normally an accumulator- (1/z-1), which is an IIR variant. However, if we take its FIR variant, it would be something similar to:

Here, normally the coefficients b0,b1,b2....bn are all '1' to perform an accumulator operation. However, I am thinking if I would be able to somehow change these coefficients (depending on m2 and b), I would be able to correct this gain and offset 'during' filtering.

I am not sure on how to get these coefficients, which is exactly where I am stuck.

For example, in a simple moving average of 20 taps, the impulse response would look something like below: (each dot denoting the coefficients - b0,b1,b2...bn)

However, to correct the gain/offset errors,could the impulse response be 'bent' like below?

The slope and intercept of this red line should be dependent on m2 and b, but not sure how to link it.

• "If I remove the offset (b) and gain(m2) errors, I would be left with violet line which is my objective."---- wouldn't you be left with the red line? if you removed only the offset error (b), then you would be left with the violet line. (or am i seeing the colors differently?) Feb 16, 2019 at 8:23
• how does one correct offset and gain errors without a reference to compare against? Feb 16, 2019 at 8:25

Simply add a compensating gain at the output of your recursive moving-averaging. This compensating gain can be updated on the fly.

Adding an varying input gain at the input of the recursive implementation would also work, but it would take more time for the gain change to be effective, it would take "N" samples for the change to be applied 100% where N is the tap length of your moving-average.

• After each summing node? How to calculate the gain values of those compensating gain? I am sure that it is somehow dependent on m2 and b, but not sure how exactly. Feb 15, 2019 at 13:31
• If you have a recursive implementation, you don't have coefficients per se. So the only place to apply a gain is either at the input or the output of your recursive Moving-average filter. You wanna know how to calculate the gain? Gain = m1/m2 from what I understand
– Ben
Feb 15, 2019 at 14:20
• I have a non-recursive implementation - (taps at each delay block), for which I wanted to find the coefficients (b0,b1,b2....bn), n is the number of taps. Feb 15, 2019 at 14:43
• I edited my question with the relation of impulse response (coefficients) with m2,b. Feb 15, 2019 at 15:01
• multiply each of your coefficients by m1/m2 Or simply add a gain at the output...
– Ben
Feb 15, 2019 at 15:09

The system requires both a subtraction and a scaling to compensate for both the offset error and gain error. Given an offset error $$b$$ and gain slope $$m$$ such compensation can be applied at the input prior to the filter with offset compensation $$b/N$$ and gain compensation $$1/m$$, or at the output after the filter with offset compensation $$b$$ and gain compensation $$1/m$$ with the same result other than fixed precision differences (which for what appears to be a small correction here would be insignificant). A third choice of scaling the coefficients by $$1/m$$ can also be done to address the gain error, but this will not be able to address the offset error as is clear with the math applied, and also will require significantly more processing than what would have otherwise been a simple summation.

Note that the moving average itself may already have the $$/N$$ factor applied in its own scaling, making the additional adjustment described at input and output equivalent (subtract $$b$$ and scale by $$1/m$$ for both cases).