-
Notifications
You must be signed in to change notification settings - Fork 1
/
read_matrices.py
86 lines (72 loc) · 2.55 KB
/
read_matrices.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
Copyright (C) 2023 Karl-Ludwig Besser
This program is used in the article:
"Securing Data in Multimode Fibers by Exploiting Mode-Dependent Light
Propagation Effects" (S. Rothe, K.-L. Besser, D. Krause, R. Kuschmierz, N.
Koukourakis, E. Jorswieck, J. Czarske. Research, vol. 6: 0065, Jan. 2023.
DOI:10.34133/research.0065).
License:
This program is licensed under the GPLv3 license. If you in any way use this
code for research that results in publications, please cite our original
article listed above.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
See the GNU General Public License for more details.
Author: Karl-Ludwig Besser, Technische Universität Braunschweig
"""
import numpy as np
import scipy.io as sio
import h5py
BOB = "bob"
EVE = "eve"
USERS = [BOB, EVE]
def get_mat_key(user, precoded=True):
PATTERN = "matrix_{}{}"
if precoded:
_pre = "_precoding"
else:
_pre = ""
#return PATTERN.format(user.capitalize(), _pre)
return PATTERN.format(user, _pre)
def read_mat_file(mat_file):
try:
mat_dict = sio.loadmat(mat_file)
mat_dict = {k: v for k, v in mat_dict.items() if not k.startswith("__")}
except NotImplementedError:
mat_dict = {}
with h5py.File(mat_file, 'r') as h5f:
for k, v in h5f.items():
_struct_mat = np.array(v)
mat_dict[k] = np.matrix(v['real'] + 1j*v['imag'])
return mat_dict
def read_measurement_file(mat_file, precoded=True):
mat_dict = read_mat_file(mat_file)
mat_orig, mat_prec = parse_mat_dict(mat_dict)
if precoded:
return mat_prec
else:
return mat_orig
def parse_mat_dict(mat_dict):
mat_orig = {k: mat_dict[get_mat_key(k, precoded=False)] for k in USERS}
try:
mat_prec = {k: mat_dict[get_mat_key(k, precoded=True)] for k in USERS}
except KeyError:
mat_prec = None
return mat_orig, mat_prec
if __name__ == "__main__":
import argparse
import matplotlib.pyplot as plt
parser = argparse.ArgumentParser()
parser.add_argument("mat_file")
args = vars(parser.parse_args())
mat_file = args['mat_file']
mat_dict = read_mat_file(mat_file)
print(mat_dict.keys())
mat_orig, mat_prec = parse_mat_dict(mat_dict)
U, S, Vh = np.linalg.svd(mat_orig[BOB])
#print(S)
#print(np.abs(np.diag(mat_prec[BOB])))
plt.matshow(np.abs(mat_prec[BOB]))
plt.matshow(np.abs(np.diag(S)-mat_prec[BOB]))
plt.show()