---
title: Op-amp circuits lab
heading: Common-mode rejection, filter design, meters and transimpedance compensation, in Python
eyebrow: C6 · Lesson 32 · Op-amp circuits lab
xp: 50
---

::: hook
Lessons 16 to 29 built a toolkit of op-amp circuits, each with design formulas. In this lab you'll turn the formulas into design functions and check the results the formulas only estimate: a difference amplifier's exact common-mode rejection with real resistor errors, the component values of multiple-feedback and Tow-Thomas filters, what an average-responding meter really reads for waveforms other than sines, and the exact feedback capacitor that flattens a transimpedance amplifier.

::: play
- The bars show what an average-responding meter, calibrated for sines, reads for a sine, a square and a triangle of the same peak, against their true RMS values.
- For a sine the two agree, by design. For a square the meter reads 11% high; for a triangle, about 4% low.
- Only a true-RMS meter reads all three correctly.
:::
:::

```sim plot
view: graph
x: k = 0.5..3.5
y: v = 0..1.3 V
point: 1, 0.7071 | label: sine RMS
point: 1, 0.7071 | color: ch2 | label: sine reading
point: 2, 1 | label: square RMS
point: 2, 1.1107 | color: ch2 | label: square reading
point: 3, 0.5774 | label: triangle RMS
point: 3, 0.5554 | color: ch2 | label: triangle reading
```

::: predict
## A difference amplifier with a gain of 10 is built from 0.1% resistors, and the worst-case formula $\frac{1 + R_2/R_1}{4\varepsilon}$ gives a CMRR of 2750. If only the two resistors on the non-inverting side are off by 0.1%, in opposite directions, what's the CMRR?

- [ ] 2750
- [x] About 5500
- [ ] About 1375
- [ ] Infinite

::: reveal
About 5500, twice the worst case: only half of the possible error is present. The worst-case formula assumes all four resistors are off by the full tolerance in the most damaging directions. Real batches usually do better, but a design that must meet a specification has to assume the worst, or trim.
:::
:::

::: nameit
## Exact common-mode rejection

For a difference amplifier with $R_1$ to the inverting input, $R_2$ in feedback, and $R_3$, $R_4$ dividing the non-inverting input,

$$v_o = \frac{R_4}{R_3 + R_4}\left(1 + \frac{R_2}{R_1}\right)v_2 - \frac{R_2}{R_1}v_1.$$

With $k = \frac{R_4}{R_3 + R_4}\left(1 + \frac{R_2}{R_1}\right)$, the common-mode gain is $k - \frac{R_2}{R_1}$, the differential gain $\frac12\left(k + \frac{R_2}{R_1}\right)$, and the CMRR is their ratio. Matched ratios make $A_{cm}$ zero.

## Filter design functions

- **Multiple-feedback band-pass** with equal capacitors C, center $f_0$, quality Q and center gain −G: $R_3 = \frac{Q}{\pi f_0C}$, $R_1 = \frac{R_3}{2G}$, and from the $f_0$ formula, $R_2 = \frac{1}{(2\pi f_0C)^2R_3 - 1/R_1}$.
- **Tow-Thomas:** $R = \frac{1}{2\pi f_0C}$, $R_Q = QR$, $R_\text{in} = \frac{R_Q}{G}$ for band-pass gain G.

## Meters

An average-responding meter rectifies, averages, and multiplies by $\frac{\pi}{2\sqrt2}$ = 1.111 so a sine reads its RMS. For any other waveform the reading is 1.111 times the rectified average, not the RMS.

## Transimpedance compensation

From lesson 24, $Q = \frac{\sqrt b}{a}$ with $a = R_fC_f + \frac{1}{\omega_u}$ and $b = \frac{R_f(C_f + C_\text{in})}{\omega_u}$. Q falls steadily as $C_f$ grows, so bisection on $C_f$ finds the value that gives any Q, such as 0.707.
:::

::: example
## A gain-10 difference amplifier ($R_1$ = 10 kΩ, $R_2$ = 100 kΩ) has its resistors off by 0.1% in the worst directions. Check the worst-case formula exactly.

::: solution
1. **Worst directions.** $R_1$ and $R_4$ low by 0.1%, $R_2$ and $R_3$ high.
2. **Exact gains.** Then
   $$\begin{aligned} k &= \frac{99.9}{10.01 + 99.9}\left(1 + \frac{100.1}{9.99}\right) \\ A_{cm} &= k - \frac{100.1}{9.99} \\ &= -0.0036 \end{aligned}$$
   against $A_d$ = 10.0.
3. **CMRR.** The ratio is 2750, matching $\frac{11}{4 \times 0.001}$ to five digits.
4. **Why it matches.** The formula's derivation is first order in ε, which at 0.1% is far more than enough.

**Answer:** the exact worst-case CMRR is 2750, the same as the formula's to five digits.
:::
:::

::: example
## Design a multiple-feedback band-pass at 1 kHz with Q = 5 and a center gain of −5, using 10 nF capacitors.

::: solution
1. **Feedback resistor.**
   $$\begin{aligned} R_3 &= \frac{5}{\pi \times 1\ \text{kHz} \times 10\ \text{nF}} \\ &= 159.2\ \text{k}\Omega \end{aligned}$$
2. **Input resistor.**
   $$\begin{aligned} R_1 &= \frac{R_3}{10} \\ &= 15.9\ \text{k}\Omega \end{aligned}$$
3. **Third resistor.** The $f_0$ formula, solved for $R_2$ with $R_3$ = 159,155 Ω and $R_1$ = 15,915 Ω:
   $$\begin{aligned} R_2 &= \frac{1}{(2\pi f_0C)^2R_3 - 1/R_1} \\ &= 1.77\ \text{k}\Omega \end{aligned}$$
   matching lesson 26.
4. **Why $R_2$ is small.** It sets $f_0$ while the other two set Q and gain, and its value sinks as Q rises.

**Answer:** $R_3$ = 159.2 kΩ, $R_1$ = 15.9 kΩ and $R_2$ = 1.77 kΩ.
:::
:::

::: example
## An average-responding meter calibrated for sines measures a 1 V peak square wave and a 1 V peak triangle. What does it read, and what are the true RMS values?

::: solution
1. **Square wave.** Its rectified average is 1 V, so the meter reads 1.111 V. Its RMS is 1 V, so the reading is 11% high.
2. **Triangle.** Its average is 0.5 V and the meter reads 0.555 V, against a true RMS of 0.577 V: 3.8% low.
3. **Sine.** Its average is 0.637 V, and
   $$0.637 \times 1.111 = 0.707\ \text{V}$$
   its RMS exactly, which is the only case the calibration covers.

**Answer:** the meter reads 1.111 V for the square (true RMS 1 V) and 0.555 V for the triangle (true RMS 0.577 V).
:::
:::

::: check
kind: python

## Step 1. Write `diff_cmrr(R1, R2, R3, R4)` returning the exact CMRR (as a ratio) of a difference amplifier. Return `float('inf')` if the common-mode gain is exactly zero.

```python starter
def diff_cmrr(R1, R2, R3, R4):
    eps = abs(R4 / R3 - R2 / R1) / (R2 / R1)
    return (1 + R2 / R1) / (4 * eps)

print(diff_cmrr(10e3, 100e3, 10.01e3, 99.9e3))
```

```python solution
def diff_cmrr(R1, R2, R3, R4):
    k = R4 / (R3 + R4) * (1 + R2 / R1)
    acm = k - R2 / R1
    ad = (k + R2 / R1) / 2
    return float('inf') if acm == 0 else abs(ad / acm)

print(diff_cmrr(10e3, 100e3, 10.01e3, 99.9e3))
```

```python tests
assert abs(diff_cmrr(10e3, 100e3, 10.01e3, 99.9e3) - 5495.0) < 0.01, f"Two resistors off: 5495; got {diff_cmrr(10e3, 100e3, 10.01e3, 99.9e3)}"
assert abs(diff_cmrr(9.99e3, 100.1e3, 10.01e3, 99.9e3) - 2749.998) < 0.01, "Worst case: 2750."
assert diff_cmrr(10e3, 100e3, 10e3, 100e3) == float('inf'), "Perfect matching rejects common mode completely."
```

::: right
Right. The CMRR depends only on how well the two ratios match.
:::
:::

::: check
kind: python

## Step 2. Write `mfb_bandpass(f0, Q, C, G)` returning `(R1, R2, R3)` for a multiple-feedback band-pass with equal capacitors and center gain −G.

```python starter
import math

def mfb_bandpass(f0, Q, C, G):
    R3 = Q / (math.pi * f0 * C)
    return R3 / (2 * G), R3, R3

print(mfb_bandpass(1e3, 5, 10e-9, 5))
```

```python solution
import math

def mfb_bandpass(f0, Q, C, G):
    R3 = Q / (math.pi * f0 * C)
    R1 = R3 / (2 * G)
    R2 = 1 / ((2 * math.pi * f0 * C) ** 2 * R3 - 1 / R1)
    return R1, R2, R3

print(mfb_bandpass(1e3, 5, 10e-9, 5))
```

```python tests
import math
R1, R2, R3 = mfb_bandpass(1e3, 5, 10e-9, 5)
assert abs(R1 - 15915.494) < 0.01 and abs(R2 - 1768.388) < 0.01 and abs(R3 - 159154.94) < 0.1, f"Expected 15.9 k, 1.77 k, 159 k; got {R1}, {R2}, {R3}"
f0 = 1 / (2 * math.pi * 10e-9) * math.sqrt((R1 + R2) / (R1 * R2 * R3))
assert abs(f0 - 1000) < 1e-6, "The design must land exactly on 1 kHz."
```

::: right
Right. Three resistors, three specifications, solved in order.
:::
:::

::: check
kind: python

## Step 3. Write `tow_thomas(f0, Q, C, G)` returning `(R, RQ, Rin)` for a Tow-Thomas band-pass with center gain G.

```python starter
import math

def tow_thomas(f0, Q, C, G):
    R = 1 / (2 * math.pi * f0 * C)
    return R, R, R

print(tow_thomas(1e3, 10, 10e-9, 1))
```

```python solution
import math

def tow_thomas(f0, Q, C, G):
    R = 1 / (2 * math.pi * f0 * C)
    return R, Q * R, Q * R / G

print(tow_thomas(1e3, 10, 10e-9, 1))
```

```python tests
R, RQ, Rin = tow_thomas(1e3, 10, 10e-9, 1)
assert abs(R - 15915.494) < 0.01 and abs(RQ - 159154.94) < 0.1 and abs(Rin - 159154.94) < 0.1, f"Lesson 27: 15.9 k, 159 k, 159 k; got {R}, {RQ}, {Rin}"
assert abs(tow_thomas(2e3, 10, 10e-9, 2)[2] - 39788.736) < 0.01, "Doubling f0 halves the resistors; gain 2 halves Rin again."
```

::: right
Right. Each specification has its own resistor.
:::
:::

::: check
kind: python

## Step 4. Write `meter(samples)` returning `(avg_abs, rms, reading)` for one period of a waveform, where `reading` is an average-responding meter's: 1.111 times the rectified average, exactly $\frac{\pi}{2\sqrt2}$.

```python starter
import numpy as np

def meter(samples):
    x = np.asarray(samples, float)
    return float(np.mean(np.abs(x))), float(np.mean(np.abs(x))), float(np.mean(np.abs(x)))

t = (np.arange(100000) + 0.5) / 100000
print(meter(np.sin(2 * np.pi * t)))
```

```python solution
import numpy as np

def meter(samples):
    x = np.asarray(samples, float)
    avg = float(np.mean(np.abs(x)))
    rms = float(np.sqrt(np.mean(x * x)))
    return avg, rms, avg * np.pi / (2 * np.sqrt(2))

t = (np.arange(100000) + 0.5) / 100000
print(meter(np.sin(2 * np.pi * t)))
```

```python tests
import numpy as np
t = (np.arange(100000) + 0.5) / 100000
a, r, m = meter(np.sin(2 * np.pi * t))
assert abs(m - r) < 1e-6, "A sine reads its RMS."
a2, r2, m2 = meter(np.where(t < 0.5, 1.0, -1.0))
assert abs(m2 - 1.11072) < 1e-4 and abs(r2 - 1) < 1e-12, f"Square: reads 1.111, RMS 1; got {m2}, {r2}"
tri = np.where(t < 0.25, 4 * t, np.where(t < 0.75, 2 - 4 * t, 4 * t - 4))
a3, r3, m3 = meter(tri)
assert abs(m3 - 0.55536) < 1e-4 and abs(r3 - 0.57735) < 1e-4, f"Triangle: reads 0.555, RMS 0.577; got {m3}, {r3}"
```

::: right
Right. The calibration factor is the sine's RMS over its average, and it's wrong for every other shape.
:::
:::

::: check
kind: python

## Step 5. Write `tia_cf(Rf, Cin, GBW, Q=0.7071)` returning the feedback capacitor that gives the transimpedance amplifier the requested Q, by bisection between 0 and 100 pF.

```python starter
import math

def tia_cf(Rf, Cin, GBW, Q=0.7071):
    return math.sqrt(Cin / (2 * math.pi * Rf * GBW))

print(tia_cf(1e6, 10e-12, 10e6))
```

```python solution
import math

def tia_cf(Rf, Cin, GBW, Q=0.7071):
    wu = 2 * math.pi * GBW
    def q(Cf):
        a = Rf * Cf + 1 / wu
        b = Rf * (Cf + Cin) / wu
        return math.sqrt(b) / a
    lo, hi = 0.0, 100e-12
    for _ in range(200):
        m = (lo + hi) / 2
        if q(m) > Q:
            lo = m
        else:
            hi = m
    return (lo + hi) / 2

print(tia_cf(1e6, 10e-12, 10e6))
```

```python tests
cf = tia_cf(1e6, 10e-12, 10e6)
assert abs(cf - 0.56397e-12) < 1e-16, f"Lesson 24: 0.564 pF; got {cf}"
assert abs(tia_cf(1e5, 30e-12, 100e6) - 0.97708e-12) < 1e-16, "100 kΩ, 30 pF, 100 MHz: 0.977 pF."
assert tia_cf(1e6, 10e-12, 10e6, 0.5) > cf, "A lower Q needs a larger capacitor."
```

::: right
Right. The bisection gives the exact capacitor; the square-root formula is its first-order cousin.
:::
:::

::: check
given: G = 1 | 10 | 100; e = 0.01 | 0.1 | 1
answer: (1 + G)/(4*e/100)
tolerance: 0.5%

## What's the worst-case CMRR of a difference amplifier with gain {G} built from {e}% resistors?

::: right
Right. $\frac{1 + R_2/R_1}{4\varepsilon}$ = {answer}.
:::

::: misses
- [G/(4*e/100)] Use $1 + R_2/R_1$.
- [(1 + G)/(e/100)] Four resistors can each be off by ε, so divide by 4ε.
- [any] $\frac{1 + G}{4\varepsilon}$.
:::
:::

::: check
kind: parts
given: V = 0.5 | 1 | 5

## An average-responding meter calibrated for sines measures waveforms of {V} V peak.

::: part
answer: V*pi/(2*sqrt(2))
unit: V
tolerance: 0.5%

### What does it read for a square wave?

::: misses
- [V] That's the square's true RMS; the meter multiplies its average by 1.111.
- [any] The square's rectified average is its peak; multiply by $\frac{\pi}{2\sqrt2}$.
:::
:::

::: part
answer: V/2*pi/(2*sqrt(2))
unit: V
tolerance: 0.5%

### And for a triangle wave?

::: misses
- [V/sqrt(3)] That's the triangle's true RMS; the meter reads 1.111 times its average.
- [any] The triangle's rectified average is half its peak.
:::
:::
:::

::: explain
## Why does an average-responding meter misread a square wave?

::: model
The meter can't measure RMS directly. It rectifies the signal, averages it, and multiplies by a fixed factor chosen so that a sine wave reads its RMS value: the sine's RMS is 1.111 times its rectified average. A square wave's RMS equals its rectified average, because it spends all its time at the peak, so the factor that corrects a sine overcorrects the square by 11%. Every waveform has its own ratio of RMS to average, so the meter is right only for sines, and a true-RMS meter computes the root of the mean square instead.
:::
:::

::: practice

::: check
given: G = 2 | 50; e = 0.05 | 0.5
answer: (1 + G)/(4*e/100)
tolerance: 0.5%

## Gain {G}, {e}% resistors. Worst-case CMRR?

::: misses
- [any] $\frac{1 + G}{4\varepsilon}$.
:::
:::
:::
