Source Material:
The following exercises are adapted from Chapter 7 of Mark Newman’s book, “Computational Physics”
Exercises: DFTs of Various Signals
In this notebook, you will be generating samples from various functions,
Then you will use the discrete Fourier transform to compute
You will plot both the sampled wave form of
Make sure that your time values
For all problems, take
[ ]:
import numpy as np
import matplotlib.pyplot as plt
from typing import Tuple
%matplotlib notebook
(1.5.1) Generate
[1, 1, 1, 1, -1, -1, -1, -1]
Also compute the Fourier coefficients,
Plot the sampled wave form of this square wave versus time and plot the Fourier spectrum,
Here is some code that you can use to plot these side-by-side:
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 4))
ax1.plot(t, y, marker="o") # plot time (t) and waveform-samples (y)
ax1.grid()
ax1.set_xlabel("t (seconds)") # make sure `t` actually represents seconds!
ax2.stem(freqs, amps, basefmt=" ", use_line_collection=True) # plot frequency (freqs) and amplitudes (amps)
ax2.set_xlim(0, 10)
ax2.grid()
ax2.set_ylabel(r"$|a_{k}|$")
ax2.set_xlabel(r"$\nu_{k}$ (Hz)") # make sure `freqs` actually represents Hertz!
fig.tight_layout()
[ ]:
# STUDENT CODE HERE
(1.5.2)
[ ]:
# STUDENT CODE HERE
(1.5.3)
What are the frequencies of the respective terms in our modulated wave, in Hz?
Do you expect that these two frequencies present in the Fourier spectrum? Why might we expect for different frequencies to be prominent in our series (hint: compare the functional form of this waveform to that of a Fourier series)?
Use the relationship
to rewrite this modulated wave as a sum of cosines. From this, predict the number, locations, and heights of the peaks in your Fourier spectrum.Plot the wave form vs time and plot the Fourier spectrum,
vs . Be sure to zoom in on your peaks and check that they are located where you expect them to be.
SOLUTION HERE
[ ]:
# STUDENT CODE HERE
(1.5.4) np.random.rand
generates random numbers on noise
:
def noise(t: np.ndarray) -> np.ndarray:
# t is a shape-(N,) array
# Use np.random.rand to draw a shape-(N,)
# array of uniform random numbers on [0, 1)
#
# Modify these numbers so that they instead fall
# on the range [-1, 1)
#
# return these random samples
Use this to generate
[ ]:
# STUDENT CODE HERE