# dc currents and voltage of differential amplifier
import math
# Variable declaration
Vcc = 12 # collector voltage
Vee = -12 # emitter voltage
Rc = 4.1*10**3 # Collector resistance
Re = 3.8*10**3 # emitter resistance
Vbe = 0.7 # voltage across base-emitter junction
#Calculations
Ie = (Vcc-Vbe)/Re
Ic = 0.5*Ie
Vo = Vcc-Ic*Rc
#Result
print("Ie = %.4f mA\nIc = %.3f mA\nVo = %.1f V"%(Ie*1000,Ic*1000,Vo))
# Differential amplifier parameters
import math
#Variable declaration
Vcc = 12 # collector voltage
Vee = -12 # emitter voltage
Rc = 1*10**6 # Collector resistance
Re = 1*10**6 # emitter resistance
Vbe = 0.7 # voltage across base-emitter junction
vi = 2.1*10**-3 # AC input voltage
beta = 75
#Calculations
#(a)
Ie = (Vcc-Vbe)/Re
ie = 0.5*Ie
re = (25*10**-3)/ie
re_dash = 4420.0#value used in the book
#(b)
g = Rc/(2*re_dash)
g = math.floor(g*10)/10
#(c)
vo = g*vi
vo = math.floor(vo*10000)/10000
#(d)
Zi = 2*beta*re
#(e)
cmg = Rc/(re+2*Re)
cmg = math.ceil(cmg*1000)/1000
#(f)
cmrr = g/cmg
#(g)
cmrr_db = 20*math.log10(cmrr)
#Result
print("(a) re = %d ohm\n(b) voltage gain for differential input = %.1f\n(c) AC output voltage = %.4f V"%(re,g,vo))
print("(d) input impedance = %d k-ohm\n(e) CMRR = %.2f\n(g) CMRR' = %.1f dB"%(Zi/1000,cmrr,cmrr_db))
#Answer for re is wong in the book
# Op-amp with negative feedback
import math
#Variable declaration
Ag = 100000.0 # open loop gain of Op-amp
fb = 0.01 # feed back factor
vi = 2*10**-3 # input voltage
#Calculations
# a
g = Ag/(1+(fb*Ag))
# b
v = vi*g
# c
Ev = v/Ag
#Result
print("(a) Closed loop gain = %.1f \n(b) Output = %.4f V\n(c) Error voltage = %.3f*10^-6 V"%(g,v,Ev*10**6))
# Op-amp with negative feedback
import math
#Variable declaration
Ag = 15000.0 # open loop gain of Op-amp
fb = 0.01 # feed back factor
vi = 2*10**-3 # input voltage
#Calculations
# a
g = Ag/(1+(fb*Ag))
# b
v = vi*g
# c
Ev = v/Ag
#Result
print("(a) Closed loop gain = %.3f \n(b) Output = %.4f V\n(c) Error voltage = %.3f*10^-5 V"%(g,v,Ev*10**5))
print("\nComparison conclusion:\nA decrease in open loop gain causes a corresponding increase in error voltage,")
print("thus keeping the output voltage nearly constant.")
# input/output impedances
import math
#variable declaration
g = 100000.0 # open loop gain
Zi = 2*10**6 # input impedance
Zo = 75 # output impedance
beta = 0.01 # feedback factor
#Calculations
D = 1+ beta*g
Zi_cl = Zi*D
Zo_cl = Zo/D
#Result
print("Closed loop input impedance = %.0f M-ohm\nClosed loop output impedance = %.4f ohm"%(Zi_cl/10**6,Zo_cl))
# closed loop gain and disensitivity
import math
#Variable declaration
g = 100000.0 # open loop gain
beta = 0.001 # feedback factor
#calculations
clg = g/(1+ beta*g)
D = 1+g*beta
# Result
print("Closed loop gain = %.1f \nDesensitivity = %.0f "%(clg,D))
# closed loop gain and upper cut off frequency
import math
#Variable declaration
f = 10**6 # unity gain frequency
alg = 100000.0 # open loop gain
b1 = 0.001 # feedback factor in case 1
b2 = 0.01 # feedback factor in case 2
b3 = 0.1 # feedback factor in case 3
#calculations
# a
f1 = f/alg
# b
g2 = alg/(1+alg*b1)
f2 = f/g2
# c
g3 = alg/(1+alg*b2)
f3 = f/g3
# d
g4 = alg/(1+alg*b3)
f4 = f/g4
#Result
print("Open loop,\tgain = %.0f \tf2 = %d Hz"%(alg,f1))
print("Closed loop,\tgain = %.1f \tf2 = %d Hz"%(g2,f2))
print("Closed loop,\tgain = %.1f \tf2 = %d Hz"%(g3,f3))
print("Closed loop,\tgain = %.3f \tf2 = %d Hz"%(g4,f4))
# Slew rating
import math
#Variable declaration
Imax = 10*10**-6 # maximum current
C = 4000*10**-12 # capacitance in pF
#Calculations
sr = Imax/C
#Result
print("Slew rate = %.1f V/ms"%(sr/1000))
# slew rate distortion
import math
#Variable declaration
sr = 0.4 # Slew rate
V = 6 # peak value of voltage
f = 10*10**3 # frequency
#Calculations
slope = 2*math.pi*f*V
#Result
print("Initial slope of sine wave =%.5f V/micro-sec"%(slope/10**6))
print("\nSince the slew rate of amplifier is %.1f V/micro-sec, there is no slew rate distortion."%(sr))
# Slew rate distortion
import math
#Variable declaration
sr = 0.5*10**6 # slew rate
V = 10 # peak value of input signal
f = 10*10**3 # frequency
#calculations
# a
slope = 2*math.pi*f*V
# b
Vp = sr/(2*math.pi*f)
#Result
print("(a) Initial slope of sine wave = %.3f V/micro-sec"%(slope/10**6))
print("Since initial slope is more than the slew rate of amplifier, slew rate distortion will occur.")
print("\n(b) To eliminate slew rate distortion,\n Vp = %.2f V"%Vp)
# inverting amplifier
import math
#Variable declaration
A = 100000.0 # open loop gain
Zo = 75.0 # output impedance
f = 1.0*10**6 # unity gain frequency
R1 = 1.0*10**3 # Source Resistance
Rf = 100.0*10**3 # feedback resistance
#Calculations
# a
g = -Rf/R1
# b
beta = R1/(R1+Rf)
D = 1+(A*beta)
# c
f2 = beta*f
# d
Zi_cl = R1
#result
print("(a) Gain = %f\n(b) disensitivity = %.1f"%(g,D))
print("(c) closed loop upper cut off frequency = %.1f*10^3 Hz\n(d) closed loop input impedance = %.0f ohm"%(f2/1000,Zi_cl))
# closed loop voltage gain, input/output impedance(refering to fig 12.11)
import math
# Variable declaration
R2 = 100.0*10**3 # Resistance R2
R1 = 100.0 # Resistance R1
A = 100000.0 # open loop gain
Zin = 2*10**6 # input impedance
Zo = 75 # output impedance
# Calculations
g = (R1+R2)/R1
beta = R1/(R1+R2)
Zi_dash = (1+A*beta)*Zin
Zo_dash = Zo/(1+A*beta)
#Result
print("Closed loop gain = %.0f \nClosed loop input impedance = %.1f M-ohm\nClosed loop output impedance = %.3f ohm"%(g,Zi_dash/10**6,Zo_dash))
# Op-amp parameters(refering to fig. 12.13)
import math
#Variable declaration
A = 100000.0 # open loop gain
f = 1.0*10**6 # unity gain frequency
R3 = 150 *10**3 # Resistance R3
Ci = 1*10**-6 # inpuut capacitor
R2 = 100*10**3 # Resistance R2
Cb = 1*10**-6 # Bias capacitor
R1 = 1.0*10**3 # Resistance R1
Co = 1*10**-6 # output capacitance
Rl = 15*10**3 # load resistance
#Calculations
# a
A = (R1+R2)/R1
# b
beta = 1/A
f2 = f/A
# c
fc1 = 1/(2*math.pi*Ci*R3)
fc2 = 1/(2*math.pi*Cb*Rl)
fc3 = 1/(2*math.pi*Co*R1)
#Result
print("(a) Avf = %.0f \n\n(b) f2' =%.1f*10^3 Hz"%(A,f2/1000))
print("\n(c) The critical frequencies are,\n fc = %.3f Hz\n fc = %.2f Hz\n fc = %.2f Hz"%(fc1,fc2,fc3))
print("\n(d) Evidently the lower cut off frequency is the highest of the above three critical frequencies, i.e. %.2f Hz"%fc3)
# output voltage(referring fig.12.14)
import math
# Variable declaration
v1 = 0.5 # input voltage 1
v2 = 1.5 # input voltage 2
v3 = 0.2 # input voltage 3
R1 = 10.0*10**3 # resistance R1
R2 = 10.0*10**3 # resistance R2
R3 = 10.0*10**3 # resistance R3
Rf = 50.0*10**3 # feedback resistance
#Calculations
vo = -Rf*((v1/R1)+(v2/R2)+(v3/R3))
#Result
print("Output Voltage = %.0f V"%vo)
print("Theoretical example")
print("Theoretical example")
# difference amplifier(referring fig. 12.18)
import math
#Variable declaration
R1 = 50.0*10**3 # Resistance R1
R2 = 10.0*10**3 # Resistance R2
Vs1 = 4.5 # input voltage at channel 1
Vs2 = 5.0 # input voltage at channel 2
#Calculations
vo = R1*(Vs2-Vs1)/R2
#Result
print("Output voltage = %.1f V"%vo)
# CMRR in dB
import math
#Variable declaration
A = 50.0 # gain of difference amplifier
v = 2.0 # input voltage
vo = 5.0*10**-3 # output voltage
#Calculations
Vcom = 0.5*(v+v)
Acom = vo/Vcom
cmrr = A/Acom
cmrr = 20*math.log10(cmrr)
#Result
print("CMRR = %.2f dB"%(cmrr))
print("Theoretical Example")
print("Theoretical Example")
# two pole high pass filter(referring fig. 12.32)
import math
#Variable declaration
R1 = 10.0*10**3 # Resistance R1
R2 = 5.6*10**3 # Resistance R2
R = 1.0*10**3 # Resistance R
C = 0.01 *10**-6 # capacitance
vi = 1.6*10**-3 # input voltage
#Calculations
# a
A = 1+(R2/R1)
# b
vo = A*vi
# c
pi = math.ceil(math.pi*10000)/10000
fc = 1/(2*pi*R*C)
# d
gain = 0.707*A
#Result
print("(a) midband gain = %.2f\n(b) output voltage = %.3f mV\n(c) fc = %.2f Hz\n(d) Gain = %.3f"%(A,vo*1000,fc,gain))
# Low pass filter(referring fig. 12.30)
import math
#Variable declaration
vi = 1.1*10**-3 # input voltage
R = 10*10**3 # Resistance
C = 0.001*10**-6 # Capacitance
R1 = 10*10**3 # Resistance R1
R2 = 5.6*10**3 # Resistance R2
#Calculations
# a
A = (R1+R2)/R1
vo1 = A*vi
# b
fc = 1/(2*math.pi*R*C)
# c
vo = 0.707*vo1
#Result
print("(a) Avf = %.2f\n Vo = %.3f*10^-3 V\n(b) fc = %.3f*10^3 Hz\n(c) at f = fc,\nVo = %.3f* 10^-3 V"%(A,vo1*1000,fc/1000,vo*1000))
print("Theoretical example")