-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze.py
More file actions
74 lines (61 loc) · 2.51 KB
/
Copy pathanalyze.py
File metadata and controls
74 lines (61 loc) · 2.51 KB
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
import pandas as pd
import numpy as np
import itertools
import matplotlib.pyplot as plt
from tqdm import tqdm
# Load CSV Data
def load_data(csv_path):
df = pd.read_csv(csv_path)
df = df[df["/SN"] > 0] # Filter out invalid exchanges
return df
# Reconstruct Exchanges
def reconstruct_exchanges(df):
exchanges = {}
for sn, group in df.groupby("/SN"):
if len(group["/AN"].unique()) != 2:
continue # Ensure exchanges involve exactly two devices
poll_device = group[group["/AN"] == group["/AN"].min()]
resp_device = group[group["/AN"] == group["/AN"].max()]
if poll_device.empty or resp_device.empty:
continue
exchange = {
"poll_tx": poll_device["/uwb_poll_tx"].values[0],
"resp_rx": resp_device["/uwb_resp_rx"].values[0],
"resp_tx": resp_device["/uwb_resp_tx"].values[0],
"poll_rx": poll_device["/uwb_poll_rx"].values[0],
"poll_mcu_tx": poll_device["/mcu_poll_tx"].values[0],
"poll_mcu_rx": poll_device["/mcu_poll_rx"].values[0],
"resp_mcu_rx": resp_device["/mcu_resp_rx"].values[0],
"resp_mcu_tx": resp_device["/mcu_resp_tx"].values[0],
}
exchanges[sn] = exchange
return exchanges
# Estimate Time Offset
def estimate_offset(exchanges):
offsets = []
for sn, ex in exchanges.items():
if all(ex[key] > 0 for key in ex):
uwb_offset = (ex["poll_rx"] - ex["poll_tx"]) - (ex["resp_tx"] - ex["resp_rx"])
mcu_offset = (ex["poll_mcu_rx"] - ex["poll_mcu_tx"]) - (ex["resp_mcu_tx"] - ex["resp_mcu_rx"])
offsets.append((sn, uwb_offset, mcu_offset))
return offsets
# Main Function
def main(csv_path):
df = load_data(csv_path)
exchanges = reconstruct_exchanges(df)
offsets = estimate_offset(exchanges)
# Convert to DataFrame for visualization
offset_df = pd.DataFrame(offsets, columns=["/SN", "UWB_Offset", "MCU_Offset"])
# Plot offsets
plt.figure(figsize=(10, 5))
plt.plot(offset_df["/SN"], offset_df["UWB_Offset"], label="UWB Offset", marker="o")
plt.plot(offset_df["/SN"], offset_df["MCU_Offset"], label="MCU Offset", marker="s")
plt.xlabel("Exchange SN")
plt.ylabel("Offset (time units)")
plt.title("Time Offset Comparison between UWB and MCU Domains")
plt.legend()
plt.grid()
plt.show()
if __name__ == "__main__":
csv_file_path = "timesync.csv" # Replace with actual path
main(csv_file_path)