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

type in differentiation

parent 6d85bd66
No related branches found
No related tags found
No related merge requests found
...@@ -3,15 +3,19 @@ Created on 29. mar. 2017 ...@@ -3,15 +3,19 @@ Created on 29. mar. 2017
@author: mmpe @author: mmpe
''' '''
from wetb.signal.filters.frq_filters import low_pass
import numpy as np import numpy as np
def differentiation(x,sample_frq=None, cutoff_frq=None): def differentiation(x, type='center', sample_frq=None, cutoff_frq=None):
"""Differentiate the signal """Differentiate the signal
Parameters Parameters
---------- ----------
x : array_like x : array_like
The input signal The input signal
type : {'right','center','left'}
right: change from current to next observation\n
center: average change from previous to next observation\n
left: change from previous to current observation\n
sample_frq : int, float or None, optional sample_frq : int, float or None, optional
sample frequency of signal (only required if low pass filer is applied) sample frequency of signal (only required if low pass filer is applied)
cutoff_frq : int, float or None, optional cutoff_frq : int, float or None, optional
...@@ -30,8 +34,14 @@ def differentiation(x,sample_frq=None, cutoff_frq=None): ...@@ -30,8 +34,14 @@ def differentiation(x,sample_frq=None, cutoff_frq=None):
if cutoff_frq is not None: if cutoff_frq is not None:
assert sample_frq is not None, "Argument sample_frq must be set to apply low pass filter" assert sample_frq is not None, "Argument sample_frq must be set to apply low pass filter"
from wetb.signal.filters.frq_filters import low_pass
x = low_pass(x, sample_frq, cutoff_frq) x = low_pass(x, sample_frq, cutoff_frq)
else: else:
x = np.array(x) x = np.array(x)
dy = np.r_[x[1]-x[0], (x[2:]-x[:-2])/2, x[-1]-x[-2]] if type=="left":
dy = np.r_[np.nan, x[1:]-x[:-1]]
if type=="center":
dy = np.r_[x[1]-x[0], (x[2:]-x[:-2])/2, x[-1]-x[-2]]
if type=="right":
dy = np.r_[x[1:]-x[:-1], np.nan]
return dy return dy
\ No newline at end of file
...@@ -12,6 +12,8 @@ class Test(unittest.TestCase): ...@@ -12,6 +12,8 @@ class Test(unittest.TestCase):
def testDifferentiation(self): def testDifferentiation(self):
np.testing.assert_array_equal(differentiation([1,2,1,0,1,1]), [1,0,-1,0,.5,0]) np.testing.assert_array_equal(differentiation([1,2,1,0,1,1]), [1,0,-1,0,.5,0])
np.testing.assert_array_equal(differentiation([1,2,1,0,1,1], 'left'), [np.nan, 1,-1,-1,1,0])
np.testing.assert_array_equal(differentiation([1,2,1,0,1,1], 'right'), [1,-1,-1,1,0, np.nan])
......
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