Exploring Beat Frequencies using the Audio ObjectΒΆ

This example uses the Audio object and Matplotlib to explore the phenomenon of beat frequencies.

In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
In [2]:
from ipywidgets import interactive
from IPython.display import Audio, display
import numpy as np
In [3]:
def beat_freq(f1=220.0, f2=224.0):
    max_time = 3
    rate = 8000
    times = np.linspace(0,max_time,rate*max_time)
    signal = np.sin(2*np.pi*f1*times) + np.sin(2*np.pi*f2*times)
    print(f1, f2, abs(f1-f2))
    display(Audio(data=signal, rate=rate))
    return signal
In [4]:
v = interactive(beat_freq, f1=(200.0,300.0), f2=(200.0,300.0))
display(v)
220.0 224.0 4.0
array([  0.00000000e+00,   3.46966469e-01,   6.83408155e-01, ...,
        -6.83408155e-01,  -3.46966469e-01,   3.84296828e-13])
In [5]:
v.kwargs
Out[5]:
{'f1': 220.0, 'f2': 224.0}
In [6]:
f1, f2 = v.children
f1.value = 255
f2.value = 260
plt.plot(v.result[0:6000])
255.0 260.0 5.0
array([  0.00000000e+00,   4.01744148e-01,   7.87106566e-01, ...,
        -7.87106566e-01,  -4.01744148e-01,  -7.97636075e-13])
Out[6]:
[<matplotlib.lines.Line2D at 0x7fa6fb6067b8>]
examples/../../build/doctrees/nbsphinx/examples_BeatFrequencies_7_4.png