{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 3 Current Voltage Sources and Differential Amplifiers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 3.1 Pg 53"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The  collector  current  of  difference  amplifier  Ic1 = Ic2 = 0.50  mA \n",
      "The collector voltages of transistors Q1 and Q2 are  Vc1 = Vc2 = 5.00  volt \n",
      "For Ve = -0.7 Volt  the  collector - emitter  voltage  Vce1  = 5.70  Volt\n",
      "For Ve = 4.3 Volt  the  collector - emitter  voltage  Vce1  = 0.70  Volt\n",
      "For Ve = -5.7 Volt  the  collector - emitter  voltage  Vce1  = 10.70  Volt\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Determine the collector current Ic1 and collector-emitter voltage Vce1 for the difference amplifier circuit\n",
    "\n",
    "V1 = 0 #    # volt\n",
    "V2 = -5 #   #volt\n",
    "Vcm =  5 #   #volt\n",
    "Vcc = 10#   #volt\n",
    "Vee = -10 #  #volt\n",
    "Ie =  1 #  #mA\n",
    "Rc =  10 #  #kilo ohm\n",
    "\n",
    "# Transistor parameters\n",
    "# base current are negligible\n",
    "Vbe = 0.7 #  # volt\n",
    "\n",
    "# The collector current of difference amplifier is\n",
    "Ic1 = Ie/2 # \n",
    "print 'The  collector  current  of  difference  amplifier  Ic1 = Ic2 = %0.2f'%Ic1,' mA '\n",
    "\n",
    "# The collector voltages of transistors Q1 and Q2 are expressed as\n",
    "\n",
    "Vc1  = Vcc-Ic1*Rc #\n",
    "print 'The collector voltages of transistors Q1 and Q2 are  Vc1 = Vc2 = %0.2f'%Vc1,' volt '\n",
    "\n",
    "# We know common mode voltage (Vcm) , from this the emitter voltage can be identified as follows\n",
    "# For the common mode voltage Vcm = 0 V , the emitter voltage is Ve = -0.7 V\n",
    "# For the common mode voltage Vcm = 5 V , the emitter voltage is Ve =  4.3 V\n",
    "# For the common mode voltage Vcm = -5 V , the emitter voltage is Ve = -5.7 V\n",
    "\n",
    "# For the different emitter voltages the collector-emitter voltage can be calculated as\n",
    "\n",
    "Ve = -0.7 #  # volt\n",
    "Vce1 = Vc1-Ve#\n",
    "print 'For Ve = -0.7 Volt  the  collector - emitter  voltage  Vce1  = %0.2f'%Vce1,' Volt'\n",
    "\n",
    "Ve = 4.3 #  # volt\n",
    "Vce1 = Vc1-Ve#\n",
    "print 'For Ve = 4.3 Volt  the  collector - emitter  voltage  Vce1  = %0.2f'%Vce1,' Volt'\n",
    "\n",
    "Ve = -5.7 #  # volt\n",
    "Vce1 = Vc1-Ve#\n",
    "print 'For Ve = -5.7 Volt  the  collector - emitter  voltage  Vce1  = %0.2f'%Vce1,' Volt'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 3.2 Pg 54"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " The differential mode gain Ad = 184.6\n",
      " The common mode gain Acm = -0.237\n"
     ]
    }
   ],
   "source": [
    "# To determine the difference-mode and common-mode gain of the difference amplifier\n",
    "\n",
    "Vcc = 10 # # volt\n",
    "Vee = -10 #  #volt\n",
    "Iq  =  0.8 #  #mA\n",
    "Ie  =  0.8 #  #mA\n",
    "Rc = 12 #  #kilo-Ohm\n",
    "Vt = 0.026 #  # volt\n",
    "\n",
    "# Transistor parameter\n",
    "beta = 100 #\n",
    "Rs = 0 #  #Ohm\n",
    "Ro = 25 #  #kilo-Ohm \n",
    "# The differential mode gain Ad\n",
    "gm = (Ie/ 2*Vt) #\n",
    "# Ad = (gm*r*Rc/r+Rc) #   # where r is r-pi\n",
    "# For Rb=0 , the differential mode gain is\n",
    "\n",
    "Ad = (Ie/(2*Vt))*Rc#\n",
    "#But\n",
    "print ' The differential mode gain Ad = %0.1f'%Ad\n",
    "\n",
    "#The common mode gain Acm\n",
    "# Acm = - (gm*Rc/1+2*gm*Re+2*Re/r)\n",
    "Acm =-(Ad/(1+(((1+beta)*Ie*Ro)/(beta*Vt))))\n",
    "print ' The common mode gain Acm = %0.3f'%Acm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 3.3 Pg 56"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The output of a difference amplifier is  Vo  = -47.40 sinwt uV \n"
     ]
    }
   ],
   "source": [
    "# To find the output of a difference amplifier when only common mode signal is applied\n",
    "\n",
    "# V1 = V2 = Vcm = 200*sin(wt) #   # micro volt (uV)\n",
    "Acm  = -0.237 #\n",
    "\n",
    "# When the common mode input signal is applied to the difference amplifier , the difference mode gain is zero\n",
    "Vcm = 200 #\n",
    "Vo = Acm*Vcm #\n",
    "print 'The output of a difference amplifier is  Vo  = %0.2f'%Vo,'sinwt uV '  # multiply by sinwt because it is in Vcm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 3.4 Pg 56"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The differential mode gain Ad = 184.6\n",
      "The common mode gain Acm = -0.237\n",
      "The CMRR of difference amplifier is = 389\n",
      "In decibel CMRR is = 51.80\n"
     ]
    }
   ],
   "source": [
    "from math import log10\n",
    "#Determine the common mode rejection ratio(CMRR) of the difference amplifier\n",
    "\n",
    "Vcc = 10 # # volt\n",
    "Vee = -10 #  #volt\n",
    "Iq  =  0.8 #  #mA\n",
    "Ie  =  0.8 #  #mA\n",
    "Rc = 12 #  #kilo-Ohm\n",
    "Vt = 0.026 #  # volt\n",
    "\n",
    "# Transistor parameter\n",
    "beta = 100 #\n",
    "Rs = 0 #  #Ohm\n",
    "Ro = 25 #  #kilo-Ohm\n",
    " \n",
    "# The differential mode gain Ad\n",
    "gm = (Ie/ 2*Vt) #\n",
    "# Ad = (gm*r*Rc/r+Rc) #   # where r is r-pi\n",
    "# For Rb=0 , the differential mode gain is\n",
    "\n",
    "Ad = (Ie/(2*Vt))*Rc#\n",
    "#But\n",
    "print 'The differential mode gain Ad = %0.1f'%Ad\n",
    "\n",
    "#The common mode gain Acm\n",
    "# Acm = - (gm*Rc/1+2*gm*Re+2*Re/r)\n",
    "Acm =-(Ad/(1+(((1+beta)*Ie*Ro)/(beta*Vt))))\n",
    "print 'The common mode gain Acm = %0.3f'%Acm\n",
    "\n",
    "# The CMRR of difference amplifier is given as\n",
    "Ad = Ad/2 #\n",
    "CMRR = abs(Ad/Acm)\n",
    "print 'The CMRR of difference amplifier is = %0.f'%CMRR\n",
    "\n",
    "# In decibel it can be expressed as\n",
    "CMRRdb = 20*log10(CMRR)\n",
    "print 'In decibel CMRR is = %0.2f'%CMRRdb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 3.5 Pg 58"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " The CMRR of difference amplifier is = 3.16e+04\n",
      " The value of resistance RE is = 2.04  Mohm \n"
     ]
    }
   ],
   "source": [
    "# To determine emitter resistance of the difference amplifier\n",
    "\n",
    "Vcc = 10 # # volt\n",
    "Vee = -10 #  #volt\n",
    "Iq  =  0.8 #  #mA\n",
    "Ie  =  0.8 #  #mA\n",
    "CMRRdb = 90 #  #dB\n",
    "Vt = 0.026 #\n",
    "\n",
    "# Transistor parameter\n",
    "beta = 100 #\n",
    "\n",
    "# CMRR = abs(Ad/Acm)\n",
    "# the CMRR of the difference amplifier is defined as\n",
    "#CMRR = ((1/2)*(1+((1+beta)*Ie*Re)/beta*Vt))\n",
    "\n",
    "# CMRRdb = 20*log10(CMRR)\n",
    "CMRR = 10**(CMRRdb/20)\n",
    "print ' The CMRR of difference amplifier is = %0.2e'%CMRR\n",
    "\n",
    "# The resistance RE is calculated as\n",
    "\n",
    "RE = (((2*CMRR)-1)/((1+beta)*Ie))*(beta*Vt)/1e3\n",
    "print ' The value of resistance RE is = %0.2f'%RE,' Mohm '"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 3.6 Pg 59"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " The differential mode gain Ad is = 321\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# determine the differential mode gain when load resistance RL = 100 k ohm\n",
    "\n",
    "RL = 100*10**3 # # k ohm  # load resistance\n",
    "IE = 0.20*10**-3 # # mA  # biasing current\n",
    "VA = 100 # # V # early voltage\n",
    "VT = 0.026 #  # threshold volt\n",
    "\n",
    "# the differential gain of differential amplifier with an active load circuit\n",
    "#Ad = Vo/Vd  = gm(ro2 || ro4 || RL  )\n",
    "ro2 = (2*VA)/IE#\n",
    "ro4 = ro2 #\n",
    "gm = IE/(2*VT) #\n",
    "\n",
    "Ad = gm/((1/ro2)+(1/ro4)+(1/RL))\n",
    "print ' The differential mode gain Ad is = %0.f'%Ad"
   ]
  }
 ],
 "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.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}