def test_add_vcc_five(self): src1_data = (1.0+2.0j, 3.0+4.0j, 5.0+6.0j, 7.0+8.0j, 9.0+10.0j) src2_data = (11.0+12.0j, 13.0+14.0j, 15.0+16.0j, 17.0+18.0j, 19.0+20.0j) src3_data = (21.0+22.0j, 23.0+24.0j, 25.0+26.0j, 27.0+28.0j, 29.0+30.0j) expected_result = (33.0+36.0j, 39.0+42.0j, 45.0+48.0j, 51.0+54.0j, 57.0+60.0j) op = blocks_swig.add_cc(5) self.help_cc(5, (src1_data, src2_data, src3_data), expected_result, op)
def test_add_vcc_one(self): src1_data = (1.0 + 2.0j, ) src2_data = (3.0 + 4.0j, ) src3_data = (5.0 + 6.0j, ) expected_result = (9.0 + 12j, ) op = blocks_swig.add_cc(1) self.help_cc(1, (src1_data, src2_data, src3_data), expected_result, op)
def test_001_detect (self): """ Send two bursts, with zeros in between, and check they are both detected at the correct position and no false alarms occur """ n_zeros = 15 fft_len = 32 cp_len = 4 sig_len = (fft_len + cp_len) * 10 sync_symbol = [(random.randint(0, 1)*2)-1 for x in range(fft_len/2)] * 2 tx_signal = [0,] * n_zeros + \ sync_symbol[-cp_len:] + \ sync_symbol + \ [(random.randint(0, 1)*2)-1 for x in range(sig_len)] tx_signal = tx_signal * 2 add = blocks.add_cc() sync = digital.ofdm_sync_sc_cfb(fft_len, cp_len) sink_freq = blocks.vector_sink_f() sink_detect = blocks.vector_sink_b() self.tb.connect(blocks.vector_source_c(tx_signal), (add, 0)) self.tb.connect(analog.noise_source_c(analog.GR_GAUSSIAN, .01), (add, 1)) self.tb.connect(add, sync) self.tb.connect((sync, 0), sink_freq) self.tb.connect((sync, 1), sink_detect) self.tb.run() sig1_detect = sink_detect.data()[0:len(tx_signal)/2] sig2_detect = sink_detect.data()[len(tx_signal)/2:] self.assertTrue(abs(sig1_detect.index(1) - (n_zeros + fft_len + cp_len)) < cp_len) self.assertTrue(abs(sig2_detect.index(1) - (n_zeros + fft_len + cp_len)) < cp_len) self.assertEqual(numpy.sum(sig1_detect), 1) self.assertEqual(numpy.sum(sig2_detect), 1)
def test_003_multiburst(self): """ Send several bursts, see if the number of detects is correct. Burst lengths and content are random. """ n_bursts = 42 fft_len = 32 cp_len = 4 tx_signal = [] for i in xrange(n_bursts): sync_symbol = [(random.randint(0, 1) * 2) - 1 for x in range(fft_len / 2)] * 2 tx_signal += [0,] * random.randint(0, 2*fft_len) + \ sync_symbol[-cp_len:] + \ sync_symbol + \ [(random.randint(0, 1)*2)-1 for x in range(fft_len * random.randint(5,23))] add = blocks.add_cc() sync = digital.ofdm_sync_sc_cfb(fft_len, cp_len) sink_freq = blocks.vector_sink_f() sink_detect = blocks.vector_sink_b() channel = channels.channel_model(0.005) self.tb.connect(blocks.vector_source_c(tx_signal), channel, sync) self.tb.connect((sync, 0), sink_freq) self.tb.connect((sync, 1), sink_detect) self.tb.run() n_bursts_detected = numpy.sum(sink_detect.data()) # We allow for one false alarm or missed burst self.assertTrue( abs(n_bursts_detected - n_bursts) <= 1, msg="""Because of statistics, it is possible (though unlikely) that the number of detected bursts differs slightly. If the number of detects is off by one or two, run the test again and see what happen. Detection error was: %d """ % (numpy.sum(sink_detect.data()) - n_bursts))
def test_003_multiburst (self): """ Send several bursts, see if the number of detects is correct. Burst lengths and content are random. """ n_bursts = 42 fft_len = 32 cp_len = 4 tx_signal = [] for i in xrange(n_bursts): sync_symbol = [(random.randint(0, 1)*2)-1 for x in range(fft_len/2)] * 2 tx_signal += [0,] * random.randint(0, 2*fft_len) + \ sync_symbol[-cp_len:] + \ sync_symbol + \ [(random.randint(0, 1)*2)-1 for x in range(fft_len * random.randint(5,23))] add = blocks.add_cc() sync = digital.ofdm_sync_sc_cfb(fft_len, cp_len) sink_freq = blocks.vector_sink_f() sink_detect = blocks.vector_sink_b() channel = channels.channel_model(0.005) self.tb.connect(blocks.vector_source_c(tx_signal), channel, sync) self.tb.connect((sync, 0), sink_freq) self.tb.connect((sync, 1), sink_detect) self.tb.run() n_bursts_detected = numpy.sum(sink_detect.data()) # We allow for one false alarm or missed burst self.assertTrue(abs(n_bursts_detected - n_bursts) <= 1, msg="""Because of statistics, it is possible (though unlikely) that the number of detected bursts differs slightly. If the number of detects is off by one or two, run the test again and see what happen. Detection error was: %d """ % (numpy.sum(sink_detect.data()) - n_bursts) )
def run_flow_graph(sync_sym1, sync_sym2, data_sym): top_block = gr.top_block() carr_offset = random.randint(-max_offset/2, max_offset/2) * 2 tx_data = shift_tuple(sync_sym1, carr_offset) + \ shift_tuple(sync_sym2, carr_offset) + \ shift_tuple(data_sym, carr_offset) channel = [rand_range(min_chan_ampl, max_chan_ampl) * numpy.exp(1j * rand_range(0, 2 * numpy.pi)) for x in range(fft_len)] src = blocks.vector_source_c(tx_data, False, fft_len) chan = blocks.multiply_const_vcc(channel) noise = analog.noise_source_c(analog.GR_GAUSSIAN, wgn_amplitude) add = blocks.add_cc(fft_len) chanest = digital.ofdm_chanest_vcvc(sync_sym1, sync_sym2, 1) sink = blocks.vector_sink_c(fft_len) top_block.connect(src, chan, (add, 0), chanest, sink) top_block.connect(noise, blocks.stream_to_vector(gr.sizeof_gr_complex, fft_len), (add, 1)) top_block.run() channel_est = None carr_offset_hat = 0 rx_sym_est = [0,] * fft_len tags = sink.tags() for tag in tags: if pmt.symbol_to_string(tag.key) == 'ofdm_sync_carr_offset': carr_offset_hat = pmt.to_long(tag.value) self.assertEqual(carr_offset, carr_offset_hat) if pmt.symbol_to_string(tag.key) == 'ofdm_sync_chan_taps': channel_est = shift_tuple(pmt.c32vector_elements(tag.value), carr_offset) shifted_carrier_mask = shift_tuple(carrier_mask, carr_offset) for i in range(fft_len): if shifted_carrier_mask[i] and channel_est[i]: self.assertAlmostEqual(channel[i], channel_est[i], places=0) rx_sym_est[i] = (sink.data()[i] / channel_est[i]).real return (carr_offset, list(shift_tuple(rx_sym_est, -carr_offset_hat)))
def test_001_detect(self): """ Send two bursts, with zeros in between, and check they are both detected at the correct position and no false alarms occur """ n_zeros = 15 fft_len = 32 cp_len = 4 sig_len = (fft_len + cp_len) * 10 sync_symbol = [(random.randint(0, 1) * 2) - 1 for x in range(fft_len / 2)] * 2 tx_signal = [0,] * n_zeros + \ sync_symbol[-cp_len:] + \ sync_symbol + \ [(random.randint(0, 1)*2)-1 for x in range(sig_len)] tx_signal = tx_signal * 2 add = blocks.add_cc() sync = digital.ofdm_sync_sc_cfb(fft_len, cp_len) sink_freq = blocks.vector_sink_f() sink_detect = blocks.vector_sink_b() self.tb.connect(blocks.vector_source_c(tx_signal), (add, 0)) self.tb.connect(analog.noise_source_c(analog.GR_GAUSSIAN, .01), (add, 1)) self.tb.connect(add, sync) self.tb.connect((sync, 0), sink_freq) self.tb.connect((sync, 1), sink_detect) self.tb.run() sig1_detect = sink_detect.data()[0:len(tx_signal) / 2] sig2_detect = sink_detect.data()[len(tx_signal) / 2:] self.assertTrue( abs(sig1_detect.index(1) - (n_zeros + fft_len + cp_len)) < cp_len) self.assertTrue( abs(sig2_detect.index(1) - (n_zeros + fft_len + cp_len)) < cp_len) self.assertEqual(numpy.sum(sig1_detect), 1) self.assertEqual(numpy.sum(sig2_detect), 1)
def test_add_vcc_one(self): src1_data = (1.0+2.0j,) src2_data = (3.0+4.0j,) src3_data = (5.0+6.0j,) expected_result = (9.0+12j,) op = blocks_swig.add_cc(1) self.help_cc(1, (src1_data, src2_data, src3_data), expected_result, op)
def test_add_vcc_five(self): src1_data = (1.0 + 2.0j, 3.0 + 4.0j, 5.0 + 6.0j, 7.0 + 8.0j, 9.0 + 10.0j) src2_data = (11.0 + 12.0j, 13.0 + 14.0j, 15.0 + 16.0j, 17.0 + 18.0j, 19.0 + 20.0j) src3_data = (21.0 + 22.0j, 23.0 + 24.0j, 25.0 + 26.0j, 27.0 + 28.0j, 29.0 + 30.0j) expected_result = (33.0 + 36.0j, 39.0 + 42.0j, 45.0 + 48.0j, 51.0 + 54.0j, 57.0 + 60.0j) op = blocks_swig.add_cc(5) self.help_cc(5, (src1_data, src2_data, src3_data), expected_result, op)
def test_000(self): N = 1000 # number of samples to use M = 5 # Number of channels fs = 1000 # baseband sampling rate ifs = M * fs # input samp rate to decimator channel = 0 # Extract channel 0 taps = filter.firdes.low_pass_2( 1, ifs, fs / 2, fs / 10, attenuation_dB=80, window=filter.firdes.WIN_BLACKMAN_hARRIS) signals = list() add = blocks.add_cc() freqs = [-200, -100, 0, 100, 200] for i in xrange(len(freqs)): f = freqs[i] + (M / 2 - M + i + 1) * fs data = sig_source_c(ifs, f, 1, N) signals.append(blocks.vector_source_c(data)) self.tb.connect(signals[i], (add, i)) head = blocks.head(gr.sizeof_gr_complex, N) s2ss = blocks.stream_to_streams(gr.sizeof_gr_complex, M) pfb = filter.pfb_decimator_ccf(M, taps, channel) snk = blocks.vector_sink_c() self.tb.connect(add, head, s2ss) for i in xrange(M): self.tb.connect((s2ss, i), (pfb, i)) self.tb.connect(pfb, snk) self.tb.run() Ntest = 50 L = len(snk.data()) t = map(lambda x: float(x) / fs, xrange(L)) # Create known data as complex sinusoids for the baseband freq # of the extracted channel is due to decimator output order. phase = 0 expected_data = map(lambda x: math.cos(2.*math.pi*freqs[2]*x+phase) + \ 1j*math.sin(2.*math.pi*freqs[2]*x+phase), t) dst_data = snk.data() self.assertComplexTuplesAlmostEqual(expected_data[-Ntest:], dst_data[-Ntest:], 4)
def __init__(self, constellation, f, N0=0.25, seed=-666L): """ constellation - a constellation object used for modulation. f - a finite state machine specification used for coding. N0 - noise level seed - random seed """ super(trellis_tb, self).__init__() # packet size in bits (make it multiple of 16 so it can be packed in a short) packet_size = 1024 * 16 # bits per FSM input symbol bitspersymbol = int(round(math.log(f.I()) / math.log(2))) # bits per FSM input symbol # packet size in trellis steps K = packet_size / bitspersymbol # TX src = blocks.lfsr_32k_source_s() # packet size in shorts src_head = blocks.head(gr.sizeof_short, packet_size / 16) # unpack shorts to symbols compatible with the FSM input cardinality s2fsmi = blocks.packed_to_unpacked_ss(bitspersymbol, gr.GR_MSB_FIRST) # initial FSM state = 0 enc = trellis.encoder_ss(f, 0) mod = digital.chunks_to_symbols_sc(constellation.points(), 1) # CHANNEL add = blocks.add_cc() noise = analog.noise_source_c(analog.GR_GAUSSIAN, math.sqrt(N0 / 2), seed) # RX # data preprocessing to generate metrics for Viterbi metrics = trellis.constellation_metrics_cf(constellation.base(), digital.TRELLIS_EUCLIDEAN) # Put -1 if the Initial/Final states are not set. va = trellis.viterbi_s(f, K, 0, -1) # pack FSM input symbols to shorts fsmi2s = blocks.unpacked_to_packed_ss(bitspersymbol, gr.GR_MSB_FIRST) # check the output self.dst = blocks.check_lfsr_32k_s() self.connect(src, src_head, s2fsmi, enc, mod) self.connect(mod, (add, 0)) self.connect(noise, (add, 1)) self.connect(add, metrics, va, fsmi2s, self.dst)
def test_000(self): N = 1000 # number of samples to use M = 5 # Number of channels fs = 1000 # baseband sampling rate ifs = M*fs # input samp rate to decimator channel = 0 # Extract channel 0 taps = filter.firdes.low_pass_2(1, ifs, fs/2, fs/10, attenuation_dB=80, window=filter.firdes.WIN_BLACKMAN_hARRIS) signals = list() add = blocks.add_cc() freqs = [-200, -100, 0, 100, 200] for i in xrange(len(freqs)): f = freqs[i] + (M/2-M+i+1)*fs data = sig_source_c(ifs, f, 1, N) signals.append(blocks.vector_source_c(data)) self.tb.connect(signals[i], (add,i)) head = blocks.head(gr.sizeof_gr_complex, N) s2ss = blocks.stream_to_streams(gr.sizeof_gr_complex, M) pfb = filter.pfb_decimator_ccf(M, taps, channel) snk = blocks.vector_sink_c() self.tb.connect(add, head, s2ss) for i in xrange(M): self.tb.connect((s2ss,i), (pfb,i)) self.tb.connect(pfb, snk) self.tb.run() Ntest = 50 L = len(snk.data()) t = map(lambda x: float(x)/fs, xrange(L)) # Create known data as complex sinusoids for the baseband freq # of the extracted channel is due to decimator output order. phase = 0 expected_data = map(lambda x: math.cos(2.*math.pi*freqs[2]*x+phase) + \ 1j*math.sin(2.*math.pi*freqs[2]*x+phase), t) dst_data = snk.data() self.assertComplexTuplesAlmostEqual(expected_data[-Ntest:], dst_data[-Ntest:], 4)
def __init__(self, noise_voltage, freq, timing): gr.hier_block2.__init__(self, "channel_model", gr.io_signature(1, 1, gr.sizeof_gr_complex), gr.io_signature(1, 1, gr.sizeof_gr_complex)) timing_offset = filter.fractional_resampler_cc(0, timing) noise_adder = blocks.add_cc() noise = analog.noise_source_c(analog.GR_GAUSSIAN, noise_voltage, 0) freq_offset = analog.sig_source_c(1, analog.GR_SIN_WAVE, freq, 1.0, 0.0) mixer_offset = blocks.multiply_cc() self.connect(self, timing_offset) self.connect(timing_offset, (mixer_offset, 0)) self.connect(freq_offset, (mixer_offset, 1)) self.connect(mixer_offset, (noise_adder, 1)) self.connect(noise, (noise_adder, 0)) self.connect(noise_adder, self)
def run_flow_graph(sync_sym1, sync_sym2, data_sym): top_block = gr.top_block() carr_offset = random.randint(-max_offset / 2, max_offset / 2) * 2 tx_data = shift_tuple(sync_sym1, carr_offset) + \ shift_tuple(sync_sym2, carr_offset) + \ shift_tuple(data_sym, carr_offset) channel = [ rand_range(min_chan_ampl, max_chan_ampl) * numpy.exp(1j * rand_range(0, 2 * numpy.pi)) for x in range(fft_len) ] src = blocks.vector_source_c(tx_data, False, fft_len) chan = blocks.multiply_const_vcc(channel) noise = analog.noise_source_c(analog.GR_GAUSSIAN, wgn_amplitude) add = blocks.add_cc(fft_len) chanest = digital.ofdm_chanest_vcvc(sync_sym1, sync_sym2, 1) sink = blocks.vector_sink_c(fft_len) top_block.connect(src, chan, (add, 0), chanest, sink) top_block.connect( noise, blocks.stream_to_vector(gr.sizeof_gr_complex, fft_len), (add, 1)) top_block.run() channel_est = None carr_offset_hat = 0 rx_sym_est = [ 0, ] * fft_len tags = sink.tags() for tag in tags: if pmt.symbol_to_string(tag.key) == 'ofdm_sync_carr_offset': carr_offset_hat = pmt.to_long(tag.value) self.assertEqual(carr_offset, carr_offset_hat) if pmt.symbol_to_string(tag.key) == 'ofdm_sync_chan_taps': channel_est = shift_tuple( pmt.c32vector_elements(tag.value), carr_offset) shifted_carrier_mask = shift_tuple(carrier_mask, carr_offset) for i in range(fft_len): if shifted_carrier_mask[i] and channel_est[i]: self.assertAlmostEqual(channel[i], channel_est[i], places=0) rx_sym_est[i] = (sink.data()[i] / channel_est[i]).real return (carr_offset, list(shift_tuple(rx_sym_est, -carr_offset_hat)))
def __init__(self, noise_voltage, freq, timing): gr.hier_block2.__init__( self, "channel_model", gr.io_signature(1, 1, gr.sizeof_gr_complex), gr.io_signature(1, 1, gr.sizeof_gr_complex), ) timing_offset = filter.fractional_interpolator_cc(0, timing) noise_adder = blocks.add_cc() noise = analog.noise_source_c(analog.GR_GAUSSIAN, noise_voltage, 0) freq_offset = analog.sig_source_c(1, analog.GR_SIN_WAVE, freq, 1.0, 0.0) mixer_offset = blocks.multiply_cc() self.connect(self, timing_offset) self.connect(timing_offset, (mixer_offset, 0)) self.connect(freq_offset, (mixer_offset, 1)) self.connect(mixer_offset, (noise_adder, 1)) self.connect(noise, (noise_adder, 0)) self.connect(noise_adder, self)
def test_000(self): N = 1000 # number of samples to use M = 5 # Number of channels to channelize fs = 1000 # baseband sampling rate ifs = M*fs # input samp rate to channelizer taps = filter.firdes.low_pass_2(1, ifs, 500, 50, attenuation_dB=80, window=filter.firdes.WIN_BLACKMAN_hARRIS) signals = list() add = blocks.add_cc() freqs = [-200, -100, 0, 100, 200] for i in xrange(len(freqs)): f = freqs[i] + (M/2-M+i+1)*fs data = sig_source_c(ifs, f, 1, N) signals.append(blocks.vector_source_c(data)) self.tb.connect(signals[i], (add,i)) s2ss = blocks.stream_to_streams(gr.sizeof_gr_complex, M) pfb = filter.pfb_channelizer_ccf(M, taps, 1) self.tb.connect(add, s2ss) snks = list() for i in xrange(M): snks.append(blocks.vector_sink_c()) self.tb.connect((s2ss,i), (pfb,i)) self.tb.connect((pfb, i), snks[i]) self.tb.run() Ntest = 50 L = len(snks[0].data()) t = map(lambda x: float(x)/fs, xrange(L)) # Adjusted phase rotations for data p0 = 0 p1 = math.pi*0.51998885 p2 = -math.pi*0.96002233 p3 = math.pi*0.96002233 p4 = -math.pi*0.51998885 # Create known data as complex sinusoids at the different baseband freqs # the different channel numbering is due to channelizer output order. expected0_data = map(lambda x: math.cos(2.*math.pi*freqs[2]*x+p0) + \ 1j*math.sin(2.*math.pi*freqs[2]*x+p0), t) expected1_data = map(lambda x: math.cos(2.*math.pi*freqs[3]*x+p1) + \ 1j*math.sin(2.*math.pi*freqs[3]*x+p1), t) expected2_data = map(lambda x: math.cos(2.*math.pi*freqs[4]*x+p2) + \ 1j*math.sin(2.*math.pi*freqs[4]*x+p2), t) expected3_data = map(lambda x: math.cos(2.*math.pi*freqs[0]*x+p3) + \ 1j*math.sin(2.*math.pi*freqs[0]*x+p3), t) expected4_data = map(lambda x: math.cos(2.*math.pi*freqs[1]*x+p4) + \ 1j*math.sin(2.*math.pi*freqs[1]*x+p4), t) dst0_data = snks[0].data() dst1_data = snks[1].data() dst2_data = snks[2].data() dst3_data = snks[3].data() dst4_data = snks[4].data() self.assertComplexTuplesAlmostEqual(expected0_data[-Ntest:], dst0_data[-Ntest:], 3) self.assertComplexTuplesAlmostEqual(expected1_data[-Ntest:], dst1_data[-Ntest:], 3) self.assertComplexTuplesAlmostEqual(expected2_data[-Ntest:], dst2_data[-Ntest:], 3) self.assertComplexTuplesAlmostEqual(expected3_data[-Ntest:], dst3_data[-Ntest:], 3) self.assertComplexTuplesAlmostEqual(expected4_data[-Ntest:], dst4_data[-Ntest:], 3)
def test_add_cc(self): src1_data = (1+1j, 2+2j, 3+3j, 4+4j, 5+5j) src2_data = (8+8j, -3-3j, 4+4j, 8+8j, 2+2j) expected_result = (9+9j, -1-1j, 7+7j, 12+12j, 7+7j) op = blocks_swig.add_cc() self.help_cc((src1_data, src2_data), expected_result, op)
def test_add_cc(self): src1_data = (1 + 1j, 2 + 2j, 3 + 3j, 4 + 4j, 5 + 5j) src2_data = (8 + 8j, -3 - 3j, 4 + 4j, 8 + 8j, 2 + 2j) expected_result = (9 + 9j, -1 - 1j, 7 + 7j, 12 + 12j, 7 + 7j) op = blocks_swig.add_cc() self.help_cc((src1_data, src2_data), expected_result, op)