diff --git a/amodem/equalizer.py b/amodem/equalizer.py index 99b1c18..be444d7 100644 --- a/amodem/equalizer.py +++ b/amodem/equalizer.py @@ -1,7 +1,9 @@ import numpy as np from numpy.linalg import lstsq -from amodem import dsp, config +from amodem import dsp +from amodem import config +from amodem import sampling import itertools import random @@ -40,7 +42,7 @@ def equalize(signal, symbols, order): assert symbols.shape[1] == Nfreq length = symbols.shape[0] - matched = np.array(carriers) * Nfreq / (0.5*Nsym) + matched = np.array(carriers) / (0.5*Nsym) matched = matched[:, ::-1].transpose().conj() y = dsp.lfilter(x=signal, b=matched, a=[1]) diff --git a/amodem/sampling.py b/amodem/sampling.py index 9d2f63d..44c8c4d 100644 --- a/amodem/sampling.py +++ b/amodem/sampling.py @@ -30,7 +30,7 @@ class Interpolator(object): class Sampler(object): def __init__(self, src, interp=None): self.freq = 1.0 - self.gain = 1.0 + self.equalizer = lambda x: x if interp is not None: self.interp = interp self.resolution = self.interp.resolution @@ -71,7 +71,7 @@ class Sampler(object): except StopIteration: pass - return frame[:count] * self.gain + return self.equalizer(frame[:count]) def resample(src, dst, df=0.0): diff --git a/amodem/train.py b/amodem/train.py index 5a6d852..61b28a4 100644 --- a/amodem/train.py +++ b/amodem/train.py @@ -1,16 +1,3 @@ -import itertools - prefix = [1]*400 + [0]*50 - - -def _equalizer_sequence(): - res = [] - - symbols = [1, 1j, -1, -1j] - for s in itertools.islice(itertools.cycle(symbols), 100): - res.extend([s]*1 + [0]*1) - - res.extend([0]*20) - return res - -equalizer = _equalizer_sequence() +equalizer_length = 500 +silence_length = 100 diff --git a/tests/test_equalizer.py b/tests/test_equalizer.py index ea1fde4..9eb958a 100644 --- a/tests/test_equalizer.py +++ b/tests/test_equalizer.py @@ -1,7 +1,8 @@ from numpy.linalg import norm import numpy as np -from amodem import train, dsp, config +from amodem import dsp +from amodem import config from amodem import equalizer @@ -9,29 +10,6 @@ def assert_approx(x, y, e=1e-12): assert norm(x - y) < e * norm(x) -def test_fir(): - a = [1, 0.8, -0.1, 0, 0] - tx = train.equalizer - rx = dsp.lfilter(x=tx, b=[1], a=a) - h_ = dsp.estimate(x=rx, y=tx, order=len(a)) - tx_ = dsp.lfilter(x=rx, b=h_, a=[1]) - assert_approx(h_, a) - assert_approx(tx, tx_) - - -def test_iir(): - alpha = 0.1 - b = [1, -alpha] - tx = train.equalizer - rx = dsp.lfilter(x=tx, b=b, a=[1]) - h_ = dsp.estimate(x=rx, y=tx, order=20) - tx_ = dsp.lfilter(x=rx, b=h_, a=[1]) - - h_expected = np.array([alpha ** i for i in range(len(h_))]) - assert_approx(h_, h_expected) - assert_approx(tx, tx_) - - def test_training(): L = 1000 t1 = equalizer.train_symbols(L) @@ -68,8 +46,8 @@ def test_isi(): gain = float(config.Nfreq) symbols = equalizer.train_symbols(length=length) - x = equalizer.modulator(symbols) - assert_approx(equalizer.demodulator(gain * x, size=length), symbols) + x = equalizer.modulator(symbols) * gain + assert_approx(equalizer.demodulator(x, size=length), symbols) den = np.array([1, -0.6, 0.1]) num = np.array([0.5]) @@ -79,5 +57,5 @@ def test_isi(): assert_approx(h, den / num) y = dsp.lfilter(x=y, b=h, a=[1]) - z = equalizer.demodulator(gain * y, size=length) + z = equalizer.demodulator(y, size=length) assert_approx(z, symbols)