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

Shifting functions horizontally

Let's say we have a "parent function" $y(u) = u^2$. This is just a simple parabola

In [5]:
u = np.linspace(-4, 4, 100)
y = u**2
plt.plot(u, y)
Out[5]:
[<matplotlib.lines.Line2D at 0x7f8288c85750>]

Let's say we want to move this parabola to the left by 2 units. To accomplish this, we need to plug in our independent variable minus 2 to our parent function, so

$ y(x-2) = (x-2)^2 $

So let's plot this

In [6]:
x = np.linspace(-4, 4, 100)
y = (x-2)**2
plt.plot(x, y)
Out[6]:
[<matplotlib.lines.Line2D at 0x7f8288c083d0>]

Next, we want to something similar for beats. The beats we've looked at so far are made up of pure cosine waves, so we just have to figure out how to shift them. In this case, the parent function is

$ f(u) = \cos (2 \pi f u) $

Let's say we want to shift by $s$ units. Then we plug in $u = t - s$, and the final equation we get is

$ f(t-s) = \cos(2 \pi f (t-s)) $

We can do this for every term in a beat. Below, I show adding more than just one frequency to accentuate the beat. In others words, when we have 5 frequencies, the beat has a larger amplitude, and a shorter "attack," or amount of time it takes to reach its peak loudness.

In [22]:
L = 8
t = np.linspace(0, L, 1000)
f1 = 5
f2 = f1 + 1/L
f3 = f2 + 1/L
f4 = f3 + 1/L
f5 = f4 + 1/L
y = np.cos(2*np.pi*f1*(t-4)) + np.cos(2*np.pi*f2*(t-4))
y += np.cos(2*np.pi*f3*(t-4))+ np.cos(2*np.pi*f4*(t-4))
y += np.cos(2*np.pi*f5*(t-4))
#env = 2*np.cos(2*np.pi*(f2-f1)*t/2)
plt.plot(t, y)
#plt.plot(t, env)
#plt.plot(t, -env)
Out[22]:
[<matplotlib.lines.Line2D at 0x1883481a2b0>]

Here's an example in the audible range

In [23]:
sr = 8000
L = 8
t = np.arange(8*sr)/sr
f1 = 440
f2 = f1 + 1/L
f3 = f2 + 1/L
f4 = f3 + 1/L
f5 = f4 + 1/L
y = np.cos(2*np.pi*f1*(t-4)) + np.cos(2*np.pi*f2*(t-4))
y += np.cos(2*np.pi*f3*(t-4))+ np.cos(2*np.pi*f4*(t-4))
y += np.cos(2*np.pi*f5*(t-4))
ipd.Audio(y, rate=sr)
Out[23]:

Let's re-examine the above cosine shifting in terms of a general sinsuoid equation. To do this, let's distribute through the $2 \pi f$ inside of the cosine:

$y(t) = \cos(2 \pi f (t - s) )$

then we get this equation

$y(t) = \cos(2 \pi f t - 2\pi f s)$

Recall that the general form of a sinusoid of amplitude 1 is as follows

$y(t) = \cos(2 \pi f t + \phi)$

So what we see is that shifting a cosine can be expressed more generally as a sinusoid of the same frequency but with phase $\phi = - 2 \pi f s$

In [ ]: