From e4267f236b1f4784edeaf762d7066a6449427c9b Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Sun, 11 Jan 2015 18:13:40 +0200 Subject: [PATCH] dsp: remove unused code --- amodem/dsp.py | 43 ++++++++++++++----------------------------- tests/test_dsp.py | 30 ------------------------------ 2 files changed, 14 insertions(+), 59 deletions(-) diff --git a/amodem/dsp.py b/amodem/dsp.py index cbfa989..4a7c671 100644 --- a/amodem/dsp.py +++ b/amodem/dsp.py @@ -4,6 +4,20 @@ from numpy import linalg from . import common +class FIR(object): + def __init__(self, h): + self.h = np.array(h) + self.x_state = [0] * len(self.h) + + def __call__(self, x): + x_ = self.x_state + h = self.h + for v in x: + x_ = [v] + x_[:-1] + yield np.dot(x_, h) + self.x_state = x_ + + class IIR(object): def __init__(self, b, a): self.b = np.array(b) / a[0] @@ -24,41 +38,12 @@ class IIR(object): self.x_state, self.y_state = x_, y_ -class FIR(object): - def __init__(self, h): - self.h = np.array(h) - self.x_state = [0] * len(self.h) - - def __call__(self, x): - x_ = self.x_state - h = self.h - for v in x: - x_ = [v] + x_[:-1] - yield np.dot(x_, h) - self.x_state = x_ - - def lfilter(b, a, x): f = IIR(b=b, a=a) y = list(f(x)) return np.array(y) -def estimate(x, y, order, lookahead=0): - offset = order - 1 - assert offset >= lookahead - b = y[offset-lookahead:len(x)-lookahead] - - A = [] # columns of x - N = len(x) - order + 1 - for i in range(order): - A.append(x[i:N+i]) - - # switch to rows for least-squares - h = linalg.lstsq(np.array(A).T, b)[0] - return h[::-1] - - 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 c9a1989..1e8f77c 100644 --- a/tests/test_dsp.py +++ b/tests/test_dsp.py @@ -29,36 +29,6 @@ def test_filter(): assert list(y) == [0.5 ** (i+1) for i in range(len(x))] -def test_estimate(): - r = np.random.RandomState(seed=0) - x = r.uniform(-1, 1, [1000]) - x[:10] = 0 - x[len(x)-10:] = 0 - - c = 1.23 - y = c * x - c_, = dsp.estimate(x=x, y=y, order=1) - assert abs(c - c_) < 1e-12 - - h = [1, 1] - y = dsp.lfilter(b=h, a=[1], x=x) - h_ = dsp.estimate(x=x, y=y, order=len(h)) - assert norm(h - h_) < 1e-12 - - h = [0.1, 0.6, 0.9, 0.7, -0.2] - L = len(h) // 2 - - y = dsp.lfilter(b=h, a=[1], x=x) - h_ = dsp.estimate( - x=x[:len(x)-L], y=y[L:], - order=len(h), lookahead=L - ) - assert norm(h - h_) < 1e-12 - - y_ = dsp.lfilter(b=h_, a=[1], x=x) - assert norm(y - y_) < 1e-12 - - def test_demux(): freqs = np.array([1e3, 2e3]) omegas = 2 * np.pi * freqs / config.Fs