{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h1> Chpater 8: INFORMATION THEORY<h1>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.1, Page No 464"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math \n",
      "#Find Information Content of Each Symbol\n",
      "\n",
      "#Variable Declaration\n",
      "px1=1/2.0\n",
      "px2=1/4.0\n",
      "px3=1/8.0\n",
      "px4=1/8.0\n",
      "\n",
      "#Calculation\n",
      "#information content of each symbol\n",
      "Ix1=math.log(1/px1,2)\n",
      "Ix2=math.log(1/px2,2)\n",
      "Ix3=math.log(1/px3,2)\n",
      "Ix4=math.log(1/px4,2)\n",
      "\n",
      "#Result\n",
      "print(\"Information Content tI(x1)= %.2f bit\" %Ix1)\n",
      "print(\" tI(x2)= %.f bits\" %Ix2)\n",
      "print(\" tI(x3)= %.f bits\" %Ix3)\n",
      "print(\" tI(x4)= %.f bits\" %Ix4)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Information Content tI(x1)= 1.00 bit\n",
        " tI(x2)= 2 bits\n",
        " tI(x3)= 3 bits\n",
        " tI(x4)= 3 bits\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.2, Page No 464"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Find amount of Information\n",
      "#Variable Declaration\n",
      "#Calculation\n",
      "pxi=1/4.0\n",
      "Ixi=(math.log10(1/pxi))/math.log10(2)\n",
      "\n",
      "#RESULTS\n",
      "print(\"The amount of Information I(Xi)= %.f \" %Ixi)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The amount of Information I(Xi)= 2 \n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.3, Page No 464"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Find Amount of Information\n",
      "\n",
      "#Variable Declaration\n",
      "px1=1/2.0\n",
      "px2=1/2.0\n",
      "\n",
      "#Calculation\n",
      "Ix1=math.log(1/px1,2) #entropy\n",
      "Ix2=math.log(1/px2,2)\n",
      "\n",
      "#Result\n",
      "print(\"The amount of Information I(X1)= %.f bit\" %Ix1)\n",
      "print(\"The amount of Information I(X2)= %.f bit\" %Ix2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The amount of Information I(X1)= 1 bit\n",
        "The amount of Information I(X2)= 1 bit\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.4, Page No 465"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Find Amount of Information \n",
      "\n",
      "#Variable Declaration\n",
      "px1=1/4.0\n",
      "px2=3/4.0\n",
      "\n",
      "#Calculation\n",
      "Ix1=math.log(1/px1,2)\n",
      "Ix2=math.log(1/px2,2)\n",
      "\n",
      "#Result\n",
      "print(\"The amount of Information I(X1)= %.f bit\" %Ix1)\n",
      "print(\"The amount of Information I(X2)= %.2f bit\" %Ix2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The amount of Information I(X1)= 2 bit\n",
        "The amount of Information I(X2)= 0.42 bit\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.9, Page No 468 "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Find Entropy,Amount of information\n",
      "\n",
      "#Variable Declaration\n",
      "px1=0.4\n",
      "px2=0.3\n",
      "px3=0.2\n",
      "px4=0.1\n",
      "\n",
      "#Calculation\n",
      "HX=-px1*math.log(px1,2)-px2*math.log(px2,2)-px3*math.log(px3,2)-px4*math.log(px4,2)\n",
      "Px1x2x1x3=px1*px2*px1*px3\n",
      "Ix1x2x1x3=-math.log(Px1x2x1x3,2)\n",
      "Px4x3x3x2=px4*px3*px3*px2\n",
      "Ix4x3x3x2=-math.log(Px4x3x3x2,2)\n",
      "\n",
      "#Result\n",
      "print(\" \\n Entropy  H(X) = %.2f bits/symbol \" %HX)\n",
      "print(\"The amount of Information I(x1x2x1x3)= %.2f bits/symbol\" %Ix1x2x1x3)\n",
      "print(\" I(x4x3x3x2) = %.2f bits/symbol \" %Ix4x3x3x2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " \n",
        " Entropy  H(X) = 1.85 bits/symbol \n",
        "The amount of Information I(x1x2x1x3)= 6.70 bits/symbol\n",
        " I(x4x3x3x2) = 9.70 bits/symbol \n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.13, Page No 471"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "\n",
      "#Find information rate the telegraphic source\n",
      "\n",
      "#Variable Declaration\n",
      "pdash=1/3.0\n",
      "pdot=2/3.0\n",
      "tdot=0.2\n",
      "tdash=0.6\n",
      "tspace=0.2\n",
      "\n",
      "#Calculation\n",
      "HX=-pdash*math.log(pdash,2)-pdot*math.log(pdot,2)\n",
      "Ts=pdot*tdot+pdash*tdash+tspace\n",
      "r=1/Ts\n",
      "R=r*HX\n",
      "\n",
      "#Result\n",
      "print('Average rate of information R = %.2f bits/s' %R)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Average rate of information R = 1.72 bits/s\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.14, Page No 471"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Find information rate of the source\n",
      "\n",
      "f=input('Enter the frequncy f=')\n",
      "px1=1/8.0\n",
      "px2=1/8.0\n",
      "px3=3/8.0\n",
      "px4=3/8.0\n",
      "\n",
      "HX=px1*math.log(1/px1,2)+px2*math.log(1/px2,2)+px3*math.log(1/px3,2)+px4*math.log(1/px4,2) #entropy of the source\n",
      "R=2*f*HX #r=2*f\n",
      "print('information rate  R=  %.1f  bits/sec ' %R)   #f=signal bandwidth\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the frequncy f=34\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "information rate  R=  123.2  bits/sec \n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.15, Page No 472"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Find information rate of the source\n",
      "#all symbols are equally likely\n",
      "\n",
      "#Variable Declaration\n",
      "px1=1/2.0\n",
      "px2=1/2.0\n",
      "px3=1/2.0\n",
      "px4=1/2.0\n",
      "\n",
      "#Calculation\n",
      "f=input('Enter the frequncy of system fm(in Hz) =')\n",
      "HX=px1*math.log(1/px1,2)+px2*math.log(1/px2,2)+px3*math.log(1/px3,2)+px4*math.log(1/px4,2)\n",
      "\n",
      "#Result\n",
      "print('\\n Entropy H(X) =%.f bits/symbol ' %HX)\n",
      "R=2*f*HX\n",
      "print('information rate =%.f bits/sec' %R)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the frequncy of system fm(in Hz) =45\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        " Entropy H(X) =2 bits/symbol \n",
        "information rate =180 bits/sec\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.16, Page No 473"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Find source entropy ,information rate\n",
      "\n",
      "#Variable Declaration\n",
      "#probability symbols\n",
      "px1=1/2.0\n",
      "px2=1/4.0\n",
      "px3=1/8.0\n",
      "px4=1/16.0\n",
      "px5=1/16.0\n",
      "Tb=10.0**-3\n",
      "\n",
      "#Calculation\n",
      "HX=px1*math.log(1/px1,2)+px2*math.log(1/px2,2)+px3*math.log(1/px3,2)+px4*math.log(1/px4)+px5*math.log(1/px5)\n",
      "\n",
      "#Result\n",
      "print('1. source entropy H(X) = %.2f bits/symbol ' %HX) #source entropy\n",
      "r=1.0/Tb\n",
      "R=r*HX #information rate\n",
      "print(' 2. Information rate R = %.2f bits/sec ' %R)\n",
      "print('Approximation error')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1. source entropy H(X) = 1.72 bits/symbol \n",
        " 2. Information rate R = 1721.57 bits/sec \n",
        "Approximation error\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.17, Page No 473"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#assume if there are 16 outcomes per second\n",
      "\n",
      "#Variable Declaration\n",
      "px1=1/2.0\n",
      "px2=1/4.0\n",
      "px3=1/8.0\n",
      "px4=1/16.0\n",
      "px5=1/16.0\n",
      "r=16.0\n",
      "\n",
      "#Calculation\n",
      "HX=px1*math.log(1/px1,2)+px2*math.log(1/px2,2)+px3*math.log(1/px3,2)+px4*math.log(1/px4,2)+px5*math.log(1/px5,2)\n",
      "\n",
      "#Result\n",
      "print('1. Entropy H(X) = %.2f bits/symbol ' %HX) #source entropy\n",
      "\n",
      "R=r*HX\n",
      "print('2., Information rate R = %.f bits/sec' %R)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1. Entropy H(X) = 1.88 bits/symbol \n",
        "2., Information rate R = 30 bits/sec\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.18, Page No 474"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#determine entropy ,information rate\n",
      "\n",
      "#Variable Declaration\n",
      "px1=1/4.0\n",
      "px2=1/5.0\n",
      "px3=1/5.0\n",
      "px4=1/10.0\n",
      "px5=1/10.0\n",
      "px6=1/20.0\n",
      "px7=1/20.0\n",
      "px8=1/20.0\n",
      "f=10*10**3.0\n",
      "fs=10*2*10**3.0\n",
      "\n",
      "#Calculation\n",
      "#entropy\n",
      "HX=px1*math.log(1/px1,2)+px2*math.log(1/px2,2)+px3*math.log(1/px3,2)+px4*math.log(1/px4,2)+px5*math.log(1/px5,2)+px6*math.log(1/px6,2)+px7*math.log(1/px7,2)+px8*math.log(1/px8,2) \n",
      "\n",
      "#Result\n",
      "print('bits/message H(X) = %.2f ' %HX)\n",
      "r=fs\n",
      "R=r*HX    #information rate\n",
      "print('bits/sec R = %.2f' %R)\n",
      "print('Approximation error')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "bits/message H(X) = 2.74 \n",
        "bits/sec R = 54828.92\n",
        "Approximation error\n"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.19, Page No 476 "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "from array import *\n",
      "#Find Channel Matrix,joint probability\n",
      "\n",
      "#Variable Declaration\n",
      "px1=0.5\n",
      "px2=0.5\n",
      "py1x1=0.9\n",
      "py2x1=0.1\n",
      "py1x2=0.2\n",
      "py2x2=0.8\n",
      "PYX=[[py1x1,py2x1],[py1x2,py2x2]]\n",
      "PX=[[px1,px2]]\n",
      "PY = [[0,0],\n",
      "    [0,0]]\n",
      "PXY = [[0,0],\n",
      "    [0,0]]\n",
      "\n",
      "for i in range(len(PYX)):\n",
      "   # iterate through columns of Y\n",
      "   for j in range(len(PX[0])):\n",
      "       # iterate through rows of Y\n",
      "       for k in range(len(PX)):\n",
      "           PY[i][j] += PYX[i][k] * PX[k][j]\n",
      "print('  PY ARRAY =  \\n')\n",
      "for r in PY:\n",
      "   print(r)\n",
      "PXd=[[px1,0],[0,px2]]\n",
      "\n",
      "\n",
      "for i in range(len(PXd)):\n",
      "   # iterate through columns of Y\n",
      "   for j in range(len(PYX[0])):\n",
      "       # iterate through rows of Y\n",
      "       for k in range(len(PYX)):\n",
      "           PXY[i][j] += PXd[i][k] * PYX[k][j]\n",
      "\n",
      "            \n",
      "print(' \\n PXY ARRAY =  \\n')\n",
      "for r in PXY:\n",
      "   print(r)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "  PY ARRAY =  \n",
        "\n",
        "[0.45, 0.45]\n",
        "[0.1, 0.1]\n",
        " \n",
        " PXY ARRAY =  \n",
        "\n",
        "[0.45, 0.05]\n",
        "[0.1, 0.4]\n"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.35, Page No 498"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Channel is aproximated by the AWGN Channel\n",
      "\n",
      "#Variable Declaration\n",
      "B=4000.0\n",
      "S=0.1*10**-3\n",
      "n=2*10**-12\n",
      "\n",
      "#Calculation\n",
      "N=n*B\n",
      "C=B*math.log(1+(S/N),2) #Capacity of Channel\n",
      "C=C/1000.0\n",
      "#Result\n",
      "print(' Capacity of Channel  C=%.3f(10^3) b/s ' %C)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Capacity of Channel  C=54.439(10^3) b/s \n"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.36i, Page No 499"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "\n",
      "#assume that  succeissive samples are statistically independent\n",
      "\n",
      "#Variable Declaration\n",
      "fm=4000.0\n",
      "fs=2*fm\n",
      "n=1.25\n",
      "\n",
      "#Calculation\n",
      "r=fs*n\n",
      "pxi=1/256.0\n",
      "HX=-math.log(pxi,2)\n",
      "R=r*HX\n",
      "R=R/1000\n",
      "print('Information Rate  R= %.f kb/s' %R)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Information Rate  R= 80 kb/s\n"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.36ii, Page No 499"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "\n",
      "#assume that  succeissive samples are statistically independent\n",
      "\n",
      "#Variable Declaration\n",
      "B=10*10**3.0\n",
      "SN=20.0\n",
      "\n",
      "#Calculation\n",
      "SNR=10**(SN/10.0)\n",
      "C=B*math.log(1+(SNR),2)\n",
      "C=C/1000\n",
      "\n",
      "#Result\n",
      "print('The channel capacity = %.2f 10^3 b/s' %C)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The channel capacity = 66.58 10^3 b/s\n"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.36iii, Page No 499"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#assume that  succeissive samples are statistically independent\n",
      "\n",
      "#Variable Declaration\n",
      "C=8*10**4.0\n",
      "B=10**4.0\n",
      "\n",
      "#Calculation\n",
      "SN=2**(C/B)-1\n",
      "SNR=10*math.log(SN,10) #SNR\n",
      "\n",
      "#Result\n",
      "print(' The S/N ratio required for error-free transmission  =%.2f dB ' %SNR)     #required SNR is greater that 24.064\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " The S/N ratio required for error-free transmission  =24.07 dB \n"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.36iv, Page No 499 "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#assume that  succeissive samples are statistically independent\n",
      "\n",
      "#Variable Declaration\n",
      "SN=20.0\n",
      "\n",
      "#Calculation\n",
      "SNR=10**(SN/10.0)\n",
      "C=8*10**4.0\n",
      "B=C/(math.log(1+SNR,2)) #Bandwidth\n",
      "B=B/1000\n",
      "\n",
      "#Result\n",
      "print('Bandwidth required for AWGN channel B =%.2f kHz ' %B)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Bandwidth required for AWGN channel B =12.02 kHz \n"
       ]
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.37, Page No 502"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Find code efficiency,redundancy\n",
      "\n",
      "#Variable Declaration\n",
      "px1=0.9\n",
      "px2=0.1\n",
      "n1=1.0\n",
      "n2=1.0\n",
      "\n",
      "#Calculation\n",
      "L=px1*n1+px2*n2 #code leght\n",
      "HX=px1*math.log(1/px1,2)+px2*math.log(1/px2,2)\n",
      "n=(HX/L) #code efficiency\n",
      "n=n*100\n",
      "\n",
      "print('Code efficiency = %.1f percent'  %n)\n",
      "r=(100-n) #code reduncy\n",
      "print('Code redundancy = %.1f percent' %r)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Code efficiency = 46.9 percent\n",
        "Code redundancy = 53.1 percent\n"
       ]
      }
     ],
     "prompt_number": 18
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.38, Page No 502"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Find code efficiency,redundancy\n",
      "\n",
      "#Variable Declaration\n",
      "pa1=0.81\n",
      "pa2=0.09 \n",
      "pa3=0.09\n",
      "pa4=0.01 \n",
      "n1=1\n",
      "n2=2 \n",
      "n3=3\n",
      "n4=3 \n",
      "\n",
      "#Calculation\n",
      "L=pa1*n1+pa2*n2+pa3*n3+pa4*n4\n",
      "HX2=pa1*math.log(1/pa1,2)+pa2*math.log(1/pa2,2)+pa3*math.log(1/pa3,2)+pa4*math.log(1/pa4,2)\n",
      "n=HX2/L*100\n",
      "\n",
      "#Result\n",
      "print(' code efficiency = %.2f  percent' %n)\n",
      "\n",
      "r=(100-n) #code reduncy\n",
      "print(' code redundancy = %.1f percent' %r)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " code efficiency = 72.71  percent\n",
        " code redundancy = 27.3 percent\n"
       ]
      }
     ],
     "prompt_number": 19
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.44, Page No 507"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Find  efficiency of the code\n",
      "\n",
      "#Variable Declaration\n",
      "px1=1/2.0\n",
      "px2=1/4.0\n",
      "px3=1/8.0\n",
      "px4=1/8.0\n",
      "n1=1.0\n",
      "n2=2.0\n",
      "n3=3.0\n",
      "n4=3.0\n",
      "\n",
      "#Calculation\n",
      "#information content of each symbol\n",
      "Ix1=-math.log(px1,2)\n",
      "Ix2=-math.log(px2,2)\n",
      "Ix3=-math.log(px3,2)\n",
      "Ix4=-math.log(px4,2)\n",
      "\n",
      "HX=px1*math.log(1/px1,2)+px2*math.log(1/px2,2)+px3*math.log(1/px3,2)+px4*math.log(1/px4,2)\n",
      "L=px1*n1+px2*n2+px3*n3+px4*n4\n",
      "\n",
      "n=HX/L*100\n",
      "\n",
      "#Result\n",
      "print('Ccode efficiency = %.f  Percent' %n)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Ccode efficiency = 100  Percent\n"
       ]
      }
     ],
     "prompt_number": 20
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.50, Page No 512"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Find entropy ,information rate\n",
      "#If there are 16 outcomes per second\n",
      "\n",
      "#Variable Declaration\n",
      "P1=1/2.0\n",
      "P2=1/4.0\n",
      "P3=1/8.0\n",
      "P4=1/16.0\n",
      "P5=1/32.0\n",
      "P6=1/32.0\n",
      "r=16     #message rate\n",
      "\n",
      "#Calculation\n",
      "H=P1*math.log(1/P1,2)+P2*math.log(1/P2,2)+P3*math.log(1/P3,2)+P4*math.log(1/P4,2)+P5*math.log(1/P5,2)+P6*math.log(1/P6,2)\n",
      "#Entropy of system\n",
      "\n",
      "#Result\n",
      "print('1. Entropy of system H = %.2f bits/message ' %H)\n",
      "R=H*r                            #R=Entropy*message rate\n",
      "print(' 2. Information rate  R = %.f bits/sec ' %R)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1. Entropy of system H = 1.94 bits/message \n",
        " 2. Information rate  R = 31 bits/sec \n"
       ]
      }
     ],
     "prompt_number": 21
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.51, Page No 512"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Calculate H(X) ,H(Y)\n",
      "\n",
      "#Variable Declaration\n",
      "px1=0.3\n",
      "px2=0.4\n",
      "px3=0.3\n",
      "\n",
      "#Calculation\n",
      "HX=px1*math.log(1/px1,2)+px2*math.log(1/px2,2)+px3*math.log(1/px3,2)  #Entropy of X\n",
      "\n",
      "\n",
      "print(' 1.Entropy of X  H(X)=%.3f bits/symbol ' %HX)\n",
      "\n",
      "PYX=[[0.8, 0.2, 0],[ 0, 1, 0],[ 0, 0.3, 0.7]]\n",
      "PX=[[px1, px2, px3]]\n",
      "PXY = [[0,0,0],\n",
      "    [0,0,0],\n",
      "    [0,0,0]]\n",
      "\n",
      "for i in range(len(PYX)):\n",
      "   # iterate through columns of PXd\n",
      "   for j in range(len(PX[0])):\n",
      "       # iterate through rows of PYX\n",
      "       for k in range(len(PX)):\n",
      "           PXY[i][j] += PYX[i][k] * PX[k][j]\n",
      "\n",
      "py1=PXY[0][0]\n",
      "py2=PXY[0][1]\n",
      "py3=PXY[0][2]\n",
      "HY=py1*math.log(1/py1,2)+py2*math.log(1/py2,2)+py3*math.log(1/py3,2)  #Entropy of Y\n",
      "print(' 2. Entropy of Y H(Y)= %.2f bits/symbol ' %HY)\n",
      "print('Approximation error')\t\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " 1.Entropy of X  H(X)=1.571 bits/symbol \n",
        " 2. Entropy of Y H(Y)= 1.51 bits/symbol \n",
        "Approximation error\n"
       ]
      }
     ],
     "prompt_number": 22
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.52, Page No 513"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Find entropy of source ,entropy of second order extension\n",
      "\n",
      "#Variable Declaration\n",
      "P1=0.7\n",
      "P2=0.15\n",
      "P3=0.15\n",
      "\n",
      "#Calculation\n",
      "HX=P1*math.log(1/P1,2)+P2*math.log(1/P2,2)+P3*math.log(1/P3,2)     #Entropy of source\n",
      "print(' 1. Entropy of system  H(X)=%.2f bits/symbol ' %HX)\n",
      "#H(X^n)=n*H(X)\n",
      "n=2    #for second order\n",
      "HX2=n*HX\n",
      "\n",
      "#Result\n",
      "print(' 2. Entropy of second order system extension of source can be H(X^2)=%.2f bits/symbol ' %(HX*2))\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " 1. Entropy of system  H(X)=1.18 bits/symbol \n",
        " 2. Entropy of second order system extension of source can be H(X^2)=2.36 bits/symbol \n"
       ]
      }
     ],
     "prompt_number": 23
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.54, Page No 514"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Find entropy of source \n",
      "\n",
      "#Variable Declaration\n",
      "S0=1/3.0\n",
      "S1=1/6.0\n",
      "S2=1/4.0\n",
      "S3=1/4.0\n",
      "\n",
      "#Calculation\n",
      "HX=S0*math.log(1/S0,2)+S1*math.log(1/S1,2)+S2*math.log(1/S2,2)+S3*math.log(1/S3,2)   #EntroSy of source\n",
      "\n",
      "#Result\n",
      "print(' Entropy of system  H(X)=%.2f bits/symbol ' %HX)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Entropy of system  H(X)=1.96 bits/symbol \n"
       ]
      }
     ],
     "prompt_number": 24
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.56, Page No 515"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Find Information capacity of telephone\n",
      "\n",
      "#Variable Declaration\n",
      "B=3.4*10**3\n",
      "SNR=30.0\n",
      "\n",
      "#Calculation\n",
      "SN=10**(SNR/10)\n",
      "C=B*math.log(1+SN,2)   #Information capacity\n",
      "C=C/1000\n",
      "\n",
      "#Result\n",
      "print(' Information capacity of telephone is C = %.2f kbps ' %C)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Information capacity of telephone is C = 33.89 kbps \n"
       ]
      }
     ],
     "prompt_number": 25
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.59, Page No 516"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Find entropy of source \n",
      "\n",
      "#Variable Declaration\n",
      "S0=1/3.0\n",
      "S1=1/6.0\n",
      "S2=1/4.0\n",
      "S3=1/4.0\n",
      "\n",
      "#Calculation\n",
      "HX=S0*math.log(1/S0,2)+S1*math.log(1/S1,2)+S2*math.log(1/S2,2)+S3*math.log(1/S3,2)   #EntroSy of source\n",
      "\n",
      "#Result\n",
      "print(' Entropy of system  H(X)=%.2f bits/symbol ' %HX)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Entropy of system  H(X)=1.96 bits/symbol \n"
       ]
      }
     ],
     "prompt_number": 26
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.60, Page No 516"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Find entropy of source \n",
      "\n",
      "#Variable Declaration\n",
      "m1=1/2.0\n",
      "m2=1/4.0\n",
      "m3=1/8.0\n",
      "m4=1/16.0\n",
      "m5=1/16.0\n",
      "\n",
      "#Calculation\n",
      "L=(m1*1)+(m2*2)+(m3*3)+(2*(m4)*4)\n",
      "\n",
      "#Result\n",
      "print(' Average number of bits per message =%.2f bits ' %L)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Average number of bits per message =1.88 bits \n"
       ]
      }
     ],
     "prompt_number": 27
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.61, Page No 517"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Find Information capacity of telephone\n",
      "\n",
      "#Variable Declaration\n",
      "B=3.4*10**3\n",
      "SNR=30.0\n",
      "\n",
      "#Calculation\n",
      "SN=10**(SNR/10)\n",
      "C=B*math.log(1+SN,2)   #Information capacity\n",
      "C=C/1000\n",
      "\n",
      "#Result\n",
      "print(' Information capacity of telephone is C = %.2f kbps ' %C)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Information capacity of telephone is C = 33.89 kbps \n"
       ]
      }
     ],
     "prompt_number": 28
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.62, Page No 517"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Find entropy of source \n",
      "\n",
      "#Variable Declaration\n",
      "p1=4.0\n",
      "m1=0.5\n",
      "m2=0.5\n",
      "m3=0.375\n",
      "m4=0.375\n",
      "m5=0.375\n",
      "m6=0.375\n",
      "\n",
      "#Calculation\n",
      "I1=p1*math.log(1/p1,2) \n",
      "HX=m1*math.log(1/m1,2)+m2*math.log(1/m2,2)+m3*math.log(1/m3,2)+m4*math.log(1/m4,2)+m5*math.log(1/m5,2)+m6*math.log(1/m6,2)   #EntroSy of source\n",
      "\n",
      "#Result\n",
      "print(' Entropy of system  H(X)=%.2f bits/symbol ' %HX)\n",
      "print('Approximation error')\t"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Entropy of system  H(X)=3.12 bits/symbol \n",
        "Approximation error\n"
       ]
      }
     ],
     "prompt_number": 29
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.65, Page No 519"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Find entropy of source \n",
      "\n",
      "#Variable Declaration\n",
      "S0=1/2.0\n",
      "S1=1/4.0\n",
      "S2=1/8.0\n",
      "S3=1/8.0\n",
      "n=1\n",
      "\n",
      "#Calculation\n",
      "H=S0*math.log(1.0/S0,2)+S1*math.log(1.0/S1,2)+S2*math.log(1.0/S2,2)+S3*math.log(1.0/S3,2)   #EntroSy of source\n",
      "L=H*n\n",
      "\n",
      "\n",
      "#Result\n",
      "print(' Code length =%.2f bits/messages ' %L)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Code length =1.75 bits/messages \n"
       ]
      }
     ],
     "prompt_number": 30
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 8.67, Page No 520"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "#Find channel capacity and new bandwidth\n",
      "\n",
      "#Variable Declaration\n",
      "B=8*10**3\n",
      "SNR=31.0\n",
      "SNR2=61\n",
      "\n",
      "#Calculation\n",
      "C=B*math.log(1+SNR,2)   #Information capacity\n",
      "B2=C/math.log(1+SNR2,2)\n",
      "#Result\n",
      "print(' Channel capacity is C = %.2f x 10^3 bits/sec ' %(C/1000))\n",
      "print(' New Bandwidth is C = %.2f x kHz ' %(B2/1000))\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Channel capacity is C = 40.00 x 10^3 bits/sec \n",
        " New Bandwidth is C = 6.72 x kHz \n"
       ]
      }
     ],
     "prompt_number": 31
    }
   ],
   "metadata": {}
  }
 ]
}