Skip to content

pulsedel

biosigpy.ppg.pulsedel.pulsedel

pulsedel(ppg, sampling_frequency, t_d, dppg=None, *, window_a=0.25, window_b=0.15, points='ABMO')

Return apex, basal, mid-amplitude and onset times in seconds.

Parameters:

Name Type Description Default
ppg array_like

PPG samples, starting at time zero. NaNs delimit valid spans.

required
sampling_frequency float

Sampling frequency in Hz.

required
t_d array_like

Detection times in seconds, in input order, including repeats and NaNs.

required
dppg array_like

Derivative aligned sample by sample with the PPG. The caller chooses the differentiator and compensates any delay.

None
window_a float

Apex search duration after the detection and basal search duration before it, in seconds.

0.25
window_b float

Apex search duration after the detection and basal search duration before it, in seconds.

0.25
points str

Distinct letters A, B, M and O, case-insensitive. Return the requested points in the given order. One letter returns an array. Default "ABMO".

'ABMO'

Returns:

Type Description
t_a, t_b, t_m, t_o : ndarray

By default, four one-dimensional arrays, one entry per detection. The midpoint uses half the amplitude between the sampled basal point and apex. Omitting dppg silently returns an all-NaN t_o of the same length. An empty t_d gives an empty array for each requested point.

Source code in src/biosigpy/ppg/pulsedel.py
 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
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def pulsedel(ppg, sampling_frequency, t_d, dppg=None, *, window_a=0.25, window_b=0.15,
             points="ABMO"):
    """Return apex, basal, mid-amplitude and onset times in seconds.

    Parameters
    ----------
    ppg : array_like
        PPG samples, starting at time zero. NaNs delimit valid spans.
    sampling_frequency : float
        Sampling frequency in Hz.
    t_d : array_like
        Detection times in seconds, in input order, including repeats and NaNs.
    dppg : array_like, optional
        Derivative aligned sample by sample with the PPG. The caller chooses
        the differentiator and compensates any delay.
    window_a, window_b : float, optional
        Apex search duration after the detection and basal search duration
        before it, in seconds.

    points : str, optional
        Distinct letters A, B, M and O, case-insensitive. Return the requested
        points in the given order. One letter returns an array. Default "ABMO".

    Returns
    -------
    t_a, t_b, t_m, t_o : ndarray
        By default, four one-dimensional arrays, one entry per detection. The midpoint
        uses half the amplitude between the sampled basal point and apex.
        Omitting dppg silently returns an all-NaN t_o of the same length.
        An empty t_d gives an empty array for each requested point.
    """
    ppg = as_real_vector(np.atleast_1d(ppg), name="ppg")
    fs = as_positive_real_scalar(sampling_frequency, name="sampling_frequency")
    t_d = as_real_vector(np.atleast_1d(t_d), name="t_d")
    window_a = as_positive_real_scalar(window_a, name="window_a")
    window_b = as_positive_real_scalar(window_b, name="window_b")
    if not ppg.size or np.isinf(ppg).any():
        raise ValueError("ppg must be nonempty and contain no infinities")
    if np.any((t_d < 0) | (t_d > (ppg.size - 1) / fs)):
        raise ValueError("detection times must lie within the signal")
    if dppg is not None:
        dppg = as_real_vector(np.atleast_1d(dppg), name="dppg")
        if dppg.size != ppg.size or np.isinf(dppg).any():
            raise ValueError("dppg must match the PPG length and contain no infinities")

    if not isinstance(points, str):
        raise TypeError("points must be a string")
    points = points.upper()
    if not points or set(points) - set("ABMO") or len(set(points)) != len(points):
        raise ValueError("points must contain distinct letters from ABMO")
    outputs = {key: np.full(t_d.size, np.nan) for key in points}
    need_apex = "A" in points or "M" in points or ("O" in points and dppg is not None)
    need_basal = "B" in points or "M" in points
    forward = int(np.floor(min(window_a * fs, ppg.size) + 0.5))
    backward = int(np.floor(min(window_b * fs, ppg.size) + 0.5))
    for k in np.flatnonzero(~np.isnan(t_d)):
        detection = int(np.floor(t_d[k] * fs + 0.5))
        if np.isnan(ppg[detection]):
            continue

        # Anchor both search windows to this detection's finite PPG span.
        basal = None
        if need_basal:
            first = max(0, detection - backward)
            gaps = np.flatnonzero(np.isnan(ppg[first:detection]))
            if gaps.size:
                first += gaps[-1] + 1
            _, offset = localmax(-ppg[first:detection + 1])
            if np.isfinite(offset):
                basal = first + int(offset)
                if "B" in outputs:
                    outputs["B"][k] = _refine_time(ppg, basal, fs)
        if not need_apex:
            continue
        last = min(ppg.size - 1, detection + forward)
        gaps = np.flatnonzero(np.isnan(ppg[detection:last + 1]))
        if gaps.size:
            last = detection + gaps[0] - 1
        _, apex_offset = localmax(ppg[detection:last + 1])
        if np.isnan(apex_offset):
            continue
        apex = detection + int(apex_offset)
        if "A" in outputs:
            outputs["A"][k] = _refine_time(ppg, apex, fs)

        if "M" in outputs and basal is not None:
            target = (ppg[basal] + ppg[apex]) / 2
            distance = np.abs(ppg[basal:apex + 1] - target)
            midpoint = np.argmin(distance)
            outputs["M"][k] = basal / fs + _refine_time(-distance, midpoint, fs)
        if "O" in outputs and dppg is not None:
            outputs["O"][k] = _find_onset(ppg, dppg, apex, fs)

    if len(points) == 1:
        return outputs[points]
    return tuple(outputs.values())

Run the PPG delineation example.