# Bipolar diff pair, example of lecture 11: I_EE = 1 mA, R_C = 1 kOhm, I_S = 2e-18 A, V_T = 26 mV.
# Large-signal: I_C1 = I_EE / (1 + exp(-(V_in1 - V_in2)/V_T)),  I_C2 = I_EE - I_C1   (alpha = 1)
#               V_X - V_Y = -R_C (I_C1 - I_C2) = -R_C I_EE tanh((V_in1 - V_in2) / (2 V_T))
import numpy as np
IEE, RC, IS, VT = 1e-3, 1e3, 2e-18, 0.026
vin = np.linspace(-0.25, 0.25, 501)            # V_in1 - V_in2 [V]
ic1 = IEE / (1 + np.exp(-vin / VT))
ic2 = IEE - ic1
vout = -RC * (ic1 - ic2)                        # V_X - V_Y [V]
np.savetxt("iochar.dat", np.column_stack([vin * 1e3, vout, ic1 * 1e3, ic2 * 1e3]),
           header="vin_mV vout_V ic1_mA ic2_mA", comments="", fmt="%.6f")
# checks of the numbers in the notes
vbe = VT * np.log(IEE / 2 / IS)                 # bias: I_C1 = I_C2 = I_EE/2
slope = np.gradient(vout, vin)[len(vin) // 2]   # dV_out/dV_in at V_in1 = V_in2
gm = IEE / 2 / VT
print(f"V_BE1 = V_BE2 = {vbe*1e3:.1f} mV")
print(f"slope at 0 (numerical) = {slope:.2f} V/V,  -R_C I_EE/(2 V_T) = {-RC*IEE/(2*VT):.2f} V/V,  -g_m R_C = {-gm*RC:.2f} V/V")
print(f"limits: {vout[0]:+.4f} V, {vout[-1]:+.4f} V  (+/- R_C I_EE = {RC*IEE:.1f} V)")
