def test_sf_tag(self): constA = [-3.0, -1.0, 1.0, 3] constB = [12.0, -12.0, 6.0, -6] src_data = (0, 1, 2, 3, 3, 2, 1, 0) expected_result = (-3, -1, 1, 3, -6, 6, -12, 12) first_tag = gr.tag_t() first_tag.key = pmt.intern("set_symbol_table") first_tag.value = pmt.init_f32vector(len(constA), constA) first_tag.offset = 0 second_tag = gr.tag_t() second_tag.key = pmt.intern("set_symbol_table") second_tag.value = pmt.init_f32vector(len(constB), constB) second_tag.offset = 4 src = blocks.vector_source_s(src_data, False, 1, [first_tag, second_tag]) op = digital.chunks_to_symbols_sf(constB) dst = blocks.vector_sink_f() self.tb.connect(src, op) self.tb.connect(op, dst) self.tb.run() actual_result = dst.data() self.assertEqual(expected_result, actual_result)
def test_sf_tag(self): constA = [-3.0, -1.0, 1.0, 3] constB = [12.0, -12.0, 6.0, -6] src_data = (0, 1, 2, 3, 3, 2, 1, 0) expected_result = [-3, -1, 1, 3, -6, 6, -12, 12] first_tag = gr.tag_t() first_tag.key = pmt.intern("set_symbol_table") first_tag.value = pmt.init_f32vector(len(constA), constA) first_tag.offset = 0 second_tag = gr.tag_t() second_tag.key = pmt.intern("set_symbol_table") second_tag.value = pmt.init_f32vector(len(constB), constB) second_tag.offset = 4 src = blocks.vector_source_s(src_data, False, 1, [first_tag, second_tag]) op = digital.chunks_to_symbols_sf(constB) dst = blocks.vector_sink_f() self.tb.connect(src, op) self.tb.connect(op, dst) self.tb.run() actual_result = dst.data() self.assertEqual(expected_result, actual_result)
def run_test (f,Kb,bitspersymbol,K,dimensionality,tot_constellation,N0,seed): tb = gr.top_block () # TX src = blocks.lfsr_32k_source_s() src_head = blocks.head (gr.sizeof_short,Kb/16) # packet size in shorts s2fsmi = blocks.packed_to_unpacked_ss(bitspersymbol,gr.GR_MSB_FIRST) # unpack shorts to symbols compatible with the FSM input cardinality enc = trellis.encoder_ss(f,0) # initial state = 0 # essentially here we implement the combination of modulation and channel as a memoryless modulation (the memory induced by the channel is hidden in the FSM) mod = digital.chunks_to_symbols_sf(tot_constellation,dimensionality) # CHANNEL add = blocks.add_ff() noise = analog.noise_source_f(analog.GR_GAUSSIAN,math.sqrt(N0/2),seed) # RX metrics = trellis.metrics_f(f.O(),dimensionality,tot_constellation,digital.TRELLIS_EUCLIDEAN) # data preprocessing to generate metrics for Viterbi va = trellis.viterbi_s(f,K,0,-1) # Put -1 if the Initial/Final states are not set. fsmi2s = blocks.unpacked_to_packed_ss(bitspersymbol,gr.GR_MSB_FIRST) # pack FSM input symbols to shorts dst = blocks.check_lfsr_32k_s(); tb.connect (src,src_head,s2fsmi,enc,mod) tb.connect (mod,(add,0)) tb.connect (noise,(add,1)) tb.connect (add,metrics) tb.connect (metrics,va,fsmi2s,dst) tb.run() ntotal = dst.ntotal () nright = dst.nright () runlength = dst.runlength () #print ntotal,nright,runlength return (ntotal,ntotal-nright)
def run_test (fo,fi,interleaver,Kb,bitspersymbol,K,dimensionality,constellation,Es,N0,IT,seed): tb = gr.top_block () # TX src = blocks.lfsr_32k_source_s() src_head = blocks.head(gr.sizeof_short,Kb/16) # packet size in shorts s2fsmi = blocks.packed_to_unpacked_ss(bitspersymbol,gr.GR_MSB_FIRST) # unpack shorts to symbols compatible with the outer FSM input cardinality enc = trellis.sccc_encoder_ss(fo,0,fi,0,interleaver,K) mod = digital.chunks_to_symbols_sf(constellation,dimensionality) # CHANNEL add = blocks.add_ff() noise = analog.noise_source_f(analog.GR_GAUSSIAN,math.sqrt(N0/2),seed) # RX dec = trellis.sccc_decoder_combined_fs(fo,0,-1,fi,0,-1,interleaver,K,IT,trellis.TRELLIS_MIN_SUM,dimensionality,constellation,digital.TRELLIS_EUCLIDEAN,1.0) fsmi2s = blocks.unpacked_to_packed_ss(bitspersymbol,gr.GR_MSB_FIRST) # pack FSM input symbols to shorts dst = blocks.check_lfsr_32k_s() #tb.connect (src,src_head,s2fsmi,enc_out,inter,enc_in,mod) tb.connect (src,src_head,s2fsmi,enc,mod) tb.connect (mod,(add,0)) tb.connect (noise,(add,1)) #tb.connect (add,head) #tb.connect (tail,fsmi2s,dst) tb.connect (add,dec,fsmi2s,dst) tb.run() #print enc_out.ST(), enc_in.ST() ntotal = dst.ntotal () nright = dst.nright () runlength = dst.runlength () return (ntotal,ntotal-nright)
def run_test (f,Kb,bitspersymbol,K,dimensionality,constellation,N0,seed): tb = gr.top_block () # TX #packet = [0]*Kb #for i in range(Kb-1*16): # last 16 bits = 0 to drive the final state to 0 #packet[i] = random.randint(0, 1) # random 0s and 1s #src = blocks.vector_source_s(packet,False) src = blocks.lfsr_32k_source_s() src_head = blocks.head(gr.sizeof_short,Kb/16) # packet size in shorts #b2s = blocks.unpacked_to_packed_ss(1,gr.GR_MSB_FIRST) # pack bits in shorts s2fsmi = blocks.packed_to_unpacked_ss(bitspersymbol,gr.GR_MSB_FIRST) # unpack shorts to symbols compatible with the FSM input cardinality enc = trellis.encoder_ss(f,0) # initial state = 0 mod = digital.chunks_to_symbols_sf(constellation,dimensionality) # CHANNEL add = blocks.add_ff() noise = analog.noise_source_f(analog.GR_GAUSSIAN,math.sqrt(N0/2),seed) # RX metrics = trellis.metrics_f(f.O(),dimensionality,constellation,digital.TRELLIS_EUCLIDEAN) # data preprocessing to generate metrics for Viterbi va = trellis.viterbi_s(f,K,0,-1) # Put -1 if the Initial/Final states are not set. fsmi2s = blocks.unpacked_to_packed_ss(bitspersymbol,gr.GR_MSB_FIRST) # pack FSM input symbols to shorts #s2b = blocks.packed_to_unpacked_ss(1,gr.GR_MSB_FIRST) # unpack shorts to bits #dst = blocks.vector_sink_s(); dst = blocks.check_lfsr_32k_s() tb.connect (src,src_head,s2fsmi,enc,mod) #tb.connect (src,b2s,s2fsmi,enc,mod) tb.connect (mod,(add,0)) tb.connect (noise,(add,1)) tb.connect (add,metrics) tb.connect (metrics,va,fsmi2s,dst) #tb.connect (metrics,va,fsmi2s,s2b,dst) tb.run() # A bit of cheating: run the program once and print the # final encoder state.. # Then put it as the last argument in the viterbi block #print "final state = " , enc.ST() ntotal = dst.ntotal () nright = dst.nright () runlength = dst.runlength () #ntotal = len(packet) #if len(dst.data()) != ntotal: #print "Error: not enough data\n" #nright = 0; #for i in range(ntotal): #if packet[i]==dst.data()[i]: #nright=nright+1 #else: #print "Error in ", i return (ntotal,ntotal-nright)
def run_test(f, Kb, bitspersymbol, K, channel, modulation, dimensionality, tot_constellation, N0, seed): tb = gr.top_block() L = len(channel) # TX # this for loop is TOO slow in python!!! packet = [0] * (K + 2 * L) random.seed(seed) for i in range(len(packet)): packet[i] = random.randint(0, 2**bitspersymbol - 1) # random symbols for i in range(L): # first/last L symbols set to 0 packet[i] = 0 packet[len(packet) - i - 1] = 0 src = blocks.vector_source_s(packet, False) mod = digital.chunks_to_symbols_sf(modulation[1], modulation[0]) # CHANNEL isi = filter.fir_filter_fff(1, channel) add = blocks.add_ff() noise = analog.noise_source_f(analog.GR_GAUSSIAN, math.sqrt(N0 / 2), seed) # RX skip = blocks.skiphead( gr.sizeof_float, L ) # skip the first L samples since you know they are coming from the L zero symbols #metrics = trellis.metrics_f(f.O(),dimensionality,tot_constellation,digital.TRELLIS_EUCLIDEAN) # data preprocessing to generate metrics for Viterbi #va = trellis.viterbi_s(f,K+L,0,0) # Put -1 if the Initial/Final states are not set. va = trellis.viterbi_combined_s( f, K + L, 0, 0, dimensionality, tot_constellation, digital.TRELLIS_EUCLIDEAN ) # using viterbi_combined_s instead of metrics_f/viterbi_s allows larger packet lengths because metrics_f is complaining for not being able to allocate large buffers. This is due to the large f.O() in this application... dst = blocks.vector_sink_s() tb.connect(src, mod) tb.connect(mod, isi, (add, 0)) tb.connect(noise, (add, 1)) #tb.connect (add,metrics) #tb.connect (metrics,va,dst) tb.connect(add, skip, va, dst) tb.run() data = dst.data() ntotal = len(data) - L nright = 0 for i in range(ntotal): if packet[i + L] == data[i]: nright = nright + 1 #else: #print "Error in ", i return (ntotal, ntotal - nright)
def run_test(f, Kb, bitspersymbol, K, channel, modulation, dimensionality, tot_constellation, N0, seed): tb = gr.top_block() L = len(channel) # TX # this for loop is TOO slow in python!!! packet = [0] * (K + 2 * L) random.seed(seed) for i in range(len(packet)): packet[i] = random.randint(0, 2 ** bitspersymbol - 1) # random symbols for i in range(L): # first/last L symbols set to 0 packet[i] = 0 packet[len(packet) - i - 1] = 0 src = blocks.vector_source_s(packet, False) mod = digital.chunks_to_symbols_sf(modulation[1], modulation[0]) # CHANNEL isi = filter.fir_filter_fff(1, channel) add = blocks.add_ff() noise = analog.noise_source_f(analog.GR_GAUSSIAN, math.sqrt(N0 / 2), seed) # RX skip = blocks.skiphead( gr.sizeof_float, L ) # skip the first L samples since you know they are coming from the L zero symbols # metrics = trellis.metrics_f(f.O(),dimensionality,tot_constellation,digital.TRELLIS_EUCLIDEAN) # data preprocessing to generate metrics for Viterbi # va = trellis.viterbi_s(f,K+L,0,0) # Put -1 if the Initial/Final states are not set. va = trellis.viterbi_combined_s( f, K + L, 0, 0, dimensionality, tot_constellation, digital.TRELLIS_EUCLIDEAN ) # using viterbi_combined_s instead of metrics_f/viterbi_s allows larger packet lengths because metrics_f is complaining for not being able to allocate large buffers. This is due to the large f.O() in this application... dst = blocks.vector_sink_s() tb.connect(src, mod) tb.connect(mod, isi, (add, 0)) tb.connect(noise, (add, 1)) # tb.connect (add,metrics) # tb.connect (metrics,va,dst) tb.connect(add, skip, va, dst) tb.run() data = dst.data() ntotal = len(data) - L nright = 0 for i in range(ntotal): if packet[i + L] == data[i]: nright = nright + 1 # else: # print "Error in ", i return (ntotal, ntotal - nright)
def __init__(self, ts, factor, alpha, samp_rate, freqs): gr.hier_block2.__init__( self, "freq_timing_estimator_hier", gr.io_signature(1, 1, gr.sizeof_gr_complex*1), gr.io_signaturev(3, 3, [gr.sizeof_char*1, gr.sizeof_float*1, gr.sizeof_float*1]), ) ################################################## # Parameters ################################################## self.ts = ts self.factor = factor self.alpha = alpha self.samp_rate = samp_rate self.freqs = freqs self.n = n = len(freqs) ################################################## # Blocks ################################################## self._filter=[0]*self.n self._c2mag2=[0]*self.n for i in range(self.n): self._filter[i]= filter.freq_xlating_fir_filter_ccc(1, (numpy.conjugate(self.ts[::-1])), self.freqs[i], self.samp_rate) self._c2mag2[i] = blocks.complex_to_mag_squared(1) self.blocks_max = blocks.max_ff(1) self.blocks_peak_detector = blocks.peak_detector_fb(self.factor, self.factor, 0, self.alpha) self.blocks_argmax = blocks.argmax_fs(1) self.blocks_null_sink = blocks.null_sink(gr.sizeof_short*1) self.digital_chunks_to_symbols = digital.chunks_to_symbols_sf((freqs), 1) self.blocks_sample_and_hold = blocks.sample_and_hold_ff() ################################################## # Connections ################################################## for i in range(self.n): self.connect((self, 0), (self._filter[i], 0)) self.connect((self._filter[i], 0), (self._c2mag2[i], 0)) self.connect((self._c2mag2[i], 0), (self.blocks_max, i)) self.connect((self._c2mag2[i], 0), (self.blocks_argmax, i)) self.connect((self.blocks_max, 0), (self.blocks_peak_detector, 0)) self.connect((self.blocks_peak_detector, 0), (self, 0)) self.connect((self.blocks_argmax, 0), (self.blocks_null_sink, 0)) self.connect((self.blocks_argmax, 1), (self.digital_chunks_to_symbols, 0)) self.connect((self.digital_chunks_to_symbols, 0), (self.blocks_sample_and_hold, 0)) self.connect((self.blocks_peak_detector, 0), (self.blocks_sample_and_hold, 1)) self.connect((self.blocks_sample_and_hold, 0), (self, 1)) self.connect((self.blocks_max, 0), (self, 2))
def run_test(fo, fi, interleaver, Kb, bitspersymbol, K, dimensionality, constellation, N0, seed): tb = gr.top_block() # TX src = blocks.lfsr_32k_source_s() src_head = blocks.head(gr.sizeof_short, Kb / 16) # packet size in shorts s2fsmi = blocks.packed_to_unpacked_ss( bitspersymbol, gr.GR_MSB_FIRST ) # unpack shorts to symbols compatible with the outer FSM input cardinality enc_out = trellis.encoder_ss(fo, 0) # initial state = 0 inter = trellis.permutation(interleaver.K(), interleaver.INTER(), 1, gr.sizeof_short) enc_in = trellis.encoder_ss(fi, 0) # initial state = 0 mod = digital.chunks_to_symbols_sf(constellation, dimensionality) # CHANNEL add = blocks.add_ff() noise = analog.noise_source_f(analog.GR_GAUSSIAN, math.sqrt(N0 / 2), seed) # RX metrics_in = trellis.metrics_f( fi.O(), dimensionality, constellation, digital.TRELLIS_EUCLIDEAN ) # data preprocessing to generate metrics for innner Viterbi gnd = blocks.vector_source_f([0], True) siso_in = trellis.siso_f( fi, K, 0, -1, True, False, trellis.TRELLIS_MIN_SUM ) # Put -1 if the Initial/Final states are not set. deinter = trellis.permutation(interleaver.K(), interleaver.DEINTER(), fi.I(), gr.sizeof_float) va_out = trellis.viterbi_s( fo, K, 0, -1) # Put -1 if the Initial/Final states are not set. fsmi2s = blocks.unpacked_to_packed_ss( bitspersymbol, gr.GR_MSB_FIRST) # pack FSM input symbols to shorts dst = blocks.check_lfsr_32k_s() tb.connect(src, src_head, s2fsmi, enc_out, inter, enc_in, mod) tb.connect(mod, (add, 0)) tb.connect(noise, (add, 1)) tb.connect(add, metrics_in) tb.connect(gnd, (siso_in, 0)) tb.connect(metrics_in, (siso_in, 1)) tb.connect(siso_in, deinter, va_out, fsmi2s, dst) tb.run() ntotal = dst.ntotal() nright = dst.nright() runlength = dst.runlength() return (ntotal, ntotal - nright)
def run_test(f, Kb, bitspersymbol, K, dimensionality, constellation, N0, seed): tb = gr.top_block() # TX numpy.random.seed(-seed) packet = numpy.random.randint(0, 2, Kb) # create Kb random bits packet[Kb - 10:Kb] = 0 packet[0:Kb] = 0 src = blocks.vector_source_s(packet.tolist(), False) b2s = blocks.unpacked_to_packed_ss(1, gr.GR_MSB_FIRST) # pack bits in shorts s2fsmi = blocks.packed_to_unpacked_ss( bitspersymbol, gr.GR_MSB_FIRST ) # unpack shorts to symbols compatible with the FSM input cardinality enc = trellis.encoder_ss(f, 0) # initial state = 0 mod = digital.chunks_to_symbols_sf(constellation, dimensionality) # CHANNEL add = blocks.add_ff() noise = analog.noise_source_f(analog.GR_GAUSSIAN, math.sqrt(N0 / 2), long(seed)) # RX va = trellis.viterbi_combined_fs( f, K, 0, 0, dimensionality, constellation, digital.TRELLIS_EUCLIDEAN ) # Put -1 if the Initial/Final states are not set. fsmi2s = blocks.unpacked_to_packed_ss( bitspersymbol, gr.GR_MSB_FIRST) # pack FSM input symbols to shorts s2b = blocks.packed_to_unpacked_ss( 1, gr.GR_MSB_FIRST) # unpack shorts to bits dst = blocks.vector_sink_s() tb.connect(src, b2s, s2fsmi, enc, mod) tb.connect(mod, (add, 0)) tb.connect(noise, (add, 1)) tb.connect(add, va, fsmi2s, s2b, dst) tb.run() # A bit of cheating: run the program once and print the # final encoder state.. # Then put it as the last argument in the viterbi block #print "final state = " , enc.ST() if len(dst.data()) != len(packet): print "Error: not enough data:", len(dst.data()), len(packet) ntotal = len(packet) nwrong = sum(abs(packet - numpy.array(dst.data()))) return (ntotal, nwrong, abs(packet - numpy.array(dst.data())))
def test_sf_callback(self): constA = [-3, -1, 1, 3] constB = [12, -12, 6, -6] src_data = (0, 1, 2, 3, 3, 2, 1, 0) expected_result=(12, -12, 6, -6, -6, 6, -12, 12) src = blocks.vector_source_s(src_data, False, 1, "") op = digital.chunks_to_symbols_sf(constA) op.set_symbol_table(constB) dst = blocks.vector_sink_f() self.tb.connect(src, op) self.tb.connect(op, dst) self.tb.run() actual_result = dst.data() self.assertEqual(expected_result, actual_result)
def test_sf_006(self): const = [-3, -1, 1, 3] src_data = (0, 1, 2, 3, 3, 2, 1, 0) expected_result = [-3, -1, 1, 3, 3, 1, -1, -3] src = blocks.vector_source_s(src_data) op = digital.chunks_to_symbols_sf(const) dst = blocks.vector_sink_f() self.tb.connect(src, op) self.tb.connect(op, dst) self.tb.run() actual_result = dst.data() self.assertEqual(expected_result, actual_result)
def test_sf_callback(self): constA = [-3, -1, 1, 3] constB = [12, -12, 6, -6] src_data = [0, 1, 2, 3, 3, 2, 1, 0] expected_result = [12, -12, 6, -6, -6, 6, -12, 12] src = blocks.vector_source_s(src_data, False, 1, []) op = digital.chunks_to_symbols_sf(constA) op.set_symbol_table(constB) dst = blocks.vector_sink_f() self.tb.connect(src, op) self.tb.connect(op, dst) self.tb.run() actual_result = dst.data() self.assertEqual(expected_result, actual_result)
def test_sf_006(self): const = [-3, -1, 1, 3] src_data = (0, 1, 2, 3, 3, 2, 1, 0) expected_result = (-3, -1, 1, 3, 3, 1, -1, -3) src = blocks.vector_source_s(src_data) op = digital.chunks_to_symbols_sf(const) dst = blocks.vector_sink_f() self.tb.connect(src, op) self.tb.connect(op, dst) self.tb.run() actual_result = dst.data() self.assertEqual(expected_result, actual_result)
def run_test(fo, fi, interleaver, Kb, bitspersymbol, K, dimensionality, constellation, Es, N0, IT, seed): tb = gr.top_block() # TX src = blocks.lfsr_32k_source_s() src_head = blocks.head(gr.sizeof_short, Kb / 16) # packet size in shorts s2fsmi = blocks.packed_to_unpacked_ss( bitspersymbol, gr.GR_MSB_FIRST ) # unpack shorts to symbols compatible with the outer FSM input cardinality #src = blocks.vector_source_s([0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1],False) enc = trellis.pccc_encoder_ss(fo, 0, fi, 0, interleaver, K) code = blocks.vector_sink_s() mod = digital.chunks_to_symbols_sf(constellation, dimensionality) # CHANNEL add = blocks.add_ff() noise = analog.noise_source_f(analog.GR_GAUSSIAN, math.sqrt(N0 / 2), seed) # RX metrics_in = trellis.metrics_f( fi.O() * fo.O(), dimensionality, constellation, digital.TRELLIS_EUCLIDEAN ) # data preprocessing to generate metrics for innner SISO scale = blocks.multiply_const_ff(1.0 / N0) dec = trellis.pccc_decoder_s(fo, 0, -1, fi, 0, -1, interleaver, K, IT, trellis.TRELLIS_MIN_SUM) fsmi2s = blocks.unpacked_to_packed_ss( bitspersymbol, gr.GR_MSB_FIRST) # pack FSM input symbols to shorts dst = blocks.check_lfsr_32k_s() tb.connect(src, src_head, s2fsmi, enc, mod) #tb.connect (src,enc,mod) #tb.connect(enc,code) tb.connect(mod, (add, 0)) tb.connect(noise, (add, 1)) tb.connect(add, metrics_in, scale, dec, fsmi2s, dst) tb.run() #print code.data() ntotal = dst.ntotal() nright = dst.nright() runlength = dst.runlength() return (ntotal, ntotal - nright)
def run_test (f,Kb,bitspersymbol,K,dimensionality,constellation,N0,seed,P): tb = gr.top_block () # TX src = blocks.lfsr_32k_source_s() src_head = blocks.head(gr.sizeof_short,Kb/16*P) # packet size in shorts s2fsmi=blocks.packed_to_unpacked_ss(bitspersymbol,gr.GR_MSB_FIRST) # unpack shorts to symbols compatible with the FSM input cardinality s2p = blocks.stream_to_streams(gr.sizeof_short,P) # serial to parallel enc = trellis.encoder_ss(f,0) # initiali state = 0 mod = digital.chunks_to_symbols_sf(constellation,dimensionality) # CHANNEL add=[] noise=[] for i in range(P): add.append(blocks.add_ff()) noise.append(analog.noise_source_f(analog.GR_GAUSSIAN,math.sqrt(N0/2),seed)) # RX metrics = trellis.metrics_f(f.O(),dimensionality,constellation,digital.TRELLIS_EUCLIDEAN) # data preprocessing to generate metrics for Viterbi va = trellis.viterbi_s(f,K,0,-1) # Put -1 if the Initial/Final states are not set. p2s = blocks.streams_to_stream(gr.sizeof_short,P) # parallel to serial fsmi2s=blocks.unpacked_to_packed_ss(bitspersymbol,gr.GR_MSB_FIRST) # pack FSM input symbols to shorts dst = blocks.check_lfsr_32k_s() tb.connect (src,src_head,s2fsmi,s2p) for i in range(P): tb.connect ((s2p,i),(enc,i),(mod,i)) tb.connect ((mod,i),(add[i],0)) tb.connect (noise[i],(add[i],1)) tb.connect (add[i],(metrics,i)) tb.connect ((metrics,i),(va,i),(p2s,i)) tb.connect (p2s,fsmi2s,dst) tb.run() # A bit of cheating: run the program once and print the # final encoder state. # Then put it as the last argument in the viterbi block #print "final state = " , enc.ST() ntotal = dst.ntotal () nright = dst.nright () runlength = dst.runlength () return (ntotal,ntotal-nright)
def run_test(fo, fi, interleaver, Kb, bitspersymbol, K, channel, modulation, dimensionality, tot_constellation, Es, N0, IT, seed): tb = gr.top_block() L = len(channel) # TX # this for loop is TOO slow in python!!! packet = [0] * (K) random.seed(seed) for i in range(len(packet)): packet[i] = random.randint(0, 2**bitspersymbol - 1) # random symbols src = blocks.vector_source_s(packet, False) enc_out = trellis.encoder_ss(fo, 0) # initial state = 0 inter = trellis.permutation(interleaver.K(), interleaver.INTER(), 1, gr.sizeof_short) mod = digital.chunks_to_symbols_sf(modulation[1], modulation[0]) # CHANNEL isi = filter.fir_filter_fff(1, channel) add = blocks.add_ff() noise = analog.noise_source_f(analog.GR_GAUSSIAN, math.sqrt(N0 / 2), seed) # RX (head, tail) = make_rx(tb, fo, fi, dimensionality, tot_constellation, K, interleaver, IT, Es, N0, trellis.TRELLIS_MIN_SUM) dst = blocks.vector_sink_s() tb.connect(src, enc_out, inter, mod) tb.connect(mod, isi, (add, 0)) tb.connect(noise, (add, 1)) tb.connect(add, head) tb.connect(tail, dst) tb.run() data = dst.data() ntotal = len(data) nright = 0 for i in range(ntotal): if packet[i] == data[i]: nright = nright + 1 #else: #print "Error in ", i return (ntotal, ntotal - nright)
def run_test (f,Kb,bitspersymbol,K,dimensionality,constellation,N0,seed): tb = gr.top_block () # TX numpy.random.seed(-seed) packet = numpy.random.randint(0,2,Kb) # create Kb random bits packet[Kb-10:Kb]=0 packet[0:Kb]=0 src = blocks.vector_source_s(packet.tolist(),False) b2s = blocks.unpacked_to_packed_ss(1,gr.GR_MSB_FIRST) # pack bits in shorts s2fsmi = blocks.packed_to_unpacked_ss(bitspersymbol,gr.GR_MSB_FIRST) # unpack shorts to symbols compatible with the FSM input cardinality enc = trellis.encoder_ss(f,0) # initial state = 0 mod = digital.chunks_to_symbols_sf(constellation,dimensionality) # CHANNEL add = blocks.add_ff() noise = analog.noise_source_f(analog.GR_GAUSSIAN,math.sqrt(N0 / 2),int(seed)) # RX va = trellis.viterbi_combined_fs(f,K,0,0,dimensionality,constellation,digital.TRELLIS_EUCLIDEAN) # Put -1 if the Initial/Final states are not set. fsmi2s = blocks.unpacked_to_packed_ss(bitspersymbol,gr.GR_MSB_FIRST) # pack FSM input symbols to shorts s2b = blocks.packed_to_unpacked_ss(1,gr.GR_MSB_FIRST) # unpack shorts to bits dst = blocks.vector_sink_s(); tb.connect (src,b2s,s2fsmi,enc,mod) tb.connect (mod,(add,0)) tb.connect (noise,(add,1)) tb.connect (add,va,fsmi2s,s2b,dst) tb.run() # A bit of cheating: run the program once and print the # final encoder state.. # Then put it as the last argument in the viterbi block #print "final state = " , enc.ST() if len(dst.data()) != len(packet): print("Error: not enough data:", len(dst.data()), len(packet)) ntotal=len(packet) nwrong = sum(abs(packet-numpy.array(dst.data()))); return (ntotal,nwrong,abs(packet-numpy.array(dst.data())))
def run_test(f, Kb, bitspersymbol, K, dimensionality, tot_constellation, N0, seed): tb = gr.top_block() # TX src = blocks.lfsr_32k_source_s() src_head = blocks.head(gr.sizeof_short, Kb / 16) # packet size in shorts s2fsmi = blocks.packed_to_unpacked_ss( bitspersymbol, gr.GR_MSB_FIRST ) # unpack shorts to symbols compatible with the FSM input cardinality enc = trellis.encoder_ss(f, 0) # initial state = 0 # essentially here we implement the combination of modulation and channel as a memoryless modulation (the memory induced by the channel is hidden in the FSM) mod = digital.chunks_to_symbols_sf(tot_constellation, dimensionality) # CHANNEL add = blocks.add_ff() noise = analog.noise_source_f(analog.GR_GAUSSIAN, math.sqrt(N0 / 2), seed) # RX metrics = trellis.metrics_f( f.O(), dimensionality, tot_constellation, digital.TRELLIS_EUCLIDEAN ) # data preprocessing to generate metrics for Viterbi va = trellis.viterbi_s( f, K, 0, -1) # Put -1 if the Initial/Final states are not set. fsmi2s = blocks.unpacked_to_packed_ss( bitspersymbol, gr.GR_MSB_FIRST) # pack FSM input symbols to shorts dst = blocks.check_lfsr_32k_s() tb.connect(src, src_head, s2fsmi, enc, mod) tb.connect(mod, (add, 0)) tb.connect(noise, (add, 1)) tb.connect(add, metrics) tb.connect(metrics, va, fsmi2s, dst) tb.run() ntotal = dst.ntotal() nright = dst.nright() runlength = dst.runlength() #print ntotal,nright,runlength return (ntotal, ntotal - nright)
def run_test(fo, fi, interleaver, Kb, bitspersymbol, K, dimensionality, constellation, Es, N0, IT, seed): tb = gr.top_block() # TX src = blocks.lfsr_32k_source_s() src_head = blocks.head(gr.sizeof_short, Kb / 16) # packet size in shorts s2fsmi = blocks.packed_to_unpacked_ss( bitspersymbol, gr.GR_MSB_FIRST ) # unpack shorts to symbols compatible with the outer FSM input cardinality enc_out = trellis.encoder_ss(fo, 0) # initial state = 0 inter = trellis.permutation(interleaver.K(), interleaver.INTER(), 1, gr.sizeof_short) enc_in = trellis.encoder_ss(fi, 0) # initial state = 0 mod = digital.chunks_to_symbols_sf(constellation, dimensionality) # CHANNEL add = blocks.add_ff() noise = analog.noise_source_f(analog.GR_GAUSSIAN, math.sqrt(N0 / 2), seed) # RX (head, tail) = make_rx(tb, fo, fi, dimensionality, constellation, K, interleaver, IT, Es, N0, trellis.TRELLIS_MIN_SUM) #(head,tail) = make_rx(tb,fo,fi,dimensionality,constellation,K,interleaver,IT,Es,N0,trellis.TRELLIS_SUM_PRODUCT) fsmi2s = blocks.unpacked_to_packed_ss( bitspersymbol, gr.GR_MSB_FIRST) # pack FSM input symbols to shorts dst = blocks.check_lfsr_32k_s() tb.connect(src, src_head, s2fsmi, enc_out, inter, enc_in, mod) tb.connect(mod, (add, 0)) tb.connect(noise, (add, 1)) tb.connect(add, head) tb.connect(tail, fsmi2s, dst) tb.run() #print enc_out.ST(), enc_in.ST() ntotal = dst.ntotal() nright = dst.nright() runlength = dst.runlength() return (ntotal, ntotal - nright)
def run_test(fo, fi, interleaver, Kb, bitspersymbol, K, dimensionality, constellation, Es, N0, IT, seed): tb = gr.top_block() # TX src = blocks.lfsr_32k_source_s() src_head = blocks.head(gr.sizeof_short, Kb / 16) # packet size in shorts s2fsmi = blocks.packed_to_unpacked_ss( bitspersymbol, gr.GR_MSB_FIRST ) # unpack shorts to symbols compatible with the outer FSM input cardinality enc = trellis.sccc_encoder_ss(fo, 0, fi, 0, interleaver, K) mod = digital.chunks_to_symbols_sf(constellation, dimensionality) # CHANNEL add = blocks.add_ff() noise = analog.noise_source_f(analog.GR_GAUSSIAN, math.sqrt(N0 / 2), seed) # RX dec = trellis.sccc_decoder_combined_fs(fo, 0, -1, fi, 0, -1, interleaver, K, IT, trellis.TRELLIS_MIN_SUM, dimensionality, constellation, digital.TRELLIS_EUCLIDEAN, 1.0) fsmi2s = blocks.unpacked_to_packed_ss( bitspersymbol, gr.GR_MSB_FIRST) # pack FSM input symbols to shorts dst = blocks.check_lfsr_32k_s() #tb.connect (src,src_head,s2fsmi,enc_out,inter,enc_in,mod) tb.connect(src, src_head, s2fsmi, enc, mod) tb.connect(mod, (add, 0)) tb.connect(noise, (add, 1)) #tb.connect (add,head) #tb.connect (tail,fsmi2s,dst) tb.connect(add, dec, fsmi2s, dst) tb.run() #print enc_out.ST(), enc_in.ST() ntotal = dst.ntotal() nright = dst.nright() runlength = dst.runlength() return (ntotal, ntotal - nright)
def run_test (fo,fi,interleaver,Kb,bitspersymbol,K,channel,modulation,dimensionality,tot_constellation,Es,N0,IT,seed): tb = gr.top_block () L = len(channel) # TX # this for loop is TOO slow in python!!! packet = [0]*(K) random.seed(seed) for i in range(len(packet)): packet[i] = random.randint(0, 2**bitspersymbol - 1) # random symbols src = blocks.vector_source_s(packet,False) enc_out = trellis.encoder_ss(fo,0) # initial state = 0 inter = trellis.permutation(interleaver.K(),interleaver.INTER(),1,gr.sizeof_short) mod = digital.chunks_to_symbols_sf(modulation[1],modulation[0]) # CHANNEL isi = filter.fir_filter_fff(1,channel) add = blocks.add_ff() noise = analog.noise_source_f(analog.GR_GAUSSIAN,math.sqrt(N0/2),seed) # RX (head,tail) = make_rx(tb,fo,fi,dimensionality,tot_constellation,K,interleaver,IT,Es,N0,trellis.TRELLIS_MIN_SUM) dst = blocks.vector_sink_s(); tb.connect (src,enc_out,inter,mod) tb.connect (mod,isi,(add,0)) tb.connect (noise,(add,1)) tb.connect (add,head) tb.connect (tail,dst) tb.run() data = dst.data() ntotal = len(data) nright=0 for i in range(ntotal): if packet[i]==data[i]: nright=nright+1 #else: #print "Error in ", i return (ntotal,ntotal-nright)
def run_test (f,Kb,bitspersymbol,K,dimensionality,constellation,N0,seed): tb = gr.top_block () # TX src = blocks.lfsr_32k_source_s() src_head = blocks.head(gr.sizeof_short,Kb/16) # packet size in shorts s2fsmi = blocks.packed_to_unpacked_ss(bitspersymbol,gr.GR_MSB_FIRST) # unpack shorts to symbols compatible with the FSM input cardinality enc = trellis.encoder_ss(f,0) # initial state = 0 mod = digital.chunks_to_symbols_sf(constellation,dimensionality) # CHANNEL add = blocks.add_ff() noise = analog.noise_source_f(analog.GR_GAUSSIAN,math.sqrt(N0/2),seed) # RX va = trellis.viterbi_combined_fs(f,K,0,-1,dimensionality,constellation,digital.TRELLIS_EUCLIDEAN) # Put -1 if the Initial/Final states are not set. fsmi2s = blocks.unpacked_to_packed_ss(bitspersymbol,gr.GR_MSB_FIRST) # pack FSM input symbols to shorts dst = blocks.check_lfsr_32k_s(); tb.connect (src,src_head,s2fsmi,enc,mod) tb.connect (mod,(add,0)) tb.connect (noise,(add,1)) tb.connect (add,va,fsmi2s,dst) tb.run() # A bit of cheating: run the program once and print the # final encoder state.. # Then put it as the last argument in the viterbi block #print "final state = " , enc.ST() ntotal = dst.ntotal () nright = dst.nright () runlength = dst.runlength () return (ntotal,ntotal-nright)
def run_test (fo,fi,interleaver,Kb,bitspersymbol,K,dimensionality,constellation,Es,N0,IT,seed): tb = gr.top_block () # TX src = blocks.lfsr_32k_source_s() src_head = blocks.head (gr.sizeof_short,Kb/16) # packet size in shorts s2fsmi = blocks.packed_to_unpacked_ss(bitspersymbol,gr.GR_MSB_FIRST) # unpack shorts to symbols compatible with the outer FSM input cardinality #src = blocks.vector_source_s([0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1],False) enc = trellis.pccc_encoder_ss(fo,0,fi,0,interleaver,K) code = blocks.vector_sink_s() mod = digital.chunks_to_symbols_sf(constellation,dimensionality) # CHANNEL add = blocks.add_ff() noise = analog.noise_source_f(analog.GR_GAUSSIAN,math.sqrt(N0/2),seed) # RX metrics_in = trellis.metrics_f(fi.O()*fo.O(),dimensionality,constellation,digital.TRELLIS_EUCLIDEAN) # data preprocessing to generate metrics for innner SISO scale = blocks.multiply_const_ff(1.0/N0) dec = trellis.pccc_decoder_s(fo,0,-1,fi,0,-1,interleaver,K,IT,trellis.TRELLIS_MIN_SUM) fsmi2s = blocks.unpacked_to_packed_ss(bitspersymbol,gr.GR_MSB_FIRST) # pack FSM input symbols to shorts dst = blocks.check_lfsr_32k_s() tb.connect (src,src_head,s2fsmi,enc,mod) #tb.connect (src,enc,mod) #tb.connect(enc,code) tb.connect (mod,(add,0)) tb.connect (noise,(add,1)) tb.connect (add,metrics_in,scale,dec,fsmi2s,dst) tb.run() #print code.data() ntotal = dst.ntotal () nright = dst.nright () runlength = dst.runlength () return (ntotal,ntotal-nright)
def run_test(fo, fi, interleaver, Kb, bitspersymbol, K, dimensionality, tot_constellation, Es, N0, IT, seed): tb = gr.top_block() # TX src = blocks.lfsr_32k_source_s() src_head = blocks.head(gr.sizeof_short, Kb / 16) # packet size in shorts s2fsmi = blocks.packed_to_unpacked_ss( bitspersymbol, gr.GR_MSB_FIRST ) # unpack shorts to symbols compatible with the iouter FSM input cardinality enc_out = trellis.encoder_ss(fo, 0) # initial state = 0 inter = trellis.permutation(interleaver.K(), interleaver.INTER(), 1, gr.sizeof_short) enc_in = trellis.encoder_ss(fi, 0) # initial state = 0 # essentially here we implement the combination of modulation and channel as a memoryless modulation (the memory induced by the channel is hidden in the innner FSM) mod = digital.chunks_to_symbols_sf(tot_constellation, dimensionality) # CHANNEL add = blocks.add_ff() noise = analog.noise_source_f(analog.GR_GAUSSIAN, math.sqrt(N0 / 2), seed) # RX (head, tail) = make_rx( tb, fo, fi, dimensionality, tot_constellation, K, interleaver, IT, Es, N0, trellis.TRELLIS_MIN_SUM ) fsmi2s = blocks.unpacked_to_packed_ss(bitspersymbol, gr.GR_MSB_FIRST) # pack FSM input symbols to shorts dst = blocks.check_lfsr_32k_s() tb.connect(src, src_head, s2fsmi, enc_out, inter, enc_in, mod) tb.connect(mod, (add, 0)) tb.connect(noise, (add, 1)) tb.connect(add, head) tb.connect(tail, fsmi2s, dst) tb.run() ntotal = dst.ntotal() nright = dst.nright() runlength = dst.runlength() # print ntotal,nright,runlength return (ntotal, ntotal - nright)
def run_test (fo,fi,interleaver,Kb,bitspersymbol,K,dimensionality,constellation,N0,seed): tb = gr.top_block () # TX src = blocks.lfsr_32k_source_s() src_head = blocks.head (gr.sizeof_short,Kb/16) # packet size in shorts s2fsmi = blocks.packed_to_unpacked_ss(bitspersymbol,gr.GR_MSB_FIRST) # unpack shorts to symbols compatible with the outer FSM input cardinality enc_out = trellis.encoder_ss(fo,0) # initial state = 0 inter = trellis.permutation(interleaver.K(),interleaver.INTER(),1,gr.sizeof_short) enc_in = trellis.encoder_ss(fi,0) # initial state = 0 mod = digital.chunks_to_symbols_sf(constellation,dimensionality) # CHANNEL add = blocks.add_ff() noise = analog.noise_source_f(analog.GR_GAUSSIAN,math.sqrt(N0/2),seed) # RX metrics_in = trellis.metrics_f(fi.O(),dimensionality,constellation,digital.TRELLIS_EUCLIDEAN) # data preprocessing to generate metrics for innner Viterbi gnd = blocks.vector_source_f([0],True); siso_in = trellis.siso_f(fi,K,0,-1,True,False,trellis.TRELLIS_MIN_SUM) # Put -1 if the Initial/Final states are not set. deinter = trellis.permutation(interleaver.K(),interleaver.DEINTER(),fi.I(),gr.sizeof_float) va_out = trellis.viterbi_s(fo,K,0,-1) # Put -1 if the Initial/Final states are not set. fsmi2s = blocks.unpacked_to_packed_ss(bitspersymbol,gr.GR_MSB_FIRST) # pack FSM input symbols to shorts dst = blocks.check_lfsr_32k_s() tb.connect (src,src_head,s2fsmi,enc_out,inter,enc_in,mod) tb.connect (mod,(add,0)) tb.connect (noise,(add,1)) tb.connect (add,metrics_in) tb.connect (gnd,(siso_in,0)) tb.connect (metrics_in,(siso_in,1)) tb.connect (siso_in,deinter,va_out,fsmi2s,dst) tb.run() ntotal = dst.ntotal () nright = dst.nright () runlength = dst.runlength () return (ntotal,ntotal-nright)
def __init__(self, seq1, seq2, factor, alpha, samp_rate, freqs): """ Description: This block is functionally equivalent to the frequency_timing_estimator block, except from the fact that each filter is matched to a sequence that can be written as the kronecker product of seq1 and seq2. Args: seq1: sequence1 of kronecker filter, which is the given training sequence. seq2: sequence2 of kronecker filter, which is the pulse for each training symbol. factor: the rise and fall factors in peak detector, which is the factor determining when a peak has started and ended. In the peak detector, an average of the signal is calculated. When the value of the signal goes over factor*average, we start looking for a peak. When the value of the signal goes below factor*average, we stop looking for a peak. factor takes values in (0,1). alpha: the smoothing factor of a moving average filter used in the peak detector takeng values in (0,1). samp_rate: the sample rate of the system, which is used in the kronecker_filter. freqs: the vector of center frequencies for each matched filter. Note that for a training sequence of length Nt, each matched filter can recover a sequence with normalized frequency offset ~ 1/(2Nt). """ gr.hier_block2.__init__(self, "freq_timing_estimator", gr.io_signature(1, 1, gr.sizeof_gr_complex*1), gr.io_signaturev(3, 3, [gr.sizeof_char*1, gr.sizeof_float*1, gr.sizeof_float*1]), ) ################################################## # Parameters ################################################## self.seq1 = seq1 self.seq2 = seq2 self.factor = factor self.alpha = alpha self.samp_rate = samp_rate self.freqs = freqs self.n = n = len(freqs) ################################################## # Blocks ################################################## self._filter=[0]*self.n self._c2mag2=[0]*self.n for i in range(self.n): self._filter[i]= cdma.kronecker_filter(seq1,seq2,samp_rate,self.freqs[i]) #self._filter[i]= filter.freq_xlating_fir_filter_ccc(1, (numpy.conjugate(self.ts[::-1])), self.freqs[i], self.samp_rate) self._c2mag2[i] = blocks.complex_to_mag_squared(1) self.blocks_max = blocks.max_ff(1) self.blocks_peak_detector = blocks.peak_detector_fb(self.factor, self.factor, 0, self.alpha) self.blocks_argmax = blocks.argmax_fs(1) self.blocks_null_sink = blocks.null_sink(gr.sizeof_short*1) self.digital_chunks_to_symbols = digital.chunks_to_symbols_sf((freqs), 1) self.blocks_sample_and_hold = blocks.sample_and_hold_ff() ################################################## # Connections ################################################## for i in range(self.n): self.connect((self, 0), (self._filter[i], 0)) self.connect((self._filter[i], 0), (self._c2mag2[i], 0)) self.connect((self._c2mag2[i], 0), (self.blocks_max, i)) self.connect((self._c2mag2[i], 0), (self.blocks_argmax, i)) self.connect((self.blocks_max, 0), (self.blocks_peak_detector, 0)) self.connect((self.blocks_peak_detector, 0), (self, 0)) self.connect((self.blocks_argmax, 0), (self.blocks_null_sink, 0)) self.connect((self.blocks_argmax, 1), (self.digital_chunks_to_symbols, 0)) self.connect((self.digital_chunks_to_symbols, 0), (self.blocks_sample_and_hold, 0)) self.connect((self.blocks_peak_detector, 0), (self.blocks_sample_and_hold, 1)) self.connect((self.blocks_sample_and_hold, 0), (self, 1)) self.connect((self.blocks_max, 0), (self, 2))
def __init__(self, seq1, seq2, factor, lookahead, alpha, freqs): """ Description: frequency timing estimator class does frequency/timing acquisition from scratch.It uses a bank of parallel correlators at each specified frequency. It then takes the max abs value of all these and passes it through a peak detector to find timing. Args: seq1: sequence1 of kronecker filter, which is the given training sequence. seq2: sequence2 of kronecker filter, which is the pulse for each training symbol. factor: the rise and fall factors in peak detector, which is the factor determining when a peak has started and ended. In the peak detector, an average of the signal is calculated. When the value of the signal goes over factor*average, we start looking for a peak. When the value of the signal goes below factor*average, we stop looking for a peak. alpha: the smoothing factor of a moving average filter used in the peak detector taking values in (0,1). freqs: the vector of normalized center frequencies for each matched filter. Note that for a training sequence of length Nt, each matched filter can recover a sequence with normalized frequency offset ~ 1/(2Nt). """ gr.hier_block2.__init__(self, "freq_timing_estimator", gr.io_signature(1, 1, gr.sizeof_gr_complex*1), gr.io_signaturev(3, 3, [gr.sizeof_char*1, gr.sizeof_float*1, gr.sizeof_float*1]), ) ################################################## # Parameters ################################################## self.seq1 = seq1 self.seq2 = seq2 self.factor = factor self.lookahead = lookahead self.alpha = alpha self.freqs = freqs self.n = n = len(freqs) self.on = 1 ################################################## # Blocks ################################################## self._filter=[0]*self.n self._c2mag2=[0]*self.n for i in range(self.n): #self._filter[i]= cdma.kronecker_filter(seq1,seq2,1,self.freqs[i]) #self._filter[i]= filter.freq_xlating_fir_filter_ccc(1, numpy.kron(seq1,seq2), self.freqs[i], 1) self._filter[i]= filter.freq_xlating_fft_filter_ccc(1, numpy.kron(seq1,seq2), self.freqs[i], 1) self._c2mag2[i] = blocks.complex_to_mag_squared(1) self.blocks_max = blocks.max_ff(1) self.blocks_peak_detector = cdma.switched_peak_detector_fb(self.factor, self.factor, self.lookahead, self.alpha, self.on) self.blocks_argmax = blocks.argmax_fs(1) self.blocks_null_sink = blocks.null_sink(gr.sizeof_short*1) self.digital_chunks_to_symbols = digital.chunks_to_symbols_sf((freqs), 1) self.blocks_sample_and_hold = blocks.sample_and_hold_ff() ################################################## # Connections ################################################## for i in range(self.n): self.connect((self, 0), (self._filter[i], 0)) self.connect((self._filter[i], 0), (self._c2mag2[i], 0)) self.connect((self._c2mag2[i], 0), (self.blocks_max, i)) self.connect((self._c2mag2[i], 0), (self.blocks_argmax, i)) self.connect((self.blocks_max, 0), (self.blocks_peak_detector, 0)) self.connect((self.blocks_peak_detector, 0), (self, 0)) self.connect((self.blocks_argmax, 0), (self.blocks_null_sink, 0)) self.connect((self.blocks_argmax, 1), (self.digital_chunks_to_symbols, 0)) self.connect((self.digital_chunks_to_symbols, 0), (self.blocks_sample_and_hold, 0)) self.connect((self.blocks_peak_detector, 0), (self.blocks_sample_and_hold, 1)) self.connect((self.blocks_sample_and_hold, 0), (self, 1)) self.connect((self.blocks_max, 0), (self, 2))
def __init__(self, ts, factor, alpha, samp_rate, freqs): """ Description: This block is designed to perform frequency and timing acquisition for a known training sequence in the presense of frequency and timing offset and noise. Its input is a complex stream. It has three outputs: 1) a stream of flags (bytes) indicating the begining of the training sequence (to be used from subsequent blocks to "chop" the incoming stream, 2) a stream with the current acquired frequency offset, and 3) a stream with the current acquired peak of the matched filter Internally, it consists of a user defined number of parallel matched filters (as many as the size of the freqs vector), each consistng of a frequency Xlating FIR filter with sample rate samp_rate, filter taps matched to the training sequence ts, and center frequency freqs[i]. The filter outputs are magnitude squared and passed through a max block and then through a peak detector. Args: ts: the training sequence. For example, in DSSS system, it's the chip-based spread training sequence. factor: the rise and fall factors in peak detector, which is the factor determining when a peak has started and ended. In the peak detector, an average of the signal is calculated. When the value of the signal goes over factor*average, we start looking for a peak. When the value of the signal goes below factor*average, we stop looking for a peak. factor takes values in (0,1). alpha: the smoothing factor of a moving average filter used in the peak detector takeng values in (0,1). samp_rate: the sample rate of the system, which is used in the freq_xlating_fir_filter. freqs: the vector of center frequencies for each matched filter. Note that for a training sequence of length Nt, each matched filter can recover a sequence with normalized frequency offset ~ 1/(2Nt). """ gr.hier_block2.__init__( self, "freq_timing_estimator", gr.io_signature(1, 1, gr.sizeof_gr_complex*1), gr.io_signaturev(3, 3, [gr.sizeof_char*1, gr.sizeof_float*1, gr.sizeof_float*1]), ) ################################################## # Parameters ################################################## self.ts = ts self.factor = factor self.alpha = alpha self.samp_rate = samp_rate self.freqs = freqs self.n = n = len(freqs) ################################################## # Blocks ################################################## self._filter=[0]*self.n self._c2mag2=[0]*self.n for i in range(self.n): self._filter[i]= filter.freq_xlating_fir_filter_ccc(1, (numpy.conjugate(self.ts[::-1])), self.freqs[i], self.samp_rate) self._c2mag2[i] = blocks.complex_to_mag_squared(1) self.blocks_max = blocks.max_ff(1) self.blocks_peak_detector = blocks.peak_detector_fb(self.factor, self.factor, 0, self.alpha) self.blocks_argmax = blocks.argmax_fs(1) self.blocks_null_sink = blocks.null_sink(gr.sizeof_short*1) self.digital_chunks_to_symbols = digital.chunks_to_symbols_sf((freqs), 1) self.blocks_sample_and_hold = blocks.sample_and_hold_ff() ################################################## # Connections ################################################## for i in range(self.n): self.connect((self, 0), (self._filter[i], 0)) self.connect((self._filter[i], 0), (self._c2mag2[i], 0)) self.connect((self._c2mag2[i], 0), (self.blocks_max, i)) self.connect((self._c2mag2[i], 0), (self.blocks_argmax, i)) self.connect((self.blocks_max, 0), (self.blocks_peak_detector, 0)) self.connect((self.blocks_peak_detector, 0), (self, 0)) self.connect((self.blocks_argmax, 0), (self.blocks_null_sink, 0)) self.connect((self.blocks_argmax, 1), (self.digital_chunks_to_symbols, 0)) self.connect((self.digital_chunks_to_symbols, 0), (self.blocks_sample_and_hold, 0)) self.connect((self.blocks_peak_detector, 0), (self.blocks_sample_and_hold, 1)) self.connect((self.blocks_sample_and_hold, 0), (self, 1)) self.connect((self.blocks_max, 0), (self, 2))
def __init__(self, seq1, seq2, factor, alpha, freqs, debug_onoff, debug_port, prefix): """ Description: frequency timing estimator class does frequency/timing acquisition from scratch.It uses a bank of parallel correlators at each specified frequency. It then takes the max abs value of all these and passes it through a peak detector to find timing. Args: seq1: sequence1 of kronecker filter, which is the given training sequence. seq2: sequence2 of kronecker filter, which is the pulse for each training symbol. factor: the rise and fall factors in peak detector, which is the factor determining when a peak has started and ended. In the peak detector, an average of the signal is calculated. When the value of the signal goes over factor*average, we start looking for a peak. When the value of the signal goes below factor*average, we stop looking for a peak. alpha: the smoothing factor of a moving average filter used in the peak detector taking values in (0,1). freqs: the vector of normalized center frequencies for each matched filter. Note that for a training sequence of length Nt, each matched filter can recover a sequence with normalized frequency offset ~ 1/(2Nt). """ gr.hier_block2.__init__(self, "freq_timing_estimator", gr.io_signature(1, 1, gr.sizeof_gr_complex*1), gr.io_signaturev(3, 3, [gr.sizeof_char*1, gr.sizeof_float*1, gr.sizeof_float*1]), ) ################################################## # Parameters ################################################## self.seq1 = seq1 self.seq2 = seq2 self.factor = factor self.alpha = alpha self.freqs = freqs self.n = n = len(freqs) self.on = 1 self.debug_onoff = debug_onoff # 1: dump ports info to file 0: no debug output self.debug_port = debug_port # 0-n_filt-1 is the output of each filter branck; n_filter is the output of maximum self.prefix = prefix ################################################## # Blocks ################################################## self._filter=[0]*self.n self._c2mag2=[0]*self.n for i in range(self.n): #self._filter[i]= cdma.kronecker_filter(seq1,seq2,1,self.freqs[i]) #self._filter[i]= filter.freq_xlating_fir_filter_ccc(1, numpy.kron(seq1,seq2), self.freqs[i], 1) self._filter[i]= filter.freq_xlating_fft_filter_ccc(1, numpy.kron(seq1,seq2), self.freqs[i], 1) self._c2mag2[i] = blocks.complex_to_mag_squared(1) self.blocks_max = blocks.max_ff(1) self.blocks_peak_detector = cdma.switched_peak_detector_fb(self.factor, self.factor, 0, self.alpha, self.on) self.blocks_argmax = blocks.argmax_fs(1) self.blocks_null_sink = blocks.null_sink(gr.sizeof_short*1) self.digital_chunks_to_symbols = digital.chunks_to_symbols_sf((freqs), 1) self.blocks_sample_and_hold = blocks.sample_and_hold_ff() if self.debug_onoff == True: num_of_file_sinks = len(self.debug_port) self._filesink = [0]*num_of_file_sinks for i in range(num_of_file_sinks): if self.debug_port[i] == self.n: filename = prefix+"max.dat" else: filename = prefix+"filter"+str(i)+".dat" print filename self._filesink[i] = blocks.file_sink(gr.sizeof_float*1, filename, False) self._filesink[i].set_unbuffered(False) # this is the block for bundling the outputs of each branch of filters and the input of peak detector ################################################## # Connections ################################################## for i in range(self.n): self.connect((self, 0), (self._filter[i], 0)) self.connect((self._filter[i], 0), (self._c2mag2[i], 0)) self.connect((self._c2mag2[i], 0), (self.blocks_max, i)) self.connect((self._c2mag2[i], 0), (self.blocks_argmax, i)) self.connect((self.blocks_max, 0), (self.blocks_peak_detector, 0)) self.connect((self.blocks_peak_detector, 0), (self, 0)) self.connect((self.blocks_argmax, 0), (self.blocks_null_sink, 0)) self.connect((self.blocks_argmax, 1), (self.digital_chunks_to_symbols, 0)) self.connect((self.digital_chunks_to_symbols, 0), (self.blocks_sample_and_hold, 0)) self.connect((self.blocks_peak_detector, 0), (self.blocks_sample_and_hold, 1)) self.connect((self.blocks_sample_and_hold, 0), (self, 1)) self.connect((self.blocks_max, 0), (self, 2)) if self.debug_onoff == True: for i in range(num_of_file_sinks): port_index = self.debug_port[i] if port_index == self.n: self.connect((self.blocks_max, 0), (self._filesink[i], 0)) else: self.connect((self._c2mag2[port_index], 0), (self._filesink[i], 0))
def __init__(self, seq1, seq2, factor, alpha, freqs): """ Description: frequency timing estimator class does frequency/timing acquisition from scratch.It uses a bank of parallel correlators at each specified frequency. It then takes the max abs value of all these and passes it through a peak detector to find timing. Args: seq1: sequence1 of kronecker filter, which is the given training sequence. seq2: sequence2 of kronecker filter, which is the pulse for each training symbol. factor: the rise and fall factors in peak detector, which is the factor determining when a peak has started and ended. In the peak detector, an average of the signal is calculated. When the value of the signal goes over factor*average, we start looking for a peak. When the value of the signal goes below factor*average, we stop looking for a peak. alpha: the smoothing factor of a moving average filter used in the peak detector taking values in (0,1). freqs: the vector of normalized center frequencies for each matched filter. Note that for a training sequence of length Nt, each matched filter can recover a sequence with normalized frequency offset ~ 1/(2Nt). """ gr.hier_block2.__init__( self, "freq_timing_estimator", gr.io_signature(1, 1, gr.sizeof_gr_complex * 1), gr.io_signaturev( 3, 3, [gr.sizeof_char * 1, gr.sizeof_float * 1, gr.sizeof_float * 1 ]), ) ################################################## # Parameters ################################################## self.seq1 = seq1 self.seq2 = seq2 self.factor = factor self.alpha = alpha self.freqs = freqs self.n = n = len(freqs) self.on = 1 ################################################## # Blocks ################################################## self._filter = [0] * self.n self._c2mag2 = [0] * self.n for i in range(self.n): #self._filter[i]= cdma.kronecker_filter(seq1,seq2,1,self.freqs[i]) #self._filter[i]= filter.freq_xlating_fir_filter_ccc(1, numpy.kron(seq1,seq2), self.freqs[i], 1) self._filter[i] = filter.freq_xlating_fft_filter_ccc( 1, numpy.kron(seq1, seq2), self.freqs[i], 1) self._c2mag2[i] = blocks.complex_to_mag_squared(1) self.blocks_max = blocks.max_ff(1) self.blocks_peak_detector = cdma.switched_peak_detector_fb( self.factor, self.factor, 0, self.alpha, self.on) self.blocks_argmax = blocks.argmax_fs(1) self.blocks_null_sink = blocks.null_sink(gr.sizeof_short * 1) self.digital_chunks_to_symbols = digital.chunks_to_symbols_sf((freqs), 1) self.blocks_sample_and_hold = blocks.sample_and_hold_ff() ################################################## # Connections ################################################## for i in range(self.n): self.connect((self, 0), (self._filter[i], 0)) self.connect((self._filter[i], 0), (self._c2mag2[i], 0)) self.connect((self._c2mag2[i], 0), (self.blocks_max, i)) self.connect((self._c2mag2[i], 0), (self.blocks_argmax, i)) self.connect((self.blocks_max, 0), (self.blocks_peak_detector, 0)) self.connect((self.blocks_peak_detector, 0), (self, 0)) self.connect((self.blocks_argmax, 0), (self.blocks_null_sink, 0)) self.connect((self.blocks_argmax, 1), (self.digital_chunks_to_symbols, 0)) self.connect((self.digital_chunks_to_symbols, 0), (self.blocks_sample_and_hold, 0)) self.connect((self.blocks_peak_detector, 0), (self.blocks_sample_and_hold, 1)) self.connect((self.blocks_sample_and_hold, 0), (self, 1)) self.connect((self.blocks_max, 0), (self, 2))