{
 "metadata": {
  "name": "Pweaved ipython notebook"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "% FIR filter design with Python and SciPy\n",
      "% Matti Pastell\n",
      "% 15th April 2013\n",
      "\n",
      "# Introduction\n",
      "\n",
      "This an example of a script that can be published using\n",
      "[Pweave](http://mpastell.com/pweave). The script can be executed\n",
      "normally using Python or published to HTML with Pweave\n",
      "Text is written in markdown in lines starting with \"`#'` \" and code\n",
      "is executed and results are included in the published document.\n",
      "The concept is similar to\n",
      "publishing documents with [MATLAB](http://mathworks.com) or using\n",
      "stitch with [Knitr](http://http://yihui.name/knitr/demo/stitch/).\n",
      "\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "from pylab import *\n",
      "import scipy.signal as signal\n",
      "    \n",
      "#Plot frequency and phase response\n",
      "def mfreqz(b,a=1):\n",
      "    w,h = signal.freqz(b,a)\n",
      "    h_dB = 20 * log10 (abs(h))\n",
      "    subplot(211)\n",
      "    plot(w/max(w),h_dB)\n",
      "    ylim(-150, 5)\n",
      "    ylabel('Magnitude (db)')\n",
      "    xlabel(r'Normalized Frequency (x$\\pi$rad/sample)')\n",
      "    title(r'Frequency response')\n",
      "    subplot(212)\n",
      "    h_Phase = unwrap(arctan2(imag(h),real(h)))\n",
      "    plot(w/max(w),h_Phase)\n",
      "    ylabel('Phase (radians)')\n",
      "    xlabel(r'Normalized Frequency (x$\\pi$rad/sample)')\n",
      "    title(r'Phase response')\n",
      "    subplots_adjust(hspace=0.5)\n",
      "\n",
      "#Plot step and impulse response\n",
      "def impz(b,a=1):\n",
      "    l = len(b)\n",
      "    impulse = repeat(0.,l); impulse[0] =1.\n",
      "    x = arange(0,l)\n",
      "    response = signal.lfilter(b,a,impulse)\n",
      "    subplot(211)\n",
      "    stem(x, response)\n",
      "    ylabel('Amplitude')\n",
      "    xlabel(r'n (samples)')\n",
      "    title(r'Impulse response')\n",
      "    subplot(212)\n",
      "    step = cumsum(response)\n",
      "    stem(x, step)\n",
      "    ylabel('Amplitude')\n",
      "    xlabel(r'n (samples)')\n",
      "    title(r'Step response')\n",
      "    subplots_adjust(hspace=0.5)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "\n",
      "## Lowpass FIR filter\n",
      "\n",
      "Designing a lowpass FIR filter is very simple to do with SciPy, all you\n",
      "need to do is to define the window length, cut off frequency and the\n",
      "window.\n",
      "\n",
      "The Hamming window is defined as:\n",
      "$w(n) = \\alpha - \\beta\\cos\\frac{2\\pi n}{N-1}$, where $\\alpha=0.54$ and $\\beta=0.46$ \n",
      "\n",
      "The next code chunk is executed in term mode, see the [Python script](FIR_design.py) for syntax.\n",
      "Notice also that Pweave can now catch multiple figures/code chunk.\n",
      "\n",
      "\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "n = 1001\n",
      "a = signal.firwin(n, cutoff = [0.2, 0.5], window = 'blackmanharris', pass_zero = False)\n",
      "mfreqz(a)\n",
      "show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": []
    }
   ],
   "metadata": {}
  }
 ]
}