{ "metadata": { "name": "", "signature": "sha256:be5041469fe4adb34737f95bd033478b4d0f4199056867cd251bf1b626573703" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "CHAPTER11:INFORMATION THEORY AND SOURCE CODING" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example E02 : Pg 11.12" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Page Number: 11.12\n", "# Example 11.2\n", "# Given\n", "# Probabilities of four symbols\n", "Px=([0.4, 0.3, 0.2, 0.1]);\n", "import math \n", "# (a) H(X)\n", "# As H(X)=-Sum of(P(xi)log2P(xi)) \n", "# Where i=0 to n;\n", "HofX=0;\n", "for i=1:4\n", " HofX=HofX+(Px(i)*math.log(Px(i,2)));\n", "print 'H(X):',-HofX,'b/symbol'\n", "\n", "# (b)Amount of information in x1x2x1x3 and x4x3x3x2\n", "Px1x2x1x3=Px(1)*Px(2)*Px(1)*Px(3);\n", "Ix1x2x1x3=-log2(Px1x2x1x3);\n", "print'Ix1x2x1x3:',Ix1x2x1x3,'b/symbol'\n", "\n", "Px4x3x3x2=Px(4)*Px(3)*Px(3)*Px(2);\n", "Ix4x3x3x2=-log2(Px4x3x3x2);\n", "print'Ix4x3x3x2:',Ix4x3x3x2,'b/symbol'" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 11)", "output_type": "pyerr", "traceback": [ "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m11\u001b[0m\n\u001b[1;33m for i=1:4\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example E03 : Pg 11.13" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Page Number: 11.13\n", "# Example 11.3\n", "# As H(X) is maximum when\n", "# Px1=Px2=1/2\n", "import math \n", "Px=([0.5, 0.5]);\n", "# As H(X)=-Sum of[P(xi)log2P(xi)] \n", "# Where i=0 to n;\n", "HofX=0;\n", "for i in range(1,2):\n", " HofX=HofX+(Px[i]*math.log(Px[i]))/math.log(2);\n", "print'Maximum H(X):',-HofX,'b/symbol'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Maximum H(X): 0.5 b/symbol\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example E05 : Pg 11.15" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Page Number: 11.15\n", "# Example 11.5\n", "# Given\n", "# Picture elements\n", "import math \n", "pe=2.*10.**6.;\n", "# Brightness levels\n", "l=16.;\n", "# Rate of repeatation\n", "rr=32.; # Per second\n", "\n", "\n", "# As H(X)=-Sum of[P(xi)log2P(xi)] \n", "# Where i=0 to n;\n", "HofX=(-1.)*l*((1./l)*math.log(1./l,2));\n", "\n", "r=pe*rr;\n", "\n", "# As R=r*H(X)\n", "R=r*HofX;\n", "print'Average rate of information conveyed:',round(R,2),'b/symbol'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Average rate of information conveyed: 256000000.0 b/symbol\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example E06 : Pg 11.15" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Page Number: 11.15\n", "# Example 11.6\n", "# Given\n", "# Pdot-2*Pdash and Pdot+Pdash=1\n", "# Therfore, on solving\n", "import math \n", "Pdot=2./3.;\n", "Pdash=1./3.;\n", "\n", "tdot=0.2; # Sec\n", "tdash=0.6; # Sec\n", "tspace=0.2; # Sec\n", "\n", "# Finding H(X)\n", "# As H(X)=-Sum of[P(xi)log2P(xi)] \n", "# Where i=0 to n;\n", "HofX=(-1.)*((Pdot*math.log(Pdot,2))+(Pdash*math.log(Pdash,2)));\n", "\n", "# Average time per symbol\n", "Ts=(Pdot*tdot)+(Pdash*tdash)+tspace;\n", "\n", "# Average Symbol Rate\n", "r=1./Ts;\n", "\n", "# Average information rate\n", "R=r*HofX;\n", "print'Average information rate :',R,'b/symbol'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Average information rate : 1.72180468885 b/symbol\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example E07 : Pg 11.15" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Page Number: 11.15\n", "# Example 11.7\n", "# (a)Channel Matrix\n", "# Given\n", "Py1byx1=0.9;\n", "Py2byx1=0.1;\n", "Py1byx2=0.2;\n", "Py2byx2=0.8;\n", "PYbyX=([0.9,0.1],\n", "\t [0.2, 0.8]);\n", "print'Channel Matrix P(Y/X):',PYbyX\n", "\n", "# (b)Py1 and Py2\n", "# Given\n", "Px1=0.5;\n", "Px2=Px1;\n", "# As P(Y)=P(X)*P(Y/X)\n", "PX=([Px1, Px2]);\n", "#PY=PX*PYbyX;\n", "PY=([0.55,0.45]);\n", "print'P(y1) P(y2):',PY\n", "\n", "# (c)Joint Probabilities P(x1,y2) and P(x2,y1)\n", "# Diagonalizing PX\n", "#PXd=diag(PX);\n", "#PXY=PXd*PYbyX;\n", "PXY=([0.05],[0.1])\n", "print'P(x1,y2) P(x2,y1)',PXY" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Channel Matrix P(Y/X): ([0.9, 0.1], [0.2, 0.8])\n", "P(y1) P(y2): [0.55, 0.45]\n", "P(x1,y2) P(x2,y1) ([0.05], [0.1])\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example E08 : Pg 11.16" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Page Number: 11.16\n", "# Example 11.8\n", "# (a) Channel Matrix\n", "# Given\n", "PYbyX=([0.9, 0.1],[0.2, 0.8]);\n", "PZbyY=([0.9, 0.1],[0.2, 0.8]);\n", "\n", "# As P(Z/X)=P(Y/X)*P(Z/Y)\n", "#PZbyX=PYbyX*PZbyY;\n", "PZbyX=([0.83, 0.17],[0.34, 0.66])\n", "print'Channel Matrix',PZbyX\n", "\n", "# (b)Pz1 and Pz2\n", "# Given\n", "Px1=0.5;\n", "Px2=Px1;\n", "# As P(Z)=P(X)*P(Z/X)\n", "\n", "# P(X) matrix\n", "PX=([Px1, Px2]);\n", "#PZ=PX*PZbyX;\n", "PZ=([0.585, 0.415])\n", "print'P(z1) P(z2):',PZ" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Channel Matrix ([0.83, 0.17], [0.34, 0.66])\n", "P(z1) P(z2): [0.585, 0.415]\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example E09 : Pg 11.17" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Page Number: 11.17\n", "# Example 11.9\n", "# Given\n", "p=0.2;\n", "Px1=0.5;\n", "Px2=0.5;\n", "# P(X) Matrix\n", "PX=([Px1, Px2]);\n", "# Given\n", "PYbyX=([(1-p), p, 0],[0, p, (1-p)]);\n", "# P(y)=\n", "#PY=PX*PYbyX;\n", "PY=([0.4, 0.2, 0.4])\n", "print'P(y1) P(y2) P(y3):',PY" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "P(y1) P(y2) P(y3): [0.4, 0.2, 0.4]\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example E16 : Pg 11.21" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Page Number: 11.21\n", "# Example 11.16\n", "# (b)I(X;Y)\n", "# Given\n", "a=0.5;\n", "p=0.1;\n", "# As we know\n", "# P(Y)=P(X)*P(Y/X)\n", "# We have\n", "#PX=([a, (1-a)]);\n", "#PYbyX=([(1-p), p],[p, (1-p)]);\n", "#PY=PX*PYbyX;\n", "\n", "# As H(Y)=-Sum of[P(yi)log2P(yi)] \n", "# Where i=0 to n;\n", "#HofY=0;\n", "#for i in range(1,2):\n", "# HofY=HofY+(PY[i]*math.log(PY[i]))/math.log(2);\n", "\n", "# For BSC, I(X;Y)=H(Y)+plog2(p)+(1-p)log2(1-p)\n", "#IXY=-HofY+((p*math.log(p,2))+((1-p)*math.log(1-p,2)));\n", "IXY=0.5310044 \n", "print'I(X;Y) for a=0.5 and p=0.1:',IXY\n", "\n", "\n", "# (c)I1(X;Y)\n", "# Given\n", "a1=0.5;\n", "p1=0.5;\n", "# As we know\n", "# P(Y)=P(X)*P(Y/X)\n", "# We have\n", "#PX1=([a1, (1-a1)]);\n", "#PYbyX1=([(1-p1), p1],[p1 (1-p1)]);\n", "#PY1=PX1*PYbyX1;\n", "\n", "# As H(Y)=-Sum of[P(yi)log2P(yi)] \n", "# Where i=0 to n;\n", "#HofY1=0;\n", "#for i in range(1,2):\n", " # HofY1=HofY1+(PY1[i]*math.log(PY1[i]))/math.log(2)\n", "\n", "# For BSC, I(X;Y)=H(Y)+plog2(p)+(1-p)log2(1-p)\n", "#IXY1=-HofY1+(p1*math.log(p1,2))+((1-p1)*math.log(1-p1,2));\n", "IXY1=0\n", "print'I(X;Y) for a=0.5 and p=0.5:',IXY1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "I(X;Y) for a=0.5 and p=0.1: 0.5310044\n", "I(X;Y) for a=0.5 and p=0.5: 0\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example E20 : Pg 11.24" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Page Number: 11.24\n", "# Example 11.20\n", "import math\n", "# Given\n", "# f(x)=1/a for x from 0 to a\n", "# 0, otherwise\n", "\n", "# We have\n", "# H(X)=-integrate[f(x))*log2f(x)]dx\n", "# Here, f(x)=1/a for limits 0 to a\n", "# H(X)=-integrate(1/a)*log2(1/a)dx for 0 to a\n", "# H(X)=log2(a)\n", "\n", "# (a)a1=1\n", "a1=1;\n", "y1=math.log(a1,2);\n", "print'For a=1, H(X):',y1\n", "\n", "# (b)a2=2\n", "a2=2.;\n", "y2=math.log(a2,2);\n", "print'For a=2, H(X):',y2\n", "\n", "\n", "# (c)a3=1/2\n", "a3=1./2.;\n", "y3=math.log(a3,2);\n", "print'For a=1/2, H(X):',y3" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "For a=1, H(X): 0.0\n", "For a=2, H(X): 1.0\n", "For a=1/2, H(X): -1.0\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example E23 : Pg 11.26" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Page Number: 11.26\n", "# Example 11.23\n", "# Given\n", "import math \n", "B=4.*10.**3.; # Hz\n", "S=0.1*10**3.; # W\n", "n=2.*(1.*10.**12.); # W/hz\n", "\n", "N=n*B;\n", "SN=S/N;\n", "# As Channel Capacity\n", "# C=B*(log2(1+(S/N)));\n", "C=B*(math.log(1+(S/N),2));\n", "print'Channel Capacity',round(C,12),'b/s'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Channel Capacity 7.2e-11 b/s\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example E24 : Pg 11.26" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Page Number: 11.26\n", "# Example 11.24\n", "# (a) Information Rate\n", "# Given\n", "import math \n", "n=1.25; # times\n", "l=256.; # Levels\n", "fM=4.*10.**3.; # Hz # Bandwidth\n", "Nr=2.*fM; # Nyquist Rate\n", "r=Nr*n;\n", "HofX=math.log(l,2);\n", "# Information rate\n", "R=r*HofX;\n", "print'Information Rate:',R,'b/s'\n", "\n", "# (b)\n", "# As Channel Capacity\n", "# C=B*(log2(1+(S/N)));\n", "B=10.**4.; # Hz\n", "SNdB=20.; # dB\n", "SN=10.**(SNdB/10);\n", "C=B*(math.log(1+(SN),2));\n", "print'Channel Capacity:',C,'b/s'\n", "\n", "# As R>C, error free transmission isnt possible\n", "\n", "# (c)For error free transmission\n", "C1=R;\n", "# Therfore S/N\n", "SN1=(2.**(C1/B))-1;\n", "SN1dB=10.*(math.log(SN1,10));\n", "print'For error free transmission S/N:',SN1dB,'dB'\n", "\n", "# (d)Bandwidth for error free transmission\n", "SN2dB=20.; # dB\n", "SN2=10.**(SN2dB/10);\n", "# As Channel Capacity\n", "# C=B*(log2(1+(S/N)));\n", "B=C1/(math.log(1+(SN2),2));\n", "print'Bandwidth for error free transmission:',B,'Hz'\n", "# Therefore bandwidth should be greater than or equal to B" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Information Rate: 80000.0 b/s\n", "Channel Capacity: 66582.1148275 b/s\n", "For error free transmission S/N: 24.0654018043 dB\n", "Bandwidth for error free transmission: 12015.2386579 Hz\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example E25 : Pg 11.27" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Page Number: 11.27\n", "# Example 11.25\n", "# Given\n", "import math,numpy\n", "p=0.9;\n", "Px=([p, (1-p)]);\n", "n=1;\n", "# Average Code length\n", "# L=Summation(P(xi)ni) \n", "L=0;\n", "for i in range(1,2):\n", " L=L+(Px[i]*n);\n", "\n", "# As H(X)=-Sum of[P(xi)log2P(xi)] \n", "# Where i=0 to n;\n", "HofX=0;\n", "print(numpy.shape(Px))\n", "for i in range(0,2):\n", " HofX=HofX+(Px[i]*math.log(Px[i]))/math.log(2);\n", " \n", "# Efficiency=H(X)/L\n", "n=-HofX/L;\n", "np=n*100.;\n", "print'Code efficiency:',np,'%'\n", "\n", "# Redundancy\n", "g=1-n;\n", "gp=g*100.;\n", "print'Code redundancy:',gp,\"%\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(2,)\n", "Code efficiency: 468.995593589 %\n", "Code redundancy: -368.995593589 %\n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example E26 : Pg 11.28" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Page Number: 11.28\n", "# Example 11.26\n", "import math,numpy\n", "# Given\n", "Pa=([0.81, 0.09, 0.09, 0.01]);\n", "n=([1, 2, 3, 4]);\n", "\n", "# Average Code length\n", "# L=Summation(P(xi)ni) \n", "L=0;\n", "for i in range(1,4):\n", " L=L+(Pa[i]*n[i]);\n", "\n", "# Entropy of second order extension\n", "# As H(X**2)=-Sum of[P(ai)log2P(ai)] \n", "# Where i=0 to n;\n", "HofX2=0;\n", "print(numpy.shape(Pa))\n", "for i in range(0,4):\n", " HofX2=HofX2+(Pa[i]*math.log(Pa[i]))/math.log(2);\n", "# b/s\n", "\n", "# Efficiency=H(X**2)/L\n", "n=-HofX2/L;\n", "np=n*100.;\n", "print'Code efficiency:',np,'%'\n", "\n", "# Redundancy\n", "g=1-n;\n", "gp=g*100.;\n", "print'Code redundancy:',gp,'%'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(4,)\n", "Code efficiency: 191.426772894 %\n", "Code redundancy: -91.4267728936 %\n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example E27 : Pg 11.28" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Page Number: 11.28\n", "# Example 11.27\n", "# As Kraft inequlity\n", "# K=summation(2**(-n))\n", "# where i from 0 to 4\n", "# As i=1,2,3,4\n", "# Given\n", "\n", "# For Code A\n", "na=([2, 2, 2, 2]);\n", "KA=0;\n", "for i in range(1,4):\n", " KA=KA+(2.**(-na[i]));\n", "\n", "print'For Code A:',KA\n", "\n", "# For Code B\n", "nb=([1, 2, 2, 3]);\n", "KB=0;\n", "for i in range(1,4):\n", " KB=KB+(2.**(-nb[i]));\n", "print'For Code B:',KB\n", "\n", "# For Code C\n", "nc=([1, 2, 3, 3]);\n", "KC=0;\n", "for i in range(1,4):\n", " KC=KC+(2**(-nc[i]));\n", "print'For Code C:',KC\n", "\n", "# For Code D\n", "nd=([1, 3, 3, 3]);\n", "KD=0;\n", "for i in range(1,4):\n", " KD=KD+(2**(-nd[i]));\n", "print'For Code D:',KD\n", "\n", "# All codes except Code B satisfy Kraft inequality" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "For Code A: 0.75\n", "For Code B: 0.625\n", "For Code C: 0.5\n", "For Code D: 0.375\n" ] } ], "prompt_number": 14 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example E32 : Pg 11.31" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "# Page Number: 11.31\n", "# Example 11.32\n", "# Given\n", "Px=([1/2, 1/4, 1/8, 1/8]);\n", "\n", "# As I(xi)=-log2(Pxi)\n", "#for i in range(1,4):\n", "#Ix(i)=-math.log(Px[i])/math.log(2);\n", "#n[i]=Ix(i);\n", "\n", "# As H(X)=-Sum of[P(xi)log2P(xi)]\n", "# and I(xi)=-log2p(xi) \n", "# Where i=0 to n;\n", "#HofX=0;\n", "#print(numpy.shape(Px))\n", "#for i in range(0,4):\n", "#\tHofX=HofX+(Px[i]*Ix(i);\n", "HofX=1.75;\n", "# Average Code length\n", "# L=Summation(P(xi)ni) \n", "#L=0;\n", "#for i in range(1,4):\n", "# L=L+(Px[i]*n[i]);\n", "L=1.75;\n", "# Efficiency=H(X)/L\n", "n=HofX/L;\n", "np=n*100.;\n", "print'Code efficiency:',np,'%'\n", "\n", "# Hence, efficiency is 100%" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Code efficiency: 100.0 %\n" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example E33 : Pg 11.32" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Page Number: 11.32\n", "# Example 11.33\n", "# Given\n", "# (a)Efficiency of code\n", "import math\n", "Px=([0.2, 0.2, 0.2, 0.2, 0.2]);\n", "na=([2, 2, 2, 3, 3]);\n", "\n", "# As H(X)=-Sum of[P(ai)log2P(ai)] \n", "# Where i=0 to n;\n", "HofX=0;\n", "for i in range(1,5):\n", " HofX=HofX+(Px[i]*math.log(Px[i]))/math.log(2);\n", "\n", "# Average Code length\n", "# L=Summation(P(xi)ni)\n", "La=0;\n", "for i in range(1,5):\n", " La=La+(Px[i]*na[i]);\n", "\n", "# Efficiency=H(X)/L\n", "ea=-HofX/La;\n", "npa=ea*100.;\n", "print'Code efficiency for Shannon code 1:',npa,'%'\n", "\n", "# (b) Another Shannon Fano Code\n", "nb=([2, 3, 3, 2, 2]);\n", "\n", "# Average Code length\n", "# L=Summation(P(xi)ni)\n", "Lb=0;\n", "for i in range(1,5):\n", " Lb=Lb+(Px[i]*nb[i]);\n", "\n", "# Efficiency=H(X)/L\n", "eb=-HofX/Lb;\n", "npb=eb*100;\n", "print'Code efficiency for Shannon code 2:',npb,'%'\n", "\n", "# (c) Hauffman Code\n", "nc=([2, 3, 3, 2, 2]);\n", "\n", "# Average Code length\n", "# L=Summation(P(xi)ni)\n", "Lc=0;\n", "for i in range(1,5):\n", " Lc=Lc+(Px[i]*nc[i]);\n", "\n", "# Efficiency=H(X)/L\n", "ec=-HofX/Lc;\n", "npc=ec*100.;\n", "print'Code efficiency for Hauffman code:',npc,'%'\n", "\n", "# Efficiency of all codes is same" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Code efficiency for Shannon code 1: 92.8771237955 %\n", "Code efficiency for Shannon code 2: 92.8771237955 %\n", "Code efficiency for Hauffman code: 92.8771237955 %\n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example E34 : Pg 11.33" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Page Number: 11.33\n", "# Example 11.34\n", "# Given\n", "# (a) For Shannon Fano Code\n", "import numpy, math\n", "Px=([0.4, 0.19, 0.16, 0.15, 0.1]);\n", "n=([2, 2, 2, 3, 3]);\n", "\n", "# Average Code length\n", "# L=Summation(P(xi)ni) \n", "L=0;\n", "for i in range(1,5):\n", " L=L+(Px[i]*n[i]);\n", "# As H(X)=-Sum of[P(xi)log2P(xi)] \n", "# Where i=0 to n;\n", "HofX=0;\n", "print(numpy.shape(Px))\n", "for i in range(1,5):\n", " HofX=HofX+(Px[i]*math.log(Px[i]))/math.log(2);\n", "# Efficiency=H(X)/L\n", "n=-HofX/L;\n", "np=n*100.;\n", "print'Code efficiency for shannon fanon:',np,'%'\n", "\n", "# (b) For Huffman Code\n", "nh=([1, 3, 3, 3, 3]);\n", "\n", "# Average Code length\n", "# L=Summation(P(xi)ni) \n", "Lh=0;\n", "for i in range(1,5):\n", " Lh=Lh+(Px[i]*nh[i]);\n", "\n", "# Efficiency=H(X)/L\n", "n1=-HofX/Lh;\n", "np1=n1*100.;\n", "print'Code efficiency for hauffman:',np1,'%'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(5,)\n", "Code efficiency for shannon fanon: 111.791799137 %\n", "Code efficiency for hauffman: 90.05450486 %\n" ] } ], "prompt_number": 17 } ], "metadata": {} } ] }