# Output voltage of circuit shown in fig. 4.3
import math
#Variable declaration
R1 = R2 = R3 = 1000.0 # input Resistors
Rf = 1000.0 # feedback resistor
Vin1 = 2 # input voltage 1
Vin2 = 1 # input voltage 2
Vin3 = 4 # input voltage 3
#Calculations
Vout = -((Rf*Vin1/R1)+(Rf*Vin2/R2)+(Rf*Vin3/R3))
#Result
print("Vout = %d V"%Vout)
# Adder circuit design
import math
#Variable declaration
Rf = 100.0 # feedback resistor in k-ohm
V1 = 1 # multiplication factor for input 1
V2 = 10 # multiplication factor for input 2
V3 = 100 # multiplication factor for input 3
#Calculations
R1 = Rf/V1
R2 = Rf/V2
R3 = Rf/V3
#Result
print("R1 = %d k-ohm\nR2 = %d k-ohm\nR3 = %d k-ohm"%(R1,R2,R3))
# Determine output voltage
import math
#Variable declaration
Rf = 12.0 # feedback resistor in k-ohm
Rs1 = 12.0 # input resistance 1
Rs2 = 2.0 # input resistance 2
Rs3 = 3.0 # input resistance 3
Vi1 = 9 # corresponding input Voltage 1
Vi2 = -3 # corresponding input Voltage 2
Vi3 = -1 # corresponding input Voltage 3
#Calculations
Vout = -Rf*((Vi1/Rs1)+(Vi2/Rs2)+(Vi3/Rs3))
#Result
print("Vout = %.0f V"%Vout)
# Summing amplifier design(refer fig. 4.4)
import math
#Variable declaration
Rf = 6 # Assume,feedback resistor in k-ohm
V1 = 1 # multiplication factor for input 1
V2 = 2 # multiplication factor for input 2
V3 = -3 # multiplication factor for input 3
#Calculations
R1 = abs(Rf/V1)
R2 = abs(Rf/V2)
R3 = abs(Rf/V3)
#Result
print("R1 = %d k-ohm\nR2 = %d k-ohm\nR3 = %d k-ohm"%(R1,R2,R3))
# Summing amplifier design(refer fig. 4.5)
import math
#Variable declaration
Rf = 40.0 # Assume,feedback resistor in k-ohm
V1 = -2.0 # multiplication factor for input 1
V2 = -3.0 # multiplication factor for input 2
V3 = -4.0 # multiplication factor for input 3
#Calculations
R1 = abs(Rf/V1)
R2 =abs(Rf/V2)
R3 = abs(Rf/V3)
#Result
print("R1 = %d k-ohm\nR2 = %.2f k-ohm\nR3 = %d k-ohm"%(R1,R2,R3))
# Output Voltage(refer fig. 4.6)
import math
#Variable declaration
V1 = 2.0 # V1 input
V2 = -1.0 # V2 input
R = 1.0 # Resistor R, assumed
Rf = 2.0*R # feedback resistor
#Calculations
Vin1 = (R*R/(R+R))*V1/(R+(R*R/(R+R)))
Vout1 = Vin1*(1+(Rf/R))
Vin2 = (R*R/(R+R))*V2/(R+(R*R/(R+R)))
Vout2 = Vin2*(1+(Rf/R))
Vout = Vout1+Vout2
#Result
print("Output voltage, Vout = %d V"%Vout)
# Limiting frequency
import math
#Variable declaration
R1 = 10*10**3 # resistor R1
Cf = 0.1*10**-6 # feedback capacitor
#Calculation
Rf = 10*R1
f = 1/(2*math.pi*Rf*Cf)
#Result
print("Limiting frequency = %.2f Hz"%(math.floor(f*100)/100))
# Design of practical integrator circuit
import math
#Variable declaration
A = 10.0 # DC gain of integrator
f = 10.0*10**3 # frequency of input square wav
#Calculations
fa = f/10 # break frequency for proper integration
R1 = 10.0*10**3 # selected
Rf = A*R1
Cf = 1/(2*math.pi*Rf*fa)
Rcomp = R1*Rf/(R1+Rf)
#Result
print("R1 = %d k-ohm\nRf = %d k-ohm\nCf = %.4f = %.1f nF\nRcomp = %.2f k-ohm"%(R1/1000,Rf/1000,Cf*10**9,Cf*10**9,Rcomp/1000))
# maximum change in output and minimum slew rate required
import math
#Variable declaration
Vin = 5 # input voltage
f = 1000.0 # input frequency
R1 = 1000.0 # Resistor R1
Cf = 0.1*10**-6 # feedback capacitor
#Calculation
Vout = Vin/(2*R1*f*Cf)
S = 2*math.pi*f*Vin
#Result
print("Delta V = %d V.\n\nOutput may saturate at +/- 14V. But %d change means the output will alternate"%(Vout,Vout))
print("between +/- %.1f V. Hence output will not saturate and will be triangular in nature."%(Vout/2))
print("\nMinimum slew rate required is %f V/usec."%(S*10**-6))
# Practical integrator circuit
import math
#Variable declaration
R1 = 120*10**3 # Resistor R1
Rf = 1.2*10**6 # feedback resistor
Cf = 10*10**-9 # feedback capacitor
V = 5 # input sine wave amplitude
f = 10*10**3 # input sine wave frequency
#Calculations
#(ii)
fa = 1/(2*math.pi*Rf*Cf)
fs = 10*fa
#(iii)
A = Rf/R1
A = 20*math.log10(A)
#(iv)
A2 = (Rf/R1)/math.sqrt(1+(f/fa)**2)
Vout = V*A2
#Result
print("(i) Circuit diagram is shown in fig.4.27(a)\n\n(ii) fa = %.2f Hz\t\t Safe frequency = %.1f Hz\n\n(iii)DC gain = %d"%(fa,fs,A))
print("\n(iv) Vout(peak) = %.0f mV\n\n(v) Rough nature of frequency response is shown in fig. 4.27(b)"%(Vout*10**3))
# Output voltage
import math
#Variable declaration
f = 2000.0 # frequency of input sin wave
V = 10*10**-3 # amplitude of sin wave
R = 50*10**3 # Resistor R
C = 2*10**-6 # feedback capacitor
#Calculations
sf = -1/(R*C)
Vout = (math.sqrt(2)*V*sf)*1000/(2*math.pi*f)
#Result
print("Vout(t) = %.4f*(cos%dt-1)"%(-Vout,2*f))
# Capacitor voltage and closed loop time constant
import math
Vin = 10.0 # input voltage
R = 2.2*10**3 # Resistor
T = 10**-3 # ON time of the pulse
C = 10**-6 # Capacitance value
#Calculations
I = Vin/R
V = I*T/C
RC = R*C*10**5
#Result
print("I = %.3f mA\nV = %.3f V\nclosed-loop time constant = %.0f"%(I*10**3,V,RC))
# Lossyintegrator circuit
import math
#Variable declaration
A = 20.0 # peak gain
w = 10000 # w value when gain is 3dB down
C = 0.01*10**-6 # feedback capacitance
# Calculations
Rf = 1/(C*w)
R1 = Rf/10**(A/20.0)
#Result
print("Rf = %d k-ohm\t\tR1 = %d k-ohm"%(Rf/1000,R1/1000))
# output voltage in case of circuit shown in fig. 4.30 with ideal op-amp
import math
#Variable declaration
Vin = 5.0 # input voltage at inverting terminal
V1 = 3.0 # input voltage at non-inverting terminal
C = 0.2*10**-6 # feedback capacitor
R = 40*10**3 # Resistor R
t = 50*10**-3 # time delay
#Calculations
Vout = -((Vin-V1)*t/(R*C))+V1
%matplotlib inline
import matplotlib.pyplot as plt
from numpy import arange,sin,pi
t = arange(0.0,5.0,0.01)
t1 = arange(0.0,6.0,0.01)
t2 = arange(0.0,5,0.01)
t3 = arange(-9.5,0.0,0.01)
plt.axis([0,5.5,-10.0,4.1])
plt.plot(t1,t1*0/t1,'b')
plt.plot(t,3-t*12.5/5.0,'b')
plt.plot(t2,-t2*9.5/t2,'--')
plt.plot(t3*5/t3,t3,'--')
plt.title("Output Voltage")
plt.xlabel("Time(x 10^-2) ")
plt.ylabel("Output Voltage in Volts")
#Result
print("Vout = %.1f V"%Vout)
# Time when output will get saturated in fig. 4.33
import math
#Variable declaration
R = 500.0*10**3 # Resistopr R
C = 10.0*10**-6 # feedback capacitor
V = -0.5 # V(t)
Vo = 12.0 # Saturation Voltage
#Calculations
Vout = -V/(R*C)
t = Vo/Vout
#Result
print("Time duration required for saturation of output voltage = %d seconds"%t)
# expression using op-amp
import math
#Variable declaration
Cf = 10.0*10**-6 # feedback capacitor, assumed value
V1 = 1 # multiplication factor for input 1
V2 = 2 # multiplication factor for input 2
V3 = 5 # multiplication factor for input 3
#Calculations
R1 = 1/(Cf*V1)
R2 = 1/(Cf*V2)
R3 = 1/(Cf*V3)
#Result
print("R1 = %d k-ohm\nR2 = %d k-ohm\nR3 = %d k-ohm\nCircuit will be ass show in fig. 4.35"%(R1/1000,R2/1000,R3/1000))
# Practical differentiator
import math
#Variable declaration
fmax = 150.0 # max frequency to be differentiate
C1 = 1*10**-6 # Capacitor C1, we choose
#Calculations
fa =fmax
Rf = 1/(2*math.pi*fa*C1)
Rf = Rf/1000 # k-ohm
Rf = math.floor(Rf*100)/100
fb = 10*fa
R1 = 1/(2*math.pi*fb*C1)
R1 = math.floor(R1*10)/10
Cf = R1*C1/(Rf*10**3)
Rcomp = R1*Rf*10**3/(R1+Rf*10**3)
#Result
print("R1 = %.1f ohm\nC1 = %d uF\nRf = %.2f k-ohm\nCf = %.1f uF\nRcomp = %.2f ohm"%(R1,C1*10**6,Rf,Cf*10**6,math.floor(Rcomp*100)/100))
# op-amp differentiator
import math
#Variable declaration
Vm = 10*10**-6 # peak value of sine wave
fa = 2000.0 # maximum frequency
R = 50*10**3 # Resistor R
C = 2*10**-6 # Capacitor C
#Calculations
Vout = -R*C*Vm*2*math.pi*fa
#Result
print("Vout = %f*cos(%d*pi*t) micro-V"%(Vout,2*fa))
# Differentiator design
import math
#Variable declaration
V = 3.0 # input sine wave ampitude
f = 200.0 # input sine wave frequency
fa = 1000.0 # maximum input frequency
#Calculations
C = 0.1*10**-6 #Assumed value
R = 1/(2*math.pi*fa*C)
R_1 = math.floor(R/100)*100
fb = 20*fa
R_dash = 1/(2*math.pi*fb*C)
R_dash_1 = 82*10**3
C_dash = R_1*C/R_dash_1
Vout = -R_1*C*(V/2)*2*math.pi*f
#Result
print("R = %.2f k-ohm = %.1f k-ohm(say)\nRom = %.1f k-ohm\nVout = %.4fcos(%d*pi*t)"%(R/1000,R_1/1000,R_1/1000,Vout,f*2))
# Instrumentation Amplifier (refer fig 4.64)
import math
#Variable declaration
R1 = 200.0 # Resistor R1
R2 = 100.0 # Resistor R2
Rf = 100.0*10**3 # Feedback Resistor Rf
Rg_min = 100.0 # 100 ohm + 0 ohm
Rg_max = 100.0*10**3+100 # 100 ohm + 100 k-ohm
#Calculation
Gain1 = (1+(2*Rf/Rg_min))*(R2/R1)
Gain2 = (1+(2*Rf/Rg_max))*(R2/R1)
#Result
print("For all practical purposes, the gain can be varied from %.1f to %.1f"%(Gain2, Gain1))
#Answer for max gain is wrong in the book
# Instrumentation Amplifier (refer fig 4.65)
import math
#Variable declaration
R1 = 100.0*10**3 # Resistor R1
R2 = 100.0*10**3 # Resistor R2
Rf = 470.0*10**3 # Feedback Resistor Rf
Gain = 100.0 # Gain
#Calculations
Rg = 2*Rf/((Gain*(R1/R2))-1)
#Result
print(" Rg = %.2f k-ohm"%(Rg/1000))
# Transducer Resistance
import math
#Variable declaration
R = 100.0 # reference resistance of the transducer
alfa = 0.00392 # Alfa value
T1 = 25 # Temperature Value 1
T2 = 100 # Temperature Value 2
#Calculations
R_25 = R*(1+alfa*T1)
R_100 = R*(1+alfa*T2)
#Result
print("It can be seen that Delta_R is %.1f ohm for a change at %d°C"%(R_25-R,T1))
print("while Delta_R is %.1f ohm for a change of %d°C."%(R_100-R,T2))
# instrumentation amplifier design
import math
#Variable declaration
P_min = 0 # minimum value of potentiometer
P_max = 50*10**3 # maximum value of potentiometer
G_min = 5.0 # minimum gain
G_max = 200.0 # maximum gain
#Calculations
R3 = 1000.0 # assumed value
R4 = 1000.0 # resistor in series with potentiometer
x1 = (R4+P_min)/R3
x2 = (R4+P_max)/R3
y = ((G_min/x1)-1)/2 # R1/R2
R2 =10*10**3 # assumed value
R1 = R2*y
#Result
print("R1 = %.0f k-ohm\nR2 = %.0f k-ohm\nR3 = %.0f k-ohm\nR4 = %.0f k-ohm\n"%(R1/1000,R2/1000,R3/1000,R4/1000))
print("Theoretical example")
# Gain of instrumentation Amplifier(refer fig. 4.73)
import math
#Variable declaration
Rf = 5.0 # Resistor Rf in k-ohm
Rg = 1.0 # Resistor Rg in k-ohm
R1 = 10.0 # Resistor R1 in k-ohm
R2 = 20.0 # Resistor R2 in k-ohm
#Calculations
A = (1+(2*Rf/Rg))*(R2/R1)
#Result
print("A = %d"%A)
# output voltage of the circuit (refer fig. 4.74)
import math
#Variable declaration
R_A1 = 1000.0 # terminal resistor for op-amp A1
Rf_A1 = 5000.0 # feedback resistor for op-amp A1
R_A2 = 1000.0 # terminal resistor for op-amp A2
Rf_A2 = 2000.0 # feedback resistor for op-amp A2
Rcom_A2= 2000.0 # Rcom resistor for op-amp A2
V1 = 1.0 # input at terminal 1 of A1 in mV
V2 = 5.0 # 5*sin(wt)-->input at terminal 2 of A1 in mV
V3 = 5.0 # input at terminal 2 of A2
#Calculations
Vin_1 = V1*(-Rf_A1/R_A1)
Vin_2 = V2*(-Rf_A1/R_A1)
Vout = Vin_1*(-Rf_A2/R_A2)+(1+Rf_A2/R_A2)*(Rcom_A2/(R_A2+Rcom_A2))*V3
Vout2 = Vin_2*(-Rf_A2/R_A2)
#Result
print("Vout = %d + %d*sin(wt) mV"%(Vout,Vout2))
# output voltage of the circuit (refer fig. 4.75)
import math
#Variable declaration
Rf = 10.0 # Resistor Rf in k-ohm
Rg = 5.0 # Resistor Rg in k-ohm
R1 = 1.0 # Resistor R1 in k-ohm
R2 = 2.0 # Resistor R2 in k-ohm
Vin2 = 2.0 # input at non-inverting terminal of A2 in mV
Vin1 = 1.0 # input at non-inverting terminal of A1 in mV
#Calculations
A = (1+(2*Rf/Rg))*(R2/R1)
Vout = A*(Vin2 - Vin1)
#Result
print("Vout = %d mV"%Vout)
# Determining the value of Rg
import math
#Variable declaration
Rf = 15.0 # Resistor Rf in k-ohm
R1 = 1.0 # Resistor R1 in k-ohm
R2 = 2.0 # Resistor R2 in k-ohm
Vin2 = 5.0*10**-3 # input at non-inverting terminal of A2
Vin1 = 2.0*10**-3 # input at non-inverting terminal of A1
Vout = 3 # output voltage
#Calculations
A = Vout/(Vin2-Vin1)
Rg = 2*Rf/((A*R1/R2)-1)
#Result
print("Rg = %.2f ohm"%(Rg*1000))
print("Theoretical example")
print("Theoretical example")
# 3 op-amp instrumentation amplifier design
import math
#Variable declaration
g = 1.0 # minimum gain required
G = 10000.0 # Maximum gain required
pot = 100.0 # potentiometer max value
#Calculations
A2 = 1.0/5.0 # Assumed gain of second stage
R1 = 100.0 # Resistor R1
R2 = R1*A2
x = ((G/A2)-1)/2 # Rf/Rgb
y = ((1/A2)-1)/2
Rgb = y*pot/(x-y)
Rf = x*Rgb
#Result
print("R1 = %d k-ohm\nR2 = %d k-ohm\nRgb = %d ohm\nRf = %d k-ohm\n"%(R1,R2,Rgb*1000,Rf))
# Value of the current flowing through the Resistor R(Refer fig. 4.106)
import math
#Variable declartion
R2 = 1000.0 # Resistor in series with R
Va = Vb = 5.0 # input voltage
#Calculations
I = Vb/R2
#Result
print("I = %d mA"%(I*10**3))
# Plot output waveform (refer fig 4.109)
##########-------PLOT--------#############
%matplotlib inline
import matplotlib.pyplot as plt
from numpy import arange,sin,pi
t = arange(0.0,1.0,0.01)
t1 = arange(0.0,3.0,0.01)
t2 = arange(0.0,1.0,0.01)
t3 = arange(-0.5,0.0,0.01)
t4 = arange(-1.0,0.0,0.01)
t5 = arange(0.0,0.5,0.01)
t6 = arange(1.0,2.0,0.01)
plt.axis([0,2.0,-1.5,0.9])
plt.plot(t1,t1*0/t1,'b')
plt.plot(t,-t*1,'b')
plt.plot(t2,-t2*1/t2,'--')
plt.plot(t5,-t5*0.5/t5,'--')
plt.plot(t3*0.5/t3,t3,'--')
plt.plot(t4*1.0/t4,t4,'--')
plt.plot(t6,-t6*1/t6,'b')
plt.title("Output Voltage")
plt.xlabel("Time(ms)")
plt.ylabel("Vo(t)")
print("Theoretical example")
# output of circuit in fig. 4.111
import math
#Variable declaration
R1 = 1.0 # Resistance to inverting terminal
R2 = 2.0 # Resistance to non-inverting terminal
Rf = 10.0 # Feedback resistance
Rcom = 1.0 # Resistance Rcom
V1 = 2.0 # input voltage at inverting terminal
V2 = 5.0 # input voltage at non-inverting terminal
#Calculations
Vo1 = -Rf*V1/R1
Vb = V2*Rcom/(R2+Rcom)
Vo2 = (1+(Rf/R1))*Vb
Vout = Vo1+Vo2
#Result
print("Vo = %.3f V"%Vout)
# Output voltage of circuit shown in fig. 4.114
import math
#Variable declaration
Rf = 10.0 # Resistor Rf in k-ohm
R1 = 5.0 # Resistor R1 in k-ohm
R2 = 4.0 # Resistor R2 in k-ohm
R3 = 2.0 # Resistor R3 in k-ohm
V1 = 5.0 # input 1
V2 = 2.0 # input 2
V3 = 3.0 # input 3
#Calculations
Vout = -((Rf*V1/R1)+(Rf*V2/R2)+(Rf*V3/R3))
#Result
print("Vout = %d V"%Vout)
# output Voltage of circuit in fig. 4.116
import math
#Variable declaration
R1 = 2.0 # Resistance to inverting terminal
R2 = 1.0 # Resistance to non-inverting terminal
Rf = 4.0 # Feedback resistance
Rcom = 1.0 # Resistance Rcom
V1 = 2.0 # input voltage at non-inverting terminal
V2 = 4.0 # input voltage at non-inverting terminal
#Calculations
Vb1 = V2*R2/(R2+Rcom)
Vo1 = (1+(Rf/R1))*Vb1
Vb2 = V1*R2/(R2+Rcom)
Vo2 = (1+(Rf/R1))*Vb2
Vout = Vo1+Vo2
#Result
print("Vo = %d V"%Vout)
# output of differential amplifier
import math
#Variable declaration
V1 = 1.0*10**-3 # input voltage 1
V2 = 2.0*10**-3 # input voltage 2
Ad = 5000.0 # differential gain
CMRR = 1000.0 # Common Mode Rejection Ratio
#Calculations
Ac = Ad/CMRR
Vd = (V1-V2)
Vc = (V1+V2)/2
Vout = Ac*Vc+Ad*Vd
#Result
print("Vo = %.4f V"%Vout)
# output Voltage of circuit in fig. 4.116
import math
#Variable declaration
R1 = 40.0 # Resistance to inverting terminal
R2 = 25.0 # Resistance to inverting terminal
Rf = 50.0 # Feedback resistance
R3 = 10.0 # Resistance to non-inverting terminal
R4 = 20.0 # Resistance to non-inverting terminal
Rcom = 30.0 # Resistance Rcom
V1 = 1.0 # input voltage at inverting terminal
V2 = 2.0 # input voltage at inverting terminal
V3 = 3.0 # input voltage at non-inverting terminal
V4 = 4.0 # input voltage at non-inverting terminal
#Calculations
# (i)
Vo1 = -(Rf/R1)*V1
# (ii)
Vo2 = -(Rf/R2)*V2
# (iii)
R1 = R1*R2/(R1+R2)
Vb3 = V3*(Rcom*R4/(Rcom+R4))/(R3+(Rcom*R4/(Rcom+R4)))
Vo3 = (1+(Rf/R1))*Vb3
Vo3 = math.floor(Vo3*10**4)/10**4
# (iv)
Vb4 = V4*(Rcom*R3/(Rcom+R3))/(R4+(Rcom*R3/(Rcom+R3)))
Vo4 = (1+(Rf/R1))*Vb4
Vo4 = math.floor(Vo4*10**4)/10**4
Vout = Vo1+Vo2+Vo3+Vo4
#Result
print("Vo = %.4f V"%Vout)