def qamtest (self, nbits): data = tuple(range(2**nbits)) src = gr.vector_source_b(data) unpack = gr.unpack_k_bits_bb(nbits) enc = raw.qam_enc(nbits) dec = raw.qam_dec(nbits) tofloat = gr.uchar_to_float() slicer = gr.binary_slicer_fb() unpacked = gr.vector_sink_b() encoded = gr.vector_sink_c() decoded = gr.vector_sink_b() dst = gr.vector_sink_b() self.tb.connect(src, unpack, enc, dec, tofloat, gr.add_const_ff(-128.0), slicer, dst) self.tb.connect(unpack, unpacked) self.tb.connect(enc, encoded) self.tb.connect(dec, decoded) self.tb.run() #print "data = ", data #print "unpacked = ", unpacked.data() #print "encoded = ", encoded.data() #print "decoded = ", decoded.data() #print "dst = ", dst.data() self.assertEqual(unpacked.data(), dst.data()) pwr = sum([abs(x)**2 for x in encoded.data()]) / len(encoded.data()) #print pwr self.assertAlmostEqual(1.0, pwr, 6)
def addfreq(self, freq): self.addfreq_edit.clearFocus() self.addfreq_edit.clear() freq_txt = "%.6fMHz" % (freq / 1e6) if freq_txt in self.freqs: self.push_text("%s is already monitored!" % freq_txt) return if abs(self.centerfreq - freq) > self.samplerate / 2.0: self.push_text("%s is outside of the bandwidth reach!" % freq_txt) return self.push_text("Monitoring %s" % freq_txt) freqshift = self.centerfreq - freq # reconfigure the flowgraph # We use stop()/wait() because lock()/unlock() seems to freeze the app # Can't find the reason... self.topblock.stop() self.topblock.wait() # self.topblock.lock() freq_xlating_fir_filter = gr.freq_xlating_fir_filter_ccc(1, (gr.firdes.low_pass(1.0, self.samplerate, XLATING_CUTOFF, XLATING_CUTOFF / 2)), freqshift, self.samplerate) pocsag_decoder = pocsag.pocsag_decoder(self.samplerate, channel_str = freq_txt, symbolrate = self.symrate, debug = self.debug_check.isChecked()) msgsink = pocsag_msgsink() # FIXME: Shouldn't we use only one general msgsink? self.topblock.connect(self.topblock.source, freq_xlating_fir_filter, pocsag_decoder, msgsink) # Connect the QT signal from the msgsink to the UI msgsink.pocsag_pagermsg.connect(self.push_pagermsg) # self.topblock.unlock() self.topblock.start() # Save the blocks self.freqs[freq_txt] = { "freq": freq, "freq_xlating_fir_filter": freq_xlating_fir_filter, "pocsag_decoder": pocsag_decoder, "msgsink": msgsink, "uchar2float": gr.uchar_to_float() # we need a converter to connect it to the qtsink } self.freq_list.addItem(freq_txt)
def punctest(self, nc, np): length = 2046 # should be divisible by nc data = [random.randint(0, 255) for i in range(length)] # last K-1 bits are padding, meaning last byte is lost data[-1] = 0 data = tuple(data) src = gr.vector_source_b(data) enc = raw.conv_enc() dec = raw.conv_dec(length * 8) punc = raw.conv_punc(nc, np) depunc = raw.conv_punc(np, nc, 128) tofloat = gr.uchar_to_float() offset = gr.multiply_const_ff(255.0) touchar = gr.float_to_uchar() dst = gr.vector_sink_b() rx = gr.vector_sink_b() self.tb.connect(src, enc, punc, tofloat, offset, touchar, depunc, dec, dst) self.tb.connect(punc, rx) self.tb.run() rxlen = len(rx.data()) self.assertEqual(rxlen, (length * 8 * 2 * np) / nc) self.assertEqual(data, dst.data())
def qamtest(self, nbits): data = tuple(range(2**nbits)) src = gr.vector_source_b(data) unpack = gr.unpack_k_bits_bb(nbits) enc = raw.qam_enc(nbits) dec = raw.qam_dec(nbits) tofloat = gr.uchar_to_float() slicer = gr.binary_slicer_fb() unpacked = gr.vector_sink_b() encoded = gr.vector_sink_c() decoded = gr.vector_sink_b() dst = gr.vector_sink_b() self.tb.connect(src, unpack, enc, dec, tofloat, gr.add_const_ff(-128.0), slicer, dst) self.tb.connect(unpack, unpacked) self.tb.connect(enc, encoded) self.tb.connect(dec, decoded) self.tb.run() #print "data = ", data #print "unpacked = ", unpacked.data() #print "encoded = ", encoded.data() #print "decoded = ", decoded.data() #print "dst = ", dst.data() self.assertEqual(unpacked.data(), dst.data()) pwr = sum([abs(x)**2 for x in encoded.data()]) / len(encoded.data()) #print pwr self.assertAlmostEqual(1.0, pwr, 6)
def test_001_enc(self): length = 2048 # DANGER: this fails for > 2048 for some reason data = [random.randint(0, 255) for i in range(length)] # last K-1 bits are padding, meaning last byte is lost data[-1] = 0 data = tuple(data) src = gr.vector_source_b(data) enc = raw.conv_enc() tofloat = gr.uchar_to_float() offset = gr.multiply_const_ff(255.0) touchar = gr.float_to_uchar() dec = raw.conv_dec(length * 8) dst = gr.vector_sink_b() self.tb.connect(src, enc, tofloat, offset, touchar, dec, dst) self.tb.run() # self.assertEqual (data, dst.data()) nerrors = 0 i = 0 for (a, b) in itertools.izip(data, dst.data()): nerr = bitCount(a ^ b) if nerr: print "%g " % (i / 2048.0), nerr nerrors += nerr i += 1 print "Number or Errors %d BER %g" % (nerrors, (nerrors * 1.0 / (length * 8))) self.assertEqual(nerrors, 0)
def test_001_enc(self): length = 2048 # DANGER: this fails for > 2048 for some reason data = [random.randint(0, 255) for i in range(length)] # last K-1 bits are padding, meaning last byte is lost data[-1] = 0 data = tuple(data) src = gr.vector_source_b(data) enc = raw.conv_enc() tofloat = gr.uchar_to_float() offset = gr.multiply_const_ff(255.0) touchar = gr.float_to_uchar() dec = raw.conv_dec(length * 8) dst = gr.vector_sink_b() self.tb.connect(src, enc, tofloat, offset, touchar, dec, dst) self.tb.run() #self.assertEqual (data, dst.data()) nerrors = 0 i = 0 for (a, b) in itertools.izip(data, dst.data()): nerr = bitCount(a ^ b) if nerr: print "%g " % (i / 2048.), nerr nerrors += nerr i += 1 print "Number or Errors %d BER %g" % (nerrors, (nerrors * 1.0 / (length * 8))) self.assertEqual(nerrors, 0)
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 = 1000 ################################################## # Blocks ################################################## self.const_source_x_0 = gr.sig_source_f(0, gr.GR_CONST_WAVE, 0, 0, 0) self.gr_descrambler_bb_0 = gr.descrambler_bb(0x8A, 0x7F, 7) self.gr_float_to_complex_0 = gr.float_to_complex(1) self.gr_packed_to_unpacked_xx_0 = gr.packed_to_unpacked_bb(1, gr.GR_MSB_FIRST) self.gr_scrambler_bb_0 = gr.scrambler_bb(0x8A, 0x7F, 7) self.gr_throttle_0 = gr.throttle(gr.sizeof_gr_complex*1, samp_rate) self.gr_throttle_0_0 = gr.throttle(gr.sizeof_char*1, samp_rate) self.gr_throttle_0_0_0 = gr.throttle(gr.sizeof_char*1, samp_rate*8) self.gr_throttle_0_0_0_0 = gr.throttle(gr.sizeof_char*1, samp_rate*8*8) self.gr_uchar_to_float_0 = gr.uchar_to_float() self.gr_unpacked_to_packed_xx_0 = gr.unpacked_to_packed_bb(1, gr.GR_MSB_FIRST) self.gr_vector_source_x_0 = gr.vector_source_b((0, 1, 3,7,255,3,1,0), True, 1) self.wxgui_scopesink2_0 = scopesink2.scope_sink_c( self.GetWin(), title="Scope Plot", sample_rate=samp_rate*32, v_scale=0, v_offset=0, t_scale=0, ac_couple=False, xy_mode=False, num_inputs=1, ) self.Add(self.wxgui_scopesink2_0.win) ################################################## # Connections ################################################## self.connect((self.gr_float_to_complex_0, 0), (self.gr_throttle_0, 0)) self.connect((self.const_source_x_0, 0), (self.gr_float_to_complex_0, 1)) self.connect((self.gr_throttle_0, 0), (self.wxgui_scopesink2_0, 0)) self.connect((self.gr_vector_source_x_0, 0), (self.gr_throttle_0_0, 0)) self.connect((self.gr_throttle_0_0, 0), (self.gr_packed_to_unpacked_xx_0, 0)) self.connect((self.gr_descrambler_bb_0, 0), (self.gr_unpacked_to_packed_xx_0, 0)) self.connect((self.gr_packed_to_unpacked_xx_0, 0), (self.gr_throttle_0_0_0, 0)) self.connect((self.gr_throttle_0_0_0, 0), (self.gr_scrambler_bb_0, 0)) self.connect((self.gr_scrambler_bb_0, 0), (self.gr_throttle_0_0_0_0, 0)) self.connect((self.gr_throttle_0_0_0_0, 0), (self.gr_descrambler_bb_0, 0)) self.connect((self.gr_uchar_to_float_0, 0), (self.gr_float_to_complex_0, 0)) self.connect((self.gr_unpacked_to_packed_xx_0, 0), (self.gr_uchar_to_float_0, 0))
def test_ccsds_27(self): src_data = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) expected = (0, 0, 0, 0, 1, 2, 3, 4, 5, 6) src = gr.vector_source_b(src_data) enc = gr.encode_ccsds_27_bb() b2f = gr.uchar_to_float() add = gr.add_const_ff(-0.5) mul = gr.multiply_const_ff(2.0) dec = gr.decode_ccsds_27_fb() dst = gr.vector_sink_b() self.tb.connect(src, enc, b2f, add, mul, dec, dst) self.tb.run() dst_data = dst.data() self.assertEqual(expected, dst_data)
def test_ccsds_27 (self): src_data = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) expected = (0, 0, 0, 0, 1, 2, 3, 4, 5, 6) src = gr.vector_source_b(src_data) enc = gr.encode_ccsds_27_bb() b2f = gr.uchar_to_float() add = gr.add_const_ff(-0.5) mul = gr.multiply_const_ff(2.0) dec = gr.decode_ccsds_27_fb() dst = gr.vector_sink_b() self.tb.connect(src, enc, b2f, add, mul, dec, dst) self.tb.run() dst_data = dst.data() self.assertEqual(expected, dst_data)
def __init__(self, bits_per_byte): gr.hier_block2.__init__(self, "BitErrors", gr.io_signature(2, 2, gr.sizeof_char), gr.io_signature(1, 1, gr.sizeof_int)) # Bit comparison comp = gr.xor_bb() intdump_decim = 100000 if N_BITS < intdump_decim: intdump_decim = int(N_BITS) self.connect(self, comp, gr.unpack_k_bits_bb(bits_per_byte), gr.uchar_to_float(), gr.integrate_ff(intdump_decim), gr.multiply_const_ff(1.0 / N_BITS), self) self.connect((self, 1), (comp, 1))
def __init__(self, bits_per_byte): gr.hier_block2.__init__(self, "BitErrors", gr.io_signature(2, 2, gr.sizeof_char), gr.io_signature(1, 1, gr.sizeof_int)) # Bit comparison comp = gr.xor_bb() intdump_decim = 100000 if N_BITS < intdump_decim: intdump_decim = int(N_BITS) self.connect(self, comp, gr.unpack_k_bits_bb(bits_per_byte), gr.uchar_to_float(), gr.integrate_ff(intdump_decim), gr.multiply_const_ff(1.0/N_BITS), self) self.connect((self, 1), (comp, 1))
def __init__(self, center_freq, offset_freq, decimate_am=1, play_audio=False, filename=None): """Configure the RTL-SDR and GNU Radio""" super(rtlsdr_am_stream, self).__init__() audio_rate = 44100 device_rate = audio_rate * 25 output_rate = audio_rate / float(decimate_am) self.rate = output_rate filename = filename or "/home/martin/src/rtl/hydrometer/rawrrr" self.gr_file_source_0 = gr.file_source( gr.sizeof_char * 1, filename, False) self.gr_deinterleave_0 = gr.deinterleave(gr.sizeof_char * 1) # self.gr_uchar_to_float_1 = gr.uchar_to_float() self.gr_uchar_to_float_0 = gr.uchar_to_float() # self.gr_add_const_vxx_1 = gr.add_const_vff((-127, )) # self.gr_add_const_vxx_0 = gr.add_const_vff((-127, )) # self.gr_multiply_const_vxx_1 = gr.multiply_const_vff((0.065, )) # self.gr_multiply_const_vxx_0 = gr.multiply_const_vff((0.065, )) # self.gr_float_to_complex_0 = gr.float_to_complex(1) # self.osmosdr_source = osmosdr.source_c("") # self.osmosdr_source.set_center_freq(freq) # self.osmosdr_source.set_sample_rate(device_rate) # taps = firdes.low_pass( # 1, device_rate, 40000, 5000, firdes.WIN_HAMMING, 6.76) # self.freq_filter = gr.freq_xlating_fir_filter_ccc( # 25, taps, -freq_offs, device_rate) # self.am_demod = blks2.am_demod_cf( # channel_rate=audio_rate, # audio_decim=1, # audio_pass=5000, # audio_stop=5500, # ) # self.resampler = blks2.rational_resampler_fff( # interpolation=1, # decimation=decimate_am, # ) self.sink = gr_queue.queue_sink_c() # self.connect(self.osmosdr_source, self.freq_filter, self.am_demod) self.connect((self.gr_file_source_0, 0), (self.gr_deinterleave_0, 0)) # self.connect((self.gr_deinterleave_0, 0), # (self.gr_uchar_to_float_0, 0)) # self.connect((self.gr_uchar_to_float_0, 0), # (self.gr_add_const_vxx_0, 0)) # self.connect((self.gr_add_const_vxx_0, 0), # (self.gr_multiply_const_vxx_0, 0)) # self.connect((self.gr_multiply_const_vxx_0, 0), # (self.gr_float_to_complex_0, 0)) # self.connect((self.gr_deinterleave_0, 1), # (self.gr_uchar_to_float_1, 0)) # self.connect((self.gr_uchar_to_float_1, 0), # (self.gr_add_const_vxx_1, 0)) # self.connect((self.gr_add_const_vxx_1, 0), # (self.gr_multiply_const_vxx_1, 0)) # self.connect((self.gr_multiply_const_vxx_1, 0), # (self.gr_float_to_complex_0, 1)) # self.connect( # self.gr_float_to_complex_0, self.freq_filter, self.am_demod) # self.connect(self.am_demod, self.resampler, self.sink) self.processed_source = (self.gr_deinterleave_0, 0) self.connect(self.processed_source, self.sink)
def test(self, ebno, data=None): random.seed(0) (nc, np) = self.puncparam # round number of bytes and symbols (FIXME, this is probably not always round) # one symbol = self.ncarriers * self.qambits * 0.5 * (np / nc) bits symbits = self.ncarriers * self.qambits * nc / (2. * np) #print "symbits = ", symbits assert int(self.nsymbols * symbits) % 8 == 0 length = int(self.nsymbols * symbits) / 8 # incl padding #print 'src: %d' % (length-1) #print 'pad: %d' % length #print 'fenc: %d' % (length * 8 * 2) #print 'punc: %f' % (length * 8 * 2. * np / nc) #print 'intrlv: %f' % ((length * 8 * 2. * np / nc) / (self.ncarriers*self.qambits)) #print 'total bits: %d' % (self.nframes * length * 8 * 2) if data is None: data = [ random.randint(0,255) for i in range((length-1)*self.nframes) ] data = tuple(data) src = gr.vector_source_b(data) #src = gr.file_source(gr.sizeof_char, "src1.datb"); dst = gr.vector_sink_b() fenc = raw.conv_enc() fdec = raw.conv_dec(length*8) punc = raw.conv_punc(nc, np) depunc = raw.conv_punc(np, nc, 128) pad = raw.conv_punc(length-1, length) depad = raw.conv_punc(length, length-1) intrlv = raw.intrlv_bit(self.ncarriers, self.qambits, False) deintrlv = raw.intrlv_bit(self.ncarriers, self.qambits, True) qenc = raw.qam_enc(self.qambits) qdec = raw.qam_dec(self.qambits) SNR = 10.0**(ebno/10.0) noise_power_in_channel = 1.0/SNR noise_voltage = math.sqrt(noise_power_in_channel/2.0) # AWGN only noise = gr.noise_source_c(gr.GR_GAUSSIAN, noise_voltage, 0) chan = gr.add_cc() self.tb.connect(noise, (chan, 1)) #chan = gr.channel_model(noise_voltage, 0.0, 1.0, [1.0, 0.0]), tofloat = gr.uchar_to_float() offset = gr.multiply_const_ff(255.0) touchar = gr.float_to_uchar() self.tb.connect(src, # length-1 pad, # length fenc, # 2*length punc, # 2*length*np/nc intrlv, # 2*length*np/nc (% ncarriers*qambits) qenc, # 2*length*np/nc / qambits (% ncarriers) chan, qdec, #tofloat, offset, touchar, deintrlv, depunc, fdec, depad, dst) #self.tb.connect(src, gr.file_sink(gr.sizeof_char, "src.datb")) #self.tb.connect(pad, gr.file_sink(gr.sizeof_char, "pad.datb")) #self.tb.connect(fenc, gr.file_sink(gr.sizeof_char, "fenc.datb")) #self.tb.connect(punc, gr.file_sink(gr.sizeof_char, "punc.datb")) #self.tb.connect(qenc, gr.file_sink(gr.sizeof_gr_complex, "qenc.dat")) #self.tb.connect(qdec, gr.file_sink(gr.sizeof_char, "qdec.datb")) #self.tb.connect(touchar, gr.file_sink(gr.sizeof_char, "qdec.datb")) #self.tb.connect(depunc, gr.file_sink(gr.sizeof_char, "depunc.datb")) #self.tb.connect(fdec, gr.file_sink(gr.sizeof_char, "fdec.datb")) #self.tb.connect(depad, gr.file_sink(gr.sizeof_char, "depad.datb")) self.tb.run() nerrors = 0 for (a,b) in itertools.izip(data, dst.data()): count = bitCount(a ^ b) nerrors += count ber = nerrors*1.0/len(data)/8 print "%d x %g @ %g dB\t#errors = %d\tBER = %g" % (self.qambits, nc/(2.0*np), ebno, nerrors, ber) return nerrors