{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 7 Filters and Rectifiers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7.1 Pg 232"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The resistor value is = 1.59  k ohm \n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "from math import pi, sqrt\n",
    "# Design active low filter with cut-off frequency 10 kHz\n",
    "fc = 10 # # kHz\n",
    "C = 0.01 # #uF # we assume\n",
    "\n",
    "# the cut-off frequency of active low pass filter is defined as\n",
    "# fc = (1/2*pi*R3*C)#\n",
    "\n",
    "# R3 can be calculated as\n",
    "R3 = (1/(2*pi*fc*C))#\n",
    "print 'The resistor value is = %0.2f'%R3,' k ohm '"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7.2 Pg 233"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The resistor value is = 106  ohm \n",
      "The pass band gain is = 1.50  \n"
     ]
    }
   ],
   "source": [
    " # Design active low filter with cut-off frequency 15 kHz\n",
    "fc = 15*10**3 # # Hz \n",
    "C = 0.1*10**-6 # #F # we assume\n",
    "\n",
    "# the cut-off frequency of active low pass filter is defined as\n",
    "# fc = (1/2*pi*R3*C)#\n",
    "\n",
    "# R3 can be calculated as\n",
    "R3 = (1/(2*pi*fc*C))#\n",
    "print 'The resistor value is = %0.f'%R3,' ohm '\n",
    "\n",
    "# the pass band gain of filter is given by\n",
    "# Af = 1+(R2/R1)#\n",
    "# assume that the inverting terminal resistor R2=0.5*R1#\n",
    "# in Af equation if we put R2=0.5R1 in R1 R1 cancellout each other \n",
    "Af = 1+(0.5)\n",
    "print 'The pass band gain is = %0.2f'%Af,' '"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7.3 Pg 234"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The resistor value is = 159  Kohm \n",
      "The resistor R2 value is = 900.00  k ohm \n",
      "The magnitude of an active low pass filter is = 1.96  \n",
      "The phase angle of the filter is = -78.69  \n"
     ]
    }
   ],
   "source": [
    "from __future__ import division\n",
    "# Design active low filter with cut-off frequency 20 kHz\n",
    "fc = 20 # # kHz \n",
    "f = 100 # # frequency of filter\n",
    "Af = 10 #  # desired pass band gain\n",
    "C = 0.05 # #nF # we assume\n",
    "\n",
    "# the cut-off frequency of active low pass filter is defined as\n",
    "# fc = (1/2*pi*R3*C)#\n",
    "\n",
    "# R3 can be calculated as\n",
    "R3 = (1/(2*pi*fc*1e3*C*1e-9))/1e3 # Kohm\n",
    "print 'The resistor value is = %0.f'%R3,' Kohm '\n",
    "\n",
    "# the pass band gain of filter is given by\n",
    "# Af = 1+(R2/R1)#\n",
    "# assume that the inverting terminal resistor R1= 100 k ohm#\n",
    "R1 = 100 # # k ohm\n",
    "R2 = (Af*R1)-R1#\n",
    "print 'The resistor R2 value is = %0.2f'%R2,' k ohm '\n",
    "\n",
    "# the magnitude of an active low pass filter is given as\n",
    "A = Af/(sqrt(1+(f/fc)**2))#\n",
    "print 'The magnitude of an active low pass filter is = %0.2f'%A,' '\n",
    "\n",
    "#the phase angle of the filter\n",
    "from math import atan , degrees\n",
    "Angle = -degrees(atan(f/fc))#\n",
    "print 'The phase angle of the filter is = %0.2f'%Angle,' '"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7.4 Pg 236"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The frequency of the first order low pass filter is = 2.65  kHz \n",
      "The pass band gain of filter is = 13.00 \n"
     ]
    }
   ],
   "source": [
    " # to determine the cut-off frequency and pass band gain Af\n",
    "R1 = 1 # # k ohm\n",
    "R2 = 12 # # k ohm\n",
    "R3 = 1.2 #  # k ohm\n",
    "C = 0.05 # #uF # we assume\n",
    "\n",
    "# the frequency of the first order low pass filter is defined as\n",
    "fc = (1/(2*pi*R3*C))#\n",
    "print 'The frequency of the first order low pass filter is = %0.2f'%fc,' kHz '\n",
    "\n",
    "# the pass band gain of filter is given by\n",
    "Af =(1+R2/R1)#\n",
    "print 'The pass band gain of filter is = %0.2f'%Af,''"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7.5 Pg 236"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The capacitor of high pass filter is = 25.13  uF \n",
      "The second resistor value is = 90.00  K ohm \n"
     ]
    }
   ],
   "source": [
    " # to design a first order high pass filter with cut-off frequency 2kHz\n",
    "Af = 10 #\n",
    "fc = 2 # # kHz \n",
    "R3 = 2 # #K ohm # we assume\n",
    "R1 = 10 # # k ohm\n",
    "# the capacitor of high pass filter is given by\n",
    "C = 2*pi*R3*fc#\n",
    "print 'The capacitor of high pass filter is = %0.2f'%C,' uF '\n",
    "\n",
    "# the voltage gain of the high pass filter is\n",
    "# Af = 1+(R2/R1)#\n",
    "R2 = R1*(Af-1)#\n",
    "print 'The second resistor value is = %0.2f'%R2,' K ohm '"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7.6 Pg 237"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The resistance R3 is = 1.59  K ohm \n"
     ]
    }
   ],
   "source": [
    " # to design an active high pass filter with cut-off frequency 10kHz\n",
    "fc = 10 # # kHz \n",
    "C = 0.01 # #uF # we assume\n",
    "# the cut-off frequency of active high pass filter is given by\n",
    "# fc = 2*pi*R3*C#\n",
    "# R3 can be calculated as\n",
    "R3 = (1/(2*pi*fc*C))#\n",
    "print 'The resistance R3 is = %0.2f'%R3,' K ohm '"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7.7 Pg 238"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The resistance R3 is = 64  Kohm \n",
      "The pass band gain is = 1.20  \n"
     ]
    }
   ],
   "source": [
    "# to design an active high pass filter with cut-off frequency 25kHz\n",
    "fc = 25 # # kHz \n",
    "C = 0.1 # #nF # we assume\n",
    "# the cut-off frequency of active high pass filter is given by\n",
    "# fc = 2*pi*R3*C#\n",
    "# R3 can be calculated as\n",
    "R3 = (1/(2*pi*fc*1e3*C*1e-9)) / 1e3 # Kohm\n",
    "print 'The resistance R3 is = %0.f'%R3,' Kohm '\n",
    "\n",
    "# the desire pass band gain of filter is given by \n",
    "#Af = 1+(R2/R1)#\n",
    "# assume that the inverting terminal resistor R2=0.2*R1#\n",
    "# in Af equation if we put R2=0.2R1 in R1 R1 cancellout each other \n",
    "Af = 1+(0.2)\n",
    "print 'The pass band gain is = %0.2f'%Af,' '"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7.8 Pg 239"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The resistance R3 is = 159  K ohm \n",
      "The resistance R2 is = 700.00  K ohm \n",
      "The magnitude of an active high pass filter is = 14.55  \n",
      "The phase angle of the filter is = 14.04  degree\n"
     ]
    }
   ],
   "source": [
    "## # to design an active high pass filter with cut-off frequency 20kHz \n",
    "Af = 15 #\n",
    "fc = 20 # #kHz\n",
    "f = 80 # # kHz  the frequency of filter \n",
    "C = 0.05 # #nF # we assume\n",
    "# the cut-off frequency of active high pass filter is given by\n",
    "# fc = 2*pi*R3*C#\n",
    "# R3 can be calculated as\n",
    "R3 = (1/(2*pi*fc*C))#\n",
    "print 'The resistance R3 is = %0.f'%(R3*1000),' K ohm '  # Round Off Error\n",
    "\n",
    "# the desire pass band gain of filter is given by \n",
    "#Af = 1+(R2/R1)#\n",
    "# assume that the inverting terminal resistor R1=50 K ohm#\n",
    "R1 = 50 # # K ohm\n",
    "R2 = (R1*Af)-(R1)\n",
    "print 'The resistance R2 is = %0.2f'%R2,' K ohm '\n",
    "\n",
    "# the magnitude of an active high pass filter is given as\n",
    "A = Af*(f/fc)/(sqrt(1+(f/fc)**2))#\n",
    "print 'The magnitude of an active high pass filter is = %0.2f'%A,' '\n",
    "\n",
    "#the phase angle of the filter\n",
    "from numpy import inf\n",
    "Angle = degrees(-atan(f/fc)+atan(inf))\n",
    "print 'The phase angle of the filter is = %0.2f'%Angle,' degree'  # Round Off Error"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7.9 Pg 241"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The lower cut-off frequency FLC of band pass filter is = 159.2  Hz \n",
      "The upper cut-off frequency FUC of band pass filter is = 15.92  kHz \n"
     ]
    }
   ],
   "source": [
    "# to calculate upper and lower cut-off frequency of the band pass filter\n",
    "R1 = 10*10**3 # #K ohm\n",
    "R2 = 10 # #K ohm\n",
    "C1 = 0.1*10**-6 # # uF\n",
    "C2 = 0.001 #  #uF\n",
    "\n",
    "# the lower cut-off frequency of band pass filter is\n",
    "fLC = 1/(2*pi*R1*C1)#\n",
    "print 'The lower cut-off frequency FLC of band pass filter is = %0.1f'%fLC,' Hz '\n",
    "\n",
    "# The upper cut-off frequency of band pass filter is\n",
    "fUC = 1/(2*pi*R2*C2)#\n",
    "print 'The upper cut-off frequency FUC of band pass filter is = %0.2f'%fUC,' kHz '"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7.10 Pg 242"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The resistance R3 Value is = 7.96  M ohm \n",
      "The resistance R6 value is = 1.59  M ohm \n"
     ]
    }
   ],
   "source": [
    "# to design an active band pass filter with lower cut-off frequency 10 kHz an upper 50 kHz\n",
    "fL = 10 # # kHz\n",
    "fH = 50 # # kHz\n",
    "C1 = 0.002 # # nF\n",
    "C2 = 0.002 #  # nF\n",
    "\n",
    "# the lower cut-off frequency of band pass filter is\n",
    "# fL = 1/(2*pi*R3*C1)#\n",
    "R3 = 1/(2*pi*fL*C1)#\n",
    "print 'The resistance R3 Value is = %0.2f'%R3,' M ohm '\n",
    "\n",
    "# The upper cut-off frequency of band pass filter is\n",
    "# fH = 1/(2*pi*R6*C2)#\n",
    "R6 = 1/(2*pi*fH*C2)#\n",
    "print 'The resistance R6 value is = %0.2f'%R6,' M ohm '"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7.11 Pg 243"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The resistance R3 Value is = 7.96  M ohm \n",
      "The resistance R6 value is = 3.98  M ohm \n",
      "The desire pass band gain of filter is = 15  \n"
     ]
    }
   ],
   "source": [
    "# to design an active band pass filter with lower cut-off frequency 20 kHz an upper 40 kHz\n",
    "fL = 20 # # kHz\n",
    "fH = 40 # # kHz\n",
    "# the inverting terminal resistance 2R1=R2 and 4R4=R5\n",
    "C1 = 0.001 # # nF\n",
    "C2 = 0.001 #  # nF\n",
    "\n",
    "# the lower cut-off frequency of band pass filter is\n",
    "# fL = 1/(2*pi*R3*C1)#\n",
    "R3 = 1/(2*pi*fL*C1)#\n",
    "print 'The resistance R3 Value is = %0.2f'%R3,' M ohm '\n",
    "\n",
    "# The upper cut-off frequency of band pass filter is\n",
    "# fH = 1/(2*pi*R6*C2)#\n",
    "R6 = 1/(2*pi*fH*C2)#\n",
    "print 'The resistance R6 value is = %0.2f'%R6,' M ohm '\n",
    "\n",
    "# the desire pass band gain of filter is defined as\n",
    "R1 = 1 # # M ohm we assume\n",
    "#we define inverting terminal resistance 2R1=R2\n",
    "R2 = 2 # # M ohm\n",
    "# then\n",
    "R4 = 1 # #M ohm\n",
    "R5 = 4 # # M ohm\n",
    "Af = (1+(R2/R1))*(1+(R5/R4))#\n",
    "print 'The desire pass band gain of filter is = %d'%Af,' '"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7.12 Pg 244"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The resistance R3 Value is = 7.96  M ohm \n",
      "The resistance R6 value is = 1.99  M ohm \n",
      "The desire pass band gain of filter is = 15.00  \n",
      "The magnitude of gain of band pass filter  is = 11.49  \n",
      "The phase angle of gain of band pass filter  is = 50 degree\n"
     ]
    }
   ],
   "source": [
    "# to design an active band pass filter with lower cut-off frequency 20 kHz an upper 80 kHz\n",
    "f = 100 # # kHz  the frequency of band pass filter\n",
    "fL = 20 # # kHz\n",
    "fH = 80 # # kHz\n",
    "# the inverting terminal resistance R1=0.5*R2 and R4=0.25*R5\n",
    "C1 = 0.001 # # nF\n",
    "C2 = 0.001 #  # nF\n",
    "\n",
    "# the lower cut-off frequency of band pass filter is\n",
    "# fL = 1/(2*pi*R3*C1)#\n",
    "R3 = 1/(2*pi*fL*C1)#\n",
    "print 'The resistance R3 Value is = %0.2f'%R3,' M ohm '\n",
    "\n",
    "# The upper cut-off frequency of band pass filter is\n",
    "# fH = 1/(2*pi*R6*C2)#\n",
    "R6 = 1/(2*pi*fH*C2)#\n",
    "print 'The resistance R6 value is = %0.2f'%R6,' M ohm '  # Round Off Error\n",
    "\n",
    "# the desire pass band gain of filter is defined as\n",
    "R1 = 1 # # M ohm we assume\n",
    "#we define inverting terminal resistance R1=0.5*R2\n",
    "R2 = 2 # # M ohm\n",
    "# then\n",
    "R4 = 1 # #M ohm\n",
    "R5 = 4 # # M ohm\n",
    "Af = (1+(R2/R1))*(1+(R5/R4))#\n",
    "print 'The desire pass band gain of filter is = %0.2f'%Af,' '\n",
    "\n",
    "# the magnitude of gain of band pass filter is given as\n",
    "A = Af*(f**2/(fL*fH))/((sqrt(1+(f/fL)**2))*(sqrt(1+(f/fH)**2)))#\n",
    "print 'The magnitude of gain of band pass filter  is = %0.2f'%A,' '  # Round Off Error\n",
    "\n",
    "#the phase angle of the filter\n",
    "from numpy import inf\n",
    "Angle = degrees(2*atan(inf)-atan(f/fL)-atan(f/fH))\n",
    "print 'The phase angle of gain of band pass filter  is = %0.f'%Angle,'degree'  # Round Off Error"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7.13 Pg 247"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The output of the half wave precision rectifier Vo is = -20.00  V \n"
     ]
    }
   ],
   "source": [
    " # to determine the output voltage of the precision rectifier circuit\n",
    "Vi = 10 # #V i/p volt\n",
    "R1 = 20 # # K ohm\n",
    "R2 = 40 # # K ohm\n",
    "Vd = 0.7 # # V   the diode voltage drop\n",
    "\n",
    "# the output of the half wave precision rectifier is defined as\n",
    "# Vo = -(R2/R1)*Vi # for Vi < 0\n",
    "#    = 0 otherwise\n",
    "# i.e for Vi > 0\n",
    "#              Vo = 0\n",
    "# for Vi < 0\n",
    "Vo = -(R2/R1)*Vi\n",
    "print 'The output of the half wave precision rectifier Vo is = %0.2f'%Vo,' V '"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7.14 Pg 247"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The output of the half wave precision rectifier Vo is = -15.00  V \n",
      "The output of the half wave precision rectifier Vo is = 15.00  V \n"
     ]
    }
   ],
   "source": [
    "# to determine the output voltage of the precision rectifier circuit for i/p voltage a) Vi = 5 b) Vi = -5\n",
    "Vi = 5 # #V i/p volt\n",
    "R1 = 5 # # K ohm\n",
    "R2 = 15 # # K ohm\n",
    "Vd = 0.7 # # V   the diode voltage drop\n",
    "\n",
    "# the output of the half wave precision rectifier is defined as\n",
    "# Vo = -(R2/R1)*Vi # for Vi < 0\n",
    "#    = 0 otherwise\n",
    "\n",
    "# for Vi = 5 V\n",
    "# i.e for Vi > 0\n",
    "#              Vo = 0\n",
    "# for Vi < 0\n",
    "Vo = -(R2/R1)*Vi#\n",
    "print 'The output of the half wave precision rectifier Vo is = %0.2f'%Vo,' V '\n",
    "\n",
    "# for Vi = -5 V\n",
    "# i.e for Vi > 0\n",
    "#              Vo = 0\n",
    "# for Vi < 0\n",
    "Vi =-5 # # V\n",
    "Vo = -(R2/R1)*Vi#\n",
    "print 'The output of the half wave precision rectifier Vo is = %0.2f'%Vo,' V '"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 7.15 Pg 248"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The gain of precision full wave rectifier A is = 6.00  \n",
      "The output voltage Vo is = 42.00  V \n",
      "The output voltage Vo is = 42.00  V \n"
     ]
    }
   ],
   "source": [
    " # to determine the output voltage of the precision rectifier circuit for i/p voltage a) Vi = 7 b) Vi = -7\n",
    "Vi = 7 # #V i/p volt\n",
    "R1 = 5 # # K ohm\n",
    "R3 = 5 # # K ohm\n",
    "R4 = 5 # # K ohm\n",
    "R2 = 15 # # K ohm\n",
    "R5 = 15 # # K ohm\n",
    "Vd = 0.7 # # V   the diode voltage drop\n",
    "\n",
    "# the output of the full wave precision rectifier is defined as\n",
    "# Vo = -A*Vi #      for Vi < 0                      equation 1\n",
    "#    = A*Vi  #      otherwise                       equation 2\n",
    "\n",
    "# or  Vo = abs(A*Vi) #\n",
    "\n",
    "# The gain of precision full wave rectifier\n",
    "A = (((R2*R5)/(R1*R3))-(R5/R4)) #\n",
    "print 'The gain of precision full wave rectifier A is = %0.2f'%A,' '\n",
    "\n",
    "\n",
    "# for Vi = 7 V           the output voltage is\n",
    "Vi = 7 #\n",
    "Vo = -A*Vi #      # from equation 1\n",
    "Vo =  A*Vi #      # from equation 2\n",
    "Vo = abs(A*Vi) #\n",
    "print 'The output voltage Vo is = %0.2f'%Vo,' V '\n",
    "\n",
    "# for Vi = -7 V           the output voltage is\n",
    "Vi = -7 #\n",
    "Vo = -A*Vi #      # from equation 1\n",
    "Vo =  A*Vi #      # from equation 2\n",
    "Vo = abs(A*Vi) #\n",
    "print 'The output voltage Vo is = %0.2f'%Vo,' V '"
   ]
  }
 ],
 "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
}