Beat Frequencies.ipynb
120 lines
| 2.4 KiB
| text/plain
|
TextLexer
Brian E. Granger
|
r16092 | { | ||
Min RK
|
r18669 | "cells": [ | ||
Brian E. Granger
|
r16092 | { | ||
Min RK
|
r18669 | "cell_type": "markdown", | ||
"metadata": {}, | ||||
"source": [ | ||||
"# Exploring Beat Frequencies using the `Audio` Object" | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "markdown", | ||||
"metadata": {}, | ||||
"source": [ | ||||
"This example uses the `Audio` object and Matplotlib to explore the phenomenon of beat frequencies." | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
Jonathan Frederic
|
r20541 | "execution_count": null, | ||
Min RK
|
r18669 | "metadata": { | ||
"collapsed": false | ||||
}, | ||||
"outputs": [], | ||||
"source": [ | ||||
"%matplotlib inline\n", | ||||
"import matplotlib.pyplot as plt\n", | ||||
"import numpy as np" | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
Jonathan Frederic
|
r20541 | "execution_count": null, | ||
Min RK
|
r18669 | "metadata": { | ||
"collapsed": false | ||||
}, | ||||
"outputs": [], | ||||
"source": [ | ||||
"from IPython.html.widgets import interactive\n", | ||||
"from IPython.display import Audio, display\n", | ||||
"import numpy as np" | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
Jonathan Frederic
|
r20541 | "execution_count": null, | ||
Min RK
|
r18669 | "metadata": { | ||
"collapsed": false | ||||
}, | ||||
"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", | ||||
Jonathan Frederic
|
r20541 | "execution_count": null, | ||
Min RK
|
r18669 | "metadata": { | ||
"collapsed": false | ||||
}, | ||||
Jonathan Frederic
|
r20541 | "outputs": [], | ||
Min RK
|
r18669 | "source": [ | ||
"v = interactive(beat_freq, f1=(200.0,300.0), f2=(200.0,300.0))\n", | ||||
"display(v)" | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
Jonathan Frederic
|
r20541 | "execution_count": null, | ||
Min RK
|
r18669 | "metadata": { | ||
"collapsed": false | ||||
}, | ||||
Jonathan Frederic
|
r20541 | "outputs": [], | ||
Min RK
|
r18669 | "source": [ | ||
"v.kwargs" | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
Jonathan Frederic
|
r20541 | "execution_count": null, | ||
Min RK
|
r18669 | "metadata": { | ||
"collapsed": false | ||||
}, | ||||
Jonathan Frederic
|
r20541 | "outputs": [], | ||
Min RK
|
r18669 | "source": [ | ||
"f1, f2 = v.children\n", | ||||
"f1.value = 255\n", | ||||
"f2.value = 260\n", | ||||
"plt.plot(v.result[0:6000])" | ||||
] | ||||
Brian E. Granger
|
r16092 | } | ||
Min RK
|
r18669 | ], | ||
Min RK
|
r20278 | "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", | ||||
Jonathan Frederic
|
r20541 | "version": "3.4.0" | ||
Min RK
|
r20278 | } | ||
}, | ||||
Min RK
|
r18669 | "nbformat": 4, | ||
"nbformat_minor": 0 | ||||
Min RK
|
r20278 | } | ||