Smoothing 1D data
## MOVING AVERAGE SMOOTHING
def smooth_moving_average(df,svar,windows_size):
import numpy as np
# build windows to be moving
box = np.ones(windows_size)/windows_size
# calculate and store y same df
df["%s_smooth%s"%(svar,windows_size)] = np.convolve(df[svar], box, mode='same')
# filter the firsts lines
return df[~df.index.isin(list(df.index[:windows_size]))]
WS= smooth_movingavg(WS,"ws1",3)
## KALMAN FILTER SMOOTHING
def smooth_kalman(df,svar,lag_size):
from filterpy.kalman import FixedLagSmoother
import numpy as np
# build object
fls = FixedLagSmoother(dim_x=2, dim_z=1)
# settings
fls.x = np.array([[0.],
[.5]])
fls.F = np.array([[1.,1.],
[0.,1.]])
fls.H = np.array([[1.,0.]])
fls.P *= 200
fls.R *= 5.
fls.Q *= 0.001
# smoothing
xhatsmooth, xhat = fls.smooth_batch(df[svar].values, N=lag_size)
df["%s_kalman_batch%s"%(svar,lag_size)] = xhatsmooth[:,0]
df["%s_kalman_standar%s"%(svar,lag_size)] = xhat[:,0]
# return
return df
from scipy.ndimage import gaussian_filter1d
def smoothing_1d_gaussian_filter(values:np.array, sigma:float, truncate:float = 4.0, mode:str = 'nearest')->np.array:
"""Smoothing a 1d signal with gaussian filter
Args:
values (np.array): Values to be smoothed.
sigma (float): Std of the gaussian kernel.
truncate (float, optional): Control the kernel size as a multiple of sigma. Defaults to 4.0.
mode (str, optional): How to handle edges. Defaults to 'nearest'.
Returns:
np.array: Smoothed values.
"""
# validate arguments
assert isinstance(values, np.ndarray)
assert isinstance(sigma, float) and 1. <= sigma <= 10.
assert isinstance(truncate, float) and 2. <= truncate <= 10
assert isinstance(mode, str) and mode in ['nearest', 'reflect', 'constant', 'wrap', 'mirror']
# validate data size
num_minimum = 2 * truncate * sigma + 1
assert len(values) >= num_minimum, f"It is required a minimum size of signal = {num_minimum}. Given {len(values)}."
# smoothing with gaussian filter and return
return gaussian_filter1d(values, sigma = sigma, order = 0, mode = 'nearest', truncate = truncate)