* PM_K-1 Stage 1 : balanced line receiver -- why CMRR needs matched resistors
*
* A balanced receiver rejects "common-mode" noise -- hum/interference that appears
* equally on both signal wires -- by SUBTRACTING the two wires. How well it cancels
* (its Common-Mode Rejection Ratio, CMRR) depends entirely on how well its four
* resistors match. This is the circuit-level reason we chose the laser-trimmed
* THAT1240 instead of a discrete op-amp + four 1% resistors.
*
* Three identical unity-gain difference amplifiers, driven common-mode (same 1V on
* both inputs), with 0% / 0.1% / 1% mismatch on one resistor.
* CMRR(dB) = -20*log10(|Vout|)  since differential gain = 1 and Vcm = 1V.
*
* Run:  ngspice -b ../eda/sim/stage1_cmrr.cir

.title Stage1 line receiver CMRR vs resistor match

Vcm cm 0 AC 1            ; common-mode drive: both inputs tied to cm

* ---- A: perfectly matched ----
EA outa 0 vpa vma 1e6
R1a cm   vma 10k
R2a outa vma 10k
R3a cm   vpa 10k
R4a vpa  0   10k

* ---- B: 0.1% mismatch on one resistor ----
EB outb 0 vpb vmb 1e6
R1b cm   vmb 10k
R2b outb vmb 10k
R3b cm   vpb 10k
R4b vpb  0   10.01k

* ---- C: 1% mismatch on one resistor ----
EC outc 0 vpc vmc 1e6
R1c cm   vmc 10k
R2c outc vmc 10k
R3c cm   vpc 10k
R4c vpc  0   10.1k

.ac dec 10 100 10k
.control
run
meas ac acm_a find vm(outa) at=1000
meas ac acm_b find vm(outb) at=1000
meas ac acm_c find vm(outc) at=1000
let cmrr_a = -20*log10(acm_a)
let cmrr_b = -20*log10(acm_b)
let cmrr_c = -20*log10(acm_c)
echo
echo "  CMRR  perfect match : $&cmrr_a dB   (limited only by the amplifier)"
echo "  CMRR  0.1% mismatch : $&cmrr_b dB"
echo "  CMRR  1%   mismatch : $&cmrr_c dB"
echo "  -> hand-matched 1% resistors give poor rejection; the THAT1240's"
echo "     laser-trimmed internal resistors are how we get >90 dB for free."
wrdata ../eda/sim/plots/stage1_cmrr.data vdb(outb) vdb(outc)
.endc
.end
