Source code for hqs_qorrelator_app.post_processing

# Copyright © 2021-2025 HQS Quantum Simulations GmbH. All Rights Reserved.
"""Collection of post-processing functions for the experimental NMR helpers."""

import numpy as np


[docs] def windowing( signal: np.ndarray, time_axis: np.ndarray, lambda_hz: float | None = None, t_c_fraction: float = 0.9, ) -> np.ndarray: """Apply hybrid windowing to signal: exponential damping + cosine taper. Args: signal (np.ndarray): Signal to pad for smoothening. time_axis (np.ndarray): Time values in seconds lambda_hz (float): Exponential damping parameter -> trial and error until correct one is found OR make a MatPlotLib slider. If decay is too small we will see strange oscillations in spectrum, which shouldn't be there. That's a sign that the decay wasn't strong enough and then when the cosine kicks in, the time-signal sharply drops, leading to oscillations in frequency space at the rate of the cosine oscillation. Check that where the cosine kicks in we don't have a strong drop-off in the time domain. Note: Want lambda to be equal to the width of the broadest peak in the spectrum. t_c_fraction (float): Fraction of T_max where cosine taper starts (should be between 0 and 1). Returns: np.ndarray: Windowed signal Raises: ValueError: The value for t_c_fraction needs to be in [0, 1]. """ t_max = time_axis[-1] if t_c_fraction > 1.0: raise ValueError( f"The value for t_c_fraction needs to be in [0, 1], it is currently: {t_c_fraction}" ) t_c = t_c_fraction * t_max if lambda_hz is None: # then use e^(-lambda t_max) = 0.01 lambda_hz = -np.log(0.01) / t_max # Exponential part (mild global damping) exponential_part = np.exp(-np.pi * lambda_hz * time_axis) # Cosine taper for t > t_c taper = np.ones_like(time_axis) late_mask = time_axis > t_c if np.any(late_mask): denominator = t_max - t_c if denominator <= 0: taper[late_mask] = 0.0 else: taper[late_mask] = 0.5 * ( 1 + np.cos(np.pi * (time_axis[late_mask] - t_c) / denominator) ) # Combined window window = exponential_part * taper return signal * window
[docs] def zero_padding( signal: np.ndarray, time_axis: np.ndarray, extension_factor: int | None = None ) -> tuple[np.ndarray, np.ndarray]: """Apply padding (zeroes) to the signal. This ensures the later correlation functions look smoother. Note that this function also returns the modified time axis, as the shape of the signal and time axis needs to match. Args: signal (np.ndarray): Signal to pad for smoothening. time_axis (np.ndarray): Time values in seconds extension_factor (int | None): by how many times to extend the time and signal arrays. Returns: tuple(np.ndarray, np.ndarray): padded signal and time axis. """ n_points = len(time_axis) if extension_factor is None: n_extend = n_points * 10 else: n_extend = n_points * extension_factor dt = time_axis[1] - time_axis[0] time_axis_extended = np.arange(n_points + n_extend) * dt signal_extended = np.append(signal, np.zeros(n_extend)) return signal_extended, time_axis_extended