Sinusoids¶

In [1]:
import numpy as np
import matplotlib.pyplot as plt
import IPython.display as ipd

$ f(t) = \sin(t), t \in [0, 2 \pi]$¶

In [2]:
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)$"])
Out[2]:
<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 = amplitude
  • f = frequency (in hz, or cycles per second)
  • T = period = 1/f (the length of each repetition, in seconds)
  • t = time
  • $\phi$ = the phase
In [3]:
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])
Out[3]:
(-5.0, 5.0)
In [4]:
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)
Out[4]:
Your browser does not support the audio element.
In [6]:
# Sawtooth wave
t = np.arange(sr)
y = t % 100
plt.plot(y[0:1000])
ipd.Audio(y, rate=sr)
Out[6]:
Your browser does not support the audio element.
In [ ]:
## 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

$f = 440 * 2^{p/12}$¶

In [7]:
def get_note_freq(p):
    return 440*2**(p/12)
In [8]:
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)
Out[8]:
Your browser does not support the audio element.
In [ ]: