We employ a strategy to remove low amplitude noise using a rolling window strategy, which effectively applies a mean filter if and only if the absolute deviation from the median within the window exceeds a certain threshold.
@nb.jit(nopython=True)
def low_amplitude_filter(
value: np.ndarray,
window_size: int = 10,
stride: int = 1,
threshold: float = 1e-6,
) -> np.ndarray:
"""
Remove small amplitude fluctuations from a time series by
replacing the values in a window with the mean of the window if
the values in the window are within a certain threshold of the
median value.
:param value: The array to smooth.
:param window_size: The size of the window to smooth.
:param stride: The stride between the windows.
:param threshold: The threshold for the difference between the
values in the window.
"""
new_arr = value.copy()
for i in range((value.size - window_size) // stride):
s = slice(i * stride, i * stride + window_size)
arr_s = new_arr[s]
condition = np.all(np.abs(arr_s - np.median(arr_s)) < threshold)
# condition = np.max(arr_s) - np.min(arr_s) < threshold
if condition:
new_arr[s] = np.mean(arr_s)
else:
new_arr[s.start : s.start + stride] = arr_s[:stride]
return new_arr