mirror of
https://github.com/vale981/master-thesis
synced 2025-03-06 10:31:37 -05:00
44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
|
"""Random and unversioned utility functions."""
|
||
|
import numpy as np
|
||
|
from numpy.typing import NDArray
|
||
|
|
||
|
|
||
|
def strobe_times(time: NDArray[np.float64], frequency: float, tolerance: float = 1e-4):
|
||
|
r"""
|
||
|
Given a time array ``time`` and an angular frequency ``frequency`` (ω) the
|
||
|
time points (and their indices) coinciding with :math:`2π / ω \cdot n` within the
|
||
|
``tolerance`` are being returned.
|
||
|
"""
|
||
|
|
||
|
stroboscope_interval = 2 * np.pi / frequency
|
||
|
sorted_times = np.sort(time)
|
||
|
tolerance = min(tolerance, (sorted_times[1:] - sorted_times[:-1]).min() / 2)
|
||
|
strobe_indices = np.where((time % stroboscope_interval) <= tolerance)[0]
|
||
|
|
||
|
if len(strobe_indices) == 0:
|
||
|
raise ValueError("Can't match the strobe interval to the times.")
|
||
|
|
||
|
strobe_times = time[strobe_indices]
|
||
|
|
||
|
return strobe_times, strobe_indices
|
||
|
|
||
|
|
||
|
def linspace_with_strobe(
|
||
|
begin: float, end: float, N: int, strobe_frequency: float
|
||
|
) -> NDArray[np.float64]:
|
||
|
"""
|
||
|
Like ``linspace`` but so that the time points defined by the
|
||
|
stroboscope angular frequency ``strobe_frequency`` are included.
|
||
|
"""
|
||
|
|
||
|
return np.unique(
|
||
|
np.sort(
|
||
|
np.concatenate(
|
||
|
[
|
||
|
np.linspace(begin, end, N),
|
||
|
np.arange(begin, end, 2 * np.pi / strobe_frequency),
|
||
|
]
|
||
|
)
|
||
|
)
|
||
|
)
|