Skip to content

Commit a12cdf8

Browse files
committed
More scripts
1 parent 06e492a commit a12cdf8

File tree

7 files changed

+419
-11
lines changed

7 files changed

+419
-11
lines changed

eval/fastzip/fastzip_tools.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,15 @@ def parse_command_line_args(
320320
snr_level,
321321
trials,
322322
)
323+
324+
def unpack_event_parameters(params):
325+
names = ["window_size",
326+
"overlap_size",
327+
"power_th",
328+
"snr_th",
329+
"peak_th",
330+
"sample_rate",
331+
"peak_status",
332+
"normalize",
333+
"alpha"]
334+
return [params[name] for name in names]

eval/fastzip/real_eval_fastzip.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import os
2+
import random
3+
import sys
4+
5+
from fastzip_tools import DATA_DIRECTORY, calc_all_event_bits_fastzip
6+
7+
sys.path.insert(1, os.getcwd() + "/..") # Gives us path to eval_tools.py
8+
from eval_tools import ( # noqa: E402
9+
load_signal_groups,
10+
wav_file_load,
11+
)
12+
from evaluator import Evaluator # noqa: E402
13+
14+
# Static default parameters
15+
KEY_LENGTH_DEFAULT = 256
16+
NUMBER_OF_CHOICES_DEFAULT = 2000
17+
WRAP_AROUND_LIMIT_DEFAULT = 10
18+
19+
EVENT_NUM_DEFAULT = 5
20+
21+
EVENT_FILE_STUB = "fastzip_event_real"
22+
23+
FUZZING_DIR = "fastzip_real_fuzz"
24+
FUZZING_STUB = "fastzip_real"
25+
26+
DEFAULT_GROUPS = [
27+
["10.0.0.238", "10.0.0.228", "10.0.0.239"],
28+
["10.0.0.231", "10.0.0.232", "10.0.0.239"],
29+
["10.0.0.233", "10.0.0.236", "10.0.0.239"],
30+
["10.0.0.227", "10.0.0.229", "10.0.0.237"],
31+
["10.0.0.235", "10.0.0.237", "10.0.0.239"],
32+
["10.0.0.234", "10.0.0.239", "10.0.0.237"],
33+
]
34+
35+
DEFAULT_SENSOR_TYPE = "mic"
36+
37+
DEFAULT_TIMESTAMP = "202408*.wav"
38+
39+
SENSOR_DATA_DIR = "/mnt/nas"
40+
41+
PARAM_DIR = "../plot_scripts/plot_data/fastzip_real_fuzz"
42+
PARAM_FILE_STUB = "fastzip_real_fuzz"
43+
44+
def main(
45+
key_length=KEY_LENGTH_DEFAULT,
46+
data_dir=SENSOR_DATA_DIR,
47+
param_dir=PARAM_DIR,
48+
sensor_type=DEFAULT_SENSOR_TYPE,
49+
groups=DEFAULT_GROUPS,
50+
timestamp=DEFAULT_TIMESTAMP,
51+
):
52+
if not os.path.isdir(DATA_DIRECTORY):
53+
os.mkdir(DATA_DIRECTORY)
54+
55+
def unpack_parameters(params):
56+
names = [
57+
"window_size",
58+
"overlap_size",
59+
"n_bits",
60+
"eqd_delta",
61+
"ewma",
62+
"alpha",
63+
"remove_noise",
64+
"bias",
65+
"normalize",
66+
"power_th",
67+
"snr_th",
68+
"peak_th",
69+
"peak_status",
70+
"sample_rate"
71+
]
72+
output = [params[name] for name in names]
73+
output.append(EVENT_NUM_DEFAULT)
74+
return output
75+
76+
group_signals, group_params = load_signal_groups(
77+
groups,
78+
sensor_type,
79+
timestamp,
80+
data_dir,
81+
param_dir,
82+
PARAM_FILE_STUB,
83+
unpack_parameters,
84+
load_func=wav_file_load,
85+
event_dir_file_stub=EVENT_FILE_STUB,
86+
data_dir=DATA_DIRECTORY
87+
)
88+
89+
def func(signals, *params):
90+
key_size = params[0]
91+
remove_noise = params[7]
92+
ewma_filter = params[5]
93+
alpha = params[6]
94+
bias = params[8]
95+
n_bits = params[3]
96+
eqd_delta = params[4]
97+
print(
98+
f"key_size: {key_size} ; remove_noise: {remove_noise} ; ewma_filter: {ewma_filter} ; alpha: {alpha} ; bias: {bias} ; n_bits: {n_bits} ; eqd_delta: {eqd_delta}"
99+
)
100+
return calc_all_event_bits_fastzip(
101+
signals, key_size, remove_noise, ewma_filter, alpha, bias, n_bits, eqd_delta
102+
)
103+
104+
105+
# Creating an evaluator object with the bit generation algorithm
106+
evaluator = Evaluator(func, event_gen=True, convert_bytes_to_bitstring=False)
107+
# Evaluating the signals with the specified number of trials
108+
evaluator.best_parameter_evaluation(
109+
group_signals, group_params, key_length, DATA_DIRECTORY, FUZZING_STUB
110+
)
111+
112+
113+
if __name__ == "__main__":
114+
main()
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import os
2+
import random
3+
import sys
4+
5+
from fastzip_tools import (
6+
DATA_DIRECTORY,
7+
MICROPHONE_SAMPLING_RATE,
8+
fastzip_event_detection_wrapper_func,
9+
unpack_event_parameters,
10+
)
11+
12+
sys.path.insert(1, os.getcwd() + "/..") # Gives us path to eval_tools.py
13+
from eval_tools import ( # noqa: E402
14+
load_signal_groups,
15+
wav_file_load,
16+
)
17+
from evaluator import Evaluator # noqa: E402
18+
19+
# Static default parameters
20+
KEY_LENGTH_DEFAULT = 128
21+
TARGET_SNR_DEFAULT = 40
22+
NUMBER_OF_CHOICES_DEFAULT = 2000
23+
WRAP_AROUND_LIMIT_DEFAULT = 10
24+
25+
FUZZING_DIR = "fastzip_real_fuzz"
26+
FUZZING_STUB = "fastzip_event_real"
27+
28+
DATA_STUB = "fastzip_event_real_longitudinal"
29+
30+
DEFAULT_GROUPS = [
31+
["10.0.0.238", "10.0.0.228", "10.0.0.239"],
32+
["10.0.0.231", "10.0.0.232", "10.0.0.239"],
33+
["10.0.0.233", "10.0.0.236", "10.0.0.239"],
34+
["10.0.0.227", "10.0.0.229", "10.0.0.237"],
35+
["10.0.0.235", "10.0.0.237", "10.0.0.239"],
36+
["10.0.0.234", "10.0.0.239", "10.0.0.237"],
37+
]
38+
39+
DEFAULT_SENSOR_TYPE = "mic"
40+
41+
DEFAULT_TIMESTAMP = "202408*.wav"
42+
43+
SENSOR_DATA_DIR = "/mnt/nas"
44+
45+
PARAM_DIR = "../plot_scripts/plot_data/fastzip_real_fuzz"
46+
PARAM_FILE_STUB = "fastzip_real_fuzz"
47+
48+
def main(
49+
key_length=KEY_LENGTH_DEFAULT,
50+
data_dir=SENSOR_DATA_DIR,
51+
param_dir=PARAM_DIR,
52+
sensor_type=DEFAULT_SENSOR_TYPE,
53+
groups=DEFAULT_GROUPS,
54+
timestamp=DEFAULT_TIMESTAMP,
55+
):
56+
if not os.path.isdir(DATA_DIRECTORY):
57+
os.mkdir(DATA_DIRECTORY)
58+
59+
group_signals, group_params = load_signal_groups(
60+
groups,
61+
sensor_type,
62+
timestamp,
63+
data_dir,
64+
param_dir,
65+
PARAM_FILE_STUB,
66+
unpack_event_parameters,
67+
load_func=wav_file_load
68+
)
69+
70+
# Creating an evaluator object with the bit generation algorithm
71+
evaluator = Evaluator(fastzip_event_detection_wrapper_func, event_gen=True)
72+
# Evaluating the signals with the specified number of trials
73+
evaluator.best_parameter_evaluation(
74+
group_signals, group_params, key_length, DATA_DIRECTORY, FUZZING_STUB
75+
)
76+
77+
78+
if __name__ == "__main__":
79+
main()

eval/perceptio/perceptio_tools.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,22 @@ def merge_events(first_event_list, second_event_list, lump_th, chunk_size, itera
8181
def process_events(
8282
events, event_signals, key_size, cluster_sizes_to_check, cluster_th, Fs
8383
):
84+
from datetime import timedelta
8485

85-
event_features = [PerceptioProcessing.generate_features(x) for x in event_signals]
86-
87-
labels, k, _ = PerceptioProcessing.kmeans_w_elbow_method(
88-
event_features, cluster_sizes_to_check, cluster_th
89-
)
90-
91-
grouped_events = PerceptioProcessing.group_events(events, labels, k)
86+
key = bytearray()
87+
for j in range(len(events) - 1):
88+
interval = (events[j + 1][0] - events[j][0]) / Fs
89+
in_microseconds = int(
90+
timedelta(seconds=interval) / timedelta(microseconds=1)
91+
)
92+
key += in_microseconds.to_bytes(
93+
8, "big"
94+
) # Going to treat every interval as a 4 byte integer
9295

93-
fps = PerceptioProcessing.gen_fingerprints(grouped_events, k, key_size // 8, Fs)
96+
if len(key) >= key_size:
97+
key = bytes(key[-key_size:])
9498

95-
return fps
99+
return [key]
96100

97101

98102
def extract_all_events(signal, top_th, bottom_th, lump_th, a, chunk_size=10000):
@@ -198,6 +202,7 @@ def generate_bits(
198202
grouped_events = PerceptioProcessing.group_events(events, labels, k)
199203

200204
fps = PerceptioProcessing.gen_fingerprints(grouped_events, k, key_size_in_bytes, Fs)
205+
201206
return fps, grouped_events
202207

203208

@@ -284,3 +289,10 @@ def get_command_line_args(
284289
target_snr,
285290
trials,
286291
)
292+
293+
def unpack_event_parameters(params):
294+
names = ["top_th",
295+
"bottom_th",
296+
"lump_th",
297+
"a"]
298+
return [params[name] for name in names]
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import os
2+
import random
3+
import sys
4+
5+
from perceptio_tools import (
6+
DATA_DIRECTORY,
7+
MICROPHONE_SAMPLING_RATE,
8+
process_events,
9+
unpack_event_parameters,
10+
)
11+
12+
sys.path.insert(1, os.getcwd() + "/..") # Gives us path to eval_tools.py
13+
from eval_tools import ( # noqa: E402
14+
load_signal_groups,
15+
wav_file_load,
16+
)
17+
from evaluator import Evaluator # noqa: E402
18+
19+
# Static default parameters
20+
KEY_LENGTH_DEFAULT = 256
21+
NUMBER_OF_CHOICES_DEFAULT = 2000
22+
WRAP_AROUND_LIMIT_DEFAULT = 10
23+
24+
EVENT_NUM_DEFAULT = 5
25+
26+
EVENT_DIR = ""
27+
EVENT_FILE_STUB = "perceptio_event_real_longitudinal"
28+
29+
FUZZING_DIR = "perceptio_real_fuzz"
30+
FUZZING_STUB = "perceptio_real"
31+
32+
DATA_STUB = "perceptio_real_longitudinal"
33+
34+
DEFAULT_GROUPS = [
35+
["10.0.0.238", "10.0.0.228", "10.0.0.239"],
36+
["10.0.0.231", "10.0.0.232", "10.0.0.239"],
37+
["10.0.0.233", "10.0.0.236", "10.0.0.239"],
38+
["10.0.0.227", "10.0.0.229", "10.0.0.237"],
39+
["10.0.0.235", "10.0.0.237", "10.0.0.239"],
40+
["10.0.0.234", "10.0.0.239", "10.0.0.237"],
41+
]
42+
43+
DEFAULT_SENSOR_TYPE = "mic"
44+
45+
DEFAULT_TIMESTAMP = "202408*.wav"
46+
47+
SENSOR_DATA_DIR = "/mnt/nas"
48+
49+
PARAM_DIR = "../plot_scripts/plot_data/perceptio_real_fuzz"
50+
PARAM_FILE_STUB = "perceptio_real_fuzz"
51+
52+
def main(
53+
key_length=KEY_LENGTH_DEFAULT,
54+
data_dir=SENSOR_DATA_DIR,
55+
param_dir=PARAM_DIR,
56+
sensor_type=DEFAULT_SENSOR_TYPE,
57+
groups=DEFAULT_GROUPS,
58+
timestamp=DEFAULT_TIMESTAMP,
59+
):
60+
if not os.path.isdir(DATA_DIRECTORY):
61+
os.mkdir(DATA_DIRECTORY)
62+
63+
def unpack_parameters(params):
64+
names = ["top_th",
65+
"bottom_th",
66+
"lump_th",
67+
"a",
68+
"cluster_size",
69+
"cluster_th"
70+
]
71+
output = [params[name] for name in names]
72+
output.extend([MICROPHONE_SAMPLING_RATE, EVENT_NUM_DEFAULT])
73+
return output
74+
75+
group_signals, group_params = load_signal_groups(
76+
groups,
77+
sensor_type,
78+
timestamp,
79+
data_dir,
80+
param_dir,
81+
PARAM_FILE_STUB,
82+
unpack_event_parameters,
83+
load_func=wav_file_load,
84+
event_dir_file_stub=EVENT_FILE_STUB
85+
)
86+
87+
def func(signals, *params):
88+
key_size = params[0]
89+
cluster_sizes_to_check = params[5]
90+
cluster_th = params[6]
91+
Fs = params[7]
92+
number_of_events = params[8]
93+
return calc_all_event_bits(
94+
signals,
95+
process_events,
96+
number_of_events,
97+
key_size // 8,
98+
cluster_sizes_to_check,
99+
cluster_th,
100+
Fs,
101+
)
102+
103+
# Creating an evaluator object with the bit generation algorithm
104+
evaluator = Evaluator(func, event_gen=True)
105+
# Evaluating the signals with the specified number of trials
106+
evaluator.best_parameter_evaluation(
107+
group_signals, group_params, key_length, DATA_DIRECTORY, FUZZING_STUB
108+
)
109+
110+
111+
if __name__ == "__main__":
112+
main()

0 commit comments

Comments
 (0)