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

Merge branch 'more_ext' into 'master'

More ext

See merge request !19
parents 820f8e8f 1b699c37
No related branches found
No related tags found
1 merge request!19More ext
Pipeline #
......@@ -26,12 +26,11 @@ from Cython.Distutils import build_ext
def setup_package():
path = 'wetb/fatigue_tools/rainflowcounting/'
module = 'wetb.fatigue_tools.rainflowcounting'
names = ['pair_range', 'peak_trough', 'rainflowcount_astm']
ex_info = [('wetb.fatigue_tools.rainflowcounting', ['pair_range', 'peak_trough', 'rainflowcount_astm']),
('wetb.signal_tools.filters', ['cy_filters'])]
extlist = [Extension('%s.%s' % (module, n),
[os.path.join(path, n)+'.pyx'],
include_dirs=[np.get_include()]) for n in names]
[os.path.join(module.replace(".","/"), n)+'.pyx'],
include_dirs=[np.get_include()]) for module, names in ex_info for n in names]
needs_sphinx = {'build_sphinx', 'upload_docs'}.intersection(sys.argv)
sphinx = ['sphinx'] if needs_sphinx else []
......
import cython
import numpy as np
cimport numpy as np
'''
Created on 29/05/2013
@author: Mads M. Pedersen (mmpe@dtu.dk)
'''
import cython
import numpy as np
cimport numpy as np
@cython.ccall
@cython.locals(alpha=cython.float, i=cython.int)
cpdef cy_low_pass_filter(np.ndarray[double,ndim=1] inp, double delta_t, double tau):
cdef np.ndarray[double,ndim=1] output
output = np.empty_like(inp, dtype=np.float)
output[0] = inp[0]
alpha = delta_t / (tau + delta_t)
for i in range(1, inp.shape[0]):
output[i] = output[i - 1] + alpha * (inp[i] - output[i - 1]) # Same as output[i] = alpha*inp[i]+(1-alpha)*output[i-1]
return output
cpdef cy_dynamic_low_pass_filter(np.ndarray[double,ndim=1] inp, double delta_t, np.ndarray[double,ndim=1] tau, int method):
cdef np.ndarray[double,ndim=1] output, alpha
cdef int i
output = np.empty_like(inp, dtype=np.float)
output[0] = inp[0]
if method == 1:
alpha = delta_t / (tau + delta_t)
for i in range(1, inp.shape[0]):
output[i] = output[i - 1] + alpha[i] * (inp[i] - output[i - 1]) # Same as output[i] = alpha*inp[i]+(1-alpha)*output[i-1]
elif method == 2:
for i in range(1, inp.shape[0]):
output[i] = (delta_t * (inp[i] + inp[i - 1] - output[i - 1]) + 2 * tau[i] * output[i - 1]) / (delta_t + 2 * tau[i])
elif method == 3:
for i in range(1, inp.shape[0]):
output[i] = output[i - 1] * np.exp(-delta_t / tau[i]) + inp[i] * (1 - np.exp(-delta_t / tau[i]))
return output
@cython.ccall
@cython.locals(alpha=cython.float, i=cython.int)
cpdef cy_high_pass_filter(np.ndarray[double,ndim=1] inp, double delta_t, double tau):
cdef np.ndarray[double,ndim=1] output
output = np.empty_like(inp, dtype=np.float)
output[0] = inp[0]
alpha = tau / (tau + delta_t)
for i in range(1, inp.shape[0]):
output[i] = alpha * (output[i - 1] + inp[i] - inp[i - 1])
return output
'''
Created on 13. jan. 2017
@author: mmpe
'''
import unittest
from wetb.signal_tools.filters import first_order
import numpy as np
class Test_first_order_filters(unittest.TestCase):
def test_low_pass(self):
a = np.random.randint(0,100,100).astype(np.float)
b = first_order.low_pass(a, 1, 1)
self.assertLess(b.std(), a.std())
if 0:
import matplotlib.pyplot as plt
plt.plot(a)
plt.plot(b)
plt.show()
def test_high_pass(self):
a = np.random.randint(0,100,100).astype(np.float)
b = first_order.high_pass(a, 1, 1)
self.assertLess(b.mean(), a.mean())
if 0:
import matplotlib.pyplot as plt
plt.plot(a)
plt.plot(b)
plt.show()
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()
\ No newline at end of file
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