使用 Audio 对象探索拍频现象

使用 Audio 对象探索拍频现象#

这个例子使用了 Audio 对象和 Matplotlib 来探究拍频(Beat Frequencies)现象。

# Imports for JupyterLite
%pip install -q ipywidgets matplotlib numpy
Note: you may need to restart the kernel to use updated packages.
from ipywidgets import interactive
from IPython.display import Audio, display
import numpy as np
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)
    display(Audio(data=signal, rate=rate))
    return signal
v = interactive(beat_freq, f1=(200.0,300.0), f2=(200.0,300.0))
display(v)
v.kwargs
{'f1': 220.0, 'f2': 224.0}
f1, f2 = v.children[:2]
f1.value = 255
f2.value = 260
plt.plot(v.result[0:6000]);
../../_images/e5f6cef64c3bb80a341a001980d1fcb6acce8d9e27e8109ed542ddc13de63154.png