def test_003_crc_equal_unpacked(self): """ Test unpacked operation with packed operation """ data = range(35) src = blocks.vector_source_b(data) unpack1 = blocks.repack_bits_bb(8, 1, self.tsb_key, False, gr.GR_LSB_FIRST) unpack2 = blocks.repack_bits_bb(8, 1, self.tsb_key, False, gr.GR_LSB_FIRST) crc_unpacked = digital.crc32_bb(False, self.tsb_key, False) crc_packed = digital.crc32_bb(False, self.tsb_key, True) sink1 = blocks.tsb_vector_sink_b(tsb_key=self.tsb_key) sink2 = blocks.tsb_vector_sink_b(tsb_key=self.tsb_key) self.tb.connect( src, blocks.stream_to_tagged_stream(gr.sizeof_char, 1, len(data), self.tsb_key), crc_packed, unpack1, sink1) self.tb.connect( src, blocks.stream_to_tagged_stream(gr.sizeof_char, 1, len(data), self.tsb_key), unpack2, crc_unpacked, sink2) self.tb.run() self.assertEqual(sink1.data(), sink2.data())
def test_preserve_tag_head_pos(self): """ Test the 'preserve head position' function. This will add a 'special' tag to item 0 on stream 1. It should be on item 0 of the output stream. """ special_tag = gr.tag_t() special_tag.key = pmt.string_to_symbol('spam') special_tag.offset = 0 special_tag.value = pmt.to_pmt('eggs') len_tag_key = "length" packet_len_1 = 5 packet_len_2 = 3 mux = blocks.tagged_stream_mux(gr.sizeof_float, len_tag_key, 1) sink = blocks.vector_sink_f() self.tb.connect( blocks.vector_source_f(range(packet_len_1)), blocks.stream_to_tagged_stream(gr.sizeof_float, 1, packet_len_1, len_tag_key), (mux, 0) ) self.tb.connect( blocks.vector_source_f(range(packet_len_2), False, 1, (special_tag,)), blocks.stream_to_tagged_stream(gr.sizeof_float, 1, packet_len_2, len_tag_key), (mux, 1) ) self.tb.connect(mux, sink) self.tb.run() self.assertEqual(sink.data(), tuple(range(packet_len_1) + range(packet_len_2))) tags = [gr.tag_to_python(x) for x in sink.tags()] tags = sorted([(x.offset, x.key, x.value) for x in tags]) tags_expected = [ (0, 'length', packet_len_1 + packet_len_2), (0, 'spam', 'eggs'), ] self.assertEqual(tags, tags_expected)
def test_preserve_tag_head_pos(self): """ Test the 'preserve head position' function. This will add a 'special' tag to item 0 on stream 1. It should be on item 0 of the output stream. """ packet_len_0 = 5 data0 = range(packet_len_0) packet_len_1 = 3 data1 = range(packet_len_1) mux = blocks.tagged_stream_mux( gr.sizeof_float, self.tsb_key, 1 # Mark port 1 as carrying special tags on the head position ) sink = blocks.tsb_vector_sink_f(tsb_key=self.tsb_key) self.tb.connect( blocks.vector_source_f(data0), blocks.stream_to_tagged_stream(gr.sizeof_float, 1, packet_len_0, self.tsb_key), (mux, 0) ) self.tb.connect( blocks.vector_source_f(range(packet_len_1), tags=(make_tag('spam', 'eggs', 0),)), blocks.stream_to_tagged_stream(gr.sizeof_float, 1, packet_len_1, self.tsb_key), (mux, 1) ) self.tb.connect(mux, sink) self.tb.run() self.assertEqual(len(sink.data()), 1) self.assertEqual(sink.data()[0], tuple(data0 + data1)) self.assertEqual(len(sink.tags()), 1) tag = gr.tag_to_python(sink.tags()[0]) tag = (tag.offset, tag.key, tag.value) tag_expected = (0, 'spam', 'eggs') self.assertEqual(tag, tag_expected)
def help_stream_tag_propagation(self, N, stream_sizes): src_data1 = stream_sizes[0]*N*[1,] src_data2 = stream_sizes[1]*N*[2,] src_data3 = stream_sizes[2]*N*[3,] # stream_mux scheme (3,2,4) src1 = blocks.vector_source_f(src_data1) src2 = blocks.vector_source_f(src_data2) src3 = blocks.vector_source_f(src_data3) tag_stream1 = blocks.stream_to_tagged_stream(gr.sizeof_float, 1, stream_sizes[0], 'src1') tag_stream2 = blocks.stream_to_tagged_stream(gr.sizeof_float, 1, stream_sizes[1], 'src2') tag_stream3 = blocks.stream_to_tagged_stream(gr.sizeof_float, 1, stream_sizes[2], 'src3') mux = blocks.stream_mux(gr.sizeof_float, stream_sizes) dst = blocks.vector_sink_f() self.tb.connect(src1, tag_stream1) self.tb.connect(src2, tag_stream2) self.tb.connect(src3, tag_stream3) self.tb.connect(tag_stream1, (mux,0)) self.tb.connect(tag_stream2, (mux,1)) self.tb.connect(tag_stream3, (mux,2)) self.tb.connect(mux, dst) self.tb.run() return (dst.data (), dst.tags ())
def help_stream_tag_propagation(self, N, stream_sizes): src_data = (stream_sizes[0] * [ 1, ] + stream_sizes[1] * [ 2, ] + stream_sizes[2] * [ 3, ]) * N src = blocks.vector_source_f(src_data, False) tag_stream1 = blocks.stream_to_tagged_stream(gr.sizeof_float, 1, stream_sizes[0], 'src1') tag_stream2 = blocks.stream_to_tagged_stream(gr.sizeof_float, 1, stream_sizes[1], 'src2') tag_stream3 = blocks.stream_to_tagged_stream(gr.sizeof_float, 1, stream_sizes[2], 'src3') demux = blocks.stream_demux(gr.sizeof_float, stream_sizes) dst0 = blocks.vector_sink_f() dst1 = blocks.vector_sink_f() dst2 = blocks.vector_sink_f() self.tb.connect(src, tag_stream1) self.tb.connect(tag_stream1, tag_stream2) self.tb.connect(tag_stream2, tag_stream3) self.tb.connect(tag_stream3, demux) self.tb.connect((demux, 0), dst0) self.tb.connect((demux, 1), dst1) self.tb.connect((demux, 2), dst2) self.tb.run() return (dst0, dst1, dst2)
def test_preserve_tag_head_pos(self): """ Test the 'preserve head position' function. This will add a 'special' tag to item 0 on stream 1. It should be on item 0 of the output stream. """ packet_len_0 = 5 data0 = list(range(packet_len_0)) packet_len_1 = 3 data1 = list(range(packet_len_1)) mux = blocks.tagged_stream_mux( gr.sizeof_float, self.tsb_key, 1 # Mark port 1 as carrying special tags on the head position ) sink = blocks.tsb_vector_sink_f(tsb_key=self.tsb_key) self.tb.connect( blocks.vector_source_f(data0), blocks.stream_to_tagged_stream(gr.sizeof_float, 1, packet_len_0, self.tsb_key), (mux, 0)) self.tb.connect( blocks.vector_source_f(list(range(packet_len_1)), tags=(make_tag('spam', 'eggs', 0), )), blocks.stream_to_tagged_stream(gr.sizeof_float, 1, packet_len_1, self.tsb_key), (mux, 1)) self.tb.connect(mux, sink) self.tb.run() self.assertEqual(len(sink.data()), 1) self.assertEqual(sink.data()[0], tuple(data0 + data1)) self.assertEqual(len(sink.tags()), 1) tag = gr.tag_to_python(sink.tags()[0]) tag = (tag.offset, tag.key, tag.value) tag_expected = (0, 'spam', 'eggs') self.assertEqual(tag, tag_expected)
def help_stream_tag_propagation(self, N, stream_sizes): src_data1 = stream_sizes[0] * N * [ 1, ] src_data2 = stream_sizes[1] * N * [ 2, ] src_data3 = stream_sizes[2] * N * [ 3, ] # stream_mux scheme (3,2,4) src1 = blocks.vector_source_f(src_data1) src2 = blocks.vector_source_f(src_data2) src3 = blocks.vector_source_f(src_data3) tag_stream1 = blocks.stream_to_tagged_stream(gr.sizeof_float, 1, stream_sizes[0], 'src1') tag_stream2 = blocks.stream_to_tagged_stream(gr.sizeof_float, 1, stream_sizes[1], 'src2') tag_stream3 = blocks.stream_to_tagged_stream(gr.sizeof_float, 1, stream_sizes[2], 'src3') mux = blocks.stream_mux(gr.sizeof_float, stream_sizes) dst = blocks.vector_sink_f() self.tb.connect(src1, tag_stream1) self.tb.connect(src2, tag_stream2) self.tb.connect(src3, tag_stream3) self.tb.connect(tag_stream1, (mux, 0)) self.tb.connect(tag_stream2, (mux, 1)) self.tb.connect(tag_stream3, (mux, 2)) self.tb.connect(mux, dst) self.tb.run() return (dst.data(), dst.tags())
def test_002_t (self): #print "TEST: discarded carriers and num sync words" # set up fg test_len = 200 vlen = 20 ts_len = test_len/vlen discarded_carriers = (-8,-4,-2,1,2,3,9) num_sync_words = 2 in_data0 = [0]*test_len in_data1 = [0]*test_len for k in range(test_len): in_data0[k] = complex(k-4, k+1) in_data1[k] = complex(k+3, k-2) src0 = blocks.vector_source_c(in_data0) s2v0 = blocks.stream_to_vector(8,vlen) s2ts0 = blocks.stream_to_tagged_stream(8,vlen,ts_len,'packet_len') src1 = blocks.vector_source_c(in_data1) s2v1 = blocks.stream_to_vector(8,vlen) s2ts1 = blocks.stream_to_tagged_stream(8,vlen,ts_len,'packet_len') div = radar.ofdm_divide_vcvc(vlen,vlen,discarded_carriers,num_sync_words) v2s = blocks.vector_to_stream(8,vlen) snk = blocks.vector_sink_c() self.tb.connect(src0,s2v0,s2ts0) self.tb.connect(src1,s2v1,s2ts1) self.tb.connect((s2ts0,0),(div,0)) self.tb.connect((s2ts1,0),(div,1)) self.tb.connect(div,v2s,snk) self.tb.run () # get ref data discarded_carriers_shift = [0]*len(discarded_carriers) for k in range(len(discarded_carriers)): discarded_carriers_shift[k] = discarded_carriers[k] + vlen/2 ref_data = [0]*test_len for k in range(test_len/vlen): for l in range(vlen): if k < num_sync_words: # do not process sync words with discarded carriers ref_data[vlen*k+l] = in_data0[vlen*k+l]/in_data1[vlen*k+l] else: # process discarded carriers if l in discarded_carriers_shift: # if actual item shall be discarded ref_data[vlen*k+l] = 0 else: # if actual item shall NOT be discarded ref_data[vlen*k+l] = in_data0[vlen*k+l]/in_data1[vlen*k+l] # check data #print "REF" #print ref_data out_data = snk.data() #print "DATA" #print out_data for k in range(len(out_data)): self.assertAlmostEqual(ref_data[k], out_data[k],4)
def test_001_t(self): # set up fg qa_seed = 0.8 qa_seq_len = 2 qa_constell_tab = (-1 + 0j, 1 + 0j) #qa_para_tab=(3.98,3.6) qa_para_tab_chebyshev = (1.4, 1.8) data = [-1 + 0j, 1 + 0j, -1 + 0j, 1 + 0j] src = blocks.vector_source_c(data) tagged_stream = blocks.stream_to_tagged_stream(gr.sizeof_gr_complex, 1, 2, 'len_streamtag') dst = blocks.vector_sink_f() CPM = chaos.CPM_cf(qa_seed, qa_seq_len, qa_constell_tab, qa_para_tab_chebyshev) #expected_data=(0.6368, 0.9205173254013062,0.5760, 0.8792, #0.6368, 0.9205173254013062,0.5760, 0.8792) expected_data_chebyshev = (0.6209, 0.3046, 0.4009, -0.4918, 0.6209, 0.3046, 0.4009, -0.4918) expected_tags = [('len_streamtag', 4, 0L), ('len_streamtag', 4, 4L)] self.tb.connect(src, tagged_stream, CPM, dst) self.tb.run() result_data = dst.data() result_tags = dst.tags() #print('data tags='+str(len(result_tags))) result_tags = [gr.tag_to_python(x) for x in result_tags] result_tags = sorted([(x.key, x.value, x.offset) for x in result_tags]) # check data #self.assertFloatTuplesAlmostEqual(expected_data_chebyshev, result_data,4) self.assertEqual(result_tags, expected_tags)
def test_001_t (self): # set up fg test_len = 1000 fft_len = 100; cp_len = fft_len/4 in_data = range(0,fft_len)*(test_len/fft_len); src = blocks.vector_source_c(in_data) s2v = blocks.stream_to_vector(8,fft_len) s2ts = blocks.stream_to_tagged_stream(8,fft_len,test_len/fft_len,'packet_len') cp_add = digital.ofdm_cyclic_prefixer(fft_len,fft_len+cp_len,0,'packet_len') cp_remove = radar.ofdm_cyclic_prefix_remover_cvc(fft_len,cp_len,'packet_len') v2s = blocks.vector_to_stream(8,fft_len) snk = blocks.vector_sink_c() self.tb.connect(src,s2v,s2ts,cp_add,cp_remove,v2s,snk) self.tb.run () # check data out_data = snk.data() out_data_real = [0]*len(out_data) for k in range(len(out_data)): out_data_real[k] = out_data[k].real for k in range(len(in_data)): self.assertEqual(in_data[k],out_data_real[k])
def __init__( self, n_written_samples, constellation_obj, #pre_diff_code, samples_per_symbol, excess_bw, burst_len, zero_pad_len, linear_gain=1.0, frequency_offset=0.0): super(GeneralModFlowgraph, self).__init__() # params self.n_written_samples = int(n_written_samples) self.n_offset_samples = int( np.random.randint(0, self.n_written_samples)) self.constellation_obj = constellation_obj self.samples_per_symbol = samples_per_symbol #TODO self.excess_bw = excess_bw # TODO self.linear_gain = float(linear_gain) self.burst_len = burst_len # TODO: make burst_len also variable randgen = random_generator.load_param(zero_pad_len) # if isinstance(zero_pad_len,tuple): # self.zero_pad_len = zero_pad_len[1] # self.pad_dist = zero_pad_len[0] # else: # self.zero_pad_len = [zero_pad_len] # self.pad_dist = 'constant' if isinstance(frequency_offset, tuple): assert frequency_offset[0] == 'uniform' self.frequency_offset = frequency_offset[1] else: # it is just a value self.frequency_offset = [frequency_offset] # print 'This is the frequency offset:',frequency_offset # self.burst_len = burst_len if not issubclass(burst_len.__class__,ts.ValueGenerator) else burst_len.generate() # self.zero_pad_len = zero_pad_len if not issubclass(zero_pad_len.__class__,ts.ValueGenerator) else zero_pad_len.generate() data2send = np.random.randint(0, 256, 1000) # phy self.data_gen = blocks.vector_source_b(data2send, True) self.mod = digital.generic_mod( self.constellation_obj, samples_per_symbol=self.samples_per_symbol, #self.pre_diff_code, excess_bw=self.excess_bw) self.tagger = blocks.stream_to_tagged_stream(gr.sizeof_gr_complex, 1, self.burst_len, "packet_len") # self.burst_shaper = digital.burst_shaper_cc((1+0*1j,),100,self.zero_pad_len,False) self.burst_shaper = specmonitor.random_burst_shaper_cc( randgen.dynrandom(), 0, self.frequency_offset, "packet_len") self.skiphead = blocks.skiphead(gr.sizeof_gr_complex, self.n_offset_samples) self.head = blocks.head(gr.sizeof_gr_complex, self.n_written_samples) self.dst = blocks.vector_sink_c() # dst = blocks.file_sink(gr.sizeof_gr_complex,args['targetfolder']+'/tmp.bin') self.setup_flowgraph()
def check_frame(self, payload, access_code, enable_crc): ## # Print Info print "----------------------------------------------------------------------" print "Testing with:" print "Payload: ", payload print "Access code: ", access_code print "CRC enabled: ", enable_crc ## # Set up Blocks src = blocks.vector_source_b(payload) to_tagged_stream = blocks.stream_to_tagged_stream(gr.sizeof_char, 1, len(payload), "packet_len") encoder = frame_encoder("packet_len", enable_crc) repacker = blocks.repack_bits_bb(8, 1, "", False, gr.GR_MSB_FIRST) decoder = frame_decoder("frame_len", 48000, enable_crc) dst = blocks.vector_sink_b() ## # Connect Blocks self.tb.connect(src,to_tagged_stream) self.tb.connect(to_tagged_stream, encoder) self.tb.connect(encoder, repacker) self.tb.connect(repacker, decoder) self.tb.connect(decoder, dst) self.tb.run () ## # Check, if received payload matches original payload self.assertEqual(payload, dst.data(), "Received Payload does not match with original") print "Frame correct."
def test_001_t(self): cipher_name = "aes-256-cbc" plainlen = 1600 packet_len = 1600 key = bytearray(numpy.random.randint(0, 256, 32).tolist()) plain = bytearray(numpy.random.randint(0, 256, plainlen).tolist()) cipher_desc = crypto.sym_ciph_desc(cipher_name, False, key) src = blocks.vector_source_b(plain) stts = blocks.stream_to_tagged_stream(1, 1, packet_len, "packet_len") tstpdu = blocks.tagged_stream_to_pdu(blocks.byte_t, "packet_len") enc = crypto.sym_enc(cipher_desc) dec = crypto.sym_dec(cipher_desc) snk = blocks.message_debug() self.tb.connect(src, stts, tstpdu) self.tb.msg_connect(tstpdu, "pdus", enc, "pdus") self.tb.msg_connect(enc, "pdus", dec, "pdus") self.tb.msg_connect(dec, "pdus", snk, "store") self.tb.run() num_msg = snk.num_messages() decrypted = bytearray( pmt.u8vector_elements(pmt.cdr((snk.get_message(0))))) self.assertEqual(plain, decrypted)
def __init__(self): gr.hier_block2.__init__(self, "ofdm_tx", gr.io_signature(1, 1, gr.sizeof_char), gr.io_signature(1, 1, gr.sizeof_gr_complex)) self.sequencr = aux.insert_sequence_numbers_bb() self.encoder = aux.encoder_reed_solomon_bb() self.framer = blocks.stream_to_tagged_stream( gr.sizeof_char, 1, config.get_bytes_per_frame(), LEN_TAG_KEY) #divide the incoming data streams in frames self.connect(self, self.sequencr, self.encoder, self.framer) #We can do some byte scrambling if needed (variable modulus modulation ) self.unpacker_data_stream = blocks.repack_bits_bb( #modulation of the data (split the bytes, and modulate the split bytes 8, config.get_bits_per_symbol(), LEN_TAG_KEY) self.modulator_data_stream = digital.chunks_to_symbols_bc( config.get_constellation().points()) self.connect(self.framer, self.unpacker_data_stream, self.modulator_data_stream) self.allocator = digital.ofdm_carrier_allocator_cvc( config.get_fft_length(), occupied_carriers=config.get_data_tones(), pilot_carriers=config.get_pilot_tones(), pilot_symbols=config.get_pilot_symbols(), sync_words=config.get_preambles(), len_tag_key=LEN_TAG_KEY) self.connect(self.modulator_data_stream, self.allocator) self.fft_block = fft.fft_vcc(config.get_fft_length(), False, (), True) self.connect(self.allocator, self.fft_block) self.prefixer = digital.ofdm_cyclic_prefixer( config.get_fft_length(), config.get_cp_length() + config.get_fft_length(), 0, LEN_TAG_KEY) self.burst_handler = aux.add_zeros_cc( 14, 104) #digital.burst_shaper_cc([],1040,1040,False, LEN_TAG_KEY) self.connect(self.fft_block, self.prefixer, self.burst_handler) self.connect(self.burst_handler, self)
def test_001b_simple_skip_nothing(self): """ Same as before, but put a skip-header in there """ fft_len = 8 equalizer = digital.ofdm_equalizer_static(fft_len, symbols_skipped=1) n_syms = 3 tx_data = [ 1, ] * fft_len * n_syms chan_tag = gr.tag_t() chan_tag.offset = 0 chan_tag.key = pmt.string_to_symbol("ofdm_sync_chan_taps") chan_tag.value = pmt.init_c32vector(fft_len, (1, ) * fft_len) src = blocks.vector_source_c(tx_data, False, fft_len, (chan_tag, )) eq = digital.ofdm_frame_equalizer_vcvc(equalizer.base(), 0, self.tsb_key) sink = blocks.tsb_vector_sink_c(fft_len, tsb_key=self.tsb_key) self.tb.connect( src, blocks.stream_to_tagged_stream(gr.sizeof_gr_complex, fft_len, n_syms, self.tsb_key), eq, sink) self.tb.run() # Check data self.assertEqual(tx_data, sink.data()[0])
def test_004( self ): # insert tags and check their propagation, non-zero offset, multiple tags per copy region period = 11000 offset = 1000 packetlen = 2000 insert = [ 1.0, ] * 1000 src = blocks.null_source(gr.sizeof_float) s2ts = blocks.stream_to_tagged_stream(gr.sizeof_float, 1, packetlen, "packet") head = blocks.head(gr.sizeof_float, 1000000) ins = blocks.vector_insert_f(insert, period, offset) dst = blocks.vector_sink_f() self.tb.connect(src, s2ts, head, ins, dst) self.tb.run() expected_result = (0, 2000, 4000, 6000, 8000, 11000, 13000, 15000, 17000, 19000, 22000, 24000, 26000) tags = dst.tags() offsets = [tag.offset for tag in tags] for i in range(len(expected_result)): self.assertTrue(expected_result[i] == offsets[i])
def test_002_t (self): # set up fg in_data = range(20) vlen_in = 5 crop_x = (2,4) crop_y = (1,4) src = blocks.vector_source_c(in_data) s2v = blocks.stream_to_vector(8,vlen_in) s2ts = blocks.stream_to_tagged_stream(8,vlen_in,len(in_data)/vlen_in,'packet_len') crop = radar.crop_matrix_vcvc(vlen_in,crop_x,crop_y) v2s = blocks.vector_to_stream(8,crop_x[1]-crop_x[0]) snk = blocks.vector_sink_c() debug = blocks.tag_debug(8,'debug') self.tb.connect(src,s2v,s2ts,crop,v2s,snk) self.tb.connect(v2s,debug) self.tb.run () # check data ref_data = (7,8,12,13,17,18) out_data = snk.data() print("INDATA:", in_data) print("REF: ", ref_data) print("OUTPUT:", out_data) for k in range(len(out_data)): self.assertEqual(out_data[k].real,ref_data[k])
def test_001_t(self): cipher_name = "aes-128-cbc" key = bytearray("aaaaaaaaaaaaaaaa") plain = bytearray("testesttesttestt") print "Key: \t{0}, hex: \t{1}".format(key, binascii.hexlify(key)) print "plain: \t{0}, hex: \t{1}".format(plain, binascii.hexlify(plain)) cipher_desc = crypto.sym_ciph_desc(cipher_name, False, key) src = blocks.vector_source_b(plain) tagger = blocks.stream_to_tagged_stream(1, 1, 16, "packet_len") enc = crypto.sym_enc_tagged_bb(cipher_desc, "packet_len") snk_enc = blocks.vector_sink_b() dec = crypto.sym_dec_tagged_bb(cipher_desc, "packet_len") snk = blocks.vector_sink_b() self.tb.connect(src, tagger, enc, dec, snk) self.tb.connect(enc, snk_enc) self.tb.run() encrypted = bytearray(snk_enc.data()) decrypted = bytearray(snk.data()) print "Encrypted hex: {0}".format(binascii.hexlify(encrypted)) print "Decrypted: \t{0}, hex: \t{1}".format( decrypted, binascii.hexlify(decrypted)) self.assertEqual(plain, decrypted)
def test_002_t(self): nsubcarrier = 16 ntimeslots = 64 sync_data = np.array([ np.complex(np.random.choice([-1, 1]), np.random.choice([-1, 1])) for i in xrange(nsubcarrier) ]) src_data = np.array([ np.complex(np.random.choice([-1, 1]), np.random.choice([-1, 1])) for i in xrange(nsubcarrier * ntimeslots) ]) src = blocks.vector_source_c(src_data, vlen=1) stts = blocks.stream_to_tagged_stream(gr.sizeof_gr_complex, vlen=1, packet_len=nsubcarrier * ntimeslots, len_tag_key="frame_len") fr = gfdms.framer_cc(nsubcarrier, ntimeslots, True, sync_data, "frame_len") dst = blocks.vector_sink_c(vlen=1) expected_result = np.concatenate( (mod.reshape_input(np.tile(sync_data, 2), 2, nsubcarrier), mod.reshape_input(src_data, ntimeslots, nsubcarrier))) self.tb.connect(src, stts) self.tb.connect(stts, fr) self.tb.connect(fr, dst) self.tb.run() result_data = dst.data() tags = dst.tags() for tag in tags: ptag = gr.tag_to_python(tag) pprint(vars(ptag)) self.assertComplexTuplesAlmostEqual( expected_result, result_data[0:nsubcarrier * ntimeslots + 2 * nsubcarrier], 6)
def test_002_with_offset (self): """ Standard test, carrier offset """ fft_len = 16 tx_symbols = list(range(1, 16)); tx_symbols = (0, 0, 1, 1j, 2, 3, 0, 0, 0, 0, 0, 0, 4, 5, 2j, 6, 0, 0, 7, 8, 3j, 9, 0, 0, 0, 0, 0, 0, 10, 4j, 11, 12, 0, 0, 13, 1j, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 2j, 0) carr_offset = 1 # Compare this with tx_symbols from the previous test expected_result = tuple(range(1, 16)) + (0, 0, 0) occupied_carriers = ((1, 3, 4, 11, 12, 14), (1, 2, 4, 11, 13, 14),) n_syms = len(tx_symbols) // fft_len offsettag = gr.tag_t() offsettag.offset = 0 offsettag.key = pmt.string_to_symbol("ofdm_sync_carr_offset") offsettag.value = pmt.from_long(carr_offset) src = blocks.vector_source_c(tx_symbols, False, fft_len, (offsettag,)) sink = blocks.tsb_vector_sink_c(tsb_key=self.tsb_key) serializer = digital.ofdm_serializer_vcc( fft_len, occupied_carriers, self.tsb_key, "", 0, "ofdm_sync_carr_offset", False ) self.tb.connect(src, blocks.stream_to_tagged_stream(gr.sizeof_gr_complex, fft_len, n_syms, self.tsb_key), serializer, sink) self.tb.run () self.assertEqual(sink.data()[0], expected_result) self.assertEqual(len(sink.tags()), 1)
def test_0010_tag_propagation(self): """ Make sure tags on the CRC aren't lost. """ # Data with precalculated CRC data = (0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1) testtag = gr.tag_t() testtag.offset = len(data) - 1 testtag.key = pmt.string_to_symbol('tag1') testtag.value = pmt.from_long(0) src = blocks.vector_source_b(data, False, 1, (testtag, )) crc_check = digital.crc32_bb(True, self.tsb_key, False) sink = blocks.tsb_vector_sink_b(tsb_key=self.tsb_key) self.tb.connect(src, blocks.stream_to_tagged_stream( gr.sizeof_char, 1, len(data), self.tsb_key), crc_check, sink) self.tb.run() self.assertEqual([len(data) - 33, ], [ tag.offset for tag in sink.tags() if pmt.symbol_to_string(tag.key) == 'tag1' ])
def test_001_t (self): # test on positive frequencies # set up fg test_len = 1000 samp_rate = 2000 freq = 200 ampl = 1 packet_len = test_len threshold = -100 samp_protect = 2 src = analog.sig_source_c(samp_rate, analog.GR_COS_WAVE, freq, ampl) head = blocks.head(8,test_len) s2ts = blocks.stream_to_tagged_stream(8,1,packet_len,"packet_len") fft = radar.ts_fft_cc(packet_len) peak = radar.find_max_peak_c(samp_rate, threshold, samp_protect, (0,0), False) debug = blocks.message_debug() self.tb.connect(src,head,s2ts,fft,peak) self.tb.msg_connect(peak,"Msg out",debug,"store") #self.tb.msg_connect(peak,"Msg out",debug,"print") self.tb.start() sleep(0.5) self.tb.stop() self.tb.wait() # check frequency in os_cfar message with given one msg = debug.get_message(0) self.assertAlmostEqual(freq,pmt.f32vector_ref(pmt.nth(1,pmt.nth(1,msg)),0),8)
def test_002_tags_plus_data(self): packet_len = 16 src_data = range(packet_len) tag1 = gr.tag_t() tag1.offset = 0 tag1.key = pmt.string_to_symbol('spam') tag1.value = pmt.from_long(23) tag2 = gr.tag_t() tag2.offset = 10 # Must be < packet_len tag2.key = pmt.string_to_symbol('eggs') tag2.value = pmt.from_long(42) src = blocks.vector_source_f(src_data, tags=(tag1, tag2)) s2ts = blocks.stream_to_tagged_stream(gr.sizeof_float, vlen=1, packet_len=packet_len, len_tag_key="packet_len") ts2pdu = blocks.tagged_stream_to_pdu(blocks.float_t, "packet_len") dbg = blocks.message_debug() self.tb.connect(src, s2ts, ts2pdu) self.tb.msg_connect(ts2pdu, "pdus", dbg, "store") self.tb.start() while dbg.num_messages() < 1: time.sleep(0.1) self.tb.stop() self.tb.wait() result_msg = dbg.get_message(0) metadata = pmt.to_python(pmt.car(result_msg)) vector = pmt.f32vector_elements(pmt.cdr(result_msg)) self.assertEqual(metadata, {'eggs': 42, 'spam': 23}) self.assertFloatTuplesAlmostEqual(tuple(vector), src_data)
def test_001_t (self): cipher_name = "aes-128-cbc" key = bytearray("aaaaaaaaaaaaaaaa") plain=bytearray("testesttesttestt") print "Key: \t{0}, hex: \t{1}".format(key,binascii.hexlify(key)) print "plain: \t{0}, hex: \t{1}".format(plain,binascii.hexlify(plain)) cipher_desc = crypto.sym_ciph_desc(cipher_name, False, key) src = blocks.vector_source_b(plain) tagger = blocks.stream_to_tagged_stream(1, 1, 16, "packet_len") enc = crypto.sym_enc_tagged_bb(cipher_desc, "packet_len") snk_enc = blocks.vector_sink_b() dec = crypto.sym_dec_tagged_bb(cipher_desc, "packet_len") snk = blocks.vector_sink_b() self.tb.connect(src, tagger, enc, dec, snk) self.tb.connect(enc, snk_enc) self.tb.run() encrypted = bytearray(snk_enc.data()) decrypted = bytearray(snk.data()) print "Encrypted hex: {0}".format(binascii.hexlify(encrypted)) print "Decrypted: \t{0}, hex: \t{1}".format(decrypted, binascii.hexlify(decrypted)) self.assertEqual(plain, decrypted)
def test_001_t(self): """ pretty simple (the carrier allocation here is not a practical OFDM configuration!) """ fft_len = 6 tx_symbols = (1, 2, 3) # ^ this gets mapped to the DC carrier because occupied_carriers[0][0] == 0 pilot_symbols = ((1j, ), ) occupied_carriers = ((0, 1, 2), ) pilot_carriers = ((3, ), ) sync_word = (list(range(fft_len)), ) expected_result = sync_word[0] + [1j, 0, 0, 1, 2, 3] # ^ DC carrier src = blocks.vector_source_c(tx_symbols, False, 1) alloc = digital.ofdm_carrier_allocator_cvc(fft_len, occupied_carriers, pilot_carriers, pilot_symbols, sync_word, self.tsb_key) sink = blocks.tsb_vector_sink_c(vlen=fft_len, tsb_key=self.tsb_key) self.tb.connect( src, blocks.stream_to_tagged_stream(gr.sizeof_gr_complex, 1, len(tx_symbols), self.tsb_key), alloc, sink) self.tb.run() self.assertEqual(sink.data()[0], expected_result)
def test_002_t (self): # set up fg in_data = range(20) vlen_in = 5 crop_x = (2,4) crop_y = (1,4) src = blocks.vector_source_c(in_data) s2v = blocks.stream_to_vector(8,vlen_in) s2ts = blocks.stream_to_tagged_stream(8,vlen_in,len(in_data)/vlen_in,'packet_len') crop = radar.crop_matrix_vcvc(vlen_in,crop_x,crop_y) v2s = blocks.vector_to_stream(8,crop_x[1]-crop_x[0]) snk = blocks.vector_sink_c() debug = blocks.tag_debug(8,'debug') self.tb.connect(src,s2v,s2ts,crop,v2s,snk) self.tb.connect(v2s,debug) self.tb.run () # check data ref_data = (7,8,12,13,17,18) out_data = snk.data() print "INDATA:", in_data print "REF: ", ref_data print "OUTPUT:", out_data for k in range(len(out_data)): self.assertEqual(out_data[k].real,ref_data[k])
def test_001_t2(self): """ pretty simple (same as before, but odd fft len) """ fft_len = 5 tx_symbols = (1, 2, 3) # ^ this gets mapped to the DC carrier because occupied_carriers[0][0] == 0 occupied_carriers = ((0, 1, 2), ) pilot_carriers = ((-2, ), ) pilot_symbols = ((1j, ), ) expected_result = [1j, 0, 1, 2, 3] # ^ DC carrier src = blocks.vector_source_c(tx_symbols, False, 1) alloc = digital.ofdm_carrier_allocator_cvc(fft_len, occupied_carriers, pilot_carriers, pilot_symbols, (), self.tsb_key) sink = blocks.tsb_vector_sink_c(vlen=fft_len, tsb_key=self.tsb_key) self.tb.connect( src, blocks.stream_to_tagged_stream(gr.sizeof_gr_complex, 1, len(tx_symbols), self.tsb_key), alloc, sink) self.tb.run() self.assertEqual(sink.data()[0], expected_result)
def test_003_connect (self): """ Connect carrier_allocator to ofdm_serializer, make sure output==input """ fft_len = 8 n_syms = 1 occupied_carriers = ((1, 2, 6, 7),) pilot_carriers = ((3,),(5,)) pilot_symbols = ((1j,),(-1j,)) #tx_data = tuple([numpy.random.randint(0, 10) for x in range(4 * n_syms)]) tx_data = (1, 2, 3, 4) src = blocks.vector_source_c(tx_data, False, 1) alloc = digital.ofdm_carrier_allocator_cvc( fft_len, occupied_carriers, pilot_carriers, pilot_symbols, (), # No sync word self.tsb_key, True # Output is shifted (default) ) serializer = digital.ofdm_serializer_vcc( alloc, "", # Len tag key 0, # Symbols skipped "", # Carrier offset key True # Input is shifted (default) ) sink = blocks.tsb_vector_sink_c(tsb_key=self.tsb_key) self.tb.connect(src, blocks.stream_to_tagged_stream(gr.sizeof_gr_complex, 1, len(tx_data), self.tsb_key), alloc, serializer, sink) self.tb.run () self.assertEqual(sink.data()[0], tx_data)
def test_001_t(self): # set up fg phr = np.random.randint(0, 2, size=(12, )) data = np.array(np.random.randint(0, 256, size=(6 * 3, ))) data_bin = np.unpackbits(np.array(data, dtype=np.uint8)) self.src = blocks.vector_source_b(data, False, 1, []) self.s2ts = blocks.stream_to_tagged_stream(gr.sizeof_char, 1, 6, "packet_len") self.ts2pdu = blocks.tagged_stream_to_pdu(blocks.byte_t, "packet_len") self.pref = ieee802_15_4.phr_prefixer(phr) self.pdu2ts = blocks.pdu_to_tagged_stream(blocks.byte_t, "packet_len") self.snk = blocks.vector_sink_b(1) self.tb.connect(self.src, self.s2ts, self.ts2pdu) self.tb.msg_connect(self.ts2pdu, "pdus", self.pref, "in") self.tb.msg_connect(self.pref, "out", self.pdu2ts, "pdus") self.tb.connect(self.pdu2ts, self.snk) self.tb.start() time.sleep(1) self.tb.stop() # check data data_out = self.snk.data() # print "input:" # for i in data: # print i # print "output:" # for i in data_out: # print data_out expected_output = np.concatenate( (phr, data_bin[0:6 * 8], phr, data_bin[6 * 8:12 * 8], phr, data_bin[12 * 8:18 * 8])) self.assertFloatTuplesAlmostEqual(data_out, expected_output)
def test_0010_tag_propagation (self): """ Make sure tags on the CRC aren't lost. """ # Data with precalculated CRC data = ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1 ) # 2, 67, 225, 188 testtag = gr.tag_t() testtag.offset = len(data)-1 testtag.key = pmt.string_to_symbol('tag1') testtag.value = pmt.from_long(0) src = blocks.vector_source_b(data, False, 1, (testtag,)) crc_check = digital.crc32_bb(True, self.tsb_key, False) sink = blocks.tsb_vector_sink_b(tsb_key=self.tsb_key) self.tb.connect( src, blocks.stream_to_tagged_stream(gr.sizeof_char, 1, len(data), self.tsb_key), crc_check, sink ) self.tb.run() self.assertEqual([len(data)-33,], [tag.offset for tag in sink.tags() if pmt.symbol_to_string(tag.key) == 'tag1'])
def test_001_t (self): # set up fg phr = np.random.randint(0,2,size=(12,)) data = np.array(np.random.randint(0,256, size=(6*3,))) data_bin = np.unpackbits(np.array(data,dtype=np.uint8)) self.src = blocks.vector_source_b(data, False, 1, []) self.s2ts = blocks.stream_to_tagged_stream(gr.sizeof_char, 1, 6, "packet_len") self.ts2pdu = blocks.tagged_stream_to_pdu(blocks.byte_t, "packet_len") self.pref = ieee802_15_4.phr_prefixer(phr) self.pdu2ts = blocks.pdu_to_tagged_stream(blocks.byte_t, "packet_len") self.snk = blocks.vector_sink_b(1) self.tb.connect(self.src, self.s2ts, self.ts2pdu) self.tb.msg_connect(self.ts2pdu, "pdus", self.pref, "in") self.tb.msg_connect(self.pref, "out", self.pdu2ts, "pdus") self.tb.connect(self.pdu2ts, self.snk) self.tb.start() time.sleep(1) self.tb.stop() # check data data_out = self.snk.data() # print "input:" # for i in data: # print i # print "output:" # for i in data_out: # print data_out expected_output = np.concatenate((phr,data_bin[0:6*8], phr, data_bin[6*8:12*8], phr, data_bin[12*8:18*8])) self.assertFloatTuplesAlmostEqual(data_out, expected_output)
def test_001_t (self): """ pretty simple (the carrier allocation here is not a practical OFDM configuration!) """ fft_len = 6 tx_symbols = (1, 2, 3) # ^ this gets mapped to the DC carrier because occupied_carriers[0][0] == 0 pilot_symbols = ((1j,),) occupied_carriers = ((0, 1, 2),) pilot_carriers = ((3,),) sync_word = (list(range(fft_len)),) expected_result = tuple(sync_word[0] + [1j, 0, 0, 1, 2, 3]) # ^ DC carrier src = blocks.vector_source_c(tx_symbols, False, 1) alloc = digital.ofdm_carrier_allocator_cvc(fft_len, occupied_carriers, pilot_carriers, pilot_symbols, sync_word, self.tsb_key) sink = blocks.tsb_vector_sink_c(vlen=fft_len, tsb_key=self.tsb_key) self.tb.connect( src, blocks.stream_to_tagged_stream(gr.sizeof_gr_complex, 1, len(tx_symbols), self.tsb_key), alloc, sink ) self.tb.run() self.assertEqual(sink.data()[0], expected_result)
def test_002_with_offset(self): """ Standard test, carrier offset """ fft_len = 16 tx_symbols = list(range(1, 16)) tx_symbols = (0, 0, 1, 1j, 2, 3, 0, 0, 0, 0, 0, 0, 4, 5, 2j, 6, 0, 0, 7, 8, 3j, 9, 0, 0, 0, 0, 0, 0, 10, 4j, 11, 12, 0, 0, 13, 1j, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 2j, 0) carr_offset = 1 # Compare this with tx_symbols from the previous test expected_result = list(range(1, 16)) + [0, 0, 0] occupied_carriers = ( (1, 3, 4, 11, 12, 14), (1, 2, 4, 11, 13, 14), ) n_syms = len(tx_symbols) // fft_len offsettag = gr.tag_t() offsettag.offset = 0 offsettag.key = pmt.string_to_symbol("ofdm_sync_carr_offset") offsettag.value = pmt.from_long(carr_offset) src = blocks.vector_source_c(tx_symbols, False, fft_len, (offsettag, )) sink = blocks.tsb_vector_sink_c(tsb_key=self.tsb_key) serializer = digital.ofdm_serializer_vcc(fft_len, occupied_carriers, self.tsb_key, "", 0, "ofdm_sync_carr_offset", False) self.tb.connect( src, blocks.stream_to_tagged_stream(gr.sizeof_gr_complex, fft_len, n_syms, self.tsb_key), serializer, sink) self.tb.run() self.assertEqual(sink.data()[0], expected_result) self.assertEqual(len(sink.tags()), 1)
def __init__(self): gr.top_block.__init__(self, "Top Block") ################################################## # Variables ################################################## self.samp_rate = samp_rate = 32000 ################################################## # Blocks ################################################## self.digital_crc32_bb_0 = digital.crc32_bb(False, "packet_len") self.blocks_tag_debug_1 = blocks.tag_debug(gr.sizeof_char*1, "Pre-CRC", ""); self.blocks_tag_debug_1.set_display(True) self.blocks_tag_debug_0 = blocks.tag_debug(gr.sizeof_char*1, "Post-CRC", ""); self.blocks_tag_debug_0.set_display(True) self.blocks_stream_to_tagged_stream_0 = blocks.stream_to_tagged_stream(gr.sizeof_char, 1, 100, "packet_len") self.blocks_head_0 = blocks.head(gr.sizeof_char*1, 100*5) self.analog_random_source_x_0 = blocks.vector_source_b(map(int, numpy.random.randint(0, 255, 1000)), True) ################################################## # Connections ################################################## self.connect((self.analog_random_source_x_0, 0), (self.blocks_head_0, 0)) self.connect((self.blocks_head_0, 0), (self.blocks_stream_to_tagged_stream_0, 0)) self.connect((self.blocks_stream_to_tagged_stream_0, 0), (self.digital_crc32_bb_0, 0)) self.connect((self.digital_crc32_bb_0, 0), (self.blocks_tag_debug_0, 0)) self.connect((self.blocks_stream_to_tagged_stream_0, 0), (self.blocks_tag_debug_1, 0))
def test_005_packet_len_tag(self): """ Standard test """ fft_len = 16 tx_symbols = list(range(1, 16)) tx_symbols = (0, 1, 1j, 2, 3, 0, 0, 0, 0, 0, 0, 4, 5, 2j, 6, 0, 0, 7, 8, 3j, 9, 0, 0, 0, 0, 0, 0, 10, 4j, 11, 12, 0, 0, 13, 1j, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 2j, 0, 0) expected_result = list(range(1, 16)) occupied_carriers = ( (1, 3, 4, 11, 12, 14), (1, 2, 4, 11, 13, 14), ) n_syms = len(tx_symbols) // fft_len packet_len_tsb_key = "packet_len" tag2 = gr.tag_t() tag2.offset = 0 tag2.key = pmt.string_to_symbol("packet_len") tag2.value = pmt.from_long(len(expected_result)) src = blocks.vector_source_c(tx_symbols, False, fft_len, (tag2, )) serializer = digital.ofdm_serializer_vcc(fft_len, occupied_carriers, self.tsb_key, packet_len_tsb_key, 0, "", False) sink = blocks.tsb_vector_sink_c(tsb_key=packet_len_tsb_key) self.tb.connect( src, blocks.stream_to_tagged_stream(gr.sizeof_gr_complex, fft_len, n_syms, self.tsb_key), serializer, sink) self.tb.run() self.assertEqual(sink.data()[0], expected_result)
def test_002_t (self): # set up fg # check on reverse by using two times test_len = 12 in_data = range(test_len) vlen_in = 3 vlen_out = 4 src = blocks.vector_source_c(in_data) stv = blocks.stream_to_vector(8,vlen_in) s2ts = blocks.stream_to_tagged_stream(8,vlen_in,test_len/vlen_in,'packet_len') transpose1 = radar.transpose_matrix_vcvc(vlen_in,vlen_out,'packet_len') transpose2 = radar.transpose_matrix_vcvc(vlen_out,vlen_in,'packet_len') vts = blocks.vector_to_stream(8,vlen_in) snk = blocks.vector_sink_c() vts2 = blocks.vector_to_stream(8,vlen_out) snk2 = blocks.vector_sink_c() self.tb.connect(src,stv,s2ts,transpose1,transpose2,vts,snk) self.tb.connect(transpose1,vts2,snk2) self.tb.run() # check data out_data_real = [0]*len(in_data) out_data = snk.data() for k in range(len(in_data)): out_data_real[k] = out_data[k].real print "Input data:", in_data print "Output data:", out_data_real print "Transpose data:", snk2.data() for k in range(len(out_data_real)): self.assertEqual(out_data_real[k],in_data[k])
def test_001_t (self): # set up fg # check samples on output test_len = 12 in_data = range(test_len) vlen_in = 3 vlen_out = 4 src = blocks.vector_source_c(in_data) stv = blocks.stream_to_vector(8,vlen_in) s2ts = blocks.stream_to_tagged_stream(8,vlen_in,test_len/vlen_in,'packet_len') transpose = radar.transpose_matrix_vcvc(vlen_in,vlen_out,'packet_len') vts = blocks.vector_to_stream(8,vlen_out) snk = blocks.vector_sink_c() self.tb.connect(src,stv,s2ts,transpose,vts,snk) self.tb.run() # check data out_data = snk.data() ref_data = [0]*len(in_data) counter = 0; for k in range(vlen_in): for l in range(test_len/vlen_in): ref_data[counter] = in_data[l*vlen_in+k] counter = counter+1 out_data_real = [0]*len(in_data) for k in range(len(in_data)): out_data_real[k] = out_data[k].real for k in range(len(out_data_real)): self.assertEqual(out_data_real[k],ref_data[k])
def test_003_t (self): # test cut frequency negative freq # set up fg test_len = 1000 samp_rate = 2000 freq1 = -200 freq2 = -205 ampl = 1 packet_len = test_len threshold = -100 samp_protect = 2 src1 = analog.sig_source_c(samp_rate, analog.GR_COS_WAVE, freq1, ampl*0.2) src2 = analog.sig_source_c(samp_rate, analog.GR_COS_WAVE, freq2, ampl) add = blocks.add_cc(); head = blocks.head(8,test_len) s2ts = blocks.stream_to_tagged_stream(8,1,packet_len,"packet_len") fft = radar.ts_fft_cc(packet_len) peak = radar.find_max_peak_c(samp_rate, threshold, samp_protect, (-200,200), True) debug = blocks.message_debug() self.tb.connect((src1,0), (add,0)) self.tb.connect((src2,0), (add,1)) self.tb.connect(add,head,s2ts,fft,peak) self.tb.msg_connect(peak,"Msg out",debug,"store") #self.tb.msg_connect(peak,"Msg out",debug,"print") self.tb.start() sleep(0.5) self.tb.stop() self.tb.wait() # check frequency in os_cfar message with given one msg = debug.get_message(0) self.assertAlmostEqual(freq1,pmt.f32vector_ref(pmt.nth(1,pmt.nth(1,msg)),0),8)
def test_001_t(self): # set up fg test_len = 1000 fft_len = 100 cp_len = fft_len / 4 in_data = list(range(0, fft_len)) * (test_len // fft_len) src = blocks.vector_source_c(in_data) s2v = blocks.stream_to_vector(8, fft_len) s2ts = blocks.stream_to_tagged_stream(8, fft_len, test_len / fft_len, 'packet_len') cp_add = digital.ofdm_cyclic_prefixer(fft_len, fft_len + cp_len, 0, 'packet_len') cp_remove = radar.ofdm_cyclic_prefix_remover_cvc( fft_len, cp_len, 'packet_len') v2s = blocks.vector_to_stream(8, fft_len) snk = blocks.vector_sink_c() self.tb.connect(src, s2v, s2ts, cp_add, cp_remove, v2s, snk) self.tb.run() # check data out_data = snk.data() out_data_real = [0] * len(out_data) for k in range(len(out_data)): out_data_real[k] = out_data[k].real for k in range(len(in_data)): self.assertEqual(in_data[k], out_data_real[k])
def test_002_t (self): """ same, but using negative carrier indices """ fft_len = 6 tx_symbols = (1, 2, 3) pilot_symbols = ((1j,),) occupied_carriers = ((-1, 1, 2),) pilot_carriers = ((3,),) expected_result = (1j, 0, 1, 0, 2, 3) src = blocks.vector_source_c(tx_symbols, False, 1) alloc = digital.ofdm_carrier_allocator_cvc(fft_len, occupied_carriers, pilot_carriers, pilot_symbols, (), self.tsb_key) sink = blocks.tsb_vector_sink_c(fft_len) self.tb.connect( src, blocks.stream_to_tagged_stream(gr.sizeof_gr_complex, 1, len(tx_symbols), self.tsb_key), alloc, sink ) self.tb.run () self.assertEqual(sink.data()[0], expected_result)
def test_003_tags_plus_data(self): packet_len = 16 src_data = list(range(packet_len)) tag1 = gr.tag_t() tag1.offset = 0 tag1.key = pmt.string_to_symbol('spam') tag1.value = pmt.from_long(23) tag2 = gr.tag_t() tag2.offset = 10 # Must be < packet_len tag2.key = pmt.string_to_symbol('eggs') tag2.value = pmt.from_long(42) src = blocks.vector_source_f(src_data, tags=(tag1, tag2)) s2ts = blocks.stream_to_tagged_stream(gr.sizeof_float, vlen=1, packet_len=packet_len, len_tag_key="packet_len") ts2pdu = pdu.tagged_stream_to_pdu(gr.types.float_t, "packet_len") dbg = blocks.message_debug() self.tb.connect(src, s2ts, ts2pdu) self.tb.msg_connect(ts2pdu, "pdus", dbg, "store") self.tb.start() self.tb.wait() result_msg = dbg.get_message(0) metadata = pmt.to_python(pmt.car(result_msg)) vector = pmt.f32vector_elements(pmt.cdr(result_msg)) self.assertEqual(metadata, {'eggs': 42, 'spam': 23}) self.assertFloatTuplesAlmostEqual(tuple(vector), src_data)
def test_003_connect(self): """ Connect carrier_allocator to ofdm_serializer, make sure output==input """ fft_len = 8 n_syms = 1 occupied_carriers = ((1, 2, 6, 7), ) pilot_carriers = ((3, ), (5, )) pilot_symbols = ((1j, ), (-1j, )) #tx_data = tuple([numpy.random.randint(0, 10) for x in range(4 * n_syms)]) tx_data = [1, 2, 3, 4] src = blocks.vector_source_c(tx_data, False, 1) alloc = digital.ofdm_carrier_allocator_cvc( fft_len, occupied_carriers, pilot_carriers, pilot_symbols, (), # No sync word self.tsb_key, True # Output is shifted (default) ) serializer = digital.ofdm_serializer_vcc( alloc, "", # Len tag key 0, # Symbols skipped "", # Carrier offset key True # Input is shifted (default) ) sink = blocks.tsb_vector_sink_c(tsb_key=self.tsb_key) self.tb.connect( src, blocks.stream_to_tagged_stream(gr.sizeof_gr_complex, 1, len(tx_data), self.tsb_key), alloc, serializer, sink) self.tb.run() self.assertEqual(sink.data()[0], tx_data)
def test_001_t(self): nsubcarrier = 4 ntimeslots = 16 filter_alpha = 0.35 tag_key = "frame_len" fft_length = nsubcarrier * ntimeslots taps = get_frequency_domain_filter('rrc', filter_alpha, ntimeslots, nsubcarrier, 2) data = get_random_qpsk(nsubcarrier * ntimeslots) D = get_data_matrix(data, nsubcarrier, group_by_subcarrier=False) print D md = gfdms.modulator_cc(nsubcarrier, ntimeslots, filter_alpha, fft_length, 1, tag_key) tagger = blocks.stream_to_tagged_stream(gr.sizeof_gr_complex, 1, fft_length, tag_key) src = blocks.vector_source_c(data) dst = blocks.vector_sink_c() self.tb.connect(src, tagger, md, dst) self.tb.run() res = np.array(dst.data()) print res ref = gfdm_modulate_block(D, taps, ntimeslots, nsubcarrier, 2, True) print ref self.assertComplexTuplesAlmostEqual(ref, res, 2)
def __init__(self, host='127.0.0.1', iq_port='9094', packet_port='9095', sample_filename='sample_filename.dat'): gr.top_block.__init__(self, "Hurdle 1 Solution Stub") ################################################## # Parameters ################################################## self.host = host self.iq_port = iq_port self.packet_port = packet_port self.sample_filename = sample_filename ################################################## # Variables ################################################## self.samp_rate = samp_rate = 4e6 self.num_garbage = num_garbage = int(1e6 / 8) ################################################## # Blocks ################################################## self.blocks_throttle_1 = blocks.throttle(gr.sizeof_char * 1, samp_rate / 8.0, True) self.blocks_tagged_stream_to_pdu_0 = blocks.tagged_stream_to_pdu( blocks.byte_t, 'packet_len') self.blocks_stream_to_tagged_stream_0 = blocks.stream_to_tagged_stream( gr.sizeof_char, 1, 1000, 'packet_len') self.blocks_socket_pdu_0_0 = blocks.socket_pdu("TCP_CLIENT", host, packet_port, 10000, False) self.blocks_socket_pdu_0 = blocks.socket_pdu("TCP_CLIENT", host, iq_port, 10000, False) self.blocks_pdu_to_tagged_stream_0 = blocks.pdu_to_tagged_stream( blocks.complex_t, 'packet_len') self.blocks_head_0 = blocks.head(gr.sizeof_char * 1, num_garbage) self.blocks_file_sink_0 = blocks.file_sink(gr.sizeof_gr_complex * 1, sample_filename, False) self.blocks_file_sink_0.set_unbuffered(False) self.analog_random_source_x_0 = blocks.vector_source_b( map(int, numpy.random.randint(0, 255, 1000)), True) ################################################## # Connections ################################################## self.msg_connect((self.blocks_socket_pdu_0, 'pdus'), (self.blocks_pdu_to_tagged_stream_0, 'pdus')) self.msg_connect((self.blocks_tagged_stream_to_pdu_0, 'pdus'), (self.blocks_socket_pdu_0_0, 'pdus')) self.connect((self.analog_random_source_x_0, 0), (self.blocks_throttle_1, 0)) self.connect((self.blocks_head_0, 0), (self.blocks_stream_to_tagged_stream_0, 0)) self.connect((self.blocks_pdu_to_tagged_stream_0, 0), (self.blocks_file_sink_0, 0)) self.connect((self.blocks_stream_to_tagged_stream_0, 0), (self.blocks_tagged_stream_to_pdu_0, 0)) self.connect((self.blocks_throttle_1, 0), (self.blocks_head_0, 0))
def test_001_t(self): nsubcarrier = 4 ntimeslots = 16 filter_alpha = 0.35 tag_key = "frame_len" fft_length = nsubcarrier * ntimeslots taps = get_frequency_domain_filter('rrc', filter_alpha, ntimeslots, nsubcarrier, 2) data = get_random_qpsk(nsubcarrier * ntimeslots) # data = np.zeros(nsubcarrier) # data[0] = 1 # data = np.repeat(data, ntimeslots) D = get_data_matrix(data, nsubcarrier, group_by_subcarrier=False) print D md = gfdms.modulator_cc(nsubcarrier, ntimeslots, filter_alpha, fft_length, 1, tag_key) tagger = blocks.stream_to_tagged_stream(gr.sizeof_gr_complex, 1, fft_length, tag_key) src = blocks.vector_source_c(data) dst = blocks.vector_sink_c() self.tb.connect(src, tagger, md, dst) self.tb.run() res = np.array(dst.data()) print res ref = gfdm_modulate_block(D, taps, ntimeslots, nsubcarrier, 2, True) print ref self.assertComplexTuplesAlmostEqual(ref, res, 2)
def test_002_t (self): # set up fg test_len = 2**15 samp_rate = 250000 freq = -2000 ampl = 1 packet_len = test_len min_output_buffer = 2*packet_len compare_sample = 5 protect_sample = 0 rel_threshold = 0.78 mult_threshold = 10 src = analog.sig_source_c(samp_rate, analog.GR_COS_WAVE, freq, ampl) src.set_min_output_buffer(min_output_buffer) head = blocks.head(8,test_len) head.set_min_output_buffer(min_output_buffer) s2ts = blocks.stream_to_tagged_stream(8,1,packet_len,"packet_len") s2ts.set_min_output_buffer(min_output_buffer) fft = radar.ts_fft_cc(packet_len) fft.set_min_output_buffer(min_output_buffer) cfar = radar.os_cfar_c(samp_rate, compare_sample, protect_sample, rel_threshold, mult_threshold) debug = blocks.message_debug() self.tb.connect(src,head,s2ts,fft,cfar) self.tb.msg_connect(cfar,"Msg out",debug,"store") self.tb.msg_connect(cfar,"Msg out",debug,"print") self.tb.start() sleep(0.5) self.tb.stop() self.tb.wait() # check frequency in os_cfar message with given one msg = debug.get_message(0) self.assertAlmostEqual(freq/pmt.f32vector_ref(pmt.nth(1,pmt.nth(1,msg)),0),1,2)
def check_frame(self, payload, access_code, enable_crc): ## # Print Info print "----------------------------------------------------------------------" print "Testing with:" print "Payload: ", payload print "Access code: ", access_code print "CRC enabled: ", enable_crc if enable_crc: crc_length = 4 else: crc_length = 0 ## # Set up BlockS src = blocks.vector_source_b(payload) to_tagged_stream = blocks.stream_to_tagged_stream(gr.sizeof_char, 1, len(payload), "packet_len") encoder = frame_encoder("packet_len", enable_crc) dst = blocks.vector_sink_b() ## # Connect Blocks self.tb.connect(src,to_tagged_stream) self.tb.connect(to_tagged_stream, encoder) self.tb.connect(encoder, dst) self.tb.run () ## # Check access code result_data = dst.data() self.assertEqual(result_data[2:10], access_code, "Access code wrong") print "Access code correct." ## # Check Payload self.assertEqual(result_data[14:(14 + len(payload))], payload, "Payload wrong") print "Payload correct." ## # Extract LSB-Length from header and form convert to MSB length_rev = result_data[10] i = 0 length = 0 while i < 8: length = (length<<1)|(length_rev>>i & 0x1) i = i+1 ## # Check Payload length in header self.assertEqual((length-crc_length), len(payload), "Payload length wrong") print "Payload length correct."
def test_001_t (self): # set up variables xlen = 50 ylen = 100 test_len = xlen*ylen samp_compare = (5,10) samp_protect = (2,4) rel_threshold = 0.78 mult_threshold = 4 # setup input data in_data = [0]*test_len for k in range(test_len): in_data[k] = random.random() x0 = 10 y0 = 10 in_data[x0+xlen*y0] = 2 x1 = 40 y1 = 15 in_data[x1+xlen*y1] = 3 x2 = 41 y2 = 13 in_data[x2+xlen*y2] = 3 # set up fg src = blocks.vector_source_c(in_data) s2v = blocks.stream_to_vector(8,xlen) s2ts = blocks.stream_to_tagged_stream(8,xlen,ylen,'packet_len') cfar = radar.os_cfar_2d_vc(xlen,samp_compare,samp_protect,rel_threshold,mult_threshold) debug = blocks.message_debug() self.tb.connect(src,s2v,s2ts,cfar) self.tb.msg_connect(cfar,"Msg out",debug,"store") self.tb.msg_connect(cfar,"Msg out",debug,"print") self.tb.run () # check data msg = debug.get_message(0) cfar_x0 = pmt.f32vector_ref(pmt.nth(1,pmt.nth(1,msg)),0) cfar_x1 = pmt.f32vector_ref(pmt.nth(1,pmt.nth(1,msg)),1) cfar_x2 = pmt.f32vector_ref(pmt.nth(1,pmt.nth(1,msg)),2) cfar_y0 = pmt.f32vector_ref(pmt.nth(1,pmt.nth(2,msg)),0) cfar_y1 = pmt.f32vector_ref(pmt.nth(1,pmt.nth(2,msg)),1) cfar_y2 = pmt.f32vector_ref(pmt.nth(1,pmt.nth(2,msg)),2) self.assertAlmostEqual(x0,cfar_x0,4) self.assertAlmostEqual(x1,cfar_x1,4) self.assertAlmostEqual(x2,cfar_x2,4) self.assertAlmostEqual(y0,cfar_y0,4) self.assertAlmostEqual(y1,cfar_y1,4) self.assertAlmostEqual(y2,cfar_y2,4)
def __init__(self): grc_wxgui.top_block_gui.__init__(self, title="Top Block") _icon_path = "/usr/share/icons/hicolor/32x32/apps/gnuradio-grc.png" self.SetIcon(wx.Icon(_icon_path, wx.BITMAP_TYPE_ANY)) ################################################## # Variables ################################################## self.samp_rate = samp_rate = 32e3 ################################################## # Blocks ################################################## self.uhd_usrp_sink_0 = uhd.usrp_sink( ",".join(("addr=192.168.10.3", "")), uhd.stream_args( cpu_format="fc32", channels=range(1), ), ) self.uhd_usrp_sink_0.set_samp_rate(32e3) self.uhd_usrp_sink_0.set_center_freq(2.417e9, 0) self.uhd_usrp_sink_0.set_gain(20, 0) self.uhd_usrp_sink_0.set_antenna("TX/RX", 0) self.uhd_usrp_sink_0.set_bandwidth(3e6, 0) self.digital_gfsk_mod_0 = digital.gfsk_mod( samples_per_symbol=2, sensitivity=1.0, bt=0.35, verbose=False, log=False, ) self.digital_crc32_bb_0 = digital.crc32_bb(False, "packet_len") self.blocks_vector_source_x_0 = blocks.vector_source_b((1,0,1,0), True, 1, []) self.blocks_stream_to_tagged_stream_0 = blocks.stream_to_tagged_stream(gr.sizeof_char, 1, 8, "packet_len") self.blks2_packet_encoder_0 = grc_blks2.packet_mod_b(grc_blks2.packet_encoder( samples_per_symbol=1, bits_per_symbol=1, preamble="", access_code="", pad_for_usrp=True, ), payload_length=0, ) self.Spread_ds_spreader_0 = Spread.ds_spreader(2, (1, 1, 0, 1), (1, 1, 1)) ################################################## # Connections ################################################## self.connect((self.Spread_ds_spreader_0, 0), (self.digital_gfsk_mod_0, 0)) self.connect((self.blks2_packet_encoder_0, 0), (self.blocks_stream_to_tagged_stream_0, 0)) self.connect((self.blocks_stream_to_tagged_stream_0, 0), (self.digital_crc32_bb_0, 0)) self.connect((self.blocks_vector_source_x_0, 0), (self.blks2_packet_encoder_0, 0)) self.connect((self.digital_crc32_bb_0, 0), (self.Spread_ds_spreader_0, 0)) self.connect((self.digital_gfsk_mod_0, 0), (self.uhd_usrp_sink_0, 0))
def test_001_t (self): #print "TEST: division without discarded carriers and sync words" # set up fg test_len = 20 vlen = 5 ts_len = test_len/vlen/2 in_data0 = [0]*test_len in_data1 = [0]*test_len for k in range(test_len): in_data0[k] = complex(k-4, k+1) in_data1[k] = complex(k+3, k-2) src0 = blocks.vector_source_c(in_data0) s2v0 = blocks.stream_to_vector(8,vlen) s2ts0 = blocks.stream_to_tagged_stream(8,vlen,ts_len,'packet_len') src1 = blocks.vector_source_c(in_data1) s2v1 = blocks.stream_to_vector(8,vlen) s2ts1 = blocks.stream_to_tagged_stream(8,vlen,ts_len,'packet_len') div = radar.ofdm_divide_vcvc(vlen,2*vlen,(),0) v2s = blocks.vector_to_stream(8,2*vlen) snk = blocks.vector_sink_c() self.tb.connect(src0,s2v0,s2ts0) self.tb.connect(src1,s2v1,s2ts1) self.tb.connect((s2ts0,0),(div,0)) self.tb.connect((s2ts1,0),(div,1)) self.tb.connect(div,v2s,snk) self.tb.run () # get ref data ref_data = [0]*test_len*2 for k in range(test_len/vlen): for l in range(vlen): ref_data[2*vlen*k+l] = in_data0[vlen*k+l]/in_data1[vlen*k+l] # check data out_data = snk.data() for k in range(len(out_data)): self.assertAlmostEqual(ref_data[k], out_data[k],4)
def test_003_t (self): """ more advanced: - 6 symbols per carrier - 2 pilots per carrier - have enough data for nearly 3 OFDM symbols - send that twice - add some random tags - don't shift """ tx_symbols = list(range(1, 16)); # 15 symbols pilot_symbols = ((1j, 2j), (3j, 4j)) occupied_carriers = ((1, 3, 4, 11, 12, 14), (1, 2, 4, 11, 13, 14),) pilot_carriers = ((2, 13), (3, 12)) expected_result = (0, 1, 1j, 2, 3, 0, 0, 0, 0, 0, 0, 4, 5, 2j, 6, 0, 0, 7, 8, 3j, 9, 0, 0, 0, 0, 0, 0, 10, 4j, 11, 12, 0, 0, 13, 1j, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 2j, 0, 0) fft_len = 16 testtag1 = gr.tag_t() testtag1.offset = 0 testtag1.key = pmt.string_to_symbol('tag1') testtag1.value = pmt.from_long(0) testtag2 = gr.tag_t() testtag2.offset = 7 # On the 2nd OFDM symbol testtag2.key = pmt.string_to_symbol('tag2') testtag2.value = pmt.from_long(0) testtag3 = gr.tag_t() testtag3.offset = len(tx_symbols)+1 # First OFDM symbol of packet 2 testtag3.key = pmt.string_to_symbol('tag3') testtag3.value = pmt.from_long(0) testtag4 = gr.tag_t() testtag4.offset = 2*len(tx_symbols)-1 # Last OFDM symbol of packet 2 testtag4.key = pmt.string_to_symbol('tag4') testtag4.value = pmt.from_long(0) src = blocks.vector_source_c(tx_symbols * 2, False, 1, (testtag1, testtag2, testtag3, testtag4)) alloc = digital.ofdm_carrier_allocator_cvc(fft_len, occupied_carriers, pilot_carriers, pilot_symbols, (), self.tsb_key, False) sink = blocks.tsb_vector_sink_c(fft_len) self.tb.connect(src, blocks.stream_to_tagged_stream(gr.sizeof_gr_complex, 1, len(tx_symbols), self.tsb_key), alloc, sink) self.tb.run () self.assertEqual(sink.data()[0], expected_result) tags_found = {'tag1': False, 'tag2': False, 'tag3': False, 'tag4': False} correct_offsets = {'tag1': 0, 'tag2': 1, 'tag3': 3, 'tag4': 5} for tag in sink.tags(): key = pmt.symbol_to_string(tag.key) if key in list(tags_found.keys()): tags_found[key] = True self.assertEqual(correct_offsets[key], tag.offset) self.assertTrue(all(tags_found.values()))
def test_002_crc_equal_unpacked (self): """ Test unpacked operation with packed operation """ data = (0, 1, 2, 3, 4, 5, 6, 7, 8) src = blocks.vector_source_b(data) unpack1 = blocks.repack_bits_bb(8, 1, self.tsb_key, False, gr.GR_LSB_FIRST) unpack2 = blocks.repack_bits_bb(8, 1, self.tsb_key, False, gr.GR_LSB_FIRST) crc_unpacked = digital.crc32_bb(False, self.tsb_key, False) crc_packed = digital.crc32_bb(False, self.tsb_key, True) sink1 = blocks.tsb_vector_sink_b(tsb_key=self.tsb_key) sink2 = blocks.tsb_vector_sink_b(tsb_key=self.tsb_key) self.tb.connect(src, blocks.stream_to_tagged_stream( gr.sizeof_char, 1, len(data), self.tsb_key), crc_packed, unpack1, sink1) self.tb.connect(src, blocks.stream_to_tagged_stream( gr.sizeof_char, 1, len(data), self.tsb_key), unpack2, crc_unpacked, sink2) self.tb.run() self.assertEqual(sink1.data(), sink2.data())
def test_001_crc_len(self): """ Make sure the output of a CRC set is 4 bytes longer than the input. """ data = list(range(16)) src = blocks.vector_source_b(data) crc = digital.crc32_bb(False, self.tsb_key) sink = blocks.tsb_vector_sink_b(tsb_key=self.tsb_key) self.tb.connect( src, blocks.stream_to_tagged_stream(gr.sizeof_char, 1, len(data), self.tsb_key), crc, sink) self.tb.run() # Check that the packets before crc_check are 4 bytes longer that the input. self.assertEqual(len(data) + 4, len(sink.data()[0]))
def test_001_t (self): src_data = (1, ) * 50 packet_len = 10 len_tag_key = 'packet_len' src = blocks.vector_source_f(src_data, False, 1) tagger = blocks.stream_to_tagged_stream(gr.sizeof_float, 1, packet_len, len_tag_key) sink = blocks.vector_sink_f() self.tb.connect(src, tagger, sink) self.tb.run () self.assertEqual(sink.data(), src_data) tags = [gr.tag_to_python(x) for x in sink.tags()] tags = sorted([(x.offset, x.key, x.value) for x in tags]) expected_tags = [(int(pos), 'packet_len', packet_len) for pos in range(0, 50, 10) ] self.assertEqual(tags, expected_tags)