diff --git a/amodem/sigproc.py b/amodem/sigproc.py index e9d6942..b3be754 100644 --- a/amodem/sigproc.py +++ b/amodem/sigproc.py @@ -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 diff --git a/tests/test_sigproc.py b/tests/test_sigproc.py index 450d1be..6ee85e3 100644 --- a/tests/test_sigproc.py +++ b/tests/test_sigproc.py @@ -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))]