From d2fdfc74253b4c74f26bfba01687e5527bfd10d7 Mon Sep 17 00:00:00 2001 From: gkontogiannhs Date: Sun, 26 May 2024 13:23:40 +0300 Subject: [PATCH 1/2] CTS-68 feat: Raise error in case of single channel signal --- caits/properties.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/caits/properties.py b/caits/properties.py index 417adc5..551d5e6 100644 --- a/caits/properties.py +++ b/caits/properties.py @@ -87,32 +87,44 @@ def rolling_rms( return rms_values -def sma_signal(signal) -> np.ndarray: +def sma_signal(signal: np.ndarray) -> np.ndarray: """Calculates the rolling Simple Moving Average (SMA) between the axes of a multi-axis signal in time-domain. - Formula: sum(abs(signal)) + Formula: sum(abs(signal)) Args: signal: The input signal as a numpy.ndarray. Returns: numpy.ndarray: The SMA of the input signal. + + Raises: + ValueError: If the input signal is single-channel (1D). """ - return np.sum(np.abs(signal), axis=1) + if signal.ndim == 1: + raise ValueError("SMA is not defined for single-channel signals.") + else: + return np.sum(np.abs(signal), axis=1) def magnitude_signal(signal: np.ndarray) -> np.ndarray: """Calculates the Magnitude between the axes of a multi-axis signal in time-domain. - Formula: sqrt(sum(signal^2)) + Formula: sqrt(sum(signal^2)) Args: signal: The input signal as a numpy.ndarray. Returns: numpy.ndarray: The magnitude of the input signal. + + Raises: + ValueError: If the input signal is single-channel (1D). """ - return np.sqrt(np.sum(signal**2, axis=1)) + if signal.ndim == 1: + raise ValueError("Magnitude is not defined for single-channel signals.") + else: + return np.sqrt(np.sum(signal**2, axis=1)) def rolling_zcr( From 8a4759d9f161c7310f9d1975c71e7509dafa9706 Mon Sep 17 00:00:00 2001 From: gkontogiannhs Date: Mon, 27 May 2024 12:32:44 +0300 Subject: [PATCH 2/2] CTS-68 bug: Capture also the case where array has shape (n, 1) --- caits/properties.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/caits/properties.py b/caits/properties.py index 551d5e6..0bf0c17 100644 --- a/caits/properties.py +++ b/caits/properties.py @@ -99,10 +99,12 @@ def sma_signal(signal: np.ndarray) -> np.ndarray: numpy.ndarray: The SMA of the input signal. Raises: - ValueError: If the input signal is single-channel (1D). + ValueError: If the input signal has only one channel, + regardless of whether it's 1D or 2D. """ - if signal.ndim == 1: - raise ValueError("SMA is not defined for single-channel signals.") + + if signal.ndim == 1 or (signal.ndim == 2 and signal.shape[1] == 1): + raise ValueError("SMA requires a signal with at least two channels.") else: return np.sum(np.abs(signal), axis=1) @@ -119,10 +121,12 @@ def magnitude_signal(signal: np.ndarray) -> np.ndarray: numpy.ndarray: The magnitude of the input signal. Raises: - ValueError: If the input signal is single-channel (1D). + ValueError: If the input signal has only one channel, + regardless of whether it's 1D or 2D. """ - if signal.ndim == 1: - raise ValueError("Magnitude is not defined for single-channel signals.") + + if signal.ndim == 1 or (signal.ndim == 2 and signal.shape[1] == 1): + raise ValueError("Magnitude requires a signal with at least two channels.") else: return np.sqrt(np.sum(signal**2, axis=1))