torch_ecg.utils.ensure_siglen#
- torch_ecg.utils.ensure_siglen(values: ndarray, siglen: int, fmt: str = 'lead_first', tolerance: float | None = None) ndarray [source]#
Ensure the (ECG) signal to be of specified length.
Strategy:
If values has length greater than siglen, the central siglen samples will be adopted; otherwise, zero padding will be added to both sides.
If tolerance is given, then if the length of values is longer than siglen by more than tolerance in percentage, the values will be sliced to have multiple of siglen samples, each with
(1 - tolerance) * siglen
overlap.
- Parameters:
values (numpy.ndarray) – Values of the n_leads-lead (ECG) signal.
siglen (int) – Length of the signal supposed to have.
fmt (str, default "lead_first") – Format of the input and output values, can be one of “lead_first” (alias “channel_first”), “lead_last” (alias “channel_last”), case insensitive.
tolerance (float, optional) – Tolerance of the length of values to be longer than siglen in percentage.
- Returns:
ECG signal in the format of fmt and of fixed length siglen, of
ndim=3
if tolerence is given, otherwisendim=2
.- Return type:
Examples
>>> values = np.random.randn(12, 4629) >>> new_values = ensure_siglen(values, 5000, fmt="lead_first") >>> new_values.shape (12, 5000) >>> new_values = ensure_siglen(values, 4000, tolerance=0.1, fmt="lead_first") >>> new_values.shape (2, 12, 4000) >>> new_values = ensure_siglen(values, 4000, tolerance=0.2, fmt="lead_first") >>> new_values.shape (1, 12, 4000)