{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#Chapter 13 , Operational Amplifiers (Op-Amps)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example 13.1 , Page Number 481"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CMRR :  80.0 db.\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "#Variables\n",
    "\n",
    "Ad = 100.0                     #Differential mode gain\n",
    "Acm = 0.01                     #Common-mode gain\n",
    "\n",
    "#Calculation\n",
    "\n",
    "CMRR =  Ad/Acm                 #CMRR\n",
    "CMRR1 = 20*math.log10(CMRR)    #CMRR (in db)\n",
    "\n",
    "#Result\n",
    "\n",
    "print \"CMRR : \",CMRR1,\"db.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example 13.2 , Page Number 481"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Common mode gain :  10.0 .\n"
     ]
    }
   ],
   "source": [
    "#Variables\n",
    "\n",
    "Ad = 1.0 * 10**5                     #Differential mode gain\n",
    "CMRR = 1.0 * 10**4                   #CMRR\n",
    "\n",
    "#Calculation\n",
    "\n",
    "Acm = Ad/CMRR                        #Common-mode gain\n",
    "\n",
    "#Result\n",
    "\n",
    "print \"Common mode gain : \",Acm,\".\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example 13.3 , Page Number 482"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Output voltage : 2.537125 V.\n",
      "Percentage error :  1.4633 %.\n"
     ]
    }
   ],
   "source": [
    "#Variables\n",
    "\n",
    "V1 = 745.0 * 10**-6            #Input voltage1 (in volts)\n",
    "V2 = 740.0 * 10**-6            #Input voltage2 (in volts)\n",
    "Vcm = (V1 + V2)/2              #Commonn mode signal (in volts)\n",
    "Vd = V1 - V2                   #Differential voltage (in volts)\n",
    "Ad = 5 * 10**5                 #Differential voltage gain\n",
    "CMRR = 1.0 * 10**4             #CMRR\n",
    " \n",
    "#Calculation\n",
    "\n",
    "Vout = Ad*Vd*(1 + 1/CMRR*Vcm/Vd)    #output voltage (in volts)\n",
    "error = Vout - Ad*Vd                #Error voltage (in volts)\n",
    "Percerror = error/Vout*100          #Percentage error\n",
    "\n",
    "#Result\n",
    "\n",
    "print \"Output voltage :\",round(Vout,6),\"V.\"\n",
    "print \"Percentage error : \",round(Percerror,4),\"%.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example 13.4 , Page Number 483"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Output voltage : + (or) -  5.0 V.\n"
     ]
    }
   ],
   "source": [
    "#Variables\n",
    "\n",
    "A = 200000.0               #Open loop voltage gain\n",
    "Vd = 25.0 * 10**-6         #Input differential voltage (in volts)\n",
    "\n",
    "#Calculation\n",
    "\n",
    "Vout = A*Vd                #output voltage (in volts) \n",
    "\n",
    "#Result\n",
    "\n",
    "print \"Output voltage : + (or) - \",Vout,\"V.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example 13.5 , Page Number 486"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rf :  27.0 kilo-ohm.\n"
     ]
    }
   ],
   "source": [
    "#Variables\n",
    "\n",
    "Af = 10.0                   #Voltage gain\n",
    "R1 = 3.0 * 10**3            #Resistance (in ohm) \n",
    "\n",
    "#Calculation\n",
    "\n",
    "Rf = (Af - 1)*R1            #Resistance (in ohm) \n",
    "\n",
    "#Result\n",
    "\n",
    "print \"Rf : \",Rf*10**-3,\"kilo-ohm.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example 13.6 , Page Number 486"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Maximum closed loop voltage gain 51.0 .\n",
      "Minimum closed loop voltage gain 1.0 .\n"
     ]
    }
   ],
   "source": [
    "#Variables\n",
    "\n",
    "R1 = 2.0 * 10**3            #Resistance (in ohm) \n",
    "Rfmin = 0.0                 #Resistance (in ohm) \n",
    "Rfmax = 100.0 * 10**3       #Resistance (in ohm) \n",
    "\n",
    "#Calculation\n",
    "\n",
    "Afmin = 1 + Rfmin/R1        #Minimum voltage gain\n",
    "Afmax = 1 + Rfmax/R1        #Maximum voltage gain \n",
    "\n",
    "#Result\n",
    "\n",
    "print \"Maximum closed loop voltage gain\",Afmax,\".\"\n",
    "print \"Minimum closed loop voltage gain\",Afmin,\".\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example 13.7 , Page Number 488"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Voltage gain :  -100.0 .\n",
      "Input resistance :  5.0 kilo-ohm.\n",
      "Output resistance :  0 ohm.\n",
      "Output voltage :  -10.0 V.\n",
      "Input current :  0.02 mA.\n"
     ]
    }
   ],
   "source": [
    "#Variables\n",
    "\n",
    "R1 = 5.0 * 10**3            #Resistance (in ohm) \n",
    "Rf = 500.0 * 10**3          #Feedback resistance (in ohm)\n",
    "Vin = 0.1                   #Input voltage (in volts)\n",
    "\n",
    "#Calculation\n",
    "\n",
    "Af = -Rf/R1                 #Voltage gain\n",
    "Rin = R1                    #Input resistance (in ohm)\n",
    "Rout = 0                    #Output resistance (in ohm)\n",
    "Vout = Af*Vin               #Output voltage (in volts)\n",
    "Iin = Vin/R1                #Input current (in Ampere)\n",
    "\n",
    "#Result\n",
    "\n",
    "print \"Voltage gain : \",Af,\".\"\n",
    "print \"Input resistance : \",Rin*10**-3,\"kilo-ohm.\"\n",
    "print \"Output resistance : \",Rout,\"ohm.\"\n",
    "print \"Output voltage : \",Vout,\"V.\"\n",
    "print \"Input current : \",Iin*10**3,\"mA.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example 13.8 , Page Number 488"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "O/P voltage when switch is open :  -2.0 V.\n",
      "O/P voltage when switch is closed :  -2.0 V.\n"
     ]
    }
   ],
   "source": [
    "#Variables\n",
    "\n",
    "Rf = 2.0 * 10**3           #Feedback resistance when S is open (in ohm)\n",
    "Vin = 1.0                  #Input voltage when S is open (in volts)\n",
    "R1 = 1.0 * 10**3           #Resistance (in ohm)\n",
    "R2 = R3 = 1.0 * 10**3      #Resistance (in ohm)\n",
    "\n",
    "#Calculation\n",
    "\n",
    "Vout = -Vin*Rf/R1          #Output voltage when S is open (in volts)\n",
    "Af = -(R3 + R2)/R1         #gain\n",
    "Vout1 = Af*Vin             #Output voltage when S is closed (in volts)\n",
    "\n",
    "#Result\n",
    "\n",
    "print \"O/P voltage when switch is open : \",Vout,\"V.\"\n",
    "print \"O/P voltage when switch is closed : \",Vout1,\"V.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example 13.9 , Page Number 489"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Voltage gain :  -1.0 .\n",
      "Current gain :  1 .\n",
      "Power gain :  1.0 .\n"
     ]
    }
   ],
   "source": [
    "#Variables\n",
    "\n",
    "Rf = 1.0 * 10**6                 #Feedback resistance (in ohm)\n",
    "Ri = 1.0 * 10**6                 #Input resistance (in ohm)\n",
    "Vi = 1                           #Input voltage (in volts)\n",
    "\n",
    "#Calculation\n",
    "\n",
    "Vo = -Rf/Ri*Vi                   #Output voltage (in volts)\n",
    "Av = Vo/Vi                       #Voltage gain \n",
    "Ai = 1                           #Current gain\n",
    "Ap = abs(Av*Ai)                  #Power gain \n",
    "\n",
    "#Result\n",
    "\n",
    "print \"Voltage gain : \",Av,\".\"\n",
    "print \"Current gain : \",Ai,\".\"\n",
    "print \"Power gain : \",Ap,\".\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example 13.13 , Page Number 492"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Amplifier gain when S is open :  1.0 .\n",
      "Amplifier gain when S is closed :  -2.0 .\n"
     ]
    }
   ],
   "source": [
    "#Variables\n",
    "\n",
    "Rf = 20.0 * 10**3                #Feedback resistance (in ohm)\n",
    "R1 = 10.0 * 10**3                #Resistance (in ohm)\n",
    "\n",
    "#Calculation\n",
    "\n",
    "Aoffnoninv = 1 + Rf/R1           #Amplifier gain when S open and non inverted\n",
    "Aoffinv = -Rf/R1                 #Amplifier gain when S open and inverted\n",
    "Aoff = Aoffinv + Aoffnoninv      #Amplifier gain when S open\n",
    "Aon = -Rf/R1                     #Amplifier gain when S is closed    \n",
    "\n",
    "#Result\n",
    "\n",
    "print \"Amplifier gain when S is open : \",Aoff,\".\"\n",
    "print \"Amplifier gain when S is closed : \",Aon,\".\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example 13.14 , Page Number 494"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "R1 :  100.0 kilo-ohm.\n",
      "R3 :  10.0 kilo-ohm.\n",
      "R3 :  1.0 kilo-ohm.\n"
     ]
    }
   ],
   "source": [
    "#Variables\n",
    "\n",
    "Rf = 100.0                       #Feedback resistance (in kilo-ohm)\n",
    "\n",
    "#Calculation\n",
    "\n",
    "R1 = Rf                          #Resistance1 (in ohm)\n",
    "R2 = Rf/10                       #Resistance2 (in ohm)\n",
    "R3 = Rf/100                      #Resistance3 (in ohm)\n",
    "\n",
    "#Result\n",
    "\n",
    "print \"R1 : \",R1,\"kilo-ohm.\\nR3 : \",R2,\"kilo-ohm.\\nR3 : \",R3,\"kilo-ohm.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example 13.15 , Page Number 494"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Output voltage :  13.0 V.\n"
     ]
    }
   ],
   "source": [
    "#Variables\n",
    "\n",
    "Rf = 12.0 * 10**3            #Feedback resistance (in ohm)\n",
    "R1 = 12.0 * 10**3            #Resistance1 (in ohm)\n",
    "R2 = 2.0 * 10**3             #Resistance2 (in ohm)\n",
    "R3 = 3.0 * 10**3             #Resistance3 (in ohm)\n",
    "Vi1 = 9.0                    #Input voltage1 (in volts)\n",
    "Vi2 = -3.0                   #Input voltage2 (in volts)\n",
    "Vi3 = -1.0                   #Input voltage3 (in volts)\n",
    "\n",
    "#Calculation\n",
    "\n",
    "Vout = -Rf*(Vi1/R1 + Vi2/R2 + Vi3/R3)      #Output voltage (in volts)\n",
    "\n",
    "#Result\n",
    "\n",
    "print \"Output voltage : \",Vout,\"V.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example 13.16 , Page Number 495"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "R1 :  6.0 kilo-ohm.\n",
      "R3 :  3.0 kilo-ohm.\n",
      "R3 :  2.0 kilo-ohm.\n"
     ]
    }
   ],
   "source": [
    "#Variables\n",
    "\n",
    "Rf = 6.0                       #Feedback resistance (in kilo-ohm)\n",
    "\n",
    "#Calculation\n",
    "\n",
    "R1 = Rf                          #Resistance1 (in ohm)\n",
    "R2 = Rf/2                        #Resistance2 (in ohm)\n",
    "R3 = Rf/3                        #Resistance3 (in ohm)\n",
    "\n",
    "#Result\n",
    "\n",
    "print \"R1 : \",R1,\"kilo-ohm.\\nR3 : \",R2,\"kilo-ohm.\\nR3 : \",R3,\"kilo-ohm.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example 13.17 , Page Number 495"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rf :  40.0 kilo-ohm.\n",
      "R2 :  13.33 kilo-ohm.\n",
      "R1 :  20.0 kilo-ohm.\n"
     ]
    }
   ],
   "source": [
    "#Variables\n",
    "\n",
    "R3 = 10.0                        #Resistance (in kilo-ohm)\n",
    "\n",
    "#Calculation\n",
    "\n",
    "Rf = 4*R3                        #Feedback resistance (in ohm)\n",
    "R2 = Rf/3                        #Resistance2 (in ohm)\n",
    "R1 = Rf/2                        #Resistance1 (in ohm)\n",
    "\n",
    "#Result\n",
    "\n",
    "print \"Rf : \",Rf,\"kilo-ohm.\\nR2 : \",round(R2,2),\"kilo-ohm.\\nR1 : \",R1,\"kilo-ohm.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example 13.18 , Page Number 495"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Output voltage :  1.0 V.\n"
     ]
    }
   ],
   "source": [
    "#Variables\n",
    "\n",
    "V1 = 2.0                       #Voltage1 (in volts)\n",
    "V2 = -1.0                      #Voltage2 (in volts)\n",
    "\n",
    "#Calculation\n",
    "\n",
    "Vs1 = V1*(1.0/2/(1+1.0/2))     #I/P at non-inverting I/P terminal (in volts)\n",
    "V1o = Vs1*(1 + 2/1)            #O/P voltage1 (in volts)\n",
    "Vs2 = V2*(1.0/2/(1+1.0/2))     #I/P voltage2 (in volts)\n",
    "V2o = Vs2*(1 + 2/1)            #O/P voltage2 (in volts)\n",
    "Vout = V1o + V2o               #Output voltage (in volts)\n",
    "\n",
    "#Result\n",
    "\n",
    "print \"Output voltage : \",Vout,\"V.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example 13.19 , Page Number 496"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Feedback resistor :  100.0 kilo-ohm.\n"
     ]
    }
   ],
   "source": [
    "#Variables\n",
    "\n",
    "R = 10.0                  #Resistance (in kilo-ohm)\n",
    "\n",
    "#Calculation\n",
    "\n",
    "Rf = 10*R                 #feedback resistance (in kilo-ohm)\n",
    "\n",
    "#Result\n",
    "\n",
    "print \"Feedback resistor : \",Rf,\"kilo-ohm.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example 13.20 , Page Number 498"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-10.0\n",
      "Output voltage : 0.0113(cos(4000*t)-1) mV.\n"
     ]
    }
   ],
   "source": [
    "#Variables\n",
    "\n",
    "C = 2.0 * 10**-6                  #Capacitance (in Farad)\n",
    "R = 50.0 * 10**3                  #Resistance (in ohm)  \n",
    "\n",
    "#Calculation\n",
    "\n",
    "scale_factor = -1/(C*R)           #Scale factor (in second)\n",
    "\n",
    "#Result\n",
    "\n",
    "print \"Output voltage : 0.0113(cos(4000*t)-1) mV.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example 13.21 , Page Number 499"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Output voltage : 13.56*cos(4000*math.pi*t).\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "#Variables\n",
    "\n",
    "C = 2.0 * 10**-6                  #Capacitance (in Farad)\n",
    "R = 50.0 * 10**3                  #Resistance (in ohm)  \n",
    "f = 2.0 * 10**3                   #Frequency (in Hertz)\n",
    "Vpeak = 10.0 * 10**-6             #Peak voltage (in volts)\n",
    "\n",
    "#Calculation\n",
    "\n",
    "scale_factor = (C*R)              #Scale factor (in second)\n",
    "\n",
    "#Result\n",
    "\n",
    "print \"Output voltage : 13.56*cos(4000*math.pi*t).\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example 13.22 , Page Number 505"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Input bias current :  8.75 micro-Ampere.\n",
      "Input offset current :  2.5 micro-Ampere.\n"
     ]
    }
   ],
   "source": [
    "#Variables\n",
    "\n",
    "IB1 = 10.0 * 10**-6                  #Base current1 (in Ampere)\n",
    "IB2 = 7.5 * 10**-6                   #Base current2 (in Ampere)   \n",
    "\n",
    "#Calculation\n",
    "\n",
    "Iinbias = (IB1 + IB2)/2              #Input bias current (in Ampere)\n",
    "Iinoffset = IB1 - IB2                #Input offset current (in Ampere)          \n",
    "\n",
    "#Result\n",
    "\n",
    "print \"Input bias current : \",round(Iinbias*10**6,2),\"micro-Ampere.\"\n",
    "print \"Input offset current : \",round(Iinoffset*10**6,2),\"micro-Ampere.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Example 13.23 , Page Number 505"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Slew rate :  5.0  V/micro-second.\n"
     ]
    }
   ],
   "source": [
    "#Variables\n",
    "\n",
    "dVout = 20.0                         #Output voltage (in volts)\n",
    "dt = 4.0                             #time (in micro-seconds) \n",
    "\n",
    "#Calculation\n",
    "\n",
    "SR = dVout/dt                        #Slew rate (in volt per micro-second)\n",
    "\n",
    "#Result\n",
    "\n",
    "print \"Slew rate : \",SR,\" V/micro-second.\""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}