text: {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Jupyter Helloworld\n",
      "\n",
      "This is inside a docker container.\n",
      "\n",
      "### Quick run on Mac\n",
      "\n",
      "```bash\n",
      "$ brew cask install docker\n",
      "# Run Applications -> Docker\n",
      "$ docker run -it --rm -p 8888:8888 -v `pwd`:/home/jovyan/work jupyter/tensorflow-notebook\n",
      "\n",
      "# Connect to container with env variables loaded\n",
      "$ docker ps\n",
      "$ docker exec -t -i container_name start.sh /bin/bash\n",
      "\n",
      "# To strip output for git commits\n",
      "$ docker exec -t -i container_name start.sh /bin/bash\n",
      "$ pip install --upgrade nbstripout\n",
      "$ nbstripout helloworld.ipynb\n",
      "```\n",
      "    \n",
      "More: https://github.com/jupyter/docker-stacks/tree/master/tensorflow-notebook"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "import numpy as np\n",
      "import tensorflow as tf"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "* *Ctrl-Enter* - Run cell\n",
      "* *Esc* - Command mode\n",
      "* *Enter* - Edit mode\n",
      "* *Shift-Enter* - New cell below\n",
      "* *Esc - M* - Make cell Markdown"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "a = np.array([10,20,30])\n",
      "b = np.random.randn(1000)\n",
      "c = np.random.rand(1000)\n",
      "a"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "import matplotlib\n",
      "import matplotlib.pyplot as plt\n",
      "a.shape[0]"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "plt.plot(b)\n",
      "plt.plot(c)"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "plt.hist(b)"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "print(np.arange(5))\n",
      "print(np.arange(5, 10))\n",
      "print(np.arange(1, 10, 2))\n",
      "\n",
      "print(np.arange(5) + 4)"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "a = np.array([np.arange(5), np.arange(5, 10), np.arange(11, 16)])\n",
      "print(a.shape)\n",
      "print(a)"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Interactivity"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "from ipywidgets import interact, interactive, fixed\n",
      "import ipywidgets as widgets\n",
      "\n",
      "def plotter(start, end, step):\n",
      "    plt.plot(np.sin(np.arange(start, end, step)))\n",
      "    \n",
      "interact(plotter, start=(0.01, 10, 1), end=(0.01, 100, 10), step=(0.01, 1, 0.01))"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "from IPython.display import Audio\n",
      "import math\n",
      "\n",
      "PI2 = math.pi * 2\n",
      "samplerate = 8000\n",
      "\n",
      "def sin_wave(freq, amplitude, duration_s):\n",
      "    return np.sin((np.arange(duration_s * samplerate) / samplerate) * PI2 * freq) * amplitude\n",
      "\n",
      "# Play two tones\n",
      "audio = np.concatenate((sin_wave(440, 10, 0.5), sin_wave(640, 10, 0.5)))\n",
      "Audio(data=audio, rate=samplerate)"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "# Play two tones simultaneously\n",
      "audio = sin_wave(440, 10, 0.5) + sin_wave(640, 10, 0.5)\n",
      "Audio(data=audio, rate=samplerate)"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Manual FFT"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "import scipy.fftpack\n",
      "points = 2048\n",
      "\n",
      "au = audio[0:points]\n",
      "fr = np.fft.rfft(au)\n",
      "\n",
      "mags = np.absolute(fr)\n",
      "phases = np.angle(fr)\n",
      "\n",
      "plt.plot(np.linspace(0, samplerate, num=(points/2 + 1)), mags)"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Matplotlib Auto-FFT\n",
      "\n",
      "Uses a hanning window by default, hence the tighter spikes. See http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.magnitude_spectrum."
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": [
      "plt.magnitude_spectrum(au, Fs=samplerate)"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": false
     },
     "outputs": [],
     "source": []
    }
   ],
   "metadata": {
    "kernelspec": {
     "display_name": "Python 3",
     "language": "python",
     "name": "python3"
    },
    "language_info": {
     "codemirror_mode": {
      "name": "ipython",
      "version": 3
     },
     "file_extension": ".py",
     "mimetype": "text/x-python",
     "name": "python",
     "nbconvert_exporter": "python",
     "pygments_lexer": "ipython3",
     "version": "3.5.2"
    }
   },
   "nbformat": 4,
   "nbformat_minor": 2
  }
  