Commit 2ee75dc0f3b9b52f748d74d17aeb269e9389e2a9

Authored by Brice COLOMBIER
1 parent a46f48b03d
Exists in master

Add operations

Showing 5 changed files with 62 additions and 10 deletions

get_mat_dim.py View file @ 2ee75dc
  1 +import numpy as np
  2 +
  3 +import argparse
  4 +import os
  5 +
  6 +if __name__ == "__main__":
  7 +
  8 + # Parsing arguments
  9 + parser = argparse.ArgumentParser(description='Preprocess traces')
  10 + parser.add_argument("traces_name", type=str)
  11 + args = parser.parse_args()
  12 +
  13 + traces = np.load(args.traces_name)
  14 + print "Matrix of {0} of size {1}".format(traces.dtype, np.shape(traces))
pairwise_operation.py View file @ 2ee75dc
... ... @@ -80,6 +80,10 @@
80 80 preprocessed_trace[current_index,:] = np.transpose(value)[:,0]
81 81 indexes[current_index] = (start_index+current_start_index, start_index+current_start_index+current_distance)
82 82 # Increase the running index
  83 + if current_index in [10350, 20466, 10364, 19934, 10378, 19948, 10392, 19962]:
  84 + print current_index
  85 + print current_start_index
  86 + print current_distance
83 87 current_index+=1
84 88 preprocessed_trace = np.transpose(preprocessed_trace)
85 89 # return preprocessed_trace, indexes
... ... @@ -4,7 +4,10 @@
4 4  
5 5 def plot(traces_file):
6 6 traces = np.load(traces_file)
7   - plt.plot(traces[1,:])
  7 + f, axarr = plt.subplots(10, sharex=True)
  8 + for i in range(10):
  9 + axarr[i].plot((traces[i,:]))
  10 + plt.grid()
8 11 plt.show()
9 12  
10 13 if __name__ == "__main__":
  1 +import numpy as np
  2 +import argparse
  3 +import os
  4 +
  5 +import matplotlib.pyplot as plt
  6 +
  7 +def realign(traces_file):
  8 + traces = np.load(traces_file)
  9 + realigned_traces = np.zeros(np.shape(traces))
  10 + ref_trace = traces[0]
  11 + print np.shape(ref_trace)
  12 + nb_traces = np.shape(traces)[0]
  13 + nb_samples = np.shape(traces)[1]
  14 + realigned_traces[0] = ref_trace
  15 + for i in range(1, nb_traces):
  16 + shift = np.argmax(np.correlate(ref_trace, traces[i], mode="full")) - nb_samples
  17 + realigned_traces[i] = np.roll(traces[i,:], shift)
  18 + dirname, filename = os.path.split(args.traces_file)
  19 + np.save(os.path.join(dirname, "realigned_"+filename), realigned_traces)
  20 +
  21 +if __name__ == "__main__":
  22 +
  23 + # Parsing arguments
  24 + parser = argparse.ArgumentParser(description='Preprocess traces')
  25 + parser.add_argument("traces_file", type=str)
  26 + args = parser.parse_args()
  27 + realign(args.traces_file)
remove_window.py View file @ 2ee75dc
... ... @@ -5,7 +5,7 @@
5 5  
6 6 import matplotlib.pyplot as plt
7 7  
8   -def remove_window(traces, start_index, stop_index):
  8 +def remove_window(traces, start_index, stop_index, plot_only=False):
9 9 return np.delete(traces, range(start_index, stop_index), axis=1)
10 10  
11 11 if __name__ == "__main__":
... ... @@ -15,6 +15,7 @@
15 15 parser.add_argument("traces_name", type=str)
16 16 parser.add_argument("--start_index", type=int)
17 17 parser.add_argument("--stop_index", type=int)
  18 + parser.add_argument("--plot_only", type=bool)
18 19 args = parser.parse_args()
19 20  
20 21 fake_nb_samples = 10
21 22  
22 23  
23 24  
... ... @@ -24,15 +25,18 @@
24 25 traces = test_array
25 26 # Load traces from file
26 27 traces = np.load(args.traces_name)
  28 + traces_to_plot = traces[1:2,:]
27 29  
28   - plt.plot(traces[1,:], color='red')
29   - remove_windowed_traces = remove_window(traces, args.start_index, args.stop_index)
30   - plt.figure()
31   - plt.plot(remove_windowed_traces[1,:], color='green')
32   - plt.show()
  30 + if args.plot_only:
  31 + plt.plot(traces_to_plot[0,:], color='red', alpha=0.5)
  32 + plt.plot(traces_to_plot[0,:args.start_index], color='green')
  33 + plt.plot(range(args.stop_index, np.shape(traces_to_plot)[1]), traces_to_plot[0,args.stop_index:], color='green')
  34 + plt.show()
  35 + else:
  36 + remove_windowed_traces = remove_window(traces, args.start_index, args.stop_index)
33 37  
34   - dirname, filename = os.path.split(args.traces_name)
35   - filename, extension = os.path.splitext(filename)
  38 + dirname, filename = os.path.split(args.traces_name)
  39 + filename, extension = os.path.splitext(filename)
36 40  
37   - # np.save(os.path.join(dirname, "remove_windowed_"+str(args.start_index)+"_"+str(args.stop_index)+"_"+filename+extension), remove_windowed_traces)
  41 + np.save(os.path.join(dirname, "remove_windowed_"+str(args.start_index)+"_"+str(args.stop_index)+"_"+filename+extension), remove_windowed_traces)