<s> from . import dsp <EOL> from . import sampling <EOL> from . import levinson <EOL> import numpy as np <EOL> import itertools <EOL> class Equalizer ( object ) : <EOL> def __init__ ( self , config ) : <EOL> self . carriers = config . carriers <EOL> self . omegas = <NUM_LIT:2> * np . pi * np . array ( config . frequencies ) / config . Fs <EOL> self . Nfreq = config . Nfreq <EOL> self . Nsym = config . Nsym <EOL> def train_symbols ( self , length , constant_prefix = <NUM_LIT:16> ) : <EOL> r = dsp . prbs ( reg = <NUM_LIT:1> , poly = <NUM_LIT> , bits = <NUM_LIT:2> ) <EOL> constellation = [ <NUM_LIT:1> , <NUM_LIT> , - <NUM_LIT:1> , - <NUM_LIT> ] <EOL> symbols = [ ] <EOL> for _ in range ( length ) : <EOL> symbols . append ( [ constellation [ next ( r ) ] for _ in range ( self . Nfreq ) ] ) <EOL> symbols = np . array ( symbols ) <EOL> symbols [ : constant_prefix , : ] = <NUM_LIT:1> <EOL> return symbols <EOL> def modulator ( self , symbols ) : <EOL> gain = <NUM_LIT:1.0> / len ( self . carriers ) <EOL> result = [ ] <EOL> for s in symbols : <EOL> result . append ( np . dot ( s , self . carriers ) ) <EOL> result = np . concatenate ( result ) . real * gain <EOL> assert np . max ( np . abs ( result ) ) <= <NUM_LIT:1> <EOL> return result <EOL> def demodulator ( self , signal , size ) : <EOL> signal = itertools . chain ( signal , itertools . repeat ( <NUM_LIT:0> ) ) <EOL> symbols = dsp . Demux ( sampler = sampling . Sampler ( signal ) , <EOL> omegas = self . omegas , Nsym = self . Nsym ) <EOL> return np . array ( list ( itertools . islice ( symbols , size ) ) ) <EOL> prefix = [ <NUM_LIT:1> ] * <NUM_LIT:200> + [ <NUM_LIT:0> ] * <NUM_LIT:50> <EOL> equalizer_length = <NUM_LIT:200> <EOL> silence_length = <NUM_LIT:50> <EOL> def train ( signal , expected , order , lookahead = <NUM_LIT:0> ) : <EOL> padding = np . zeros ( lookahead ) <EOL> assert len ( signal ) == len ( expected ) <EOL> x = np . concatenate ( [ signal , padding ] ) <EOL> y = np . concatenate ( [ padding , expected ] ) <EOL> N = order + lookahead <EOL> Rxx = np . zeros ( N ) <EOL> Rxy = np . zeros ( N ) <EOL> for i in range ( N ) : <EOL> Rxx [ i ] = np . dot ( x [ i : ] , x [ : len ( x ) - i ] ) <EOL> Rxy [ i ] = np . dot ( y [ i : ] , x [ : len ( x ) - i ] ) <EOL> return levinson . solver ( t = Rxx , y = Rxy ) </s>
