add linear filter utility function.

This commit is contained in:
Roman Zeyde
2014-08-14 08:56:09 +03:00
parent 35f077982b
commit 787b356dd0
3 changed files with 12 additions and 13 deletions

View File

@@ -27,6 +27,12 @@ class Filter(object):
yield y
def lfilter(b, a, x):
f = Filter(b=b, a=a)
y = list(f(x))
return np.array(y)
def train(S, training):
A = np.array([S[1:], S[:-1], training[:-1]]).T
b = training[1:]

View File

@@ -36,18 +36,13 @@ def run(size, chan):
assert rx_data == tx_data
def apply_filter(b, a, x):
f = sigproc.Filter(b=b, a=a)
y = list(f(list(x)))
return np.array(y)
def test_lowpass():
run(1024, lambda x: apply_filter(b=[0.9], a=[1.0, -0.1], x=x))
run(1024, lambda x: sigproc.lfilter(b=[0.9], a=[1.0, -0.1], x=x))
def test_highpass():
run(1024, lambda x: apply_filter(b=[0.9], a=[1.0, 0.1], x=x))
run(1024, lambda x: sigproc.lfilter(b=[0.9], a=[1.0, 0.1], x=x))
def test_small():

View File

@@ -26,12 +26,10 @@ def test_linreg():
def test_filter():
f = sigproc.Filter(b=[1], a=[1])
x = range(10)
y = list(f(x))
assert [float(i) for i in x] == y
y = sigproc.lfilter(b=[1], a=[1], x=x)
assert (np.array(x) == y).all()
f = sigproc.Filter(b=[0.5], a=[1, -0.5])
x = [1] + [0] * 10
y = list(f(x))
assert y == [0.5 ** (i+1) for i in range(len(x))]
y = sigproc.lfilter(b=[0.5], a=[1, -0.5], x=x)
assert list(y) == [0.5 ** (i+1) for i in range(len(x))]