{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h1>Chapter 2: Antenna Basics<h2>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2-3.1, Page number: 14<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "\n",
      "#Variable declaration\n",
      "e_half_power = 1/math.sqrt(2)    #E(theta) at half power (relative quantity)\n",
      "\n",
      "#Calculation\n",
      "theta = math.acos(math.sqrt(e_half_power)) # theta (radians)\n",
      "hpbw = 2*theta*180/math.pi     # Half power beamwidth (degrees)\n",
      "\n",
      "#Result\n",
      "print \"The half power beamwidth is \", round(hpbw), \"degrees\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The half power beamwidth is  66.0 degrees\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2-3.2, Page number: 14<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "\n",
      "#Variable declaration\n",
      "e_half_power = 1/math.sqrt(2)   #E(theta) at half power(unitless)\n",
      "e_null = 0      #E(theta) = 0 at null points (unitless)\n",
      "theta_1 = 0     #theta' (degrees)\n",
      "theta = 1       #theta (degrees)\n",
      "\n",
      "#Calculation\n",
      "for x in range(3):      #Iterate till theta = i\n",
      "    theta = 0.5*math.acos(e_half_power/math.cos(theta_1*math.pi/180))  \n",
      "                        #theta(radian)\n",
      "    theta_1 = theta*180/math.pi       #set i = theta for next iteration (degrees)\n",
      "        \n",
      "hpbw = 2*(theta*180/math.pi)    #Half-power beamwidth (Degrees)\n",
      "theta = 0.5*math.acos(e_null)   #theta (radians)\n",
      "fnbw = 2*(theta*180/math.pi)    #Beamwidth between first null (degrees)\n",
      "\n",
      "#Result\n",
      "print \"The half power beamwidth is\", round(hpbw), \"degrees\"\n",
      "print \"The beamwidth between first nulls is \", round(fnbw), \"degrees\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The half power beamwidth is 41.0 degrees\n",
        "The beamwidth between first nulls is  90.0 degrees\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2-4.1, Page number: 17<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import scipy.integrate\n",
      "import math\n",
      "\n",
      "#Variable declaration\n",
      "def integrand(x):\n",
      "    return math.sin(x)      #Function sin(theta)\n",
      "\n",
      "def integrand2(x):\n",
      "    return 1                #Contant function\n",
      "\n",
      "#Calculation\n",
      "omega = scipy.integrate.quad(integrand, 20*math.pi/180, 40*math.pi/180) \n",
      "omega = omega[0]*(180/math.pi) * scipy.integrate.quad(integrand2 ,30 ,70)[0]\n",
      "#Integration between the ranges gives solid angle, omega (square degrees)\n",
      "\n",
      "#Result\n",
      "print \"The solid angle, omega is\", round(omega), \"square degrees\" "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The solid angle, omega is 398.0 square degrees\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2-4.2, Page number: 17<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "import scipy.integrate\n",
      "\n",
      "#Variable declaration\n",
      "def func1(x,y):\n",
      "    return (math.cos(x)**4)*math.sin(x)*1 #Function for integration\n",
      "\n",
      "#Calculation\n",
      "beam_area = scipy.integrate.dblquad(func1, 0, 2*math.pi, \n",
      "                                    lambda x: 0, lambda x: math.pi/2)\n",
      "#Beam area (steradians)\n",
      "\n",
      "#Result\n",
      "print \"The beam area of the given pattern is\", round(beam_area[0], 2), \"sr\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The beam area of the given pattern is 1.26 sr\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2-7.1, Page number: 21<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import scipy.integrate\n",
      "from math import pi,sin,cos,log10\n",
      "\n",
      "#Variable declaration\n",
      "n = 10     #Number of isotropic point sources\n",
      "dr = pi/2    #Distance(radians)\n",
      "hpbw = 40    #Half power beamwidth (degrees)\n",
      "def integrand(x,phi):\n",
      "    E_norm  = (sin(pi/20))*(sin((pi/2)*(5*cos(phi)-6))/sin((pi/20)*(5*cos(phi)-6)))\n",
      "    return (E_norm**2)\n",
      "\n",
      "#Calculation\n",
      "gain = scipy.integrate.dblquad(integrand, 0, 2*pi,\n",
      "                                        lambda x: 0, lambda x: pi/2)[0]\n",
      "gain = (4*pi)/gain    #Gain (unitless)\n",
      "gain_db = 10*log10(gain)#Gain (dB)\n",
      "gain_hpbw = 40000/(hpbw**2)    #Gain from approx. equation (unitless)\n",
      "gain_hpbw_db = 10*log10(gain_hpbw)    #Gain from approx. equation (dB)\n",
      "gain_diff = gain_hpbw_db - gain_db    #Difference in gain (dB)\n",
      "\n",
      "#Result\n",
      "print \"The gain G is\", round(gain_db,2),\"dB\"\n",
      "print \"The gain from approx. equation is\", round(gain_hpbw_db),\"dB\"\n",
      "print \"The difference is\", round(gain_diff,2),\"dB\"\n",
      "\n",
      "#The solution is incorrect due to the incorrect integration of the normalized power pattern\n",
      "#As a result, the difference in gain is slightly higher"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The gain G is 10.01 dB\n",
        "The gain from approx. equation is 14.0 dB\n",
        "The difference is 3.97 dB\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2-7.2, Page number: 21<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "import scipy.integrate\n",
      "\n",
      "#Variable declaration\n",
      "theta_hp = 90\n",
      "phi_hp = 90\n",
      "\n",
      "def integrand(theta, phi):\n",
      "    return ((math.sin(theta)**3)*(math.sin(phi)**2))\n",
      "\n",
      "#Calculation\n",
      "direct_exact = 4*math.pi/scipy.integrate.dblquad(integrand, 0, math.pi,\n",
      "                                        lambda x: 0, lambda x: math.pi)[0]\n",
      "                                        #Exact Directivity(No unit) \n",
      "direct_apprx = 41253.0/(theta_hp*phi_hp)    #Approximate directivity (No unit)\n",
      "db_diff = 10*math.log10(direct_exact/direct_apprx) #Difference (decibels)\n",
      "\n",
      "#Result\n",
      "print \"The exact directivity is\", round(direct_exact, 1)\n",
      "print \"The approximate directivity is\", round(direct_apprx,1)\n",
      "print \"The decibel difference is \", round(db_diff, 1), \"dB\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The exact directivity is 6.0\n",
        "The approximate directivity is 5.1\n",
        "The decibel difference is  0.7 dB\n"
       ]
      }
     ],
     "prompt_number": 18
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2-10.1, Page number: 28<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "\n",
      "#Variable declaration\n",
      "Z = 120*math.pi                 #Intrinsic impedence of free space (ohm)\n",
      "\n",
      "#Calculation\n",
      "max_aper = Z/(320*math.pi**2)   #Max. effective aperture (lambda^2)\n",
      "direct = 4*math.pi*max_aper     #directivity (unitless)\n",
      "\n",
      "#Result\n",
      "print \"The maximum effective aperture is\", round(max_aper, 3), \"lambda^2\"\n",
      "print \"The direcitivity is\", direct"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The maximum effective aperture is 0.119 lambda^2\n",
        "The direcitivity is 1.5\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2-10.2, Page number: 29<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "\n",
      "#Variable declaration\n",
      "R_r = 73                            #Radiation resistance (ohm)\n",
      "\n",
      "#Calculation\n",
      "eff_aper = 30/(R_r*math.pi)         #Effective aperture (lambda^2)\n",
      "directivity = 4*math.pi*eff_aper    #Directivity (unitless)\n",
      "\n",
      "#Result\n",
      "print \"The effective aperture is\", round(eff_aper, 2), \"lambda^2\"\n",
      "print \"The directivity is\", round(directivity,2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The effective aperture is 0.13 lambda^2\n",
        "The directivity is 1.64\n"
       ]
      }
     ],
     "prompt_number": 20
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2-11.1, Page number: 31<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Variable declaration\n",
      "P_t = 15        #Transmitter power (W)\n",
      "A_et = 2.5      #Effective aperture of transmitter (meter^2)\n",
      "A_er = 0.5      #Effective aperture of receiver (meter^2)\n",
      "r = 15e3        #Distance between the antennas (Line of sight) (m)\n",
      "freq  = 5e9     #Frequency (Hz)\n",
      "c = 3e8         #speed of light (m/s)\n",
      "\n",
      "#Calculation\n",
      "wave_len = c/freq                               #Wavelength (m)\n",
      "P_r = (P_t*A_et*A_er)/((r**2)*(wave_len**2))    #received power (W)\n",
      "\n",
      "#Result\n",
      "print \"The power delivered to the receiver is\", round(P_r,6), \"watts\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The power delivered to the receiver is 2.3e-05 watts\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2-16.1, Page number: 40<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Variable declaration\n",
      "E1 = 3      #Magnitude of electric field in x direction (V/m)\n",
      "E2 = 6      #Magnitude of electric field in y direction (V/m)\n",
      "Z = 377     #Intrinsic impedence of free space (ohm)\n",
      "\n",
      "#Calculation\n",
      "avg_power = 0.5*(E1**2 + E2**2)/Z       #average power per unit area (W/m^2)\n",
      "\n",
      "#Result\n",
      "print \"The average power per unit area is\", avg_power, \"watts/meter^2\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The average power per unit area is 0.0596816976127 watts/meter^2\n"
       ]
      }
     ],
     "prompt_number": 22
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2-17.1, Page number: 43<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "\n",
      "#Variable declaration\n",
      "AR_w = 4          #Axial Ratio for left elliptically polarized wave (unitless)\n",
      "tau_w = 15        #Tilt angle for left elliptically polarized wave (degrees)\n",
      "AR_a = -2         #Axial Ratio for right elliptically polarized wave (unitless)  \n",
      "tau_a = 45        #Tilt angle for right elliptically polarized wave (degrees)\n",
      "tau_w2 = 20.7     #2*Tilt angle for left elliptically polarized wave (degrees)    \n",
      "tau_a2 = 39.3     #2*Tilt angle for right elliptically polarized wave (degrees)\n",
      "\n",
      "#Calculation\n",
      "eps_a2 = 2*math.atan2(1,AR_a)*180/math.pi  #polarisation latitude (degrees)\n",
      "eps_w2 = 2*math.atan2(1,AR_w)*180/math.pi   #antenna latitude (degrees)\n",
      "gamma_w2 =math.acos(math.cos(eps_w2*math.pi/180)*math.cos(tau_w2*math.pi/180));\n",
      "            #great-circle angle - antenna (radians)\n",
      "gamma_a2 =math.acos(math.cos(eps_a2*math.pi/180)*math.cos(tau_a2*math.pi/180));\n",
      "            #great-circle angle - wave (radians)\n",
      "M_Ma = (gamma_w2*180/math.pi) + (gamma_a2*180/math.pi)  \n",
      "            #total great-circle angle (degrees)\n",
      "F = math.cos((M_Ma/2)*math.pi/180)**2    \n",
      "            #Polarisation matching factor (relative quantity)\n",
      "\n",
      "#Result\n",
      "print \"The polarization matching factor is\", round(F,2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The polarization matching factor is 0.44\n"
       ]
      }
     ],
     "prompt_number": 23
    }
   ],
   "metadata": {}
  }
 ]
}