We start by importing a few packages. For each question, write your code where prompted for code and write written answers to the questions when applicable.
import numpy as np
import numpy.random as random
from numpy.fft import fft
import matplotlib.pyplot as plt
%matplotlib inline
def sine_sampler(freq, sampling_rate, nsample, mean_noise, var_noise):
t = np.arange(0, 1, 1/sampling_rate)
xt = np.sin(freq*t)
signal = xt + np.random.normal(scale=np.sqrt(var_noise), size=(nsample, ))
return xt, signal, t
fig, ax = plt.subplots(nrows=3, ncols=1, figsize = (24, 24))
sampling_rate = 300
tf = 1
n_sample = tf*sampling_rate
mean_noise = 0
signal = np.zeros((3, n_sample))
for i, var_noise in enumerate([0, 0.1, 0.5]):
xt_10, signal_10, t = sine_sampler(10, sampling_rate, n_sample, mean_noise, var_noise)
xt_20, signal_20, _ = sine_sampler(20, sampling_rate, n_sample, mean_noise, var_noise)
signal_ = signal_10
signal[i] = signal_
ax[i].plot(t, signal_)
ax[i].set_title('Noise Variance: ' + str(var_noise) + ' Superimposed signal')
# Write code here
sp = np.fft.fft(signal, axis = 1)
fig, ax = plt.subplots(nrows=3, ncols=1, figsize = (24, 24))
ax[0].plot(sp[0])
ax[1].plot(sp[1])
ax[2].plot(sp[2])
# Write code here
freq = np.fft.fftshift(np.fft.fftfreq(n_sample, d=1/sampling_rate))
sp = ft = np.fft.fftshift(np.fft.fft(signal, axis=1))
fig, ax = plt.subplots(nrows=3, ncols=1, figsize = (24, 24))
ax[0].scatter(freq, sp[0])
ax[1].scatter(freq, sp[1])
ax[2].scatter(freq, sp[2])
# Write answer here
# Do your filtering here and plot the frequency domain signals.
# Write your justification for your filtering here
# Do the IFFT and plot the signals in the time domain here.
# Write answer here
# Write answer/code here (ungraded)