{
 "metadata": {
  "name": "",
  "signature": "sha256:5d045633a134a6b88641045e0705da6aba6d8735cb400399955dbb6bc55628cc"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 7 - Sampling Theory and Pulse modulation"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 1 - pg 324"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#calculate the nyquist rate\n",
      "\n",
      "import math\n",
      "#given\n",
      "#analog signal x(t) = 3*cos(50*math.pi*t) + 10*sin(300*math.pi*t) - cos(100*math.pi*t)\n",
      "#comparing signal with x(t) = 3*cos(w_1*t) + 10*sin(w_2*t) - cos(w_3*t)\n",
      "#therefore\n",
      "w_1 = 50*math.pi;#first frequency in rad/sec\n",
      "w_2 = 300*math.pi;#second frequency in rad/sec\n",
      "w_3 = 100*math.pi;#third frequency in rad/sec\n",
      "\n",
      "#calculations\n",
      "f_1 = w_1/(2*math.pi);#first frequency in Hz\n",
      "f_2 = w_2/(2*math.pi);#second frequency in Hz\n",
      "f_3 = w_3/(2*math.pi);#third frequency in Hz\n",
      "f_m = f_2#maximum frequency \n",
      "f_s = 2*f_m#nyquist rate for a signal\n",
      "\n",
      "#results\n",
      "print \"Nyquist rate (Hz) = \",f_s\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Nyquist rate (Hz) =  300.0\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 2 - pg 325"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#calculate the Nyquist rate and interval\n",
      "import math\n",
      "#given\n",
      "#x(t) = (1/()2*math.pi))*cos(4000*math.pi*t)*cos(1000*math.pi*t)\n",
      "#expanding\n",
      "print(\"x(t) = (1/(2*math.pi)*cos(4000*math.pi*t)*cos(1000*math.pi*t)\");\n",
      "print(\"x(t) = (1/(4*math.pi)*2*cos(4000*math.pi*t)*cos(1000*math.pi*t)\");\n",
      "print(\"x(t) = (1/(4*math.pi))*[cos(4000*math.pi*t + 1000*pi*t)*cos(4000*math.pi*t - 1000*math.pi*t)]\")\n",
      "print(\"x(t) = (1/(4*math.pi))*[cos(5000*math.pi*t + cos(3000*math.pi*t))]\")\n",
      "#by comparing above equation with x(t) = (1/(4*math.pi))*[cos(w_1*t) + cos(w_2*t)] \n",
      "w_1 = 5000.*math.pi\n",
      "w_2 = 3000.*math.pi\n",
      "\n",
      "#calculations\n",
      "f_1 = w_1/(2*math.pi);\n",
      "f_2 = w_2 /(2*math.pi);\n",
      "f_m = f_1\n",
      "f_s = 2*f_m#Nyquist rate\n",
      "T_s = 1/f_s#Nyquist interval\n",
      "\n",
      "#results\n",
      "print \"Nyquist rate (Hz) = \",f_s\n",
      "print \"Nyquist interval  (msec) = \",T_s*1000\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "x(t) = (1/(2*math.pi)*cos(4000*math.pi*t)*cos(1000*math.pi*t)\n",
        "x(t) = (1/(4*math.pi)*2*cos(4000*math.pi*t)*cos(1000*math.pi*t)\n",
        "x(t) = (1/(4*math.pi))*[cos(4000*math.pi*t + 1000*pi*t)*cos(4000*math.pi*t - 1000*math.pi*t)]\n",
        "x(t) = (1/(4*math.pi))*[cos(5000*math.pi*t + cos(3000*math.pi*t))]\n",
        "Nyquist rate (Hz) =  5000.0\n",
        "Nyquist interval  (msec) =  0.2\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 3 - pg 326"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#calculate the discrete time signal for all conditions\n",
      "\n",
      "#given\n",
      "#x(t) = 8*cos(200*%pi*t)\n",
      "f= 100.#highest frequency component of continuous time signal in hertz\n",
      "f_s2 = 400.#sampling frequency in hertz for second condition\n",
      "f_s3 = 400.#sampling frequency in hertz for third condition\n",
      "f_s4 = 150.#sampling frequency in hertz for fourth condition since 0 < f_s4 < f_s2/2 \n",
      "\n",
      "#calcultions\n",
      "NR = 2*f#Nyquist rate\n",
      "F_1 = f/NR;\n",
      "F_2 = f/f_s2;\n",
      "F_3 = f/f_s3;\n",
      "F_4 = f/f_s4;\n",
      "f_4 = f_s4*F_4;\n",
      "\n",
      "#results\n",
      "print \"The discrete time signal x(n) for the first condition is x(n) = 8*cos(2*pi*\",F_1,\"*n)\"\n",
      "print \"the discrete time signal x(n) for the second condition is x(n) = 8*cos(2*pi*\",F_2,\"*n)\"\n",
      "print \"the discrete time signal x(n) for the third condition is x(n) = 8*cos(2*pi*\",F_3,\"*n)\"\n",
      "print \"The discrete time signal x(n) for the fourth condition is x(n) = 8*cos(2*pi*\",f_4,\"*t)\"\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The discrete time signal x(n) for the first condition is x(n) = 8*cos(2*pi* 0.5 *n)\n",
        "the discrete time signal x(n) for the second condition is x(n) = 8*cos(2*pi* 0.25 *n)\n",
        "the discrete time signal x(n) for the third condition is x(n) = 8*cos(2*pi* 0.25 *n)\n",
        "The discrete time signal x(n) for the fourth condition is x(n) = 8*cos(2*pi* 100.0 *t)\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4 - pg 327"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#calculate the nyquist rate\n",
      "import math\n",
      "#given\n",
      "#x(t) = 6*cos(50*math.pi*t)  + 20*sin(300*math.pi*t) - 10*cos(100*math.pi*t)\n",
      "#by comparing with standard eqn x(t) = A_1*cos(w_1*t)  + A_2*sin(w_2*t) + A_3*cos(w_3*t) we get \n",
      "w_1 = 50*math.pi#frequency in rad/sec\n",
      "w_2 =300*math.pi#frequency in rad/sec\n",
      "w_3 = 100*math.pi#frequency in rad/sec\n",
      "\n",
      "#calculations\n",
      "f_1 = w_1/(2*math.pi)#frequency in hertz\n",
      "f_2 = w_2/(2*math.pi)#frequency in hertz\n",
      "f_3 = w_3/(2*math.pi)#frequency in hertz\n",
      "if f_1 > f_2  and f_1> f_3:\n",
      "     f_max = f_1\n",
      "elif f_2 > f_1 and f_2> f_3:\n",
      "    f_max = f_2\n",
      "elif f_3 > f_1 and f_3> f_2:\n",
      "    f_max = f_3\n",
      "\n",
      "f_s = 2*f_max;#nyquist rate\n",
      "\n",
      "#results\n",
      "print \"Nyquist rate for a continuous signal (Hz) = \",f_s\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Nyquist rate for a continuous signal (Hz) =  300.0\n"
       ]
      }
     ],
     "prompt_number": 5
    }
   ],
   "metadata": {}
  }
 ]
}