From d81ec630a521402f0b2929b73b1a1d40acce4a56 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Wed, 11 Feb 2015 17:20:51 +0200 Subject: [PATCH] dsp: move lfilter and IIR to tests --- amodem/dsp.py | 26 -------------------------- tests/test_dsp.py | 5 +++-- tests/test_equalizer.py | 16 ++++++++-------- tests/test_transfer.py | 6 +++--- tests/utils.py | 27 +++++++++++++++++++++++++++ 5 files changed, 41 insertions(+), 39 deletions(-) create mode 100644 tests/utils.py diff --git a/amodem/dsp.py b/amodem/dsp.py index 8280035..d8da7d5 100644 --- a/amodem/dsp.py +++ b/amodem/dsp.py @@ -18,32 +18,6 @@ class FIR(object): self.x_state = x_ -class IIR(object): - def __init__(self, b, a): - self.b = np.array(b) / a[0] - self.a = np.array(a[1:]) / a[0] - self.x_state = [0] * len(self.b) - self.y_state = [0] * (len(self.a) + 1) - - def __call__(self, x): - x_, y_ = self.x_state, self.y_state - for v in x: - x_ = [v] + x_[:-1] - y_ = y_[:-1] - num = np.dot(x_, self.b) - den = np.dot(y_, self.a) - y = num - den - y_ = [y] + y_ - yield y - self.x_state, self.y_state = x_, y_ - - -def lfilter(b, a, x): - f = IIR(b=b, a=a) - y = list(f(x)) - return np.array(y) - - class Demux(object): def __init__(self, sampler, omegas, Nsym): self.Nsym = Nsym diff --git a/tests/test_dsp.py b/tests/test_dsp.py index 36479d9..ea45285 100644 --- a/tests/test_dsp.py +++ b/tests/test_dsp.py @@ -1,6 +1,7 @@ from amodem import dsp from amodem import sampling from amodem import config +import utils import numpy as np import random @@ -20,11 +21,11 @@ def test_linreg(): def test_filter(): x = range(10) - y = dsp.lfilter(b=[1], a=[1], x=x) + y = utils.lfilter(b=[1], a=[1], x=x) assert (np.array(x) == y).all() x = [1] + [0] * 10 - y = dsp.lfilter(b=[0.5], a=[1, -0.5], x=x) + y = utils.lfilter(b=[0.5], a=[1, -0.5], x=x) assert list(y) == [0.5 ** (i+1) for i in range(len(x))] diff --git a/tests/test_equalizer.py b/tests/test_equalizer.py index a18a7ff..3caa800 100644 --- a/tests/test_equalizer.py +++ b/tests/test_equalizer.py @@ -2,7 +2,7 @@ from numpy.linalg import norm from numpy.random import RandomState import numpy as np -from amodem import dsp +import utils from amodem import equalizer from amodem import config config = config.fastest() @@ -24,14 +24,14 @@ def test_commutation(): x = np.random.RandomState(seed=0).normal(size=1000) b = [1, 1j, -1, -1j] a = [1, 0.1] - y = dsp.lfilter(x=x, b=b, a=a) - y1 = dsp.lfilter(x=dsp.lfilter(x=x, b=b, a=[1]), b=[1], a=a) - y2 = dsp.lfilter(x=dsp.lfilter(x=x, b=[1], a=a), b=b, a=[1]) + y = utils.lfilter(x=x, b=b, a=a) + y1 = utils.lfilter(x=utils.lfilter(x=x, b=b, a=[1]), b=[1], a=a) + y2 = utils.lfilter(x=utils.lfilter(x=x, b=[1], a=a), b=b, a=[1]) assert_approx(y, y1) assert_approx(y, y2) - z = dsp.lfilter(x=y, b=a, a=[1]) - z_ = dsp.lfilter(x=x, b=b, a=[1]) + z = utils.lfilter(x=y, b=a, a=[1]) + z_ = utils.lfilter(x=x, b=b, a=[1]) assert_approx(z, z_) @@ -50,7 +50,7 @@ def test_signal(): x = np.sign(RandomState(0).normal(size=length)) den = np.array([1, -0.6, 0.1]) num = np.array([0.5]) - y = dsp.lfilter(x=x, b=num, a=den) + y = utils.lfilter(x=x, b=num, a=den) lookahead = 2 h = equalizer.train( @@ -60,5 +60,5 @@ def test_signal(): h = h[lookahead:] assert_approx(h, den / num) - x_ = dsp.lfilter(x=y, b=h, a=[1]) + x_ = utils.lfilter(x=y, b=h, a=[1]) assert_approx(x_, x) diff --git a/tests/test_transfer.py b/tests/test_transfer.py index a7636f6..b58bcd2 100644 --- a/tests/test_transfer.py +++ b/tests/test_transfer.py @@ -1,8 +1,8 @@ from amodem import main from amodem import common -from amodem import dsp from amodem import sampling from amodem import config +import utils import numpy as np import os @@ -85,11 +85,11 @@ def test_timing(freq_err): def test_lowpass(): - run(1024, chan=lambda x: dsp.lfilter(b=[0.9], a=[1.0, -0.1], x=x)) + run(1024, chan=lambda x: utils.lfilter(b=[0.9], a=[1.0, -0.1], x=x)) def test_highpass(): - run(1024, chan=lambda x: dsp.lfilter(b=[0.9], a=[1.0, 0.1], x=x)) + run(1024, chan=lambda x: utils.lfilter(b=[0.9], a=[1.0, 0.1], x=x)) def test_attenuation(): diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 0000000..8853dc0 --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,27 @@ +import numpy as np + + +class IIR(object): + def __init__(self, b, a): + self.b = np.array(b) / a[0] + self.a = np.array(a[1:]) / a[0] + self.x_state = [0] * len(self.b) + self.y_state = [0] * (len(self.a) + 1) + + def __call__(self, x): + x_, y_ = self.x_state, self.y_state + for v in x: + x_ = [v] + x_[:-1] + y_ = y_[:-1] + num = np.dot(x_, self.b) + den = np.dot(y_, self.a) + y = num - den + y_ = [y] + y_ + yield y + self.x_state, self.y_state = x_, y_ + + +def lfilter(b, a, x): + f = IIR(b=b, a=a) + y = list(f(x)) + return np.array(y)