-
Notifications
You must be signed in to change notification settings - Fork 2
/
simulation.py
executable file
·71 lines (55 loc) · 1.89 KB
/
simulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
# from sklearn import linear_model
from lasso import cd_lasso
# Simulation to make sure we can run the compressive sampling algorithm on some
# data at least.
datafile_name = 'data/samples.csv'
reconstruction_filename = 'reconstructed_py.csv'
data = np.loadtxt(datafile_name, delimiter=',')
t0 = data[:,0]
X0 = data[:,1]
N0 = t0.shape[0]
f0 = 4 # Hz
T0 = 1.0 / f0 # s
T_window = 60 # s
N_window = int(T_window // T0)
if N_window % 4 != 0:
N_window = N_window - (N_window % 4)
# Incomplete windows are too much of a hassle to deal with right now.
N0 = N0 - (N0 % (N_window // 2))
t0 = t0[:N0]
X0 = X0[:N0]
psi = np.empty((N_window, N_window))
n, k = np.meshgrid(np.arange(N_window), np.arange(N_window))
psi = np.cos((np.pi / N_window) * (n + 0.5) * k)
psi[0,:] *= (1.0 / np.sqrt(2))
psi *= np.sqrt(2.0 / N_window)
# Random permutation of identity matrix, chopped off to take fewer samples.
phi_mat_filename = 'data/phi_mat.csv'
M = N_window // 12
try:
phi = np.loadtxt(phi_mat_filename, delimiter=',')
except OSError:
phi = np.random.permutation(np.eye(N_window))
phi = phi[:M,:]
A = np.dot(phi, psi.T)
Xr = np.empty(X0.shape)
for i in range(0, N0 - N_window + 1, N_window // 2):
X_window = X0[i:i+N_window]
Y = np.dot(phi, X_window)
# lasso = linear_model.Lasso(alpha=0.01, fit_intercept=False)
# lasso.fit(A, Y)
# s = lasso.coef_
s = cd_lasso(Y, A)
xr = np.dot(psi.T, s)
Xr[i+N_window//4:i+3*N_window//4] = xr[N_window//4:3*N_window//4]
plt.plot(t0, X0, label='original')
plt.plot(t0, Xr, label='reconstructed')
plt.legend()
plt.show()
print('Corrcoef: ', np.corrcoef(X0[N_window//4:-N_window//4],
Xr[N_window//4:-N_window//4]))
out_data = np.hstack((t0.reshape(-1, 1), Xr.reshape(-1, 1)))
np.savetxt(reconstruction_filename, out_data, delimiter=',')