Fourier Projection on Sinusoids

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

Dot Product

Def. Dot product on two arrays $y1[x]$ and $y2[x]$ is defined as

$y1[0]*y2[0] + y1[1]*y2[1] + y1[2]*y2[2] + ... + y1[N-1]*y2[N-1]$

in other words, it's the sum of element-wise products between two parallel arrays

$y1.y2 = \sum_{x = 0}^{N-1} y1[x]*y2[x]$

The python python way

In [ ]:
dotprod = 0
for i in range(len(y1)): # Assuming len(y1) = len(y2)
    dotproduct += y1[i]*y2[i]

The numpy way

In [ ]:
dotprod = np.sum(y1*y2)
In [16]:
n_samples = 100
t = np.linspace(0, 1, n_samples+1)[0:n_samples]
y1 = np.cos(2*np.pi*3*t + np.pi/4)
y2 = np.sin(2*np.pi*3*t)
prod = y1*y2

r = max(np.max(np.abs(prod)), np.max(np.abs(y1)), np.max(np.abs(y2)))

t = np.arange(t.size)
plt.figure(figsize=(12, 16))
plt.subplot(311)
plt.stem(t[y1 > 0], y1[y1 > 0], 'C1', markerfmt='C1o', use_line_collection=True)
plt.stem(t[y1 <= 0], y1[y1 <= 0], 'C0', markerfmt='C0o', use_line_collection=True)
plt.ylim([-1.1*r, 1.1*r])
plt.title("$y_1(t)$")

plt.subplot(312)
plt.stem(t[y2 > 0], y2[y2 > 0], 'C1', markerfmt='C1o', use_line_collection=True)
plt.stem(t[y2 <= 0], y2[y2 <= 0], 'C0', markerfmt='C0o', use_line_collection=True)
plt.ylim([-1.1*r, 1.1*r])
plt.title("$y_2(t)$")

plt.subplot(313)
if np.sum(prod > 0) > 0:
    plt.stem(t[prod > 0], prod[prod > 0], 'C1', markerfmt='C1o', use_line_collection=True)
if np.sum(prod <= 0) > 0:
    plt.stem(t[prod <= 0], prod[prod <= 0], 'C0', markerfmt='C0o', use_line_collection=True)
plt.ylim([-1.1*r, 1.1*r])
plt.title("$y_1(t)y_2(t), sum = %.3g$"%np.sum(prod))
Out[16]:
Text(0.5, 1.0, '$y_1(t)y_2(t), sum = -35.4$')

Rule # 1:

Given two arrays $y1$ and $y2$ of length $N$, each of which holds samples of a pure sine or a pure cosine that goes through an integer number of periods > 0, then

  • If $y1 = y2$, then $y1.y2 > 0$
  • Otherwise, $y1.y2 = 0$

Analogy with Definite Integrals

Suppose we want to evaluate the integral

$\int_{0}^1 \cos(2 \pi f_1 t) \cos(2 \pi f_2t) dt$

$f_1$ and $f_2$ are both integers $>0$, $f_1 \neq f_2$

Recall:

$cos(A)cos(B) = \frac{cos(A-B) + cos(A+B)}{2}$

$\int_{0}^1 \frac{1}{2} (\cos(2 \pi (f_1-f_2) t) + \cos(2 \pi (f_1 +f_2) t)) dt$

$ \frac{1}{2}( \frac{1}{2 \pi (f_1-f_2)} ) \sin(2 \pi (f_1-f_2)t) + \frac{1}{2}( \frac{1}{2 \pi (f_1+f_2)} ) \sin(2 \pi (f_1+f_2)t) ]^{1}_{0} $

When $f1 \neq f2$, this whole definite integral evalues to 0

Now examine

$\int_{0}^1 \cos^2(2 \pi f t) dt = \int_0^1 \frac{1 + \cos(4 \pi f t)}{2} dt$

$f_1$ is an integer > 0

$[ t/2 + \frac{1}{8 \pi f} \sin(4 \pi f t) ]^{t=1}_{t=0} = \frac{1}{2} $

Rule #2

Lemma: The dot product is distributive

If $y1[n] = x[n] + y[n]$

then

$y1.y2 = x.y2 + y.y2$

Corollary: If $y1$ is a sum of pure cosines/sines that each go through an integer number of periods, $y2$ is an individual pure cosine/sine that goes through an integer number of periods, then

  • If $y_2$ is contained in $y_1$, then $y_1.y_2 > 0$
  • If $y_2$ is not contained in $y_1$, then $y_1.y_2 = 0$

General sinusoids

Recall that

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

can be written as

$f(t) = \cos(\phi)\cos(2 \pi f t) - \sin(\phi)\sin(2 \pi f t)$

If you want to know if a sinusoid at a particular frequency $f$ exists in your array, then check both the pure cosine and the pure sine at that frequency

In [ ]: