{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter No 15 - Basic Information theory"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example15.1, page no 533"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The source entropy is: 1.50 bits/symbol\n"
     ]
    }
   ],
   "source": [
    "from math import log\n",
    "#Given\n",
    "P_A=0.5# probability of producing symbol 'A'\n",
    "P_B=0.25# probability of producing symbol 'B'\n",
    "P_C=0.25# probability of producing symbol 'C'\n",
    "def log2(x):\n",
    "    return log(x,2)\n",
    "H=P_A*log2(1/P_A)+P_B*log2(1/P_B)+P_C*log2(1/P_C)# the source entropy\n",
    "print 'The source entropy is: %0.2f bits/symbol'%(H)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example15.2, page no 535"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The source entropy is: 1.94 bits/symbol\n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import log\n",
    "def log2(x):\n",
    "    return log(x,2)\n",
    "\n",
    "#Given\n",
    "P_A=0.5\n",
    "P_B=0.25\n",
    "P_C=1/32\n",
    "P_D=1/8\n",
    "P_E=1/16\n",
    "P_F=1/32# probabilities of producing respective symbol\n",
    "H=(P_A*log2(1/P_A))+(P_B*log2(1/P_B))+(P_C*log2(1/P_C))+(P_D*log2(1/P_D))+(P_E*log2(1/P_E))+(P_F*log2(1/P_F))# Source Entropy\n",
    "n=6\n",
    "T=1\n",
    "print 'The source entropy is: %0.2f bits/symbol'%(round(1000*H)/1000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example15.3, page no 536"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a)\n",
      "  Channel capacity is: 20 Kbits/sec\n",
      "    Bandwidth: 5 KHz\n",
      "b)\n",
      "  SNR for 3KHz bandwidth: 100.59\n"
     ]
    }
   ],
   "source": [
    "from math import log\n",
    "def log2(x):\n",
    "    return log(x,2)\n",
    "\n",
    "#Given\n",
    "#a\n",
    "B1=4e3#Channel Bandwidth\n",
    "SNR1=31#Channel SNR\n",
    "C1=B1*log2(1+SNR1)#Channel Capacity\n",
    "SNR2=14#Reduced SNR\n",
    "B2=round(C1/log2(1+SNR2))#Bandwidth for reduced SNR with same Channel capacity\n",
    "\n",
    "#b\n",
    "B3=3e3#Reduced Bandwidth\n",
    "SNR3=(2**(C1/B3))-1#Signal Power for reduced bandwidth\n",
    "print 'a)\\n  Channel capacity is: %d Kbits/sec\\n    Bandwidth: %d KHz\\nb)\\n  SNR for 3KHz bandwidth: %0.2f'%(C1*1e-3,B2*1e-3,SNR3)\n",
    "# the Answer in the book is wrong.It is printed as 90.4 for SNR3 but it should be 100.59"
   ]
  }
 ],
 "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
}
