Segmentation

Peak detection based segmentation

pyPCG.segment.adv_peak(signal: pcg_signal, percent_th: float = 0.5) tuple[ndarray[tuple[int, ...], dtype[float64]], ndarray[tuple[int, ...], dtype[int64]]][source]

Adaptive peak detection, based on local maxima and following value drop

Parameters:
  • signal (pcg_signal) – input signal to detect peaks (usually this is the envelope)

  • percent_th (float, optional) – percent drop in value to be considered a real peak. Defaults to 0.5.

Returns:

detected peak values and their locations

Return type:

tuple[np.ndarray,np.ndarray]

pyPCG.segment.peak_sort_diff(peak_locs: ndarray[tuple[int, ...], dtype[int64]]) tuple[ndarray[tuple[int, ...], dtype[int64]], ndarray[tuple[int, ...], dtype[int64]]][source]

Sort detected peaks based on time differences.

A short time difference corresponds with systole -> S1-S2, a long time difference corresponds with diastole -> S2-S1

Parameters:

peak_locs (np.ndarray) – Detected peak locations in samples

Raises:

ValueError – Less than two peaks detected

Returns:

S1 and S2 locations

Return type:

tuple[np.ndarray,np.ndarray]

pyPCG.segment.segment_peaks(peak_locs: ndarray[tuple[int, ...], dtype[int64]], envelope_signal: pcg_signal, start_drop: float = 0.6, end_drop: float = 0.6) tuple[ndarray[tuple[int, ...], dtype[int64]], ndarray[tuple[int, ...], dtype[int64]]][source]

Create start and end locations from the detected peaks based on the provided envelope.

The relative drop in envelope value is marked as the start and end positions of the given heartsound

Parameters:
  • peak_locs (np.ndarray) – detected peak locations in samples

  • envelope_signal (pcg_signal) – precalculated envelope of the signal (homomorphic recommended)

  • start_drop (float, optional) – precent drop in value for start location. Defaults to 0.6.

  • end_drop (float, optional) – percent drop in value for end location. Defaults to 0.6.

Returns:

heartsound boundary locations

Return type:

tuple[np.ndarray,np.ndarray]

LR-HSMM based segmentation

enum pyPCG.segment.heart_state(value)[source]

Heart states enum

Valid values are as follows:

S1 = <heart_state.S1: 1>

First heartsound

SYS = <heart_state.SYS: 2>

Systole section

S2 = <heart_state.S2: 3>

Second heartsound

DIA = <heart_state.DIA: 4>

Diastole section

unknown = <heart_state.unknown: 0>

Default value, unknown state

pyPCG.segment.load_hsmm(path: str) LR_HSMM[source]

Load pretrained LR-HSMM model.

Note

Training is done internally, it is not recommended to use it right now

Parameters:

path (str) – path to pretrained model json file

Returns:

pretrained model loaded in

Return type:

lr_hsmm.LR_HSMM

pyPCG.segment.segment_hsmm(model: LR_HSMM, signal: pcg_signal, recalc: bool = False) ndarray[tuple[int, ...], dtype[float64]][source]

Use a trained LR-HSMM model to segment a pcg signal

Parameters:
Raises:

ValueError – Samplerate discrepancy

Returns:

heartcycle states

Return type:

np.ndarray

pyPCG.segment.convert_hsmm_states(states: ndarray[tuple[int, ...], dtype[float64]], state_id: int | heart_state) tuple[ndarray[tuple[int, ...], dtype[int64]], ndarray[tuple[int, ...], dtype[int64]]][source]

Convert selected LR-HSMM state to start and end times

Parameters:
  • states (np.ndarray) – output states of LR-HSMM

  • state_id (int, heart_state) – selected state to convert

Raises:

ValueError – Unrecognized heart cycle state

Returns:

state boundaries in samples

Return type:

tuple[np.ndarray,np.ndarray]