fix sigproc.Filter

This commit is contained in:
Roman Zeyde
2014-08-12 17:56:02 +03:00
parent 8a156c7e54
commit 97fc5b52f4
2 changed files with 18 additions and 3 deletions

View File

@@ -16,11 +16,14 @@ class Filter(object):
def __call__(self, x):
x_ = [0] * len(self.b)
y_ = [0] * len(self.a)
y_ = [0] * (len(self.a) + 1)
for v in x:
x_ = [v] + x_[:-1]
y = np.dot(x_, self.b) - np.dot(y_, self.a)
y_ = [y] + y_[1:]
y_ = y_[:-1]
num = np.dot(x_, self.b)
den = np.dot(y_, self.a)
y = num - den
y_ = [y] + y_
yield y

View File

@@ -23,3 +23,15 @@ def test_linreg():
a_, b_ = sigproc.linear_regression(x, y)
assert abs(a - a_) < 1e-10
assert abs(b - b_) < 1e-10
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
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))]