Worst-case analysis in Python saves time and gives reliable results.
Introduction
By leveraging the power of Python, worst-case analyses can be performed faster and more reliably than the usual methods used by the aerospace industry.
When delivering hardware to a customer it is typical to also deliver analyses as data products. One such analysis is the worst-case analysis. Hardware performance must be analytically verified to meet requirements for the life of the mission, across all operational environments, with worst-case component variations.
Charles Hymowitz at EDN has written an excellent series on the challenges of worst-case analysis. Due to these difficulties the analysis is often completed late in the design process. The potential for finding hardware performance deficiencies late is a technical and programmatic risk.
To reduce that risk I have developed a worst-case analysis tool in Python to make it easier to perform worst-case analyses early in the design process. This post will work through an example of how this tool might be used. The code found in this post is also available on GitHub.
Motivation
An example of high-side current sensing
Let us select a practical example to work through. Texas Instruments has written an application note on high-side current sensing circuit design. The first design note reads, “DC common mode rejection (CMRR) performance is dependent on the matching of the gain setting resistors, R2-R5.” Nowhere in the note is there a suggested resistor tolerance or an expected end-to-end measurement uncertainty. This is the job of a worst-case analysis.
The typical spreadsheet method
The typical method for performing such a worst-case analysis is to construct a spreadsheet with the extreme-value component variations and manually calculate the desired results. I have created such a spreadsheet assuming a 1 A load current, a 36 V bus, 1% resistor tolerances, and a ±150 µV amplifier offset voltage per the OPA192 datasheet.
There are a few downsides to the spreadsheet approach:
- Difficult to make. The engineer must ensure the calculations are indeed the worst-case. Is the worst-case when R3 is at a minimum, a maximum, or somewhere in between? Depending on the equations involved it may not be so obvious.
- Difficult to understand. An independent review of the analysis requires an engineer to click on every cell of the spreadsheet and parse the equation inside for correctness.
- Difficult to interpret. Once a result is achieved it remains hard to be definitive about which input parameters are driving the final variations and to what degree.
Getting started with Python
Let us perform this calculation with Python. To start, import the necessary modules: numpy, scipy, and worstcase.
import numpy as np
from scipy.linalg import solve
from worstcase import param, derive, unit
The param and derive objects will do all the heavy-lifting in the following analysis. The unit object is simply an alias for the default Pint UnitRegistry.
Defining the input parameters
Begin by defining resistors R1 through R5 and the amplifier offset voltage Vos. A parameter is the foundational object of any calculation using the worstcase module. A parameter represents a quantity’s nominal value, lower bound, and upper bound.
R1 = param.bytol(nom=100 * unit.mohm, tol=0.01, rel=True, tag="R1")
R2 = param.bytol(nom=1.001 * unit.kohm, tol=0.01, rel=True, tag="R2")
R3 = param.bytol(nom=50.5 * unit.kohm, tol=0.01, rel=True, tag="R3")
R4 = param.bytol(nom=1.001 * unit.kohm, tol=0.01, rel=True, tag="R4")
R5 = param.bytol(nom=50.5 * unit.kohm, tol=0.01, rel=True, tag="R5")
VOS = param.bytol(nom=0 * unit.mV, tol=150 * unit.uV, rel=False, tag="VOS")
The above parameters are defined by their tolerance (relative or absolute). They could alternatively be defined by range param.byrange(nom, lb, ub). The tag is used only for the string representation.
print([R1, R2, R3, R4, R5, VOS])
'''
[R1: 100 mΩ (nom), 99 mΩ (lb), 101 mΩ (ub),
R2: 1.001 kΩ (nom), 991 Ω (lb), 1.011 kΩ (ub),
R3: 50.5 kΩ (nom), 49.99 kΩ (lb), 51.01 kΩ (ub),
R4: 1.001 kΩ (nom), 991 Ω (lb), 1.011 kΩ (ub),
R5: 50.5 kΩ (nom), 49.99 kΩ (lb), 51.01 kΩ (ub),
VOS: 0 mV (nom), -150 µV (lb), 150 µV (ub)]
'''
Defining the output voltage
Next we will define a function to calculate the amplifier’s output voltage. In addition to the input parameters defined above, we will also include the bus voltage and load current as arguments.
@derive.byev(r1=R1, r2=R2, r3=R3, r4=R4, r5=R5, vos=VOS)
def VO(vbus, iload, r1, r2, r3, r4, r5, vos):
vp = vbus * r3 / (r2 + r3)
vn = vp + vos
vo = vn - (vbus - r1 * iload - vn) * r5 / r4
return vo
The @derive.byev decorator takes the decorator arguments and forms a partial binding into the function arguments. The decorator returns a “derived” parameter. In this case the parameter is derived by Extreme-Value analysis using brute force calculation. Root-Sum-Squared and Monte-Carlo analyses are also supported by @derive.byrss and @derive.bymc, respectively.
Computing the end-to-end measurement uncertainty
1 A load current
Given the output voltage defined as a derived parameter, we can now compute the amplifier output voltage for a 1 A load current and a 36 V bus. Note that derived parameters (such as VOUT_1A shown below) can subsequently be used to define other derived parameters. This is extremely useful for setting up a complex series of inter-dependent calculations… but we won’t do that here.
VOUT_1A = VO(vbus=36 * unit.V, iload=1 * unit.A, tag="VOUT_1A")
print(VOUT_1A)
'''
VOUT_1A: 5.045 V (nom), 3.647 V (lb), 6.387 V (ub)
'''
Next we will compute the end-to-end measurement uncertainty by defining a new function, similar to before. In fact, we can make use of the function we already defined for solving the output voltage.
@derive.byev(r1=R1, r2=R2, r3=R3, r4=R4, r5=R5, vos=VOS)
def IUNC(r1, r2, r3, r4, r5, vos, vbus, iload):
vo = VO(vbus, iload, r1, r2, r3, r4, r5, vos)
return vo / VO(vbus, iload).nom * iload - iload
IUNC_1A = IUNC(vbus=36 * unit.V, iload=1 * unit.A, tag="IUNC_1A")
print(IUNC_1A)
'''
IUNC_1A: 0 A (nom), -277 mA (lb), 266 mA (ub)
'''
Derived parameters also support sensitivity studies with the ss() method. By providing a parameter (or a list of them) as an argument, a new derived parameter is returned where only the parameters provided as arguments are varied. All other underlying parameters are kept at their nominal value.
IUNC_1A_sensitivities = [
IUNC_1A(tag="IUNC_1A-R1-sens").ss(R1),
IUNC_1A(tag="IUNC_1A-VOS-sens").ss(VOS),
IUNC_1A(tag="IUNC_1A-R2-thru-R5-sens").ss([R2, R3, R4, R5]),
]
print(IUNC_1A_sensitivities)
'''
[IUNC_1A-R1-sens: 0 A (nom), -10 mA (lb), 10 mA (ub),
IUNC_1A-VOS-sens: 0 A (nom), -1.53 mA (lb), 1.53 mA (ub),
IUNC_1A-R2-thru-R5-sens: 0 A (nom), -265.3 mA (lb), 254.7 mA (ub)]
'''
Just as the TI application note had alluded to, the end-to-end measurement uncertainty is being driven by resistors R2 through R5.
50 mA load current
Mismatches between resistors R2 through R5 supposedly degrade the amplifier’s common mode rejection. We can demonstrate the error source is common mode by rerunning the same computations for a 50 mA load current. Doing so reveals a worst-case end-to-end measurement uncertainty similar to that of a 1 A load current.
VOUT_50mA = VO(vbus=36 * unit.V, iload=50 * unit.mA, tag="VOUT_50mA")
IUNC_50mA = IUNC(vbus=36 * unit.V, iload=50 * unit.mA, tag="IUNC_50mA")
print([VOUT_50mA, IUNC_50mA])
'''
[VOUT_50mA: 252.2 mV (nom), -1.193 V (lb), 1.642 V (ub),
IUNC_50mA: 0 mA (nom), -286.5 mA (lb), 275.5 mA (ub)]
'''
IUNC_50mA_sensitivities = [
IUNC_50mA(tag="IUNC_50mA-R1-sens").ss(R1),
IUNC_50mA(tag="IUNC_50mA-VOS-sens").ss(VOS),
IUNC_50mA(tag="IUNC_50mA-R2-thru-R5-sens").ss([R2, R3, R4, R5]),
]
print(IUNC_50mA_sensitivities)
'''
[IUNC_50mA-R1-sens: 0 mA (nom), -500 µA (lb), 500 µA (ub),
IUNC_50mA-VOS-sens: 0 mA (nom), -1.53 mA (lb), 1.53 mA (ub),
IUNC_50mA-R2-thru-R5-sens: 0 mA (nom), -284.4 mA (lb), 273.5 mA (ub)]
'''
Computing the common mode gain
We can calculate the common mode gain to verify it is the root cause of the end-to-end measurement uncertainty. As was done in the earlier spreadsheet, we could solve for the common mode gain by performing the necessary algebra. Since we are already using Python, why not compute it numerically instead?
Vcm is the common mode voltage measured at the center of the sense resistor, R1. Vdm is the differential mode voltage measured across the sense resistor. The above equation has three unknowns: common mode gain Acm, differential mode gain Adm, and output voltage offset Voffset. Therefore we can select three different operating points and use a linear solver to solve the resulting system of equations.
@derive.byev(R1, R2, R3, R4, R5, VOS, tag="CMGAIN")
def CMGAIN(r1, r2, r3, r4, r5, vos):
def cmgain_constraints(vbus, iload):
vo = VO(vbus, iload, r1, r2, r3, r4, r5, vos)
vdm = r1 * iload
vcm = vbus - r1 * iload / 2
return vo.m, vdm.m, vcm.m # strip units for scipy compatibility
# three equations for three unknowns (Adm, Acm, Voff)
vo1, vdm1, vcm1 = cmgain_constraints(0 * unit.V, 0 * unit.A)
vo2, vdm2, vcm2 = cmgain_constraints(1 * unit.V, 10 * unit.A)
vo3, vdm3, vcm3 = cmgain_constraints(3 * unit.V, 20 * unit.A)
b = np.array([vo1, vo2, vo3])
A = np.array([[vcm1, vdm1, 1], [vcm2, vdm2, 1], [vcm3, vdm3, 1]])
x = solve(A, b)
return x[0] * unit([]) # reappend units (dimensionless)
print(CMGAIN)
print(CMGAIN(tag="CMGAIN-R2-thru-R5-sens").ss([R2, R3, R4, R5]))
'''
CMGAIN: 2.842E-14 (nom), -0.04 (lb), 0.03846 (ub)
CMGAIN-R2-thru-R5-sens: 2.842E-14 (nom), -0.04 (lb), 0.03846 (ub)
'''
The common mode gain is about ±40 mV/V and the sensitivity study confirms it is entirely driven by resistors R2 through R5. This is rather terrible performance. For a 36 V bus without any load current the amplifier output voltage will be about 1.4 V worst-case against an expected output voltage of 0 V. If the intended full-scale range of the amplifier is 5 V, then 1.4 V is almost a 30% full-scale error!
Increasing the accuracy
Now that we have computed the end-to-end measurement uncertainty and understand its drivers, we can go back to our initial parameter definitions and reduce the tolerances until we reach an acceptable level of performance.
Upgrade to 0.1% resistors
Here are some selected outputs when resistors R2 through R5 are updated to be 0.1% tolerance. Performance is improved by a factor of 10. Those same resistors still remain the drivers in the overall end-to-end measurement uncertainty.
IUNC_1A: 0 A (nom), -37.59 mA (lb), 37.44 mA (ub)
[IUNC_1A-R1-sens: 0 A (nom), -10 mA (lb), 10 mA (ub),
IUNC_1A-VOS-sens: 0 A (nom), -1.53 mA (lb), 1.53 mA (ub),
IUNC_1A-R2-thru-R5-sens: 0 A (nom), -26.04 mA (lb), 25.94 mA (ub)]
IUNC_50mA: 0 mA (nom), -29.98 mA (lb), 29.86 mA (ub)
[IUNC_50mA-R1-sens: 0 mA (nom), -500 µA (lb), 500 µA (ub),
IUNC_50mA-VOS-sens: 0 mA (nom), -1.53 mA (lb), 1.53 mA (ub),
IUNC_50mA-R2-thru-R5-sens: 0 mA (nom), -27.94 mA (lb), 27.83 mA (ub)]
CMGAIN: 2.842E-14 (nom), -0.00393 (lb), 0.003915 (ub)
Upgrade again to 0.01% resistors
Here are some selected outputs when resistors R2 through R5 are updated to be 0.01% tolerance. Performance is improved by an additional factor of 5. The overall end-to-end measurement uncertainty is now driven by different parameters depending on the load current (at higher currents the sense resistor becomes the driver).
IUNC_1A: 0 A (nom), -14.13 mA (lb), 14.13 mA (ub)
[IUNC_1A-R1-sens: 0 A (nom), -10 mA (lb), 10 mA (ub),
IUNC_1A-VOS-sens: 0 A (nom), -1.53 mA (lb), 1.53 mA (ub),
IUNC_1A-R2-thru-R5-sens: 0 A (nom), -2.599 mA (lb), 2.598 mA (ub)]
IUNC_50mA: 0 mA (nom), -4.82 mA (lb), 4.818 mA (ub)
[IUNC_50mA-R1-sens: 0 mA (nom), -500 µA (lb), 500 µA (ub),
IUNC_50mA-VOS-sens: 0 mA (nom), -1.53 mA (lb), 1.53 mA (ub),
IUNC_50mA-R2-thru-R5-sens: 0 mA (nom), -2.789 mA (lb), 2.788 mA (ub)]
CMGAIN: 2.842E-14 (nom), -0.0003923 (lb), 0.0003921 (ub)
A design choice
Based on these results, the design engineer has a choice. They can either pay for ultra-high precision resistors or change to an instrumentation amplifier specifically designed for high-side current sensing.
Which option is selected likely depends on what stage of the design process they are in. Changing the amplifier is the right choice early on. Buying better resistors could be a suitable band-aid to fix an already existing piece of hardware.
Conclusion
Using a TI application note on high-side current sensing circuit design as a practical example, a worst-case analysis was demonstrated with both a typical spreadsheet and Python. The worstcase analysis package is an effective alternative to the spreadsheet method by alleviating the spreadsheet’s downsides:
- Easy to make. The engineer need only to define the governing equations for the analysis. The Python analysis tool will find the worst-case on its own.
- Easy to understand. Equations written in Python are far more intelligible than equations in a spreadsheet. Additionally, Python can be used to numerically solve for quantities of interest in order to circumvent complicated algebra.
- Easy to interpret. The Python analysis tool offers a sensitivity study feature to inspect the uncertainty drivers of any derived quantity.
Using Python to perform worst-case analyses allows the design engineer to fully analyze their designs earlier in the design process. This reduces the technical and programmatic risk of identifying hardware performance deficiencies late.