{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 7 : Noise"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example7.2, page no 276"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The mean square noise voltage is: 18.363 mV\n"
     ]
    }
   ],
   "source": [
    "from numpy import sqrt,pi\n",
    "from mpmath  import quad\n",
    "#Given\n",
    "mue=25#\n",
    "rp=5e3\n",
    "Rl=10e3\n",
    "C=1e-9\n",
    "gm=mue/rp\n",
    "Req=2.5/gm\n",
    "\n",
    "k=1.381e-23\n",
    "T=293\n",
    "R1=1e5\n",
    "# Power density spectrum for respective res\n",
    "d1=2*k*T*R1\n",
    "d2=2*k*T*Req\n",
    "d3=2*k*T*Rl\n",
    "xo=0\n",
    "x1=1e14\n",
    "#w=0:inf\n",
    "#H1(w)=(-gm*rp*Rl)/(rp+Rl+(1J*w*rp*Rl*C))\n",
    "Vo=sqrt((20231.65e2/pi)*quad(lambda w:1/(((3e9)**2)+(w**2)),[xo,x1]))\n",
    "print 'The mean square noise voltage is: %0.3f mV'%(Vo*1e3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example7.3, page no 279"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The mean square noise voltage is: 22.414 uV\n"
     ]
    }
   ],
   "source": [
    "from numpy import sqrt,pi\n",
    "from mpmath  import quad\n",
    "\n",
    "#Given\n",
    "mue=25\n",
    "rp=5e3\n",
    "Rs=1e3#input resistance\n",
    "#Coupling Capacitors are assumed as short circuit\n",
    "Rg=1e5\n",
    "gm=25/5e3\n",
    "Req=2.5/gm\n",
    "F=1+((((Req*(Rs+Rg)**2)+(Rg*Rs**2))/(Rs*Rg**2)))\n",
    "xo=0\n",
    "x1=1e10\n",
    "#w=0:inf\n",
    "\n",
    "vo=sqrt((30145e-8/pi)*quad(lambda w:1/(((3e5)**2)+(w**2)),[xo,x1]))\n",
    "print 'The mean square noise voltage is: %.3f uV'%(vo*1e6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example7.4, page no 283"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "overall noise Figure is: 4.33\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "#Given\n",
    "Ap1=10\n",
    "Ap2=10\n",
    "Ap3=10#  # Gain of each states\n",
    "F_1=6\n",
    "F_2=6\n",
    "F_3=6#   #Noise figure of each state\n",
    "F1= round(10**(F_1/10))\n",
    "F2= round(10**(F_2/10))\n",
    "F3= round(10**(F_3/10))#   # approximating the values\n",
    "\n",
    "F=F1+((F2-1)/Ap1)+((F3-1)/(Ap1*Ap2))\n",
    "print 'overall noise Figure is: %.2f'%(F)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example7.5, page no 283"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The overall noise figure is: 7.04\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "#Given\n",
    "Fif=15# Noise figure of IF amplifier\n",
    "Ap1=10# Gain of Preamplifier\n",
    "Fpa=6#Noise figure of preamplifier\n",
    "F2=10**(Fif/10)\n",
    "F1=10**(Fpa/10)\n",
    "\n",
    "F=F1+((F2-1)/Ap1)#overall noise figure\n",
    "print 'The overall noise figure is: %.2f'%(F)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example7.6, page no 284"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overall Noise figure is: 2.055\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "#Given\n",
    "mue=25# tube parameters\n",
    "rp=10e3# tube parameters\n",
    "gm=2.5e-3# transconductance\n",
    "Req=2.5/gm# equivalent resistance\n",
    "Rs=1000\n",
    "Rg=1e5\n",
    "F1=1+(((Req*((Rs+Rg)**2))+Rg*Rs**2)/(Rs*(Rg**2)))#noise figure of the first stage\n",
    "Rg2=9.1e3\n",
    "Rs2=10e3\n",
    "Es=1# assuming Es=1 for ease of calculation\n",
    "Pi=((Es/2e3)**2)*1e3\n",
    "Po=1.532e-2*Es**2\n",
    "Ap1=Po/Pi\n",
    "F2=1+(((Req*((Rs2+Rg2)**2))+Rg2*Rs2**2)/(Rs2*(Rg2**2)))# noise figure of the second stage\n",
    "F=(F1)+((F2-1)/Ap1)\n",
    "print 'Overall Noise figure is: %.3f'%(F)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example7.8, page no 285"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The equivalent noise temp is: 4.913 K\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "#Given\n",
    "g01=30# gain of 1st stage\n",
    "g02=20#gain of 2nd stage\n",
    "g03=40#gain of 3rd stage\n",
    "F2=6# Noise factor of stage 2\n",
    "F3=12# Noise factor of stage 3\n",
    "Te1=4# Eq noise temp of stage 1\n",
    "T=290# Room \n",
    "G01=round(10**(g01/10))\n",
    "G02=round(10**(g02/10))\n",
    "G03=round(10**(g03/10))\n",
    "F_2=round(10**(F2/10))\n",
    "F_3=round(10**(F3/10))\n",
    "Te2=round((F_2-1))*T\n",
    "Te3=round((F_3-1))*T\n",
    "Te=Te1+(Te2/G01)+(Te3/(G01*G02))# Eq overall noise temp\n",
    "print 'The equivalent noise temp is: %.3f K'%(Te)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example7.9, page no 286"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Equivalent noise temperature is 7.028 K\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "#Given\n",
    "g01=round(10**(25/10))#low noise amplifier gain\n",
    "Te1=4#low noise amplifier noise temp\n",
    "g02=round(10**(1.7))#preamplifier gain\n",
    "F2=round(10**0.6)#preamplifier noise figure\n",
    "F3=round(10**1.2)#preamplifier noise figure\n",
    "T=290# room temp\n",
    "Te2=round((F2-1)*T)\n",
    "Te3=round((F3-1)*T)\n",
    "Te=Te1+(Te2/g01)+(Te3/(g01*g02))#Overall noise Temperature\n",
    "print 'Equivalent noise temperature is %.3f K'%(Te)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example7.10, page no 286"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "S/N ratio for FM is 43.29 dBs\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import log10\n",
    "#Given\n",
    "SNRam=25# Signal to noise ratio of AM\n",
    "PcFM_AM=0.9#\n",
    "mf=5\n",
    "SNRfm=(10*log10(3*(mf**2)*(PcFM_AM)))+SNRam\n",
    "print 'S/N ratio for FM is %.2f dBs'%(SNRfm)\n",
    "# Note :  There are some calculation errors in the solution presented in the book"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example7.11, page no 287"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a)\n",
      "  New SNR for 3dB increase in input s/g is 23 dBs\n",
      "b)  When Modulation depth is increased to 60%\n",
      " SNR becomes 25.676045 dBs\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import log10\n",
    "#Given\n",
    "ma=0.3\n",
    "SNR=20# s/n ratio\n",
    "SNR1=10**(0.1*SNR)\n",
    "SNR_new=SNR+3\n",
    "ma2=0.6# increased new depth of modulation\n",
    "Pt_Ni=SNR1*((1+(ma**2))/(ma**2))\n",
    "SNR2=10*log10(Pt_Ni*((ma2**2)/(1+((ma2**2)/2))))\n",
    "\n",
    "print 'a)\\n  New SNR for 3dB increase in input s/g is %d dBs\\nb)  When Modulation depth is increased to 60%%\\n SNR becomes %f dBs'%(SNR_new,(SNR2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example7.12, page no 287"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a)  Bit Transmission rate: 60 kbits/s\n",
      "    Signal to Quantization noise ratio 128 \n",
      "b)\n",
      "  Bit Transmission rate: 5 kbits/sample\n",
      "    Signal to Quantization noise ratio: 64\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import log\n",
    "#Given\n",
    "fmax=5e3#max s/g freq\n",
    "S_fmin=2*fmax# Min sampling freq\n",
    "B_S=6#Binary bits sent per sample\n",
    "BTR=B_S*S_fmin#Bit Transmission rate\n",
    "Q=2**B_S#No of Quantizable levels\n",
    "MQN=0.5/Q#Max Quantization noise\n",
    "S_QNR=MQN**-1# Signal to Quantization noise ratio\n",
    "#b\n",
    "S_QNRreq=0.5*S_QNR# Signal to Quantization noise ratio\n",
    "Qreq=0.5*S_QNRreq#No of Quantizable levels\n",
    "B_Sreq=log(Qreq,2)#Binary bits sent per sample\n",
    "print 'a)  Bit Transmission rate: %d kbits/s\\n    Signal to Quantization noise ratio %d \\nb)\\n  Bit Transmission rate: %d kbits/sample\\n    Signal to Quantization noise ratio: %d'%(BTR/1000,S_QNR,B_Sreq,S_QNRreq)"
   ]
  }
 ],
 "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
}
