Normalize

class torch_ecg._preprocessors.Normalize(method: str = 'z-score', mean: Real | ndarray = 0.0, std: Real | ndarray = 1.0, per_channel: bool = False, **kwargs: Any)[source]

Bases: PreProcessor

Normalization of the signals.

Perform z-score normalization on sig, to make it has fixed mean and standard deviation; or perform min-max normalization on sig, or normalize sig using mean and std via \((sig - mean) / std\). More precisely,

\[\begin{split}\begin{align*} \text{Min-Max normalization:} & \quad \frac{sig - \min(sig)}{\max(sig) - \min(sig)} \\ \text{Naive normalization:} & \quad \frac{sig - m}{s} \\ \text{Z-score normalization:} & \quad \left(\frac{sig - mean(sig)}{std(sig)}\right) \cdot s + m \end{align*}\end{split}\]
Parameters:
  • method ({"naive", "min-max", "z-score"}, optional) – Normalization method, case insensitive, default “z-score”.

  • mean (numbers.Real or numpy.ndarray, default 0.0) – Mean value of the normalized signal, or mean values for each lead of the normalized signal. Useless if method is “min-max”.

  • std (numbers.Real or numpy.ndarray, default 1.0) – Standard deviation of the normalized signal, or standard deviations for each lead of the normalized signal. Useless if method is “min-max”.

  • per_channel (bool, default False) – If True, normalization will be done per channel.

Examples

from torch_ecg.cfg import DEFAULTS
sig = DEFAULTS.RNG.randn(1000)
pp = Normalize(method="z-score", mean=0.0, std=1.0)
sig, _ = pp(sig, 500)
apply(sig: ndarray, fs: Real) Tuple[ndarray, int][source]

Apply the preprocessor to sig.

Parameters:
  • sig (numpy.ndarray) –

    The ECG signal, can be
    • 1d array, which is a single-lead ECG;

    • 2d array, which is a multi-lead ECG of “lead_first” format;

    • 3d array, which is a tensor of several ECGs, of shape (batch, lead, siglen).

  • fs (numbers.Real) – Sampling frequency of the ECG signal. NOT used currently.

Returns:

  • normalized_sig (numpy.ndarray) – The normalized ECG signal.

  • fs (int) – The sampling frequency of the normalized ECG signal.

extra_repr_keys() List[str][source]

Extra keys for __repr__() and __str__().