-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8_Beat-Frequencies.py
46 lines (29 loc) · 1.1 KB
/
8_Beat-Frequencies.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# coding: utf-8
# # Exploring Beat Frequencies
#
# This simple code will let us play with close frequencies and hear the beatings created by intermodulation. It's also a great example of the interactivity we can achieve with Jupyter Notebook.
# standard bookkeeping
get_ipython().magic('matplotlib inline')
import matplotlib.pyplot as plt
import numpy as np
from IPython.display import Audio, display
# interactivity here:
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
# In[5]:
plt.rcParams["figure.figsize"] = (14,4)
# Let's define a simple fuction that generates, plots and plays two sinusoids at the given frequencies:
# In[6]:
def beat_freq(f1=220.0, f2=224.0):
# the clock of the system
LEN = 4 # seconds
Fs = 8000.0
n = np.arange(0, int(LEN * Fs))
s = np.cos(2*np.pi * f1/Fs * n) + np.cos(2*np.pi * f2/Fs * n)
# start from the first null of the beating frequency
K = int(Fs / (2 * abs(f2-f1)))
s = s[K:]
# play the sound
display(Audio(data=s, rate=Fs))
# display one second of audio
plt.plot(s[0:int(Fs)])