{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 9 Oscillators"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 9_1 PG-9.13"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " Therefore frequency of oscillation is 29.414 Hz \n",
      "\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt, pi\n",
    "R=4.7e3 #  each resistance of the RC phase shift oscillator\n",
    "C=0.47e-6 #  each capacitance of the RC phase shift oscillator\n",
    "f=1/(2*pi*sqrt(6)*R*C) #  \n",
    "print \"\\n Therefore frequency of oscillation is %.3f Hz \\n\"%(f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 9_2 PG-9.13"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " each resistance of the RC phase shift oscillator is 72.194 kohm \n",
      "\n",
      "\n",
      " R1=1 kohm \n",
      "\n",
      "\n",
      " Rf=29 kohm \n",
      "\n",
      "the design circuit is shown \n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt, pi\n",
    "f=900e3 #  frequency of oscillation\n",
    "C=1e-12 #  each capacitance of the RC phase shift oscillator\n",
    "R=1/(2*pi*sqrt(6)*f*C) #  \n",
    "print \"\\n each resistance of the RC phase shift oscillator is %.3f kohm \\n\"%(R*1e-3)\n",
    "G=29 #  opamp gain Rf/R1=29\n",
    "R1=1e3 #  \n",
    "print \"\\n R1=%.0f kohm \\n\"%(R1*1e-3)\n",
    "Rf=G*R1 #  \n",
    "print \"\\n Rf=%.0f kohm \\n\"%(Rf*1e-3)\n",
    "print \"the design circuit is shown \""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 9_3 PG-9.14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "  each resistance of the RC phase shift oscillator is 649.747 ohm \n",
      "\n",
      " The standard value of R=680 ohm\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt, pi\n",
    "f=1e3 #  frequency of oscillation\n",
    "C=0.1e-6 #  We choose the value of each capacitance of the RC phase shift oscillator\n",
    "R=1/(2*pi*sqrt(6)*f*C) #  \n",
    "print \"\\n  each resistance of the RC phase shift oscillator is %.3f ohm \\n\"%(R)\n",
    "print \" The standard value of R=680 ohm\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 9_4 PG-9.14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " Therefore frequency of oscillation is 129.949 Hz \n",
      "\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt, pi\n",
    "R=5e3 #  each resistance of the RC phase shift oscillator\n",
    "C=0.1e-6 #  each capacitance of the RC phase shift oscillator\n",
    "f=1/(2*pi*sqrt(6)*R*C) #  \n",
    "print \"\\n Therefore frequency of oscillation is %.3f Hz \\n\"%(f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 9_5 PG-9.20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "We know that for a colpitts oscillator f=1/(2*pi*sqrt(L*Ceq))\n",
      "\n",
      " Ceq = 1.01321 nF \n",
      "\n",
      "\n",
      " Therefore C = 2.02642 nF \n",
      "\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt, pi\n",
    "L=100e-6 #  \n",
    "f=500e3 #  \n",
    "print \"We know that for a colpitts oscillator f=1/(2*pi*sqrt(L*Ceq))\"\n",
    "Ceq=1/(f**2*4*pi**2*L)\n",
    "Ceq1=Ceq*1e9 #  \n",
    "print \"\\n Ceq = %.5f nF \\n\"%(Ceq1)\n",
    " #  C1=C2=C\n",
    "C=Ceq1*2 #  Ceq=(C*C)/(C+C)\n",
    "print \"\\n Therefore C = %.5f nF \\n\"%(C)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 9_6 PG-9.20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " Therefore frequency of oscillation is 1.927 MHz \n",
      "\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt, pi\n",
    "L=50e-6 #  \n",
    "C1=150e-12 #  \n",
    "C2=1.5e-9 #  \n",
    "Ceq=(C1*C2)/(C1+C2) #  \n",
    "f=1/(2*pi*sqrt(L*Ceq)) #  \n",
    "f=f*1e-6 #  \n",
    "print \"\\n Therefore frequency of oscillation is %.3f MHz \\n\"%(f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 9_7 PG-9.21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " Therefore L=202.642 micro H \n",
      "\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt, pi\n",
    "C=1000e-12 #  \n",
    "C1=C #  \n",
    "C2=C #  \n",
    "f=500e3 #  \n",
    "Ceq=(C1*C2)/(C1+C2) #  \n",
    "L=1/(4*pi**2*f**2*Ceq) #  since f=1/(2*pi*sqrt(L*Ceq)) #  \n",
    "L=L*1e6 #  \n",
    "print \"\\n Therefore L=%.3f micro H \\n\"%(L)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 9_8 PG-9.21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " Therefore inductor L = 7.6 microH \n",
      "\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt, pi\n",
    "C1=100e-12 #  \n",
    "C2=50e-12 #  \n",
    "f=10e6 #  \n",
    "Ceq=(C1*C2)/(C1+C2) #  \n",
    "L=1/(4*pi**2*f**2*Ceq) #  f=1/(2*pi*sqrt(L*Ceq)) #  \n",
    "L=L*1e6 #  \n",
    "print \"\\n Therefore inductor L = %.1f microH \\n\"%(L)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 9_9 PG-9.24"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " Therefore frequency of oscillation is 9189 Hz \n",
      "\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt, pi\n",
    "L1=0.5e-3 #  \n",
    "L2=1e-3 #  \n",
    "C=0.2e-6 #  \n",
    "Leq=L1+L2 #  total inductance  for Hartley oscillator\n",
    "f=1/(2*pi*sqrt(Leq*C)) #  \n",
    "print \"\\n Therefore frequency of oscillation is %.f Hz \\n\"%(f)\n",
    "#  there is a slight difference between the answer given in the book\n",
    "#  and the and output in the book they have taken the approximate value "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 9_10 PG-9.24"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "For f=fmax=2050kHz\n",
      "\n",
      " C=2.98 pF \n",
      "\n",
      "\n",
      " For f=fmin=950kHz\n",
      "\n",
      " C=13.89 pF \n",
      "\n",
      "\n",
      " Hence C must be varied between 2.98 pF and 13.89 pF \n",
      "\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt, pi\n",
    "L1=2e-3 #  \n",
    "L2=20e-6 #  \n",
    "Leq=L1+L2 #  total inductance  for Hartley oscillator\n",
    "fmax=2050e3 #  maximum frequency\n",
    "fmin=950e3 #  minimum frequency\n",
    "print \"For f=fmax=2050kHz\"\n",
    "f=fmax #  \n",
    "C=1/(4*pi**2*f**2*Leq) #  since f=1/(2*pi*sqrt(Leq*C)) #  \n",
    "C=C*1e12\n",
    "print \"\\n C=%.2f pF \\n\"%(C)\n",
    "print \"\\n For f=fmin=950kHz\"\n",
    "f=fmin #  \n",
    "C1=1/(4*pi**2*f**2*Leq) #  since f=1/(2*pi*sqrt(Leq*C)) #  \n",
    "C1=C1*1e12\n",
    "print \"\\n C=%.2f pF \\n\"%(C1)\n",
    "print \"\\n Hence C must be varied between %.2f pF and %.2f pF \\n\"%(C,C1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 9_11 PG-9.25"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "For f=fmax=2.5MHz\n",
      "\n",
      " C=2.006 pF \n",
      "\n",
      "\n",
      " For f=fmin=1MHz\n",
      "\n",
      " C=12.540 pF \n",
      "\n",
      "\n",
      " Hence C must be varied between 2.006 pF and 12.54 pF \n",
      "\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt, pi\n",
    "L1=20e-6 #  \n",
    "L2=2e-3\n",
    "Leq=L1+L2 #  total inductance  for Hartley oscillator\n",
    "fmax=2.5e6 #  maximum frequency\n",
    "fmin=1e6 #  minimum frequency\n",
    "print \"For f=fmax=2.5MHz\"\n",
    "f=fmax #  \n",
    "C=1/(4*pi**2*f**2*Leq) #  since f=1/(2*pi*sqrt(Leq*C)) #  \n",
    "C=C*1e12\n",
    "print \"\\n C=%.3f pF \\n\"%(C)\n",
    "print \"\\n For f=fmin=1MHz\"\n",
    "f=fmin #  \n",
    "C1=1/(4*pi**2*f**2*Leq) #  since f=1/(2*pi*sqrt(Leq*C)) #  \n",
    "C1=C1*1e12\n",
    "print \"\\n C=%.3f pF \\n\"%(C1)\n",
    "print \"\\n Hence C must be varied between %.3f pF and %.2f pF \\n\"%(C,C1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 9_12 PG-9.32"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " series resonant frequency for crystal oscillator fs=0.863 MHz \n",
      "\n",
      "\n",
      " parallel resonant frequency for crystal oscillator=0.899 MHz \n",
      "\n",
      "\n",
      " increase in parallel frequency fp=4.163 % \n",
      "\n",
      "\n",
      " Therefore Q factor=433.861 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt, pi\n",
    "L=0.4 #  \n",
    "C=0.085e-12 #  \n",
    "R=5e3 #  \n",
    "Cm=1e-12 #  \n",
    "f=1/(2*pi*sqrt(L*C)) #  series resonant frequency for crystal oscillator\n",
    "print \"\\n series resonant frequency for crystal oscillator fs=%.3f MHz \\n\"%(f*1e-6)\n",
    "Ceq=C*Cm/(C+Cm) #  \n",
    "fp=1/(2*pi*sqrt(L*Ceq)) #  parallel resonant frequency for crystal oscillator\n",
    "print \"\\n parallel resonant frequency for crystal oscillator=%.3f MHz \\n\"%(fp*1e-6)\n",
    "increase=(fp-f)/f*100 #  \n",
    "print \"\\n increase in parallel frequency fp=%.3f %% \\n\"%(increase)\n",
    "w=2*pi*f #  \n",
    "Q=w*L/R #  Q factor\n",
    "print \"\\n Therefore Q factor=%.3f \\n\"%(Q)\n",
    "#  in the book fs=0.856MHz is wrong,correct answer is fs=.863MHz\n",
    "#  in the book 1Jncrease=5.023% is wrong the correct answer is 1Jncrease=4.163%\n",
    "#  in the Q=430.272  which is wrong the correct answer is Q=433.861"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 9_13 PG-9.32"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " series resonant frequency for crystal oscillator fs=1.125 MHz \n",
      "\n",
      "\n",
      " parallel resonant frequency for crystal oscillator=1.128 MHz \n",
      "\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt, pi\n",
    "C=0.01e-12 #  \n",
    "Cm=2e-12 #  \n",
    "L=2 #  \n",
    "R=2e3 #  \n",
    "fs=1/(2*pi*sqrt(L*C)) #  series resonant frequency for crystal oscillator\n",
    "print \"\\n series resonant frequency for crystal oscillator fs=%.3f MHz \\n\"%(fs*1e-6)\n",
    "Ceq=C*Cm/(C+Cm) #  \n",
    "fp=1/(2*pi*sqrt(L*Ceq)) #  parallel resonant frequency for crystal oscillator\n",
    "print \"\\n parallel resonant frequency for crystal oscillator=%.3f MHz \\n\"%(fp*1e-6)"
   ]
  }
 ],
 "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
}
