-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacq_traces.py
175 lines (147 loc) · 5.19 KB
/
acq_traces.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
import h5py
import os
from scipy import fft
from scipy import signal
import statistics
import scipy.stats as stats
class acq:
def __init__(self, no_samples, chunk):
self.chunk = chunk
self.no_samples = no_samples
self.buff_init()
self.pack = 0
self.count = 0
if('leakages' not in os.listdir()):
os.mkdir("leakages")
else:
self.files = os.listdir("leakages")
self.count_files('h5')
def count_files(self, xten):
self.pack = 0
for i in self.files:
if(xten in i):
self.pack += 1
def buff_init(self):
self.buff = np.zeros((self.chunk, self.no_samples), dtype='int8')
def save_trace(self, trace):
self.buff[self.count] = trace
self.count += 1
if self.count == self.chunk:
file = h5py.File(f"leakages//{self.pack}.h5", 'w')
file.create_dataset('leakages', data=self.buff)
self.count = 0
self.buff_init()
self.pack += 1
class Leakeges:
def __init__(self, dest ):
self.dir = dest + '//0.h5'
self.f = h5py.File(self.dir, 'r')
self.buff = self.f['leakages'][:]
self.rows, self.col = self.buff.shape
self.count = len(os.listdir('leakages'))
self.traces = np.zeros(((self.count * self.rows), self.col), dtype='int8')
def read(self):
for i in tqdm(range(self.count)):
self.dir = 'leakages' + f"//{i}.h5"
self.f = h5py.File(self.dir, 'r')
self.buff = self.f['leakages'][:]
self.traces[(i * self.rows):((i * self.rows) + self.rows)] = self.buff
return self.traces
class alignment:
def align_trace(self, ref, trace, norm, visulize):
self.ref1 = np.concatenate((ref, np.zeros(len(trace) - len(ref))))
if (np.std(trace) != 0):
if norm:
self.ref1 = (self.ref1 - np.mean(self.ref1)) / (np.std(self.ref1) * len(self.ref1))
trace = (trace - np.mean(trace)) / (np.std(trace))
self.corr = signal.correlate(trace, self.ref1, 'full')
self.corr = self.corr[int(len(trace)):int(2 * len(trace))]
# corr=corr[0:200]
self.max_corr = np.max(self.corr)
# print(max_corr)
self.peak_position = np.where(self.corr == self.max_corr)[0][0]
else:
# input('error')
self.peak_position = 100
if visulize:
fig, (ax1, ax2) = plt.subplots(2)
ax1.plot(trace)
ax2.plot(self.corr)
plt.show()
# self.trace_out = trace[self.peak_position:self.peak_position + len(ref)]
return self.peak_position
class TracesProcessing:
def ttest(self, group_a, group_b):
n_a = len(group_a)
n_b = len(group_b)
ttest_value = np.zeros(group_a.shape[1])
for i in tqdm(range(group_a.shape[1])):
avg_a = np.average(group_a[:, i])
avg_b = np.average(group_b[:, i])
std_a2 = (np.std(group_a[:, i])) ** 2
std_b2 = (np.std(group_b[:, i])) ** 2
upper = avg_a - avg_b
lower = np.sqrt((std_a2 / n_a) + (std_b2 / n_b))
ttest_value[i] = upper / lower
# ttest_value[i] = stats.ttest_ind(group_a[:, i], group_b[:, i], equal_var=False)[1]
return ttest_value
def var(self, group_a):
n_a = len(group_a)
var = np.zeros(group_a.shape[1])
for i in tqdm(range(group_a.shape[1])):
var[i] = np.var(group_a[:, i])
return var
def run_test():
f = h5py.File('19.h5', 'r')
f = f['leakages'][:]
group_a = f[0:2500]
group_b = f[2500:]
# group_a = np.random.randint(200, size=(100, 100))
# group_b = np.random.randint(200, size=(100, 100))
w = wlc_ttest()
ttest = w.ttest(group_a, group_b)
fig, (ax1, ax2) = plt.subplots(2)
ax1.plot(np.average(f, axis=0))
ax2.plot(ttest)
plt.show()
def write_traces():
chunck = 5
no_samples = 200
a = acq(no_samples, chunck)
for i in tqdm(range(15)):
trace = np.random.uniform(20, size=200)
a.save_trace(trace)
def read_traces():
dir = 'leakages'
leaks = Leakeges(dir)
return leaks.read()
def align_traces():
f = h5py.File('19.h5', 'r')
f = f['leakages'][:]
f = f[:, 10000:32000]
# ref = f[0, 1000: 17000]
ref = np.load('ref.npy')
a = alignment()
aligned_traces = np.zeros((f.shape[0], 20600))
# indexMax = a.align_trace(ref, f[1], norm=True, visulize=True)
for x, y in enumerate(f):
indexMax = a.align_trace(ref, y, norm=True, visulize=False)
# print('index', indexMax)
aligned_traces[x] = y[indexMax:indexMax + 20600]
# for i in range(50):
# plt.plot(aligned_traces[i])
# plt.show()
w = TracesProcessing()
ttest = w.ttest(aligned_traces[0:2000], aligned_traces[2000:4000])
fig, (ax1, ax2) = plt.subplots(2)
ax1.plot(np.average(aligned_traces, axis=0))
ax2.plot(ttest)
plt.show()
if __name__ == "__main__":
traces = read_traces()
# write_traces()
# align_traces()
# run_test()