{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 2: Measurement Of Electrical Quantities"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example2_1,pg 23"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# output voltage \n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "ic=1*10**-3                     #constant current source\n",
      "Rf=15*10**3                     #feedback resistance\n",
      "Rs=10*10**3                     #input resistance\n",
      "Rx=1.0*10**3                    #unknown resistance\n",
      "R1=10.0                         #unknown resistance\n",
      "R2=1*10**3                      #input resistance\n",
      "\n",
      "#Calculations\n",
      "\n",
      "# for fig. 2.7\n",
      "Vo1=ic*Rf*(Rx/(1+(Rx*Rs)))      #output voltage case-1\n",
      "#for fig. 2.8\n",
      "Vo2=ic*Rx*(R1/(1+R1*R2))        #output voltage case-2\n",
      "\n",
      "#Result\n",
      "print(\"output voltage for case-1:\")\n",
      "print(\"Vo1 = %.4f V\\n\"%Vo1)\n",
      "print(\"output voltage for case-2:\")\n",
      "print(\"Vo2 = %.0f mV\\n\"%(Vo2*10**3))\n",
      "#Answer for case-1 is different in the book"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "output voltage for case-1:\n",
        "Vo1 = 0.0015 V\n",
        "\n",
        "output voltage for case-2:\n",
        "Vo2 = 1 mV\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example2_2,pg 27"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# find Ad CMRR and Acm\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "V1=5.0                      #input-1\n",
      "V2=5.0                      #input-2\n",
      "V12=50*10**-3               #difference input\n",
      "Vo=2.0                      #output voltage\n",
      "acc=0.01                    #accuracy\n",
      "\n",
      "#Calculations\n",
      "Ad=(Vo/V12)                 #diffrential gain\n",
      "#error at the output should be less than (2/100)V or 20mV.if common mode gain is the only source of error then \n",
      "err=Vo*acc                  #error\n",
      "Acm=(err/V1)                #common mode gain\n",
      "CMRR=20*math.log10(Ad/Acm)  #common mode rejection ratio in dB\n",
      "\n",
      "#Result\n",
      "print(\"diffrential gain:\")\n",
      "print(\"Ad=%.1f \\n\"%Ad)\n",
      "print(\"common mode gain:\")\n",
      "print(\"Acm=%.4f \"%Acm)\n",
      "print(\"\\nCMRR=%.1f dB\\n\"%CMRR)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "diffrential gain:\n",
        "Ad=40.0 \n",
        "\n",
        "common mode gain:\n",
        "Acm=0.0040 \n",
        "\n",
        "CMRR=80.0 dB\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example2_3,pg 484"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# find full scale output and minimum input\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "Aol=1.0*10**5              #open loop gain\n",
      "R2=10.0*10**3              #Resistor R2\n",
      "R3=10.0*10**3              #Resistor R3\n",
      "R1=100*10**3               #input resistance\n",
      "Vac=24.0                   #maximum input\n",
      "\n",
      "#Calculations\n",
      "Vo=(R2/R1)*Vac             #output full scale\n",
      "Vth=0.6                    #threshold voltage\n",
      "Vn=(Vth/Aol)               #minimum input\n",
      "\n",
      "#Result\n",
      "print(\"output FS voltage:\")\n",
      "print(\"Vo = %.2f V\\n\"%Vo)\n",
      "print(\"minimum input voltage:\")\n",
      "print(\"Vn = %.1f micro-V\\n\"%(Vn*10**6))\n",
      "#Answer for Vn is wrong in the book"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "output FS voltage:\n",
        "Vo = 2.40 V\n",
        "\n",
        "minimum input voltage:\n",
        "Vn = 6.0 micro-V\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example2_4,pg 484"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# output voltage\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "Vp=1.0                    #peak input voltage\n",
      "f=50.0                    #frequency\n",
      "#R1=R2\n",
      "\n",
      "#Calculations\n",
      "#since halfwave rectification is done,integration gives the value\n",
      "pi =math.floor(math.pi*100)/100\n",
      "Vo=0.5*((2*Vp)/pi)         #output voltage,pi=3.14 \n",
      "\n",
      "#Result\n",
      "print(\"output voltage:\")\n",
      "print(\"Vo = %.3f V\\n\"%Vo)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "output voltage:\n",
        "Vo = 0.318 V\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example2_5,pg 484"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# find unknown resistance\n",
      "\n",
      "import math\n",
      "#VBariable declaration\n",
      "ic=0.1*10**-3               #constant current source\n",
      "Vo=2.0                      #output voltage\n",
      "Rf=22.0*10**3               #feedback resistance\n",
      "Rs=10.0*10**3               #input resistance\n",
      "\n",
      "#Calculations\n",
      "Rx=(1/(((ic*Rf)/(Vo*Rs))-(1/Rs)))\n",
      "\n",
      "#Result\n",
      "print(\"unknown resistance:\")\n",
      "print(\"Rx = %.0f k-ohm\\n\"%(Rx/1000))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "unknown resistance:\n",
        "Rx = 100 k-ohm\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example2_6,pg 484"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# find CMRR\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "a=0.9                           #parameter of diff. amplr.\n",
      "b=1.1                           #parameter of diff. amplr.\n",
      "\n",
      "#Calculations\n",
      "CMRR=0.5*(((1+a)*b+(1+b)*a))/((1+a)*b-(1+b)*a)\n",
      "\n",
      "#Results\n",
      "print(\"CMRR = %.2f\"%CMRR)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "CMRR = 9.95\n"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example2_7,pg 485"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# tolerance in parameters\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "CMRR=10.0*10**4                      #common mode rejection ratio\n",
      "\n",
      "\n",
      "#Calculations\n",
      "#set a=beta+k1*delbeta and b=beta(-/+)k2*delbeta\n",
      "#CMRR=0.5*((4(+/-)3*delbeta*(k1-k2))/((+/-)delbeta*(k1-k2)))\n",
      "#CMRR=0.5*((4(+/-)3*(a1-a2))/((+/-)(a1-a2)))\n",
      "#a1->k1*delbeta, a2->k2*delbeta\n",
      "\n",
      "delalpha=(2/CMRR)                 #a1-a2=delalpha\n",
      "\n",
      "#Result\n",
      "print(\"tolerance in parameters:\")\n",
      "print(\"delalpha = %.0f * 10^-5 \"%(delalpha*10**5))\n",
      "print(\"Therefore, if a varies by 1 percent, b should not vary more than 2*10^-3 percent of variation of a.\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "tolerance in parameters:\n",
        "delalpha = 2 * 10^-5 \n",
        "Therefore, if a varies by 1 percent, b should not vary more than 2*10^-3 percent of variation of a.\n"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example2_8,pg 485"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# output voltage\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "R1=10*10**3             #resistor R1\n",
      "R2=10*10**3             #resistor R2\n",
      "V1=1.0                  #input voltage-1\n",
      "V2=1.0                  #input voltage-2\n",
      "R31=10.0*10**3          #Resistor R3,case-1\n",
      "R32=100.0*10**3         #Resistor R3,case-2\n",
      "R33=1000.0*10**3        #Resistor R3,case-3\n",
      "\n",
      "\n",
      "#Calculations\n",
      "Vo1=((1+(R2/R1)+(R2/R31))*V1)-(R2/R1)*V2\n",
      "Vo2=((1+(R2/R1)+(R2/R32))*V1)-(R2/R1)*V2\n",
      "Vo3=((1+(R2/R1)+(R2/R33))*V1)-(R2/R1)*V2\n",
      "Vo4 = ((1+(R2/R1)+(0))*V1)-(R2/R1)*V2          #by substituting R3 = infinity in above equation abo\n",
      "#Result\n",
      "print(\"output voltage case-1:\")\n",
      "print(\"Vo1 = %.2f V\\n\"%Vo1)\n",
      "print(\"output voltage case-2:\")\n",
      "print(\"Vo2 = %.2f V\\n\"%Vo2)\n",
      "print(\"output voltage case-3:\")\n",
      "print(\"Vo3 = %.2f V\\n\"%Vo3)\n",
      "print(\"output voltage case-4:\")\n",
      "print(\"Vo3 = %.2f V\"%Vo4)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "output voltage case-1:\n",
        "Vo1 = 2.00 V\n",
        "\n",
        "output voltage case-2:\n",
        "Vo2 = 1.10 V\n",
        "\n",
        "output voltage case-3:\n",
        "Vo3 = 1.01 V\n",
        "\n",
        "output voltage case-4:\n",
        "Vo3 = 1.00 V\n"
       ]
      }
     ],
     "prompt_number": 19
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example2_9,pg 486"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# difference in output voltage\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "#(R3/R1)=0.98^-1(R2/R4)\n",
      "R1=10.0*10**3              # Resistor R1\n",
      "R3=10.0*10**3              # Resistor R3\n",
      "I1=130.0*10**-6            # Current \n",
      "\n",
      "#Calculations\n",
      "Vo1=R1*(1+0.98)*I1       #output for case-1, (R2/R4)=0.98\n",
      "#(R1/R3)=(R4/R2)\n",
      "Vo2=R1*(1+(R3/R1))*I1    #output for case-2\n",
      "Vo12=((Vo2-Vo1)/Vo2)*100 #percent difference\n",
      "\n",
      "#Result\n",
      "print(\"difference in output voltage:\")\n",
      "print(\"%% difference  = %.1f %%\"%Vo12)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "difference in output voltage:\n",
        "% difference  = 1.0 %\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example2_10,pg 486"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# find crest factor\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "dutcyc=0.4                  #duty cycle\n",
      "\n",
      "#Calculations\n",
      "CF=math.sqrt((1-dutcyc)/dutcyc)  #crest factor\n",
      "\n",
      "#Result\n",
      "print(\"crest factor:\")\n",
      "print(\"CF = %.3f \"%(math.floor(CF*1000)/1000))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "crest factor:\n",
        "CF = 1.224 \n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example2_11,pg 486"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Find unknown resisance\n",
      "\n",
      "import math\n",
      "R1=10*10**3            #resistor R1\n",
      "R4=10*10**3            #resistor R4\n",
      "Idss=1*10**-3          #drain saturation current\n",
      "Vp=2.2                 #peak voltage\n",
      "Vo=10.0                #output voltage\n",
      "V2=2.0                 #input-1\n",
      "V1=-2.0                #input-2\n",
      "\n",
      "#Calculations\n",
      "R5=((R1*R4)/Vo)*((-2*Idss/(Vp**2)))*V1*V2\n",
      "\n",
      "#Result\n",
      "print(\"R5 = %.1f k-ohm\"%(R5/1000))\n",
      "#R5 should satisfy the condition R5=((1+R1*(-2*Idss*Vp)/R2)*R3*R6) and with Vp negative it is obiviously possible"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "R5 = 16.5 k-ohm\n"
       ]
      }
     ],
     "prompt_number": 9
    }
   ],
   "metadata": {}
  }
 ]
}