{
 "metadata": {
  "name": "",
  "signature": "sha256:2b685229d679e1d41a25fddbfc5abcd92050271aa14c1131a12e709bfacddba1"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import h5py\n",
      "import numpy as np\n",
      "import matplotlib.pyplot as plt"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "file    = h5py.File('test_rps.hdf5', 'r')   # 'r' means that hdf5 file is open in read-only mode\n",
      "dataset = file['RPS01']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print dataset.value[4,2]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "36.7822\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "plt.plot( dataset.value[:,0], dataset.value[:,2], color = 'r')\n",
      "#plt.xlim([1.45415117E9,1.4541513E9])\n",
      "plt.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "prev_val= dataset.value[0,2]\n",
      "n = 0\n",
      "angle = np.zeros((dataset.shape[0]))\n",
      "for i in range(dataset.value.shape[0]):\n",
      "    if (dataset.value[i,2] - prev_val) > 300:\n",
      "        n -= 1\n",
      "        angle[i] = dataset.value[i,2] + n*360\n",
      "        prev_val = dataset.value[i,2]\n",
      "    elif -(dataset.value[i,2] - prev_val) > 300:  # compute angular speed in backward direction.\n",
      "        n += 1\n",
      "        angle[i] = dataset.value[i,2] - n*360\n",
      "        prev_val = dataset.value[i,2]\n",
      "    else:\n",
      "        angle[i] = dataset.value[i,2] + n*360\n",
      "        prev_val = dataset.value[i,2]\n",
      "        "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 57
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Five point difference numerical calculation. Source: http://mathfun528.blogspot.cz/2011/07/numerical-differentiation.html"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "angle_speed = np.zeros_like(angle)\n",
      "\n",
      "for i in range(2,angle.shape[0]-2):\n",
      "     angle_speed[i] = (-angle[i + 2] + 8*angle[i + 1] - 8*angle[i - 1] + angle[i - 2])/12"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 58
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "fig, ax1 = plt.subplots()\n",
      "\n",
      "ax2 = ax1.twinx()\n",
      "ax1.set_xlabel('Sample #')\n",
      "ax1.set_ylabel('Angle')\n",
      "ax2.set_ylabel('Angular speed')\n",
      "\n",
      "ax1.plot(dataset.value[:,0], angle,'b',dataset.value[:,0], dataset.value[:,2],'r')\n",
      "ax2.plot(dataset.value[:,0], angle_speed,'g')\n",
      "\n",
      "plt.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 59
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 45
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 45
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 45
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 45
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 45
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}