Skip to content

Instantly share code, notes, and snippets.

@rcassani
Last active April 17, 2020 19:20
Show Gist options
  • Save rcassani/a472b922fc4accb3e9551f3f16dedb76 to your computer and use it in GitHub Desktop.
Save rcassani/a472b922fc4accb3e9551f3f16dedb76 to your computer and use it in GitHub Desktop.
Interactive Jupyter Notebook
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Interactve Jupyter Notebook\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### This Notebook shows the addition of two sine waves with frequencies 3 Hz and 7 Hz. \n",
"The weight for each sine wave, **w1** and **w2** respectivelly can be changed."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "0e96e9892b364e44a1028b82df2dc204",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(FloatSlider(value=1.0, description='w1', max=1.0), FloatSlider(value=1.0, description='w…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import numpy as np\n",
"from matplotlib import pyplot as plt\n",
"from ipywidgets import interact\n",
"from ipywidgets import interactive, fixed\n",
"%matplotlib inline\n",
"\n",
"fs = 100 #Hz \n",
"t = np.arange(0,200) / fs # time 1D\n",
"x1 = np.sin(2*np.pi*3*t) # 3 Hz sine wave\n",
"x2 = np.sin(2*np.pi*7*t) # 7 Hz sine wave\n",
"x = x1 + x2 # adding both sine waves \n",
"\n",
"def add_sines(x1, w1=1, w2=1):\n",
" plt.figure(2)\n",
" x = w1*x1 + w2*x2\n",
" plt.plot(t, x)\n",
" plt.ylim(-2.2, 2.2)\n",
" plt.xlabel('time (s)')\n",
" plt.ylabel('amplitude')\n",
" plt.title(str(w1)+ '$\\cdot$sin(2$\\pi\\cdot$3$\\cdot$t) + ' + str(w2) + '$\\cdot$sin(2$\\pi\\cdot$7$\\cdot$t)');\n",
" plt.show()\n",
"\n",
"\n",
"interactive_plot = interactive(add_sines, w1=(0, 1, 0.1), w2=(0, 1, 0.1), \n",
" x1=fixed(x1), x2=fixed(x2), t=fixed(t))\n",
"output = interactive_plot.children[-1]\n",
"output.layout.height = '350px'\n",
"interactive_plot"
]
}
],
"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.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
numpy==1.16.4
matplotlib==3.1.1
ipywidgets==7.5.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment