# How to compensate gain and offset errors

I have an analog circuit which has an offset error as well as an gain error.

I can measure the analog output of the circuit and I am able to manipulate the gain of the circuit by means of a factor k_gain_comp and the offset by an additive value offset_comp.

What is the right approach to compensate those errors? Should first the gain error be compensates or the offset error? How can k_gain_comp and offset_comp be calculated?

• Usually, the offset is calibrated out first. It is easy to set a known input (often ground or with the input capped somehow) and run an accumulation of many noisy input values. Average those to get the offset. Save it. Now you can subtract that from all your adc values and work out the gain figure(s) using a known input or inputs. Average a similar number of values, of course. – jonk Oct 25 '17 at 9:18
• For clarification: Are we talking about compensating these errors by adjustments within the analog circuit? Or compensating by digital processing after the analog signal went through an ADC? Or both? – Grebu Oct 25 '17 at 13:47
• We do not manipulate the analog circuit. The errors will be compensated by software before the signal goes through the ADC. – Maxwell1919 Oct 25 '17 at 15:26

If you have a linear circuit where Vout = f(Vin) and f(0) is ideally 0, it's easy, just null the input offset with 0V applied and apply something close to full scale and adjust the gain. No iterations required.

For more complex situations where f is nonlinear and/or f(0) is not zero you may need to adjust the gain and offset both before and after the nonlinearity and/or you may have to iterate the adjustments.

Ideally you want to minimize the interaction of the adjustments, to do that you have to analyze where the errors are coming from and adjust them appropriately. For example, take a simple signal conditioner that produces 4mA out for -10mV in and 20mA for +90mV in. If most of the offset error is in the front end (before gain) you can adjust out the offset there. So you might apply 0mV and adjust the offset (applied before the gain adjust) to get 5.6mA at the output. Then apply +90mV and adjust the gain to get 20mA. However if the gain has affected the zero a bit you may have to iterate a few times to get the adjustment accurate enough.

Similarly, if the offset is more a result of tolerance of resistors in the output (say you use a zero-drift op-amp but compromise by using cheap 1% resistors rather than fancy ones) you may do better to apply -10mV and adjust for 4mA then +90mV and adjust for 20mA, repeat until both are within tolerance.

If there is a nonlinearity in your circuit (perhaps you are linearizing a sensor) you may need more adjustments, since an error before a nonlinearity will lead to a error in the curve if you adjust it by fiddling the gain after then nonlinearity, and vice versa.

Some of this becomes moot if you digitize the signals and manipulate them in the digital domain, but you still may have to worry about accuracy at the DAC and the ADC in a similar fashion.

• If you have a linear circuit where Vout = f(Vin) and f(0) is ideally 0, it's easy, just null the input offset with 0V applied and apply something close to full scale and adjust the gain. No iterations required. This seems to be the way to go for me. – Maxwell1919 Oct 25 '17 at 15:42
• Will try it out as soon as I have the chance to and accept the answer if the calibration was successful. Thanks a lot! – Maxwell1919 Oct 25 '17 at 15:43
• I found out that the gain is linear only up until 90 percent of the full range. Above 90 percent it starts to flatten. Can I still apply the strategy mentioned in the first passage of your answer (maybe not using the full scale for calibrating the gain but f.e. only 50 percent of it)? – Maxwell1919 Oct 25 '17 at 20:49
• @Maxwell1919 You can do that, but you will lose a bit of accuracy the lower you go. If you can use 75% or 80% it will be better. If you calibrate at 50%, then any error in your gain adjustment will double at 100%. – Spehro Pefhany Oct 25 '17 at 21:22

You can define a straight line as y = mx + c.

There are only two unknowns, the gain or slope m, and the offset c, so you only need to make two measurements to calculate them both.

Make two measurements y0 and y1 for two inputs x0 and x1, then solve for m and c using simultaneous equations see wikipedia, or as two equations is quite simple, trial and error adjustment of m and c until the calculated ys match the measured ys.

The concept of order of compensation does not apply as such. However, with this usual formula, you are multiplying x by the gain before adding c. You could also define a straight line as y = m(x+c), where we add the offset to the measurement before multiplying by gain. We usually use the first form, as it's easier to graph by inspection, and easier to generalise to more terms.

As long as you use the same form to calculate y from x when you are making the calibration measurements, and when you are making the corrections, it does not matter which you use.

• Thanks for that! So, basically I know the reference "straight line" y1 = m1 * x with no offset c1. Then, I make two measurements to calculate m2 and c2 of the error-prone "straight line" y2 = m2 * x + c2. Afterwards, I compensate the gain and offset errors as follows y2_comp = (k_comp*m2) * x + (c_comp+c2), where k_comp = m1/m2 and c_comp = -c2. Could you please confirm the last formula? – Maxwell1919 Oct 25 '17 at 10:34
• No, you're all over the place. Re-read the third paragraph of my answer. I suggest that you do the trial and error thang to adjust m and c so that the calculated y0 and y1 match the measured y0 and y1, I suggest you use a spreadsheet to help with that. – Neil_UK Oct 25 '17 at 10:53
Should first the gain error be compensates or the offset error?

That's up to the particulars of how exactly the compensation circuits get in there and tweak things, and where in the overall process these compensations are applied.

You are basically asking what is better:

Y = mX + b

Y = m(X+ b)

While different values of m and b are required to get the same result from each case, it should be obvious that the two methods are mathematically equivalent.

That said, usually the latter (nulling the offset first before compensating the gain) is desirable. Offsets may get so large after multiplied by the gain of the system, that the result ends up clipping to some limit before the offset can be removed. Even if not clipping, a system can be more stressed, use more power, or whatever, with a off-center signal.

• Whoever downvoted this, what exactly do you think is wrong, misleading, or badly written? – Olin Lathrop Oct 27 '17 at 21:30