Skip to content

localmax

biosigpy.tools.localmax.localmax

localmax(signal: ArrayLike, *, axis: int | None = None) -> tuple[float, float] | tuple[np.ndarray, np.ndarray]

Return the highest local peak value and position per signal.

Parameters:

Name Type Description Default
signal array_like

Nonempty real vector or matrix. NaNs are ignored during detection.

required
axis int

Zero-based search axis. By default use the first non-singleton axis, or axis 0 when all dimensions are singleton. For matrices, axis 0 searches columns and axis 1 searches rows.

None

Returns:

Type Description
peak_values, peak_positions : float or ndarray

Peak values and zero-based positions in the original input along the search axis. Both are NaN when no peak exists. A vector yields two scalars; a matrix yields vectors along the unsearched axis.

Notes

A peak must be bounded by strictly lower neighbors. A plateau uses its middle retained sample, choosing the earlier middle sample for an even length. Ties between highest peaks select the first one.

Source code in src/biosigpy/tools/localmax.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def localmax(
    signal: ArrayLike, *, axis: int | None = None
) -> tuple[float, float] | tuple[np.ndarray, np.ndarray]:
    """Return the highest local peak value and position per signal.

    Parameters
    ----------
    signal : array_like
        Nonempty real vector or matrix. NaNs are ignored during detection.
    axis : int, optional
        Zero-based search axis. By default use the first non-singleton
        axis, or axis 0 when all dimensions are singleton. For matrices,
        axis 0 searches columns and axis 1 searches rows.

    Returns
    -------
    peak_values, peak_positions : float or ndarray
        Peak values and zero-based positions in the original input along
        the search axis. Both are NaN when no peak exists. A vector yields
        two scalars; a matrix yields vectors along the unsearched axis.

    Notes
    -----
    A peak must be bounded by strictly lower neighbors. A plateau uses its
    middle retained sample, choosing the earlier middle sample for an even
    length. Ties between highest peaks select the first one.
    """
    values = np.asarray(signal)
    if values.ndim not in (1, 2):
        raise ValueError("signal must be a vector or two-dimensional matrix")
    if values.dtype.kind not in "iuf":
        raise TypeError("signal must be real numeric data")
    if values.size == 0:
        raise ValueError("signal must not be empty")
    if axis is None:
        axis = next((i for i, size in enumerate(values.shape) if size > 1), 0)
    else:
        axis = as_integer_scalar(axis, name="axis")
        if not 0 <= axis < values.ndim:
            raise ValueError("axis is outside the signal dimensions")

    if values.ndim == 1:
        return _highest_peak(values)
    peaks = [_highest_peak(vector) for vector in np.moveaxis(values, axis, -1)]
    heights, positions = zip(*peaks)
    return np.asarray(heights), np.asarray(positions)