{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import dautil as dl\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from ch6util import read_wav\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "context = dl.nb.Context('analyzing_audio')\n",
    "lr = dl.nb.LatexRenderer(chapter=6, start=9, context=context)\n",
    "lr.render(r'\\left|\\mathcal{F}^{-1}\\left\\{\\mbox{log}(\\left|\\mathcal{F}\\left\\{ f(t) \\right\\}\\right|^2)\\right\\}\\right|^2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def amplitude(arr):\n",
    "    return np.abs(np.fft.fft(arr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "rate, audio = read_wav()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.options.mimic_seaborn()\n",
    "dl.nb.RcWidget(context)\n",
    "dl.nb.LabelWidget(2, 2, context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sp = dl.plotting.Subplotter(2, 2, context)\n",
    "t = np.arange(0, len(audio)/float(rate), 1./rate)\n",
    "sp.ax.plot(t, audio)\n",
    "freqs = np.fft.fftfreq(audio.size, 1./rate)\n",
    "indices = np.where(freqs > 0)[0]\n",
    "sp.label()\n",
    "\n",
    "magnitude = amplitude(audio)\n",
    "sp.next_ax().semilogy(freqs[indices], magnitude[indices])\n",
    "sp.label()\n",
    "\n",
    "cepstrum = dl.ts.power(np.fft.ifft(np.log(magnitude ** 2)))\n",
    "sp.next_ax().semilogy(cepstrum)\n",
    "sp.label()\n",
    "\n",
    "\n",
    "npieces = 200\n",
    "stft_amps = []\n",
    "\n",
    "for i, c in enumerate(dl.collect.chunk(audio[: npieces ** 2], len(audio)/npieces)):\n",
    "    amps = amplitude(c)\n",
    "    stft_amps.extend(amps)\n",
    "\n",
    "stft_freqs = np.linspace(0, rate, npieces)\n",
    "stft_times = np.linspace(0, len(stft_amps)/float(rate), npieces)\n",
    "sp.next_ax().contour(stft_freqs/rate, stft_freqs,\n",
    "           np.log(stft_amps).reshape(npieces, npieces))\n",
    "sp.label()\n",
    "    \n",
    "HTML(sp.exit())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
