import numpy as np
import matplotlib.pyplot as plt
import IPython.display as ipd
t = np.linspace(0, 8*np.pi, 100)
plt.plot(t, np.sin(t))
plt.plot(t, np.cos(t))
plt.xlabel("t (in radians)")
plt.legend(["$\sin(t)$", "$\cos(t)$"])
<matplotlib.legend.Legend at 0x7f62304c2910>
$ f(t) = A \cos(2 \pi f t + \phi)$
$ f(t) = A \cos(\frac{2 \pi t}{T} + \phi)$
A = 1
f = 3
t = np.linspace(0, 5, 1000)
phi = np.pi/3
y = A*np.cos(2*np.pi*f*t + phi)
plt.plot(t, y)
plt.ylim([-5, 5])
(-5.0, 5.0)
sr = 44100
f1 = 440
f2 = 660
t = np.arange(sr)/sr
y = np.cos(2*np.pi*f1*t) #+ np.cos(2*np.pi*f2*t)
ipd.Audio(y, rate=sr)
# Sawtooth wave
t = np.arange(sr)
y = t % 100
plt.plot(y[0:1000])
ipd.Audio(y, rate=sr)
## Formula for frequency of musical notes
### Equal-tempered scale
note A, A#, B, C, C#, D, D#, E, F, F#, G, G#, A A#
p 0 1 2 3 4 5 6 7 8 9 10 11 12 13
def get_note_freq(p):
return 440*2**(p/12)
t = np.arange(int(sr/2))/sr
f = get_note_freq(0)
y = np.array([])
for p in range(12):
f = get_note_freq(p)
y = np.concatenate((y, np.cos(2*np.pi*f*t)))
ipd.Audio(y, rate=sr)