torch_ecg.utils.normalize

torch_ecg.utils.normalize(sig: ndarray, method: str, mean: Real | Iterable[Real] = 0.0, std: Real | Iterable[Real] = 1.0, sig_fmt: str = 'channel_first', per_channel: bool = False) ndarray[source]

Normalize a signal.

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:
  • sig (numpy.ndarray) – The signal to be normalized.

  • method ({"naive", "min-max", "z-score"}) – Normalization method, case insensitive.

  • mean (numbers.Real or array_like, 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 array_like, 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”.

  • sig_fmt (str, default "channel_first") – Format of the signal, can be of one of “channel_last” (alias “lead_last”), or “channel_first” (alias “lead_first”), ignored if sig is 1d array (single-lead).

  • per_channel (bool, default False) – If True, normalization will be done per channel. Ignored if sig is 1d array (single-lead).

Returns:

nm_sig – The normalized signal.

Return type:

numpy.ndarray

Note

In cases where normalization is infeasible (std = 0), only the mean value will be shifted.