-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
51 lines (40 loc) · 1.52 KB
/
test.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
import numpy as np
import matplotlib.pyplot as plt
from scipy.linalg import dft
from ksvd import ksvd
from sparseRep import random_sensor, sense, reconstruct
def generate_sequence(N, signal_length):
# generate random dictionary
D = dft(signal_length).real
# generate random outputs
y = np.zeros((N,signal_length))
for i in range(y.shape[0]):
x = np.random.choice(D.shape[1], int(D.shape[1]/10))
y[i,:] = np.sum(D[x,:], axis=0)
y[i,:] = np.multiply(y[i,:], np.random.uniform(1, 2,size=y.shape[1]))
return y
if __name__ == "__main__":
# Generate train data: N samples of white noise signals
N = 500
signal_length = 50
sense_size = 25
data = generate_sequence(N, signal_length)
# K-SVD algorithm to learn sparse representation of data
D_size = 60
max_sparsity = int(0.25*D_size)
max_iter = 50
D,_,e = ksvd(data, D_size, max_sparsity,
maxiter=max_iter, debug=True)
# generate test data
test_data = generate_sequence(1, signal_length)
# compressive sensing
sensor = random_sensor((signal_length, sense_size))
representation = sense(test_data, sensor)
# sparse reconstruction
reconstruction = reconstruct(representation, sensor, D, max_sparsity)
print (D.shape, sensor.shape)
plt.plot(test_data[0,:], '--', label="true signal")
plt.plot(reconstruction, '.-', label="reconstruction")
plt.title("True vs. reconstructed signal")
plt.legend()
plt.show()