# 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?) – robert bristow-johnson Feb 16 at 8:23
• how does one correct offset and gain errors without a reference to compare against? – robert bristow-johnson Feb 16 at 8:25

## 1 Answer

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. – sundar Feb 15 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 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. – sundar Feb 15 at 14:43
• I edited my question with the relation of impulse response (coefficients) with m2,b. – sundar Feb 15 at 15:01
• multiply each of your coefficients by m1/m2 Or simply add a gain at the output... – Ben Feb 15 at 15:09