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.
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.