<s> from numpy . random import RandomState <EOL> import numpy as np <EOL> import utils <EOL> from amodem import equalizer <EOL> from amodem import dsp <EOL> from amodem import config <EOL> config = config . fastest ( ) <EOL> def assert_approx ( x , y , e = <NUM_LIT> ) : <EOL> x = x . flatten ( ) <EOL> y = y . flatten ( ) <EOL> assert dsp . norm ( x - y ) < e * dsp . norm ( x ) <EOL> def test_training ( ) : <EOL> L = <NUM_LIT:1000> <EOL> e = equalizer . Equalizer ( config ) <EOL> t1 = e . train_symbols ( L ) <EOL> t2 = e . train_symbols ( L ) <EOL> assert ( t1 == t2 ) . all ( ) <EOL> def test_commutation ( ) : <EOL> x = np . random . RandomState ( seed = <NUM_LIT:0> ) . normal ( size = <NUM_LIT:1000> ) <EOL> b = [ <NUM_LIT:1> , <NUM_LIT> , - <NUM_LIT:1> , - <NUM_LIT> ] <EOL> a = [ <NUM_LIT:1> , <NUM_LIT:0.1> ] <EOL> y = utils . lfilter ( x = x , b = b , a = a ) <EOL> y1 = utils . lfilter ( x = utils . lfilter ( x = x , b = b , a = [ <NUM_LIT:1> ] ) , b = [ <NUM_LIT:1> ] , a = a ) <EOL> y2 = utils . lfilter ( x = utils . lfilter ( x = x , b = [ <NUM_LIT:1> ] , a = a ) , b = b , a = [ <NUM_LIT:1> ] ) <EOL> assert_approx ( y , y1 ) <EOL> assert_approx ( y , y2 ) <EOL> z = utils . lfilter ( x = y , b = a , a = [ <NUM_LIT:1> ] ) <EOL> z_ = utils . lfilter ( x = x , b = b , a = [ <NUM_LIT:1> ] ) <EOL> assert_approx ( z , z_ ) <EOL> def test_modem ( ) : <EOL> L = <NUM_LIT:1000> <EOL> e = equalizer . Equalizer ( config ) <EOL> sent = e . train_symbols ( L ) <EOL> gain = config . Nfreq <EOL> x = e . modulator ( sent ) * gain <EOL> received = e . demodulator ( x , L ) <EOL> assert_approx ( sent , received ) <EOL> def test_signal ( ) : <EOL> length = <NUM_LIT> <EOL> x = np . sign ( RandomState ( <NUM_LIT:0> ) . normal ( size = length ) ) <EOL> x [ - <NUM_LIT:20> : ] = <NUM_LIT:0> <EOL> den = np . array ( [ <NUM_LIT:1> , - <NUM_LIT> , <NUM_LIT:0.1> ] ) <EOL> num = np . array ( [ <NUM_LIT:0.5> ] ) <EOL> y = utils . lfilter ( x = x , b = num , a = den ) <EOL> lookahead = <NUM_LIT:2> <EOL> h = equalizer . train ( <EOL> signal = y , expected = x , order = len ( den ) , lookahead = lookahead ) <EOL> assert dsp . norm ( h [ : lookahead ] ) < <NUM_LIT> <EOL> h = h [ lookahead : ] <EOL> assert_approx ( h , den / num ) <EOL> x_ = utils . lfilter ( x = y , b = h , a = [ <NUM_LIT:1> ] ) <EOL> assert_approx ( x_ , x ) </s>
