This commit is contained in:
root
2025-04-28 14:35:09 +02:00
parent 7645e49794
commit cd7eb8401e
81 changed files with 1064 additions and 293 deletions

View File

@@ -20,22 +20,37 @@ def initialize(params):
initialized = True
def find_edge(data, step_length=50, edge_type="falling"):
# prepare a step function and refine it
def find_edge(data, step_length=50, edge_type="falling", roi=None):
"""
Find an edge in the given data using cross-correlation with a step waveform.
If a region-of-interest (roi) is provided as [start, end], the search is limited
to that slice of the data and the resulting edge position is offset appropriately.
"""
# If ROI is provided, slice the data accordingly.
if roi is not None:
data_roi = data[roi[0]:roi[1]]
else:
data_roi = data
# Prepare the step function.
step_waveform = np.ones(shape=(step_length,))
if edge_type == "rising":
step_waveform[: int(step_length / 2)] = -1
elif edge_type == "falling":
step_waveform[int(step_length / 2) :] = -1
step_waveform[int(step_length / 2):] = -1
# find edges
xcorr = signal.correlate(data, step_waveform, mode="valid")
# Perform cross-correlation on the (possibly sliced) data.
xcorr = signal.correlate(data_roi, step_waveform, mode="valid")
edge_position = np.argmax(xcorr)
xcorr_amplitude = np.amax(xcorr)
# correct edge_position for step_length
# Correct edge_position for step_length.
edge_position += np.floor(step_length / 2)
# If ROI is provided, add the offset to get the position in the full signal.
if roi is not None:
edge_position += roi[0]
return {
"edge_pos": edge_position,
"xcorr": xcorr,
@@ -71,7 +86,8 @@ def process(data, pulse_id, timestamp, params):
prof_sig_norm = prof_sig_savgol
if events[fel_on_event] and not events[dark_event]:
edge_results = find_edge(prof_sig_norm, step_length, edge_type)
# Limit the edge search to the region-of-interest provided in params.
edge_results = find_edge(prof_sig_norm, step_length, edge_type, roi=params.get("roi"))
edge_results["arrival_time"] = np.polyval(calib, edge_results["edge_pos"])
else:
edge_results = {
@@ -82,7 +98,7 @@ def process(data, pulse_id, timestamp, params):
}
edge_results["arrival_time"] = None
# Set bs outputs
# Set beam-synchronization outputs.
output = {}
for key, value in edge_results.items():
output[f"{device}:{key}"] = value
@@ -90,24 +106,9 @@ def process(data, pulse_id, timestamp, params):
output[f"{device}:raw_wf"] = prof_sig
output[f"{device}:raw_wf_savgol"] = prof_sig_savgol
# if events[dark_event]:
# output[f"{device}:dark_wf"] = prof_sig
# output[f"{device}:dark_wf_savgol"] = prof_sig_savgol
# else:
# output[f"{device}:dark_wf"] = None
# output[f"{device}:dark_wf_savgol"] = None
if buffer_dark:
output[f"{device}:avg_dark_wf"] = np.mean(buffer_dark, axis=0)
else:
#output[f"{device}:avg_dark_wf"] = np.zeros_like(prof_sig)
#Changed By Gobbo to avoid type errors
output[f"{device}:avg_dark_wf"] = None # np.zeros_like(prof_sig)
output[f"{device}:avg_dark_wf"] = None
# if buffer_savgol:
# output[f"{device}:avg_dark_wf_savgol"] = np.mean(buffer_savgol, axis=0)
# else:
# output[f"{device}:avg_dark_wf_savgol"] = None
return output
return output