Skip to content

nstdb

Classes

NstdbNoise

NstdbNoise(target_rate: int)

Noise stress test database (NSTDB) noise generator.

Parameters:

  • target_rate

    (int) –

    Target rate in Hz

Source code in heartkit/datasets/nstdb.py
def __init__(
    self,
    target_rate: int,
):
    """Noise stress test database (NSTDB) noise generator.

    Args:
        target_rate (int): Target rate in Hz
    """

    self.target_rate = target_rate
    self._noises: dict[str, npt.NDArray] | None = None

Attributes

sampling_rate property
sampling_rate: int

Sampling rate in Hz

Functions

set_target_rate
set_target_rate(target_rate: int)

Set target rate.

Source code in heartkit/datasets/nstdb.py
def set_target_rate(self, target_rate: int):
    """Set target rate."""
    if target_rate == self.target_rate:
        return
    self._noises = None
    self.target_rate = target_rate
get_noise
get_noise(noise_type: str) -> npt.NDArray

Get noise data of type.

Parameters:

  • noise_type
    (str) –

    Noise type

Source code in heartkit/datasets/nstdb.py
def get_noise(self, noise_type: str) -> npt.NDArray:
    """Get noise data of type.

    Args:
        noise_type (str): Noise type
    """
    if self._noises is None:
        self._load_noise_data()
    # END IF
    return self._noises[noise_type]
apply_noise
apply_noise(data: NDArray, noise_level: float, axis: int = 0) -> npt.NDArray

Add noise to ECG signal.

Parameters:

  • data
    (NDArray) –

    ECG signal

  • noise_level
    (float) –

    Noise level

  • axis
    (int, default: 0 ) –

    Axis to apply noise. Defaults to 0.

Returns:

  • NDArray

    npt.NDArray: Noisy ECG signal

Source code in heartkit/datasets/nstdb.py
def apply_noise(self, data: npt.NDArray, noise_level: float, axis: int = 0) -> npt.NDArray:
    """Add noise to ECG signal.

    Args:
        data (npt.NDArray): ECG signal
        noise_level (float): Noise level
        axis (int, optional): Axis to apply noise. Defaults to 0.

    Returns:
        npt.NDArray: Noisy ECG signal
    """

    if self._noises is None:
        self._load_noise_data()
    # END IF

    frame_size = data.shape[axis]
    bw = self._noises["bw"]
    ma = self._noises["ma"]
    em = self._noises["em"]

    # NOTE: If signal is too long, apply patches across

    bw_lead = np.random.randint(0, bw.shape[1])
    bw_start = np.random.randint(bw.shape[0] - frame_size)
    bw_end = bw_start + frame_size

    ma_lead = np.random.randint(0, ma.shape[1])
    ma_start = np.random.randint(ma.shape[0] - frame_size)
    ma_end = ma_start + frame_size

    em_lead = np.random.randint(0, em.shape[1])
    em_start = np.random.randint(em.shape[0] - frame_size)
    em_end = em_start + frame_size

    bw_amp = np.abs(np.random.normal(0, noise_level))
    ma_amp = np.abs(np.random.normal(0, noise_level))
    em_amp = np.abs(np.random.normal(0, noise_level))

    data += bw_amp * bw[bw_start:bw_end, bw_lead]
    data += ma_amp * ma[ma_start:ma_end, ma_lead]
    data += em_amp * em[em_start:em_end, em_lead]

    return data
close
close()

Close noise generator.

Source code in heartkit/datasets/nstdb.py
def close(self):
    """Close noise generator."""
    self._noises = None