Skip to content
Snippets Groups Projects
Commit 6ae582ff authored by Peter Lünenschloß's avatar Peter Lünenschloß
Browse files

filterAdde to lib.ts_operators

parent df2bb0a4
No related branches found
No related tags found
1 merge request!256Filter funcs
......@@ -12,6 +12,7 @@ import numpy as np
import numba as nb
from sklearn.neighbors import NearestNeighbors
from scipy.stats import iqr, median_abs_deviation
from scipy.signal import filtfilt, butter
import numpy.polynomial.polynomial as poly
logger = logging.getLogger("SaQC")
......@@ -328,6 +329,35 @@ def shift2Freq(data: Union[pd.Series, pd.DataFrame], method: str, freq: str, fil
return data.reindex(target_ind, method=direction, tolerance=tolerance, fill_value=fill_value)
def butterFilter(x, cut_off_freq, nyq_freq=0.5, filter_order=2,
fill_method='linear', filter_type='low'):
"""
Applies butter filter.
X is expected to be regularly sampled.
Parameters
----------
x: pd.Series
input timeseries
cut_off_freq: float
The cutoff frequencie. Relates to multiples of the sampling freq
nyq_freq: float
The niquist frequencie. relates to multiples if the sampling rate
fill_method: str
Any method keword, accepted by pandas.Series.interpolate.
Returns
-------
"""
na_mask = x.isna()
x = x.interpolate(fill_method).interpolate('ffill').interpolate('bfill')
b, a = butter(N=filter_order, Wn=cut_off_freq / nyq_freq, btype=filter_type)
y = pd.Series(filtfilt(b, a, x), x.index, name=x.name)
y[na_mask] = np.nan
return y
@nb.njit
def _coeffMat(x, deg):
# helper function to construct numba-compatible polynomial fit function
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment