{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exploring Beat Frequencies using the `Audio` Object\n", "source : https://github.com/crocha700/jupyter_notebook/blob/master/examples/Interactive%20Widgets/Beat%20Frequencies.ipynb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This example uses the `Audio` object and Matplotlib to explore the phenomenon of beat frequencies." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "#from IPython.html.widgets import interactive\n", "from ipywidgets import interactive\n", "\n", "from IPython.display import Audio, display\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def beat_freq(f1=220.0, f2=224.0):\n", " max_time = 3\n", " rate = 8000\n", " times = np.linspace(0,max_time,rate*max_time)\n", " signal = np.sin(2*np.pi*f1*times) + np.sin(2*np.pi*f2*times)\n", " print(f1, f2, abs(f1-f2))\n", " display(Audio(data=signal, rate=rate))\n", " return signal" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "f07bbe940cec48b1a2e2de09968f1e05", "version_major": 2, "version_minor": 0 }, "text/html": [ "
Failed to display Jupyter Widget of type interactive
.
\n", " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", " that the widgets JavaScript is still loading. If this message persists, it\n", " likely means that the widgets JavaScript library is either not installed or\n", " not enabled. See the Jupyter\n", " Widgets Documentation for setup instructions.\n", "
\n", "\n", " If you're reading this message in another frontend (for example, a static\n", " rendering on GitHub or NBViewer),\n", " it may mean that your frontend doesn't currently support widgets.\n", "
\n" ], "text/plain": [ "interactive(children=(FloatSlider(value=220.0, description='f1', max=240.0, min=200.0), FloatSlider(value=224.0, description='f2', max=240.0, min=200.0), Output()), _dom_classes=('widget-interact',))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "v = interactive(beat_freq, f1=(200.0,240.0), f2=(200.0,240.0))\n", "display(v)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'f1': 220.0, 'f2': 224.0}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v.kwargs" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(FloatSlider(value=220.0, description='f1', max=240.0, min=200.0), FloatSlider(value=224.0, description='f2', max=240.0, min=200.0))\n" ] } ], "source": [ "print(v.children[0:2])" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[