{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 9 Phase Locked Loop"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 9.1 Pg 284"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The output voltage of switching regulator circuit is = -0.30  V \n",
      "The output voltage of switching regulator circuit is = 1.50  V \n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt, pi\n",
    "# to find output voltage for a constant input signal frequency of 200 KHz\n",
    "fo = 2*pi*1*10**3 # # KHz/V  # VCO sensitivity range 4.1\n",
    "fc = 500  # # Hz a free running frequency\n",
    "f1 = 200 # # Hz  input frequency\n",
    "f2 = 2*10**3 # # Hz  input frequency\n",
    "\n",
    "# the output voltage of PLL is defined as\n",
    "#Vo = (wo-wc)/ko\n",
    "ko = fo #\n",
    "# when i/p locked with o/p wo=wi\n",
    "# Vo = (wi-wc)/ko #\n",
    "\n",
    "#for the i/p frequency fi = 200 Hz\n",
    "fi = 200 #  # Hz\n",
    "Vo = (((2*pi*fi)-(2*pi*fc))/ko)#\n",
    "print 'The output voltage of switching regulator circuit is = %0.2f'%Vo,' V '\n",
    "\n",
    "#for the i/p frequency fi = 200 Hz\n",
    "fi = 2*10**3 #  # Hz\n",
    "Vo = (((2*pi*fi)-(2*pi*fc))/ko)#\n",
    "print 'The output voltage of switching regulator circuit is = %0.2f'%Vo,' V '"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 9.2 Pg 285"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The sum frequency produce by phase detector is = 900.00  KHz \n",
      "The difference frequency produce by phase detector is = 100.00  KHz \n",
      "The phase detector frequencies are outside of the low pass filter\n",
      "The VCO will be in its free running frequency \n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# to find VCO output frequency\n",
    "fc = 400  # # KHz a free running frequency\n",
    "f = 10 #  # KHz  low pass filter bandwidth\n",
    "fi = 500 # # KHz  input frequency\n",
    "\n",
    "# In PLL a phase detector produces the sum and difference frequencies are defined as\n",
    "\n",
    "sum = fi+fc #\n",
    "print 'The sum frequency produce by phase detector is = %0.2f'%sum,' KHz '\n",
    "\n",
    "difference = fi-fc #\n",
    "print 'The difference frequency produce by phase detector is = %0.2f'%difference,' KHz '\n",
    "\n",
    "print 'The phase detector frequencies are outside of the low pass filter'#\n",
    "\n",
    "print 'The VCO will be in its free running frequency '"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 9.3 Pg 286"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The sensitivity of phase detector Kd is = 0.45 \n",
      "The maximum control voltage of VCO Vfmax = 1.40  V\n",
      "The maximum frequency swing of VCO = 35.00  KHz\n",
      "The maximum range of frequency which lock a PLL is = 15.00  KHz \n",
      "The maximum range of frequency which lock a PLL is = 85.00  KHz \n",
      "The maximum and minimum rage between 15 KHz to 85 KHZ \n",
      "The lock range is = 70.00  KHz \n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt, pi\n",
    "# to determine the lock range of PLL\n",
    "Ko = 25 # # KHz\n",
    "fo = 50 # # KHz\n",
    "A = 2 #\n",
    "Vd = 0.7 #\n",
    "AL = 1 #\n",
    "\n",
    "# the amximum output swing of phase detector \n",
    "# Vd = Kd*(pi/2) #\n",
    "\n",
    "# the sensitivity of phase detector Kd is\n",
    "Kd = Vd*(2/pi) #\n",
    "print 'The sensitivity of phase detector Kd is = %0.2f'%Kd,''\n",
    "\n",
    "# The maximum control voltage of VCO Vfmax\n",
    "Vfmax = (pi/2)*Kd*A #\n",
    "print 'The maximum control voltage of VCO Vfmax = %0.2f'%Vfmax,' V'\n",
    "\n",
    "# the maximum frequency swing of VCO\n",
    "fL = (Ko*Vfmax)#\n",
    "print 'The maximum frequency swing of VCO = %0.2f'%fL,' KHz'\n",
    "\n",
    "# The maximum range of frequency which lock a PLL are\n",
    "fi = fo-fL #\n",
    "print 'The maximum range of frequency which lock a PLL is = %0.2f'%fi,' KHz '\n",
    "\n",
    "fi = fo+fL #\n",
    "print 'The maximum range of frequency which lock a PLL is = %0.2f'%fi,' KHz '\n",
    "\n",
    "print 'The maximum and minimum rage between 15 KHz to 85 KHZ '\n",
    "\n",
    "\n",
    "# the lock range is\n",
    "fLock = 2*fL #\n",
    "print 'The lock range is = %0.2f'%fLock,' KHz '"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 9.4 Pg 286"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The current through the control resistor R is = 0.60  mA \n",
      "The charging time of capacitor is = 5.00  msec \n",
      "The total time period of tringular and square wave is = 10.00  msec \n",
      "The output frequency of VCO is = 0.10  KHz \n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# to determine the output frequency capacitor charging time of VCO\n",
    "Vcc = 12 #\n",
    "Vcs = 6\n",
    "R = 10 # # K ohm\n",
    "C = 1 # # uF\n",
    "\n",
    "# the current through the control resistor R\n",
    "i =(Vcc-Vcs)/R #\n",
    "print 'The current through the control resistor R is = %0.2f'%i, ' mA '\n",
    "\n",
    "# The charging time of capacitor \n",
    "t = (0.25*Vcc*C)/i #\n",
    "print 'The charging time of capacitor is = %0.2f'%t, ' msec '\n",
    "\n",
    "# In VCO the capacitor charging and discharging time period are equal ,so the total time period of tringular and square wave forms can be written as 2*t #\n",
    "t = ((0.5*Vcc*C)/i)#\n",
    "print 'The total time period of tringular and square wave is = %0.2f'%t, ' msec '\n",
    "\n",
    "# the output frequency of VCO is\n",
    "fo = 1/t #\n",
    "print 'The output frequency of VCO is = %0.2f'%fo, ' KHz '"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 9.5 Pg 287"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The charging or discharging time of capacitor is = 25.00  msec \n",
      "The output frequency of VCO is is = 20.00  Hz \n",
      "The output frequency of VCO is = 625.00  Kohm\n",
      "The current through the control resistor R is = 1.60  uA \n",
      "The capacitor charging current is = 2000.00  V = 0.33Vcc \n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt, pi\n",
    "# to design VCO with output square wave pulse time of 50 msec\n",
    "Vcc =6 #\n",
    "Vcs = 5 #\n",
    "R = 22 # #K ohm\n",
    "C = 0.02 # # uF\n",
    "t = 50*10**-3 # # sec   output square wave pluse\n",
    "\n",
    "# In VCO the capacitor charging and discharging time period are equal ,so the total time period of tringular and square wave forms can be written as 2*t #\n",
    "\n",
    "\n",
    "# the charging or discharging time of capacitor \n",
    "tcap = t/2*1e3 #\n",
    "print 'The charging or discharging time of capacitor is = %0.2f'%tcap, ' msec '\n",
    "\n",
    "# the output frequency of VCO is\n",
    "fo = 1/t #\n",
    "print 'The output frequency of VCO is is = %0.2f'%fo, ' Hz '\n",
    "\n",
    "# the output frequency of VCO\n",
    " # fo = (1/4*R*C)#\n",
    "R = 1/(4*fo*1e3*C*1e-9)/1e3 # Kohm\n",
    "print 'The output frequency of VCO is = %0.2f'%R, ' Kohm'\n",
    "\n",
    "# the current through the control resistor R\n",
    "i =(Vcc-Vcs)/R*1e3 #\n",
    "print 'The current through the control resistor R is = %0.2f'%i, ' uA '\n",
    "\n",
    "# the capacitor charging current \n",
    "# (V/t)=(i/C) #\n",
    "V = (i/C)*tcap #\n",
    "print 'The capacitor charging current is = %0.2f'%V, ' V = 0.33Vcc '"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 9.6 Pg 289"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The center frequency of VCO is is = 0.17  kHz \n",
      "The lock range of PLL is = 2.67  KHz/V \n",
      "The lock range of PLL is = 25.59 k Hz/V \n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt, pi\n",
    "# to determine the center frequency of VCO lock and capture range of PLL\n",
    "R = 15 # # K ohm\n",
    "C = 0.12 # # uF\n",
    "Vcc = 12 #\n",
    "\n",
    "# the center frequency of VCO fo\n",
    "fo = (1.2/4/(R*1e3)/(C*1e-6))/1e3#\n",
    "print 'The center frequency of VCO is is = %0.2f'%fo, ' kHz '\n",
    "\n",
    "fo = 4 # # KHz\n",
    "# the lock range of PLL\n",
    "fL = (8*fo/Vcc) #\n",
    "print 'The lock range of PLL is = %0.2f'%fL, ' KHz/V '\n",
    "\n",
    "# the capture range of PLL\n",
    "fc = ((fo-fL)/(2*pi*3.6*10**3*C*1e-6)**(1/2)) #\n",
    "print 'The lock range of PLL is = %0.2f'%fc, 'k Hz/V '\n",
    "# ans wrong in the textbook."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 9.7 Pg 290"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The total time period of VCO is = 5.00  usec \n",
      "The charging or discharging time of capacitor is = 2.50  usec \n",
      "The voltage swing of VCO for 12 V supply is = 3.00  V \n",
      "The lock range of PLL FL is = 0.955  Hz \n",
      "The capture range is = 437.02  Hz \n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import sqrt, pi\n",
    "# determine the lock range of the FSK demodulator\n",
    "Vcc = 12 #\n",
    "Fvco = 0.25*Vcc #\n",
    "f = 200*10**3 # # Hz\n",
    "\n",
    "\n",
    "# the total time period of VCO \n",
    "t = 1/f*1e6 #\n",
    "print 'The total time period of VCO is = %0.2f'%t, ' usec '\n",
    "\n",
    "# In VCO the capacitor charging and discharging time period are equal ,so the total time period of tringular and square wave forms can be written as 2*t #\n",
    "\n",
    "\n",
    "# the charging or discharging time of capacitor \n",
    "tcap = t/2 #\n",
    "print 'The charging or discharging time of capacitor is = %0.2f'%tcap, ' usec '\n",
    "\n",
    "# the voltage swing of VCO for 12 V supply\n",
    "Fvco = 0.25*Vcc #\n",
    "print 'The voltage swing of VCO for 12 V supply is = %0.2f'%Fvco, ' V '\n",
    "\n",
    "# The lock range of PLL \n",
    "#FL = (1/2*pi*f)*(Fvco/tcap)#\n",
    "FL = (3/(2*pi*f*tcap*1e-6))#\n",
    "print 'The lock range of PLL FL is = %0.3f'%FL, ' Hz '\n",
    "\n",
    "# the capture range \n",
    "fcap = sqrt(f*FL)#\n",
    "print 'The capture range is = %0.2f'%fcap, ' Hz '"
   ]
  }
 ],
 "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
}