Skip to content
Snippets Groups Projects
Commit 256c9c99 authored by Mads M. Pedersen's avatar Mads M. Pedersen
Browse files

added differentiation filter

parent 3f909e23
No related branches found
No related tags found
No related merge requests found
from ._despike import *
from ._differentiation import *
\ No newline at end of file
...@@ -4,8 +4,7 @@ Created on 13/07/2016 ...@@ -4,8 +4,7 @@ Created on 13/07/2016
@author: MMPE @author: MMPE
''' '''
import numpy as np import numpy as np
from wetb.signal.filters.first_order import low_pass from wetb.signal.filters import replacer, frq_filters
from wetb.signal.filters import replacer
replace_by_nan = replacer.replace_by_nan replace_by_nan = replacer.replace_by_nan
...@@ -42,15 +41,15 @@ def univeral_thresshold_finder(data, variation='mad', plt=None): ...@@ -42,15 +41,15 @@ def univeral_thresshold_finder(data, variation='mad', plt=None):
thresshold = np.sqrt(2 * np.log(data.shape[0])) * variation # Universal thresshold (expected maximum of n random variables) thresshold = np.sqrt(2 * np.log(data.shape[0])) * variation # Universal thresshold (expected maximum of n random variables)
return thresshold_finder(data, thresshold, plt) return thresshold_finder(data, thresshold, plt)
def despike(data, dt, spike_finder=univeral_thresshold_finder, spike_replacer=replace_by_nan, plt=None): def despike(data, spike_length, spike_finder=univeral_thresshold_finder, spike_replacer=replace_by_nan, plt=None):
"""Despike data """Despike data
Parameters Parameters
--------- ---------
data : array_like data : array_like
data data
dt : int or float spike_length : int
time step Typical spike duration [samples]
spike_finder : function spike_finder : function
Function returning indexes of the spikes Function returning indexes of the spikes
spike_replacer : function spike_replacer : function
...@@ -63,8 +62,10 @@ def despike(data, dt, spike_finder=univeral_thresshold_finder, spike_replacer=re ...@@ -63,8 +62,10 @@ def despike(data, dt, spike_finder=univeral_thresshold_finder, spike_replacer=re
if plt: if plt:
plt.plot(data, label='Input') plt.plot(data, label='Input')
data = np.array(data).copy() data = np.array(data).copy()
lp_data = low_pass(data, dt, 1) lp_data = low_pass(data, spike_length, 1)
hp_data = data - lp_data hp_data = data - lp_data
hp_data = frq_filters.high_pass(data, spike_length*4, 1, order=2)
#spike_length, sample_frq/2, 1, order=1)
spike_mask = spike_finder(hp_data, plt=plt) spike_mask = spike_finder(hp_data, plt=plt)
despike_data = spike_replacer(data, spike_mask) despike_data = spike_replacer(data, spike_mask)
if plt: if plt:
......
...@@ -103,14 +103,9 @@ def bin_fit(x,y, bins=10, kind='linear', bin_func=np.nanmean, bin_min_count=3, l ...@@ -103,14 +103,9 @@ def bin_fit(x,y, bins=10, kind='linear', bin_func=np.nanmean, bin_min_count=3, l
else: else:
raise NotImplementedError("Argument for handling upper observations, %s, not implemented"%upper) raise NotImplementedError("Argument for handling upper observations, %s, not implemented"%upper)
#Create mean function
def fit(x):
x = x[:].copy().astype(np.float)
x[x<bin_x_fit[0]] = np.nan
x[x>bin_x_fit[-1]] = np.nan
return interp1d(bin_x_fit, bin_y_fit, kind)(x[:])
return bin_x_fit, fit return bin_x_fit, _interpolate_fit(bin_x_fit, bin_y_fit, kind)
def perpendicular_bin_fit(x, y, bins = 30, fit_func=None, bin_min_count=3, plt=None): def perpendicular_bin_fit(x, y, bins = 30, fit_func=None, bin_min_count=3, plt=None):
"""Fit a curve to the values, (x,y) using bins that are perpendicular to an initial fit """Fit a curve to the values, (x,y) using bins that are perpendicular to an initial fit
...@@ -190,6 +185,15 @@ def perpendicular_bin_fit(x, y, bins = 30, fit_func=None, bin_min_count=3, plt=N ...@@ -190,6 +185,15 @@ def perpendicular_bin_fit(x, y, bins = 30, fit_func=None, bin_min_count=3, plt=N
plt.plot(pbfx, pbfy, 'gray', label="perpendicular fit") plt.plot(pbfx, pbfy, 'gray', label="perpendicular fit")
plt.legend() plt.legend()
#PlotData(None, bfx,bfy) #PlotData(None, bfx,bfy)
bin_x_fit, bin_y_fit = np.array(pc).T
return np.array(pc).T return bin_x_fit, _interpolate_fit(bin_x_fit, bin_y_fit, kind="linear")
#Create mean function
def _interpolate_fit(bin_x_fit, bin_y_fit, kind='linear'):
def fit(x):
x = x[:].copy().astype(np.float)
x[x<bin_x_fit[0]] = np.nan
x[x>bin_x_fit[-1]] = np.nan
return interp1d(bin_x_fit, bin_y_fit, kind)(x[:])
return fit
No preview for this file type
...@@ -88,11 +88,11 @@ class TestFit(unittest.TestCase): ...@@ -88,11 +88,11 @@ class TestFit(unittest.TestCase):
x,y = ds('Wsp_metmast'), ds('Power') x,y = ds('Wsp_metmast'), ds('Power')
if 0: if 0:
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
fx, fy = perpendicular_bin_fit(x,y,30,plt=plt) fx, fit = perpendicular_bin_fit(x,y,30,plt=plt)
plt.show() plt.show()
else: else:
fx, fy = perpendicular_bin_fit(x,y,30) fx, fit = perpendicular_bin_fit(x,y,30)
self.assertEqual(len(fx), 30) self.assertEqual(len(fx), 30)
......
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