dsp: remove unused code

This commit is contained in:
Roman Zeyde
2015-01-11 18:13:40 +02:00
parent cfc6de9eb0
commit e4267f236b
2 changed files with 14 additions and 59 deletions

View File

@@ -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

View File

@@ -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