import os import pandas as pd # Path settings input_file = r'G:\Users\Chipistil\Desktop\High-throughput-T2NP-Memristor-based-Dual-clock-edge-sampling-TRNG\Data Conversion\tdelay+tr.txt' output_file = r'G:\Users\Chipistil\Desktop\High-throughput-T2NP-Memristor-based-Dual-clock-edge-sampling-TRNG\Data Conversion\td.xlsx' data = [] with open(input_file, 'r') as f: for line in f: line = line.strip() if line: cols = line.split() if len(cols) >= 3: col1 = round(float(cols[0]), 4) col2 = round(float(cols[1]), 4) col3 = round(float(cols[2]), 4) data.append((col1, col2, col3)) td_list = [] n = len(data) i = 0 def find_dg_cluster(start_idx): cluster_start = None consecutive_230 = 0 in_cluster = False sep_count = 0 for j in range(start_idx, n): current = data[j][2] if current == 2.30: if not in_cluster: if cluster_start is None: cluster_start = j consecutive_230 += 1 if consecutive_230 >= 6: in_cluster = True sep_count = 0 else: consecutive_230 += 1 sep_count = 0 elif current == 0.00 and in_cluster: sep_count += 1 if sep_count >= 6: return (cluster_start, j - sep_count), j + 1 else: if in_cluster: return (cluster_start, j-1), j else: cluster_start = None consecutive_230 = 0 sep_count = 0 if in_cluster: return (cluster_start, n-1), n return None, n def locate_key_points(start, end): dg1_start = next((j for j in range(start, end+1) if data[j][2] > 0.1), None) dg1_end = next((j for j in range(start, end+1) if data[j][2] == 2.30), None) if not dg1_start or not dg1_end: return None, None dg1_idx = (dg1_start + dg1_end) / 2 col2_values = [data[j][1] for j in range(start, end+1)] col2_indices = list(range(start, end+1)) rise_start = None rise_end = None for idx in range(len(col2_values)): if col2_values[idx] > 0.4 and rise_start is None: if idx + 10 <= len(col2_values): next_10_values = col2_values[idx:idx+10] count_above_2 = sum(1 for val in next_10_values if val > 2) if count_above_2 >= 5: rise_start = idx if rise_start is not None and col2_values[idx] > 2.9: rise_end = idx break if rise_start is not None and rise_end is None: max_val = max(col2_values[rise_start:]) for idx in range(rise_start, len(col2_values)): if col2_values[idx] == max_val: rise_end = idx break if rise_start is not None and rise_end is not None: n_rise = rise_end - rise_start + 1 if n_rise % 2 == 1: t2_idx = col2_indices[rise_start + n_rise // 2] else: mid1 = col2_indices[rise_start + n_rise // 2 - 1] mid2 = col2_indices[rise_start + n_rise // 2] t2_idx = (mid1 + mid2) / 2 else: t2_idx = None return dg1_idx, t2_idx while i < n: dg_range, new_i = find_dg_cluster(i) if dg_range: start, end = dg_range dg1_idx, t2_idx = locate_key_points(start, end) if all([dg1_idx, t2_idx]): dg1_time = (data[int(dg1_idx)][0] + data[int(dg1_idx) + 1][0]) / 2 if isinstance(t2_idx, float): t2_time = (data[int(t2_idx)][0] + data[int(t2_idx) + 1][0]) / 2 else: t2_time = data[t2_idx][0] td = round(t2_time - dg1_time, 4) td_list.append((td, round(dg1_time, 4), round(t2_time, 4))) i = new_i else: i += 1 if td_list: df = pd.DataFrame(td_list, columns=['td', 't1', 't2']) df.to_excel(output_file, index=False) print(f"Processing completed, found {len(td_list)} valid td values, written to Excel file") else: print("No valid td values found")