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).
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()
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:
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?
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.