NMM 2020 Jake Read 09 Transforms

In [3]:
import numpy as np
import math
import matplotlib.pyplot as plt
import scipy as sp

13.2

a) plot a time series for the sum of two sine waves at 697 and 1209 hz, sampling at 10,000 sps for 0.25s,

In [4]:
t = np.arange(0, 0.25, 0.0001)
y = np.zeros(len(t))
for i in range(len(t)):
    y[i] = math.sin(697*t[i]) + math.sin(1209*t[i])
plt.plot(y)
Out[4]:
[<matplotlib.lines.Line2D at 0x194015bd2c8>]

b) calculate and plot the DCT

In [7]:
dct = sp.fft.dct(y)
print(dct)
print(len(dct))
[ 3.56095707e+01  5.48121431e+01  3.56534609e+01 ... -2.64961296e-04
 -6.30955732e-05 -8.83201984e-05]
2500
In [10]:
plt.plot(dct[0:250])
Out[10]:
[<matplotlib.lines.Line2D at 0x194017b2d48>]

c) invert the dct and verify that it matches

In [11]:
idct = sp.fft.idct(dct)
plt.plot(idct)
Out[11]:
[<matplotlib.lines.Line2D at 0x194018174c8>]

d) randomly sample and plot a subset of 5% of the points (125)

In [64]:
ts = np.random.choice(t, 125)
ys = np.zeros(125)
ts.sort()
for i in range(len(ts)):
    ys[i] = math.sin(697*ts[i]) + math.sin(1209*ts[i])
plt.plot(ts, ys)
plt.scatter(ts, ys)
Out[64]:
<matplotlib.collections.PathCollection at 0x194074b79c8>

13.4

a) generate pairs of uniform values s1,s2 with each component contained in 0,1, and plot

In [67]:
s1 = np.random.uniform(0, 1, 1000)
s2 = np.random.uniform(0, 1, 1000)
plt.scatter(s1, s2)
Out[67]:
<matplotlib.collections.PathCollection at 0x194085d1c48>
In [69]:
s = np.array([s1, s2])
A = np.array([[1,2],[3,1]])
x = A.dot(s)
plt.scatter(x[0],x[1])
Out[69]:
<matplotlib.collections.PathCollection at 0x19408640808>
In [76]:
x = x - np.mean(x)
covariance = np.cov(x)
evals, evects = np.linalg.eig(covariance)

x = evects.T.dot(x)
#plt.scatter(x[0],x[1])

x = (x.T / np.sqrt(evals)).T
plt.scatter(x[0], x[1])
Out[76]:
<matplotlib.collections.PathCollection at 0x1940876dfc8>
In [ ]: