{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Dependencies\n", "===============" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[nltk_data] Downloading package timit to /home/boisgera/nltk_data...\n", "[nltk_data] Package timit is already up-to-date!\n" ] } ], "source": [ "from __future__ import division\n", "\n", "from IPython.display import *\n", "\n", "from numpy import *; seterr(all=\"ignore\")\n", "from numpy import linalg\n", "from numpy import random\n", "\n", "%matplotlib notebook\n", "from matplotlib.pyplot import *\n", "\n", "import wish\n", "\n", "import audio.wave as wave\n", "from audio.filters import FIR, AR\n", "import audio.frames\n", "import audio.index; import nltk; nltk.download(\"timit\")\n", "from audio.lp import lp\n", "from audio.quantizers import Quantizer" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "df = 16000\n", "dt = 1.0 / df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sandbox\n", "========" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "FIR\n", "---" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.0\n", "1.0\n", "[ 0. 7. -3.]\n", "0.0\n", "2.0\n", "[1. 0. 7.]\n", "[0. 1.]\n" ] } ], "source": [ "fir = FIR([1.0])\n", "print fir(2.0)\n", "print fir(1.0)\n", "print fir([0.0, 7.0, -3.0])\n", "delay = FIR([0.0, 1.0])\n", "fir = delay\n", "print fir(2.0)\n", "print fir(1.0)\n", "print fir([0.0, 7.0, -3.0])\n", "print fir.a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Audio Source (NLTK)\n", "=============" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "a crab challenged me but a quick stab vanquished him" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "utterances = audio.index.search(type=audio.index.Utterance)\n", "utterance = utterances[0]\n", "display(utterance)\n", "data = utterance.audio\n", "audio.wave.write(data, \"voice.wav\", df=16000)\n", "Audio(\"voice.wav\")" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "phone: h# (a crab challenged me but a quick stab vanquished him).\n", "word: a [q-ey]\n", "word: crab [kcl-k-r-ae-bcl]\n", "word: challenged [ch-ae-l-ax-n-dcl-jh-dcl-d]\n", "phone: epi (a crab challenged me but a quick stab vanquished him).\n", "word: me [m-iy]\n", "phone: pau (a crab challenged me but a quick stab vanquished him).\n", "word: but [b-eh-dx]\n", "word: a [ix]\n", "word: quick [kcl-k-w-ih-kcl-k]\n", "word: stab [s-tcl-t-ae-bcl-b]\n", "word: vanquished [v-ae-ng-kcl-k-w-ix-sh-tcl-t]\n", "word: him [hh-ih-m]\n", "phone: h# (a crab challenged me but a quick stab vanquished him).\n" ] } ], "source": [ "for item in utterance:\n", " print type(item).__name__.lower() + \": \" + str(item)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " 0. bcl (black [bcl-b-l-ae-kcl]).\n", " 1. bcl (betide [bcl-b-iy-tcl-t-ay-dcl]).\n", " 2. bcl (be [bcl-b-iy]).\n", " 3. bcl (both [bcl-b-ow-th]).\n", " 4. bcl (blues [bcl-b-l-ux-z]).\n", " 5. bcl (be [bcl-b-ix]).\n", " 6. bcl (bowl [bcl-b-ow-l]).\n", " 7. bcl (publicity [p-ax-bcl-b-l-ih-s-ix-dx-iy]).\n", " 8. bcl (be [bcl-b-iy]).\n", " 9. bcl (rhubarb [r-uw-bcl-b-aa-r-bcl]).\n", " 10. bcl (rhubarb [r-uw-bcl-b-aa-r-bcl]).\n", " 11. bcl (biblical [b-ih-bcl-b-l-ih-kcl-k-el]).\n", " 12. bcl (maybe [m-ey-bcl-b-iy]).\n", " 13. bcl (blistered [bcl-b-l-ih-s-tcl-t-axr-dcl]).\n", " 14. bcl (underbrush [ah-n-axr-bcl-b-r-ah-sh]).\n", " 15. bcl (arbitrate [q-aa-r-bcl-b-ix-tcl-t-r-ey-tcl-t]).\n", " 16. bcl (tomboy [tcl-t-aa-m-bcl-b-oy]).\n", " 17. bcl (brother [bcl-b-r-ah-dh-er]).\n", " 18. bcl (battery [bcl-b-ae-dx-er-iy]).\n", " 19. bcl (built [bcl-b-ih-l-tcl]).\n", " 20. bcl (books [bcl-b-uh-kcl-s]).\n", " 21. bcl (be [bcl-b-iy]).\n", " 22. bcl (buy [bcl-b-ay]).\n", " 23. bcl (about [ax-h-bcl-b-aw-tcl]).\n", " 24. bcl (honeybees [hv-ah-n-iy-bcl-b-iy-z]).\n", " 25. bcl (be [bcl-b-iy]).\n", " 26. bcl (birth [bcl-b-axr-th]).\n", " 27. bcl (becoming [bcl-b-iy-kcl-k-ah-m-ix-ng]).\n", " 28. bcl (available [ax-v-ey-l-ax-bcl-b-el]).\n", " 29. bcl (bequests [bcl-b-iy-kcl-k-w-eh-s]).\n", " 30. bcl (obstacles [q-aa-bcl-s-tcl-t-ix-kcl-k-el-z]).\n", " 31. bcl (bluejay [bcl-b-l-ux-dcl-jh-ey]).\n", " 32. bcl (building [bcl-b-ih-l-dcl-ix-ng]).\n", " 33. bcl (both [bcl-b-ow-th]).\n", " 34. bcl (interchangeably [q-ih-n-tcl-t-axr-tcl-ch-ey-n-dcl-jh-ix-bcl-b-l-iy]).\n", " 35. bcl (able [q-ey-bcl-el]).\n", " 36. bcl (suburbanites [s-axr-bcl-b-er-bcl-axr-n-ay-tcl-s]).\n", " 37. bcl (suburbanites [s-axr-bcl-b-er-bcl-axr-n-ay-tcl-s]).\n", " 38. bcl (valuables [v-ae-y-ix-bcl-b-el-s]).\n", " 39. bcl (bank [bcl-b-ae-ng-kcl]).\n", " 40. bcl (impossible [ih-m-pcl-p-aa-s-bcl-b-el]).\n", " 41. bcl (bureaucracy [bcl-b-y-er-aa-kcl-k-axr-s-iy]).\n", " 42. bcl (subdued [s-ah-bcl-d-ux-dcl-d]).\n", " 43. bcl (by [bcl-b-ay]).\n", " 44. bcl (big [bcl-b-ih-gcl]).\n", " 45. bcl (bizarre [bcl-b-ix-z-aa-r]).\n", " 46. bcl (boy [bcl-b-oy]).\n", " 47. bcl (bought [bcl-b-ao-tcl]).\n", " 48. bcl (breakfast [bcl-b-r-eh-kcl-k-f-ax-h-s-tcl-t]).\n", " 49. bcl (you're [y-er-bcl]).\n", " 50. bcl (crab [kcl-k-r-ae-bcl]).\n", " 51. bcl (stab [s-tcl-t-ae-bcl-b]).\n", " 52. bcl (both [bcl-b-ow-th]).\n", " 53. bcl (be [bcl-b-iy]).\n", " 54. bcl (blowing [bcl-b-l-ow-ih-ng]).\n", " 55. bcl (by [bcl-b-ay]).\n", " 56. bcl (be [bcl-b-iy]).\n", " 57. bcl (problem [pcl-p-r-aa-bcl-b-l-ax-m]).\n", " 58. bcl (be [bcl-b-iy]).\n", " 59. bcl (suburban [s-ix-bcl-b-er-v-ih-n]).\n", " 60. bcl (gab [gcl-g-ae-bcl]).\n", " 61. bcl (habit [hv-ae-bcl-b-ih-tcl]).\n", " 62. bcl (big [bcl-b-ih-gcl]).\n", " 63. bcl (biology [bcl-b-ay-ax-l-ax-dcl-jh-iy]).\n", " 64. bcl (celebrates [s-eh-l-ax-bcl-b-r-ey-tcl-s]).\n", " 65. bcl (born [bcl-b-ao-r-n]).\n" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "audio.index.search(\"bcl\", type=audio.index.Phone)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Short-Term Prediction\n", "======================" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "class STP(Quantizer):\n", " \"Short-Term Predictor\"\n", " def __init__(self, order=16, method=\"autocorrelation\"):\n", " self.fir = FIR(a=r_[1.0, zeros(order)]) \n", " self.ar = AR(a=zeros(order))\n", " self.order = order\n", " self.method = method\n", " def encode(self, data):\n", " if self.method == \"covariance\" and self.order >= len(data):\n", " raise ValueError(\"not enough data samples\")\n", " a = lp(data, order=self.order, method=self.method)\n", " self.fir.a[:] = r_[1.0, -a]\n", " error = self.fir(data)\n", " return (a, error) \n", " def decode(self, data):\n", " a, error = data\n", " self.ar.a[:] = a\n", " return self.ar(error)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "def stp_error(data, T=0.02, order=16, method=\"autocorrelation\"):\n", " length = len(data)\n", " n = int(T * df) # number of samples for T s at the given frequency.\n", " frames = audio.frames.split(data, n, pad=True)\n", " stp = STP(order=order, method=method)\n", " error = zeros(n*len(frames))\n", " for i, frame in enumerate(frames):\n", " a, error_frame = stp.encode(frame)\n", " error[i*n:(i+1)*n] = error_frame\n", " return error[:length]" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "error = stp_error(data)\n", "SNR2 = mean(data*data) / mean(error*error)\n", "print \"SNR\", 10*log10(SNR2), \"dB\"\n", "\n", "T = 20.0 / 1000 # 20 ms\n", "n = int(df * T)\n", "data_frames = audio.frames.split(data, n, pad=True)\n", "error_frames = audio.frames.split(error, n, pad=True)\n", "\n", "fig, (ax1, ax2) = subplots(2,1, sharex=True)\n", "SNRs = [10.0 * log10(mean(d*d) / mean(e*e)) for d,e in zip(data_frames, error_frames)]\n", "ax1.plot(data, \"k\", alpha=0.1, label=\"original\")\n", "ax1.plot(error, \"r\", label=\"stp residual\")\n", "ax1.set_title(\"Waveforms\")\n", "ax1.axis(\"tight\")\n", "ax1.legend()\n", "ax1.grid()\n", "ax2.plot(r_[0:len(SNRs)] * n, SNRs, \"b\")\n", "ax2.plot([0,len(data_frames)*n], [10*log10(SNR2), 10*log10(SNR2)], \"k--\")\n", "ax2.set_xlabel(\"sample index\")\n", "ax2.set_ylabel(\"SNR [dB]\")\n", "ax2.set_title(\"Signal-to-Noise Ratio\")\n", "ax2.grid()" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "wave.write(error, \"voice-error-stp.wav\", df=df)\n", "display(Audio(\"voice.wav\"))\n", "display(Audio(\"voice-error-stp.wav\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pitch Estimation\n", "==========" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "def ltp_parameters(history, frame,\n", " offset_min=1, offset_max=None,\n", " gain_min=0.0, gain_max=inf,\n", " SNR_min=1.0, SNR_max=inf,\n", " select=argmax,\n", " returns=\"offset, gain\"):\n", " p = len(history)\n", " data = r_[history, frame] # full data\n", " m = len(frame)\n", " n = len(data)\n", " nxcorrs = zeros(p+1)\n", " gains = zeros(p+1)\n", " SNRs = zeros(p+1)\n", " valids = zeros(p+1, dtype=bool)\n", " frame_norm = linalg.norm(frame)\n", " normed_frame = frame / frame_norm\n", " for i in range(p + 1):\n", " windowed_data = data[n-i-m:n-i]\n", " windowed_data_norm = linalg.norm(windowed_data)\n", " normed_windowed_data = windowed_data / windowed_data_norm\n", " nxcorr = nxcorrs[i] = dot(normed_frame, normed_windowed_data)\n", " SNR = SNRs[i] = 1.0 / sqrt(1 - nxcorr*nxcorr)\n", " #print \">\", SNR\n", " gain = gains[i] = nxcorr / windowed_data_norm * frame_norm\n", " valid = True\n", " if offset_min is not None:\n", " valid = valid and (offset_min <= i)\n", " if offset_max is not None:\n", " valid = valid and (i <= offset_max)\n", " valid = valid and (gain_min <= gain <= gain_max)\n", " valid = valid and (SNR_min <= SNR <= SNR_max)\n", " valids[i] = valid\n", "\n", " criteria = SNRs.copy() \n", " criteria[logical_not(valids)] = -inf\n", " offset = select(criteria)\n", " if not valids[offset]: # everything is invalid!\n", " raise ValueError(\"no valid set of parameters\")\n", " else:\n", " gain = gains[offset]\n", " nxcorr = nxcorrs[offset]\n", " SNR = SNRs[offset]\n", "\n", " return wish.grant(returns)" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "offset: 75 gain: 1.1689857342477985\n" ] }, { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "figure()\n", "m = arange(len(SNRs))\n", "plot(m[offset_min:],SNRs[offset_min:], \"r\", alpha=0.25, label=\"SNR\")\n", "n = arange(len(SNRs))\n", "plot(n[valids],SNRs[valids], \"bx\", linewidth=1.0,label=\"valid\")\n", "xlabel(\"offset\")\n", "ylabel(\"SNR (linear scale)\")\n", "axis(\"tight\")\n", "legend(loc=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Long-Term Prediction\n", "====================" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [], "source": [ "class LTP(Quantizer):\n", " def __init__(self, order, **options):\n", " self.fir = FIR(a=r_[1.0, zeros(order)])\n", " self.history = zeros(order)\n", " self.ar = AR(a=zeros(order))\n", " self.order = order\n", " self.options = options\n", " def encode(self, frame):\n", " a = zeros_like(self.fir.a)\n", " a[0] = 1.0\n", " try:\n", " offset, gain = ltp_parameters(self.history, frame, **self.options)\n", " a[offset] = - gain\n", " except ValueError:\n", " offset, gain = 0, 0.0\n", " self.fir.a[:] = a \n", " error = self.fir(frame)\n", " self.history = r_[self.history[len(frame):], frame]\n", " return (offset, gain), error\n", " def decode(self, data):\n", " (offset, gain), error = data\n", " a = zeros_like(self.ar.a)\n", " a[offset-1] = gain\n", " self.ar.a[:] = a\n", " return self.ar(error)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "320\n", "40\n" ] } ], "source": [ "f_min = 50.0\n", "f_max = 400.0\n", "order_ltp = int(df/f_min)\n", "print order_ltp\n", "offset_min = int(df/f_max)\n", "print offset_min" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [], "source": [ "def ltp_error(data, T=0.005, order=order_ltp, **options):\n", " length = len(data)\n", " n = int(T * df) # number of samples for T s at the given sampling frequency.\n", " frames = audio.frames.split(data, n, pad=True)\n", " ltp = LTP(order=order, **options)\n", " error = zeros(n*len(frames))\n", " offset = zeros_like(error)\n", " gain = zeros_like(error)\n", " for i, frame in enumerate(frames):\n", " (offset_, gain_), error_frame = ltp.encode(frame)\n", " error[i*n:(i+1)*n] = error_frame\n", " offset[i*n:(i+1)*n] = ones_like(error_frame) * offset_\n", " gain[i*n:(i+1)*n] = ones_like(error_frame) * gain_\n", " error = error[:length]\n", " offset = offset[:length]\n", " gain = gain[:length]\n", " return error, offset, gain" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('