Module 8: Discovering The Discrete Fourier Transform

Chris Tralie

Due Friday 2/12

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

In this module, you're going to explore sums of products of sinusoids and make some observations. This will form the "basis" of Fourier analysis, the most important analytical tool we'll use in this course. Please write up brief answers to the questions below as you experiment with code in a notebook, and then submit them as a document or comment on canvas.

The code below sets up two waveforms and and plots each one, as well as the element-wise product of the two. It also reports the sum of all of the samples of the element-wise product as the title of the third plot. This sum of element-wise products is referred to as a dot product, and it is the central operation in the discrete Fourier transform.

In the given example, the original signals are cosines at 2hz and 3hz, respectively, and their dot product is an incredibly small number. In fact, you can consider this number and any other number in this range to be numerically zero; all of the negative samples (in blue) pretty much completely cancel out the positive ones (in orange).

In [21]:
n_samples = 100
t = np.linspace(0, 1, n_samples+1)[0:n_samples]
y1 = np.cos(2*np.pi*2*t)
y2 = np.sin(2*np.pi*2*t)
prod = y1*y2

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

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

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

plt.subplot(413)
if np.sum(prod > 0) > 0:
    plt.stem(t[prod > 0], prod[prod > 0], 'C1', markerfmt='C1o')
if np.sum(prod <= 0) > 0:
    plt.stem(t[prod <= 0], prod[prod <= 0], 'C0', markerfmt='C0o')
plt.ylim([-1.1*r, 1.1*r])
plt.title("$y_1(t)y_2(t), sum = %.3g$"%np.sum(prod))

"""
plt.subplot(414)
cpluss = y1 + y2
plt.stem(t[cpluss > 0], cpluss[cpluss > 0], 'C1', markerfmt='C1o')
if np.sum(cpluss <= 0) > 0:
    plt.stem(t[cpluss <= 0], cpluss[cpluss <= 0], 'C0', markerfmt='C0o')
plt.title("$y_1(t) + y_2(t), sum = %.3g$"%np.sum(cpluss))
"""


plt.savefig("2_4_cosine.png")
plt.show()

Exercise 1

You should modify the above code to try different signals. In every example you look at, the signal will go through an integer number of periods. Given this setup, examine the following combinations of $y_1(t)$ and $y_2(t)$ and their plots, and report which sums are numerically zero (the negatives cancel out the positives) and which ones are not:

  1. $y_1(t)$ is a 2hz cosine and $y_2(t)$ is a 4hz cosine
  1. $y_1(t)$ is a 2hz cosine and $y_2(t)$ is also a 2hz cosine
  1. $y_1(t)$ is a 3hz cosine and $y_2(t)$ is also a 3hz cosine
  1. $y_1(t)$ is a 3hz cosine and $y_2(t)$ is a 3hz sine
  1. $y_1(t)$ is the sum of a 2hz cosine, a 3hz cosine, and a 4hz cosine, and $y_2(t)$ is a 3hz cosine
  1. $y_1(t)$ is the sum of a 2hz cosine, a 3hz cosine, and a 4hz cosine, and $y_2(t)$ is a 1hz cosine
  1. $y_1(t)$ is the sum of a 1hz cosine and a 5hz cosine, and $y_2(t)$ is a 1hz sine
  1. $y_1(t)$ is the sum of a 1hz cosine and a 5hz cosine, and $y_2(t)$ is a 1hz cosine
  1. $y_1(t)$ is the sum of a 1hz cosine and a 5hz cosine, and $y_2(t)$ is a 3hz cosine
  1. $y_1(t)$ is a 2hz sinusoid with a phase of $\phi = \pi/4$, and $y_2(t)$ is a 2h cosine
  1. $y_1(t)$ is a 2hz sinusoid with a phase of $\phi = \pi/4$, and $y_2(t)$ is a 2h sine

Exercise 2:

Given your observations above, what patterns do you notice? In particular, if you think of $y_1(t)$ as a signal and $y_2(t)$ as the "tester" or "probe" sinusoid, when do probes lead to a nonzero sum of products?

Fun Fact

I learned this Discrete Fourier Transform back in 2005 for a science fair project by doing these kinds of experiments in Microsoft Excel before I'd even taken calculus! (I was lucky to stumble across some random internet tutorial that meshed perfectly with my background knowledge in trig only). But then in college I actually learned the math in detail. So I'm going to take you through my journey in this course.

In [ ]: