From 1ecaf275e9c412ad2b6dd5064f10da4c6aa9cafd Mon Sep 17 00:00:00 2001 From: Rafael Silva Date: Thu, 3 Jul 2025 13:39:24 +0200 Subject: [PATCH 1/3] Add resample_signal to signals.tools module --- biosppy/signals/tools.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/biosppy/signals/tools.py b/biosppy/signals/tools.py index 9bb699e9..2b305917 100644 --- a/biosppy/signals/tools.py +++ b/biosppy/signals/tools.py @@ -2266,3 +2266,31 @@ def detrend_smoothness_priors(signal, smoothing_factor=10): z_detrended = np.array(signal) - z_trend return utils.ReturnTuple((z_detrended.T, z_trend.T), ('detrended', 'trend')) + + +def resample_signal(signal, sampling_rate, resampling_rate): + """ + Resample a signal to a new sampling frequency. It assumes that the input signal is uniformly sampled. + + Parameters + ---------- + signal : array-like + The signal to resample. + sampling_rate : float + The original sampling frequency of the signal. + resampling_rate : float + The new sampling frequency. + + Returns + ------- + resampled_signal : array-like + The resampled signal. + """ + + # check inputs + if signal is None: + raise TypeError("Please specify a signal to resample.") + + resampled_signal = ss.resample(signal, int(len(signal) * resampling_rate / sampling_rate)) + + return utils.ReturnTuple((resampled_signal,), ('signal',)) From 94a93b6490ba6ff3c47c696860f465befc15976c Mon Sep 17 00:00:00 2001 From: Rafael Silva Date: Thu, 31 Jul 2025 10:54:17 +0200 Subject: [PATCH 2/3] Add decimation to resample_signal It applies a low-pass filter before downsampling a signal to avoid aliasing. Filter type and order can be passed to the function. --- biosppy/signals/tools.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/biosppy/signals/tools.py b/biosppy/signals/tools.py index 2b305917..95b93655 100644 --- a/biosppy/signals/tools.py +++ b/biosppy/signals/tools.py @@ -2268,7 +2268,7 @@ def detrend_smoothness_priors(signal, smoothing_factor=10): return utils.ReturnTuple((z_detrended.T, z_trend.T), ('detrended', 'trend')) -def resample_signal(signal, sampling_rate, resampling_rate): +def resample_signal(signal, sampling_rate, resampling_rate, lowpass_filter=True, **kwargs): """ Resample a signal to a new sampling frequency. It assumes that the input signal is uniformly sampled. @@ -2280,6 +2280,12 @@ def resample_signal(signal, sampling_rate, resampling_rate): The original sampling frequency of the signal. resampling_rate : float The new sampling frequency. + lowpass_filter : bool, optional + If True, apply a low-pass filter before downsampling to avoid aliasing (decimation). Default is True. + **kwargs : dict, optional + Additional keyword arguments to pass to the `filter_signal` function if it is applied. + - ftype : type of filter to use (default is 'FIR'). + - order : order of the filter (default is 4). Returns ------- @@ -2291,6 +2297,18 @@ def resample_signal(signal, sampling_rate, resampling_rate): if signal is None: raise TypeError("Please specify a signal to resample.") + # filter if necessary + if lowpass_filter and resampling_rate < sampling_rate: + cutoff_freq = resampling_rate / 2.0 + signal, _, _ = filter_signal( + signal=signal, + ftype=kwargs.get('ftype', 'FIR'), + band="lowpass", + order=kwargs.get('order', 4), + frequency=cutoff_freq, + sampling_rate=sampling_rate, + ) + resampled_signal = ss.resample(signal, int(len(signal) * resampling_rate / sampling_rate)) return utils.ReturnTuple((resampled_signal,), ('signal',)) From c56b2bbe508d3e500ef30168dd8ab5ffb590092e Mon Sep 17 00:00:00 2001 From: Rafael Silva Date: Fri, 22 Aug 2025 14:11:52 +0200 Subject: [PATCH 3/3] Revert "Add decimation to resample_signal" This reverts commit 94a93b6490ba6ff3c47c696860f465befc15976c. --- biosppy/signals/tools.py | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/biosppy/signals/tools.py b/biosppy/signals/tools.py index 95b93655..2b305917 100644 --- a/biosppy/signals/tools.py +++ b/biosppy/signals/tools.py @@ -2268,7 +2268,7 @@ def detrend_smoothness_priors(signal, smoothing_factor=10): return utils.ReturnTuple((z_detrended.T, z_trend.T), ('detrended', 'trend')) -def resample_signal(signal, sampling_rate, resampling_rate, lowpass_filter=True, **kwargs): +def resample_signal(signal, sampling_rate, resampling_rate): """ Resample a signal to a new sampling frequency. It assumes that the input signal is uniformly sampled. @@ -2280,12 +2280,6 @@ def resample_signal(signal, sampling_rate, resampling_rate, lowpass_filter=True, The original sampling frequency of the signal. resampling_rate : float The new sampling frequency. - lowpass_filter : bool, optional - If True, apply a low-pass filter before downsampling to avoid aliasing (decimation). Default is True. - **kwargs : dict, optional - Additional keyword arguments to pass to the `filter_signal` function if it is applied. - - ftype : type of filter to use (default is 'FIR'). - - order : order of the filter (default is 4). Returns ------- @@ -2297,18 +2291,6 @@ def resample_signal(signal, sampling_rate, resampling_rate, lowpass_filter=True, if signal is None: raise TypeError("Please specify a signal to resample.") - # filter if necessary - if lowpass_filter and resampling_rate < sampling_rate: - cutoff_freq = resampling_rate / 2.0 - signal, _, _ = filter_signal( - signal=signal, - ftype=kwargs.get('ftype', 'FIR'), - band="lowpass", - order=kwargs.get('order', 4), - frequency=cutoff_freq, - sampling_rate=sampling_rate, - ) - resampled_signal = ss.resample(signal, int(len(signal) * resampling_rate / sampling_rate)) return utils.ReturnTuple((resampled_signal,), ('signal',))