{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 6 - Multitransistor and Multistage Amplifier"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6_1 Page No. 168"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Av= 0.10\n",
      "Av(dB)=20*log10(Av)= -20.00 dB \n",
      "Av= 0.71\n",
      "Av(dB)=20*log10(Av)= -3.01 dB \n",
      "Av= 1.00\n",
      "Av(dB)=20*log10(Av)= 0.00 dB \n",
      "Av= 10.00\n",
      "Av(dB)=20*log10(Av)= 20.00 dB \n",
      "Av= 100.00\n",
      "Av(dB)=20*log10(Av)= 40.00 dB \n",
      "Av= 1000.00\n",
      "Av(dB)=20*log10(Av)= 60.00 dB \n"
     ]
    }
   ],
   "source": [
    "from math import log10\n",
    "from __future__ import division      \n",
    "Av=0.1\n",
    "print \"Av= %0.2f\"%(Av) #Voltage gain\n",
    "AvdB=20*log10(Av)\n",
    "print \"Av(dB)=20*log10(Av)= %0.2f\"%(AvdB),\"dB \" #Voltage gain in decibel\n",
    "Av=0.707\n",
    "print \"Av= %0.2f\"%(Av) #Voltage gain\n",
    "AvdB=20*log10(Av)\n",
    "print \"Av(dB)=20*log10(Av)= %0.2f\"%(AvdB),\"dB \" #Voltage gain in decibel\n",
    "Av=1\n",
    "print \"Av= %0.2f\"%(Av) #Voltage gain\n",
    "AvdB=20*log10(Av)\n",
    "print \"Av(dB)=20*log10(Av)= %0.2f\"%(AvdB),\"dB \" #Voltage gain in decibel\n",
    "Av=10\n",
    "print \"Av= %0.2f\"%(Av) #Voltage gain\n",
    "AvdB=20*log10(Av)\n",
    "print \"Av(dB)=20*log10(Av)= %0.2f\"%(AvdB),\"dB \" #Voltage gain in decibel\n",
    "Av=100\n",
    "print \"Av= %0.2f\"%(Av) #Voltage gain\n",
    "AvdB=20*log10(Av)\n",
    "print \"Av(dB)=20*log10(Av)= %0.2f\"%(AvdB),\"dB \" #Voltage gain in decibel\n",
    "Av=1000\n",
    "print \"Av= %0.2f\"%(Av) #Voltage gain\n",
    "AvdB=20*log10(Av)\n",
    "print \"Av(dB)=20*log10(Av)= %0.2f\"%(AvdB),\"dB \" #Voltage gain in decibel\n",
    "#NOTE:calculated voltage gain in dB for Av=0.707 is -3.0116117dB    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6_2 Page No. 169"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ri= 500.00 ohm\n",
      "RL= 50.00 ohm\n",
      "Vom= 1.00  volts\n",
      "Vim= 0.00  volts\n",
      "Av(in dB)=20*log10(Vo/Vi)= 60.00  dB \n",
      "Iim= Vim/Ri= 0.00  A\n",
      "Iom= Vom/RL= 0.02  A\n",
      "Ai=20*log10(Io/Ii)= 80.00  dB \n",
      "pi= Vi**2/Ri= 0.00  W\n",
      "po= Vo**2/RL= 0.01  W\n",
      "Ap=10*log10(po/pi)= 70.00  dB \n"
     ]
    }
   ],
   "source": [
    "from math import sqrt,log10\n",
    "from __future__ import division  \n",
    "Ri=0.5*10**(3)\n",
    "print \"Ri= %0.2f\"%(Ri)+ \" ohm\"  # Amplifier input resistance\n",
    "RL=0.05*10**(3)\n",
    "print \"RL= %0.2f\"%(RL)+ \" ohm\"  # Load resistance\n",
    "Vom=1\n",
    "print \"Vom= %0.2f\"%(Vom),\" volts\" # Output voltage \n",
    "Vo=Vom/sqrt(2)#RMS value of Output voltage \n",
    "Vim=1*10**(-3)\n",
    "print \"Vim= %0.2f\"%(Vim),\" volts\" # Peak Input voltage\n",
    "Vi=Vim/sqrt(2)#RMS Input voltage \n",
    "Av=20*log10(Vo/Vi)\n",
    "print \"Av(in dB)=20*log10(Vo/Vi)= %0.2f\"%(Av),\" dB \" #Voltage gain in decibel\n",
    "Iim=Vim/Ri\n",
    "print \"Iim= Vim/Ri= %0.2f\"%(Iim),\" A\" # Input peak current\n",
    "Ii=Iim/sqrt(2) #RMS value of input current\n",
    "Iom=Vom/RL \n",
    "print \"Iom= Vom/RL= %0.2f\"%(Iom),\" A\" # Output peak current\n",
    "Io=Iom/sqrt(2) #RMS value of Output current\n",
    "Ai=20*log10(Io/Ii)\n",
    "print \"Ai=20*log10(Io/Ii)= %0.2f\"%(Ai),\" dB \" #Current gain in decibel\n",
    "pi=Vi**2/Ri\n",
    "print \"pi= Vi**2/Ri= %0.2f\"%(pi),\" W\" # Input power \n",
    "po=Vo**2/RL\n",
    "print \"po= Vo**2/RL= %0.2f\"%(po),\" W\" # Output power \n",
    "Ap=10*log10(po/pi)\n",
    "print \"Ap=10*log10(po/pi)= %0.2f\"%(Ap),\" dB \" #Power gain in decibel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6_3 Page No. 172"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RL= 1000.00 ohm\n",
      "RF= 500000.00 ohm\n",
      "Beta_o = 50.00\n",
      "rbe= 1000.00 ohm\n",
      "gm = 0.05  A/V\n",
      "rc= 50000.00 ohm\n",
      "part(i)\n",
      "Adm1=(-gm*RL)= -50.00\n",
      "Adm2=(0.5*gm*RL)= 25.00\n",
      "Rid=2*rbe= 2000.00 ohm\n",
      "Acm=(-RL)/(2*RF)= -1.00e-03\n",
      "Ric=Beta_o*RF= 2.50e+07 ohm\n",
      "CMRR=2*gm*RF= 50000.00\n",
      "part(ii)\n",
      "Vi1= -5.00e-04  volts\n",
      "Vi2= 5.00e-04  volts\n",
      "Vcm= 0.01  volts\n",
      "Vd=Vi1-Vi2= -1.00e-03  volts\n",
      "Vod=abs(Vd*Adm2)= 0.03  volts\n",
      "Voc=abs(Vcm*Acm)= 1.00e-05  volts\n",
      "percentage error=(Voc/Vod)*100= 0.04 %\n",
      "part(iii)\n",
      "RLeff=(RL*Rid)/(RL+Rid)= 666.67 ohm\n",
      "Adm=gm*RLeff= 33.33\n",
      "Acm=(-RLeff)/(2*RF)= -6.67e-04\n",
      "CMRR=abs(Adm/(Acm))= 50000.00\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division  \n",
    "RL=1*10**(3)\n",
    "print \"RL= %0.2f\"%(RL)+ \" ohm\"  #Load resistance\n",
    "RF=500*10**(3)\n",
    "print \"RF= %0.2f\"%(RF)+ \" ohm\"  #Feedback resistance\n",
    "Beta_o=50\n",
    "print \"Beta_o = %0.2f\"%(Beta_o) #BJT gain\n",
    "rbe=1*10**(3)\n",
    "print \"rbe= %0.2f\"%(rbe)+ \" ohm\"  #Base-emitter resistance\n",
    "gm=50*10**(-3)\n",
    "print \"gm = %0.2f\"%(gm),\" A/V\"#  transconductance for BJT \n",
    "rc=50*10**(3)\n",
    "print \"rc= %0.2f\"%(rc)+ \" ohm\"  #collector resistance\n",
    "print \"part(i)\"\n",
    "Adm1=(-gm*RL)\n",
    "print \"Adm1=(-gm*RL)= %0.2f\"%(Adm1) # Differential mode gain for BJT for DIDO and SIDO modes\n",
    "Adm2=(0.5*gm*RL)\n",
    "print \"Adm2=(0.5*gm*RL)= %0.2f\"%(Adm2) # Differential mode gain for BJT for DISO and SISO modes\n",
    "Rid=2*rbe\n",
    "print \"Rid=2*rbe= %0.2f\"%(Rid)+ \" ohm\"  #input differential mode resistance\n",
    "Acm=(-RL)/(2*RF)\n",
    "print \"Acm=(-RL)/(2*RF)= %0.2e\"%(Acm) # Common mode gain for BJT for DISO and SISO modes\n",
    "Ric=Beta_o*RF\n",
    "print \"Ric=Beta_o*RF= %0.2e\"%(Ric)+ \" ohm\"  # common mode input resistance\n",
    "CMRR=2*gm*RF\n",
    "print \"CMRR=2*gm*RF= %0.2f\"%(CMRR)  # common mode rejection ratio\n",
    "print \"part(ii)\"\n",
    "Vi1=(-0.5)*10**(-3)\n",
    "print \"Vi1= %0.2e\"%(Vi1),\" volts\" # input voltage1 \n",
    "Vi2=(+0.5)*10**(-3)\n",
    "print \"Vi2= %0.2e\"%(Vi2),\" volts\" # input voltage2\n",
    "Vcm=(10)*10**(-3)\n",
    "print \"Vcm= %0.2f\"%(Vcm),\" volts\" # common mode voltage\n",
    "Vd=Vi1-Vi2\n",
    "print \"Vd=Vi1-Vi2= %0.2e\"%(Vd),\" volts\" # differential voltage\n",
    "Vod=abs(Vd*Adm2)\n",
    "print \"Vod=abs(Vd*Adm2)= %0.2f\"%(Vod),\" volts\" # output differential voltage for DISO and SISO modes\n",
    "Voc=abs(Vcm*Acm)\n",
    "print \"Voc=abs(Vcm*Acm)= %0.2e\"%(Voc),\" volts\" # output common mode  voltage\n",
    "Error=(Voc/Vod)*100\n",
    "print \"percentage error=(Voc/Vod)*100= %0.2f\"%(Error),\"%\"#percentage error due to CM signal\n",
    "print \"part(iii)\"\n",
    "RLeff=(RL*Rid)/(RL+Rid)\n",
    "print \"RLeff=(RL*Rid)/(RL+Rid)= %0.2f\"%(RLeff)+ \" ohm\"  # Effective load resistance\n",
    "Adm=gm*RLeff\n",
    "print \"Adm=gm*RLeff= %0.2f\"%(Adm) # Modified Differential mode gain for BJT for DIDO and SIDO modes\n",
    "Acm=(-RLeff)/(2*RF)\n",
    "print \"Acm=(-RLeff)/(2*RF)= %0.2e\"%(Acm) # Modified Common mode gain for BJT for DISO and SISO modes\n",
    "CMRR=abs(Adm/(Acm))\n",
    "print \"CMRR=abs(Adm/(Acm))= %0.2f\"%(CMRR)  # Modified common mode rejection ratio\n",
    "#NOTE:  In Book, Formulae used for Acm in part(iii) is written as Acm=(-RL)/(2*RF)but ans is calculated by using RLeff in place of RL.So i have written formulae as Acm=(-RLeff)/(2*RF) in programming.\n",
    "# Assigned variable name: in part(i) Adm for DIDO and SIDO modes is represented by Adm1 and Adm for DISO and SISO modes is represented by Adm2 to resist any anamoly in the programming."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6_4 Page No. 175"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "VCC= 10.00  volts\n",
      "VEE=VCC= 10.00  volts\n",
      "IQ = 0.00  ampere\n",
      "VBE= 0.70  volts\n",
      "part(i)\n",
      "RL=VCC/IQ= 5000.00 ohm\n",
      "Pomax=VCC**2/(2*RL)= 0.01  W\n",
      "PDC=2*VCC*IQ= 0.04  W\n",
      "Efficiency,Etta_max=(Pomax/PDC)*100= 25.00 %\n",
      "PDmax=VCC*IQ= 0.02  W\n",
      "part(ii)\n",
      "Vcm= 5.00  volts\n",
      "Po=Vcm**2/(2*RL)= 2.50e-03  W\n",
      "Efficiency,Etta=(Po/PDC)*100= 6.25  %\n",
      "PDCavg=PDmax-Po= 0.02  W\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division  \n",
    "VCC=(10)\n",
    "print \"VCC= %0.2f\"%(VCC),\" volts\" # Collector voltage supply\n",
    "VEE=VCC\n",
    "print \"VEE=VCC= %0.2f\"%(VEE),\" volts\" # Emitter supply voltage\n",
    "IQ=2*10**(-3)\n",
    "print \"IQ = %0.2f\"%(IQ),\" ampere\" #  operating current for CC class-Aamplifier\n",
    "VBE=(0.7)\n",
    "print \"VBE= %0.2f\"%(VBE),\" volts\" # Base-emitter voltage \n",
    "print \"part(i)\"\n",
    "RL=VCC/IQ\n",
    "print \"RL=VCC/IQ= %0.2f\"%(RL)+ \" ohm\"  #Load resistance\n",
    "Pomax=VCC**2/(2*RL)\n",
    "print \"Pomax=VCC**2/(2*RL)= %0.2f\"%(Pomax),\" W\" # maximum Output power \n",
    "PDC=2*VCC*IQ\n",
    "print \"PDC=2*VCC*IQ= %0.2f\"%(PDC),\" W\" # Total D.C power supply\n",
    "Etta_max=(Pomax/PDC)*100\n",
    "print \"Efficiency,Etta_max=(Pomax/PDC)*100= %0.2f\"%(Etta_max),\"%\" #maximum power amplifier conversion efficiency\n",
    "PDmax=VCC*IQ\n",
    "print \"PDmax=VCC*IQ= %0.2f\"%(PDmax),\" W\" # maximum power dissipation \n",
    "print \"part(ii)\"\n",
    "Vcm=(5)\n",
    "print \"Vcm= %0.2f\"%(Vcm),\" volts\" # common mode voltage\n",
    "Po=Vcm**2/(2*RL)\n",
    "print \"Po=Vcm**2/(2*RL)= %0.2e\"%(Po),\" W\" # Output power \n",
    "Etta=(Po/PDC)*100\n",
    "print \"Efficiency,Etta=(Po/PDC)*100= %0.2f\"%(Etta),\" %\" # power amplifier conversion efficiency\n",
    "PDCavg=PDmax-Po#Using law of conservation of energy\n",
    "print \"PDCavg=PDmax-Po= %0.2f\"%(PDCavg),\" W\" # Average power dissipated in BJT"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6_5  Page No. 178"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "VCC= 10.00  volts\n",
      "VEE=VCC= 10.00  volts\n",
      "ICQ_0 = 1.00e-02  ampere\n",
      "RL= 5.00 ohm\n",
      "part(i)\n",
      "Po=0.00  W\n",
      "PDC=2*VCC*ICQ_0= 0.20  W\n",
      "part(ii)\n",
      "Vcm=VCC = 10.00  volts\n",
      "Icm = VCC/RL=2.00e+00  ampere\n",
      "Po=(1/2)*(Icm*Vcm)=10.00  W\n",
      "ICavg=(Icm)/(pi)=6.37e-01  ampere\n",
      "PDC=2*VCC*ICavg= 12.73  W\n",
      "Efficiency,Etta=(Po/PDC)*100= 78.54  %\n",
      "part(iii)\n",
      "Vcm1= 5.00  volts\n",
      "ICavg1=(Vcm1)/(pi*RL)=3.18e-01  ampere\n",
      "Po1=(Vcm1**2)/(2*RL)=2.50  W\n",
      "PDC1=2*VCC*ICavg1= 6.37e+00  W\n",
      "Efficiency,Etta=(Po1/PDC1)*100= 39.27  %\n"
     ]
    }
   ],
   "source": [
    "from math import pi,sqrt\n",
    "from __future__ import division  \n",
    "VCC=(10)\n",
    "print \"VCC= %0.2f\"%(VCC),\" volts\" # Collector voltage supply\n",
    "VEE=VCC\n",
    "print \"VEE=VCC= %0.2f\"%(VEE),\" volts\" # Emitter supply voltage\n",
    "ICQ_0=10*10**(-3)\n",
    "print \"ICQ_0 = %0.2e\"%(ICQ_0),\" ampere\" #  Zero signal collector current\n",
    "RL=5\n",
    "print \"RL= %0.2f\"%(RL)+ \" ohm\"  #Load resistance\n",
    "print \"part(i)\"\n",
    "Po=0# Since Output power at Zero signal condition is Zero\n",
    "print \"Po=%0.2f\"%(Po),\" W\" # Output power at Zero signal condition\n",
    "PDC=2*VCC*ICQ_0\n",
    "print \"PDC=2*VCC*ICQ_0= %0.2f\"%(PDC),\" W\" # Total D.C power supply for Zero signal condition\n",
    "print \"part(ii)\"\n",
    "Vcm=VCC#For Full output voltage swing Vcm=VCC\n",
    "print \"Vcm=VCC = %0.2f\"%(Vcm),\" volts\" # common mode voltage for full swing condition\n",
    "Icm=VCC/RL\n",
    "print \"Icm = VCC/RL=%0.2e\"%(Icm),\" ampere\" # common mode current\n",
    "Po=(1/2)*(Icm*Vcm)\n",
    "print \"Po=(1/2)*(Icm*Vcm)=%0.2f\"%(Po),\" W\" # Output power at full swing condition\n",
    "ICavg=(Icm)/(pi)\n",
    "print \"ICavg=(Icm)/(pi)=%0.2e\"%(ICavg),\" ampere\" #  Average value of common mode current\n",
    "PDC=2*(ICavg*VCC)\n",
    "print \"PDC=2*VCC*ICavg= %0.2f\"%(PDC),\" W\" # Total D.C power supply for full swing condition\n",
    "Etta=(Po/PDC)*100\n",
    "print \"Efficiency,Etta=(Po/PDC)*100= %0.2f\"%(Etta),\" %\" # power amplifier conversion efficiency\n",
    "print \"part(iii)\"\n",
    "Vcm1=(5)#given value\n",
    "print \"Vcm1= %0.2f\"%(Vcm1),\" volts\" # common mode voltage for output swing Vcm=5 V\n",
    "ICavg1=(Vcm1)/(pi*RL)\n",
    "print \"ICavg1=(Vcm1)/(pi*RL)=%0.2e\"%(ICavg1),\" ampere\" #  Average value of common mode current\n",
    "Po1=(Vcm1**2)/(2*RL)\n",
    "print \"Po1=(Vcm1**2)/(2*RL)=%0.2f\"%(Po1),\" W\" # Output power for output swing Vcm=5 V\n",
    "PDC1=2*(ICavg1*VCC)\n",
    "print \"PDC1=2*VCC*ICavg1= %0.2e\"%(PDC1),\" W\" # Total D.C power supply for  output swing Vcm=5 V\n",
    "Etta=(Po1/PDC1)*100\n",
    "print \"Efficiency,Etta=(Po1/PDC1)*100= %0.2f\"%(Etta),\" %\" # power amplifier conversion efficiency for output swing Vcm=5 V\n",
    "# NOTE:Correct value of Efficiency,Etta=(Po1/PDC1)*100= 39.269908 % for part(iii) but book ans is 39.31%(because of approximation used during calculation)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6_6 Page No. 180"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Av= 100000.00\n",
      "VCC= 10.00  volts\n",
      "vo= VCC=10.00  volts\n",
      "Vdmax= VCC/Av=1.00e-04  volts\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division  \n",
    "Av=1*10**(5)\n",
    "print \"Av= %0.2f\"%(Av) #Voltage gain\n",
    "VCC=(10)\n",
    "print \"VCC= %0.2f\"%(VCC),\" volts\" # Collector voltage supply\n",
    "vo=VCC\n",
    "print \"vo= VCC=%0.2f\"%(vo),\" volts\" # maximum output voltage\n",
    "Vdmax=VCC/Av\n",
    "print \"Vdmax= VCC/Av=%0.2e\"%(Vdmax),\" volts\" # Difference input voltage at OP-amp terminals"
   ]
  }
 ],
 "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
}