Beispiel #1
0
    def __init__(self, frequency):
        gr.top_block.__init__(self)

        sample_rate = 32000
        ampl = 0.1

        # Source.
        addr = ''
        stream_args = uhd.stream_args('fc32')
        self.src = uhd.usrp_source(addr, stream_args)
        #src.set_subdev_spec(options.spec, 0)
        self.src.set_samp_rate(32000)
        self.src.set_center_freq(frequency)
        self.src.set_gain(30)
        self.src.set_antenna('TX/RX')

        # SNR
        snr = digital.mpsk_snr_est_cc(type=0)
        snr.set_alpha(0.001)
        snr.set_tag_nsample(1000000)

        rms = blocks.rms_cf()
        rms.set_alpha(0.0001)

        self.msgq = gr.msg_queue(16)

        sink = blocks.message_sink(4, self.msgq, 'bleh')
        self.connect(self.src, snr, rms, sink)
Beispiel #2
0
    def __init__(self, frequency):
        gr.top_block.__init__(self)

        sample_rate = 32000
        ampl = 0.1

        # Source.
        addr = ''
        stream_args = uhd.stream_args('fc32')
        self.src = uhd.usrp_source(addr, stream_args)
        #src.set_subdev_spec(options.spec, 0)
        self.src.set_samp_rate(32000)
        self.src.set_center_freq(frequency)
        self.src.set_gain(30)
        self.src.set_antenna('TX/RX')

        # SNR
        snr = digital.mpsk_snr_est_cc(type=0)
        snr.set_alpha(0.001)
        snr.set_tag_nsample(1000000)

        rms = blocks.rms_cf()
        rms.set_alpha(0.0001)

        self.msgq = gr.msg_queue(16)

        sink = blocks.message_sink(4, self.msgq, 'bleh')
        self.connect(self.src, snr, rms, sink)
    def test_mpsk_snr_est_svn(self):
        expected_result = [10.92, 6.02, 4.78, 4.98, 5.51]

        N = 10000
        alpha = 0.001
        op = digital.mpsk_snr_est_cc(digital.SNR_EST_SVR, N, alpha)

        actual_result = self.mpsk_snr_est_setup(op)
        self.assertFloatTuplesAlmostEqual(expected_result, actual_result, 2)
    def test_mpsk_snr_est_m2m4(self):
        expected_result = [11.02, 6.20, 4.98, 5.16, 5.66]

        N = 10000
        alpha = 0.001
        op = digital.mpsk_snr_est_cc(digital.SNR_EST_M2M4, N, alpha)

        actual_result = self.mpsk_snr_est_setup(op)
        self.assertFloatTuplesAlmostEqual(expected_result, actual_result, 2)
    def test_mpsk_snr_est_simple(self):
	expected_result = [8.20, 4.99, 3.23, 2.01, 1.03]

        N = 10000
        alpha = 0.001
        op = digital.mpsk_snr_est_cc(digital.SNR_EST_SIMPLE, N, alpha)

        actual_result = self.mpsk_snr_est_setup(op)
        self.assertFloatTuplesAlmostEqual(expected_result, actual_result, 2)
    def test_mpsk_snr_est_m2m4(self):
	expected_result = [8.01, 3.19, 1.97, 2.15, 2.65]

        N = 10000
        alpha = 0.001
        op = digital.mpsk_snr_est_cc(digital.SNR_EST_M2M4, N, alpha)

        actual_result = self.mpsk_snr_est_setup(op)
        self.assertFloatTuplesAlmostEqual(expected_result, actual_result, 2)
    def test_mpsk_snr_est_svn(self):
	expected_result = [7.91, 3.01, 1.77, 1.97, 2.49]

        N = 10000
        alpha = 0.001
        op = digital.mpsk_snr_est_cc(digital.SNR_EST_SVR, N, alpha)

        actual_result = self.mpsk_snr_est_setup(op)
        self.assertFloatTuplesAlmostEqual(expected_result, actual_result, 2)
    def test_mpsk_snr_est_skew(self):
	expected_result = [8.31, 1.83, -1.68, -3.56, -4.68]

        N = 10000
        alpha = 0.001
        op = digital.mpsk_snr_est_cc(digital.SNR_EST_SKEW, N, alpha)

        actual_result = self.mpsk_snr_est_setup(op)
        self.assertFloatTuplesAlmostEqual(expected_result, actual_result, 2)
    def test_mpsk_snr_est_skew(self):
        expected_result = [11.48, 5.91, 3.30, 2.08, 1.46]

        N = 10000
        alpha = 0.001
        op = digital.mpsk_snr_est_cc(digital.SNR_EST_SKEW, N, alpha)

        actual_result = self.mpsk_snr_est_setup(op)
        self.assertFloatTuplesAlmostEqual(expected_result, actual_result, 2)
def simulate_other_est(SNR_range, est_type):
    """ Create flow graph, run, read out estimated SNR with usage of gnuradio implemented estimator
    :param SNR_range: range of SNR for which noise voltage will be calculated
    """
    est_snr = []
    for SNR in SNR_range:
        noise_volt = noise_voltage(SNR)

        src = blocks.vector_source_c(bits.tolist(), False)
        chn = snr.channel_model_snr(0.0, 0.0, 1.0, [1, ],
                                    0)  # noise_voltage, frequency_offset, epsilon, taps, noise_seed, block_tags
        est = digital.mpsk_snr_est_cc(est_type, ntag, alpha)
        sink = blocks.null_sink(gr.sizeof_gr_complex)

        chn.set_noise_voltage(noise_volt)
        tb = gr.top_block()
        tb.connect(src, chn, est, sink)
        tb.run()

        est_snr.append(est.snr())
    return est_snr
	def __init__(self, samps_per_sym, samp_rate, freq, file_sink, ampl):
		gr.hier_block2.__init__(self, "receive_path",
				gr.io_signature(0, 0, 0), # Input signature
				gr.io_signature(0, 0, 0)) # Output signature

		##################################################
		# Variables
		##################################################
		self.file_sink = file_sink
		self.rx_gain = rx_gain = 5
		self.samps_per_sym = samps_per_sym
		self.samp_rate = samp_rate
		self.freq = freq
		self.ampl = ampl 

		##################################################
		# Blocks
		##################################################
		self.uhd_usrp_source_0 = uhd.usrp_source(
			device_addr="",
			stream_args=uhd.stream_args(
				cpu_format="fc32",
				channels=range(1),
			),
		)
		self.uhd_usrp_source_0.set_samp_rate(samp_rate)
		self.uhd_usrp_source_0.set_center_freq(freq, 0)
		self.uhd_usrp_source_0.set_gain(rx_gain, 0)


		self.rational_resampler_xxx_0 = filter.rational_resampler_ccc(
				interpolation=4,
				decimation=125,
				taps=None,
				fractional_bw=None,
		)

		self.digital_dxpsk_demod_0 = digital.dbpsk_demod(
			samples_per_symbol=samps_per_sym,
			excess_bw=0.35,
			freq_bw=6.28/100.0,
			phase_bw=6.28/100.0,
			timing_bw=6.28/100.0,
			mod_code="gray",
			verbose=False,
			log=False)

		self.digital_mpsk_snr_est_cc_0 = digital.mpsk_snr_est_cc(2, 10000, 0.001)

		self.blocks_multiply_const_vxx_0 = blocks.multiply_const_vcc((ampl, ))

		self.blocks_file_sink_0 = blocks.file_sink(gr.sizeof_char*1, file_sink, True)
		self.blocks_file_sink_0.set_unbuffered(True)

		self.blks2_packet_decoder_0 = grc_blks2.packet_demod_b(grc_blks2.packet_decoder(
				access_code="",
				threshold=-1,# END TRANSMIT_PATH

				callback=lambda ok, payload: self.blks2_packet_decoder_0.recv_pkt(ok, payload),
			),
		)

		##################################################
		# Connections
		##################################################
		self.connect((self.blocks_multiply_const_vxx_0, 0),(self.digital_mpsk_snr_est_cc_0, 0))
		self.connect((self.digital_mpsk_snr_est_cc_0, 0), (self.digital_dxpsk_demod_0, 0))
		self.connect((self.blks2_packet_decoder_0, 0), (self.blocks_file_sink_0, 0))
		self.connect((self.digital_dxpsk_demod_0, 0), (self.blks2_packet_decoder_0, 0))
		self.connect((self.uhd_usrp_source_0, 0), (self.rational_resampler_xxx_0))
		self.connect((self.rational_resampler_xxx_0,0),(self.blocks_multiply_const_vxx_0,0))
Beispiel #12
0
    def __init__(self, n_filts=32, bits_per_sym=2, alpha_probe=0.1, th_probe=0, constellation=digital.constellation_calcdist([-1-1j, 1-1j, 1+1j, -1+1j], [], 4, 1).base(), samp_per_sym=5, bw_costas=2*math.pi/100, bw_clock_sync=2*math.pi/100, bw_fll=2*math.pi/100, len_sym_srrc=11, alfa=0.45):
        grc_wxgui.top_block_gui.__init__(self, title="Test Hier Rx Usrp")
        _icon_path = "/usr/share/icons/hicolor/32x32/apps/gnuradio-grc.png"
        self.SetIcon(wx.Icon(_icon_path, wx.BITMAP_TYPE_ANY))

        ##################################################
        # Parameters
        ##################################################
        self.n_filts = n_filts
        self.bits_per_sym = bits_per_sym
        self.alpha_probe = alpha_probe
        self.th_probe = th_probe
        self.constellation = constellation
        self.samp_per_sym = samp_per_sym
        self.bw_costas = bw_costas
        self.bw_clock_sync = bw_clock_sync
        self.bw_fll = bw_fll
        self.len_sym_srrc = len_sym_srrc
        self.alfa = alfa

        ##################################################
        # Variables
        ##################################################
        self.filtro_srrc = filtro_srrc = firdes.root_raised_cosine(n_filts,samp_per_sym*n_filts,1.0,alfa,samp_per_sym*len_sym_srrc*n_filts)

        ##################################################
        # Blocks
        ##################################################
        self.wxgui_scopesink2_0 = scopesink2.scope_sink_c(
        	self.GetWin(),
        	title="Scope Plot",
        	sample_rate=250000/samp_per_sym,
        	v_scale=0.3,
        	v_offset=0,
        	t_scale=0.3,
        	ac_couple=False,
        	xy_mode=True,
        	num_inputs=1,
        	trig_mode=wxgui.TRIG_MODE_AUTO,
        	y_axis_label="Counts",
        )
        self.Add(self.wxgui_scopesink2_0.win)
        self.uhd_usrp_source_0 = uhd.usrp_source(
        	device_addr="",
        	stream_args=uhd.stream_args(
        		cpu_format="fc32",
        		channels=range(1),
        	),
        )
        self.uhd_usrp_source_0.set_samp_rate(50000*samp_per_sym)
        self.uhd_usrp_source_0.set_center_freq(850000000, 0)
        self.uhd_usrp_source_0.set_gain(18, 0)
        self.uhd_usrp_source_0.set_antenna("TX/RX", 0)
        self.digital_pfb_clock_sync_xxx_0 = digital.pfb_clock_sync_ccf(samp_per_sym, bw_clock_sync, (filtro_srrc), n_filts, 16, 5, 1)
        self.digital_mpsk_snr_est_cc_0 = digital.mpsk_snr_est_cc(2, 1000, 0.1)
        self.digital_fll_band_edge_cc_0 = digital.fll_band_edge_cc(samp_per_sym, alfa, len_sym_srrc*samp_per_sym, bw_fll)
        self.digital_diff_decoder_bb_0 = digital.diff_decoder_bb(2**bits_per_sym)
        self.digital_costas_loop_cc_0_0_0 = digital.costas_loop_cc(bw_costas, 2**bits_per_sym)
        self.digital_constellation_decoder_cb_0 = digital.constellation_decoder_cb(constellation)
        self.blocks_vector_sink_x_0 = blocks.vector_sink_b(1)
        self.blocks_unpack_k_bits_bb_0 = blocks.unpack_k_bits_bb(bits_per_sym)
        self.blocks_file_sink_0_0 = blocks.file_sink(gr.sizeof_gr_complex*1, "/home/belza/pruebasUSRP/file_rx_sym", False)
        self.blocks_file_sink_0_0.set_unbuffered(True)
        self.analog_agc2_xx_0 = analog.agc2_cc(0.6e-1, 1e-3, 2, 15)
        self.analog_agc2_xx_0.set_max_gain(15)

        ##################################################
        # Connections
        ##################################################
        self.connect((self.digital_diff_decoder_bb_0, 0), (self.blocks_unpack_k_bits_bb_0, 0))
        self.connect((self.digital_constellation_decoder_cb_0, 0), (self.digital_diff_decoder_bb_0, 0))
        self.connect((self.analog_agc2_xx_0, 0), (self.digital_fll_band_edge_cc_0, 0))
        self.connect((self.digital_fll_band_edge_cc_0, 0), (self.digital_pfb_clock_sync_xxx_0, 0))
        self.connect((self.uhd_usrp_source_0, 0), (self.analog_agc2_xx_0, 0))
        self.connect((self.blocks_unpack_k_bits_bb_0, 0), (self.blocks_vector_sink_x_0, 0))
        self.connect((self.digital_pfb_clock_sync_xxx_0, 0), (self.digital_costas_loop_cc_0_0_0, 0))
        self.connect((self.digital_costas_loop_cc_0_0_0, 0), (self.blocks_file_sink_0_0, 0))
        self.connect((self.digital_costas_loop_cc_0_0_0, 0), (self.wxgui_scopesink2_0, 0))
        self.connect((self.digital_costas_loop_cc_0_0_0, 0), (self.digital_mpsk_snr_est_cc_0, 0))
        self.connect((self.digital_mpsk_snr_est_cc_0, 0), (self.digital_constellation_decoder_cb_0, 0))
Beispiel #13
0
def main():
    gr_estimators = {"simple": digital.SNR_EST_SIMPLE,
                     "skew": digital.SNR_EST_SKEW,
                     "m2m4": digital.SNR_EST_M2M4,
                     "svr": digital.SNR_EST_SVR}
    py_estimators = {"simple": snr_est_simple,
                     "skew": snr_est_skew,
                     "m2m4": snr_est_m2m4,
                     "svr": snr_est_svr}
    
    
    parser = OptionParser(option_class=eng_option, conflict_handler="resolve")
    parser.add_option("-N", "--nsamples", type="int", default=10000,
                      help="Set the number of samples to process [default=%default]")
    parser.add_option("", "--snr-min", type="float", default=-5,
                      help="Minimum SNR [default=%default]")
    parser.add_option("", "--snr-max", type="float", default=20,
                      help="Maximum SNR [default=%default]")
    parser.add_option("", "--snr-step", type="float", default=0.5,
                      help="SNR step amount [default=%default]")
    parser.add_option("-t", "--type", type="choice",
                      choices=gr_estimators.keys(), default="simple",
                      help="Estimator type {0} [default=%default]".format(
                            gr_estimators.keys()))
    (options, args) = parser.parse_args ()

    N = options.nsamples
    xx = scipy.random.randn(N)
    xy = scipy.random.randn(N)
    bits = 2*scipy.complex64(scipy.random.randint(0, 2, N)) - 1

    snr_known = list()
    snr_python = list()
    snr_gr = list()
    
    # when to issue an SNR tag; can be ignored in this example.
    ntag = 10000

    n_cpx = xx + 1j*xy

    py_est = py_estimators[options.type]
    gr_est = gr_estimators[options.type]

    SNR_min = options.snr_min
    SNR_max = options.snr_max
    SNR_step = options.snr_step
    SNR_dB = scipy.arange(SNR_min, SNR_max+SNR_step, SNR_step)
    for snr in SNR_dB:
        SNR = 10.0**(snr/10.0)
        scale = scipy.sqrt(SNR)
        yy = bits + n_cpx/scale
        print "SNR: ", snr

        Sknown = scipy.mean(yy**2)
        Nknown = scipy.var(n_cpx/scale)/2
        snr0 = Sknown/Nknown
        snr0dB = 10.0*scipy.log10(snr0)
        snr_known.append(snr0dB)

        snrdB, snr = py_est(yy)        
        snr_python.append(snrdB)

        gr_src = gr.vector_source_c(bits.tolist(), False)
        gr_snr = digital.mpsk_snr_est_cc(gr_est, ntag, 0.001)
        gr_chn = gr.channel_model(1.0/scale)
        gr_snk = gr.null_sink(gr.sizeof_gr_complex)
        tb = gr.top_block()
        tb.connect(gr_src, gr_chn, gr_snr, gr_snk)
        tb.run()

        snr_gr.append(gr_snr.snr())

    f1 = pylab.figure(1)
    s1 = f1.add_subplot(1,1,1)
    s1.plot(SNR_dB, snr_known, "k-o", linewidth=2, label="Known")
    s1.plot(SNR_dB, snr_python, "b-o", linewidth=2, label="Python")
    s1.plot(SNR_dB, snr_gr, "g-o", linewidth=2, label="GNU Radio")
    s1.grid(True)
    s1.set_title('SNR Estimators')
    s1.set_xlabel('SNR (dB)')
    s1.set_ylabel('Estimated SNR')
    s1.legend()

    pylab.show()
	def __init__(self):
		gr.top_block.__init__(self, "Uhd Snr Receiver")
		Qt.QWidget.__init__(self)
		self.setWindowTitle("Uhd Snr Receiver")
		self.setWindowIcon(Qt.QIcon.fromTheme('gnuradio-grc'))
		self.top_scroll_layout = Qt.QVBoxLayout()
		self.setLayout(self.top_scroll_layout)
		self.top_scroll = Qt.QScrollArea()
		self.top_scroll.setFrameStyle(Qt.QFrame.NoFrame)
		self.top_scroll_layout.addWidget(self.top_scroll)
		self.top_scroll.setWidgetResizable(True)
		self.top_widget = Qt.QWidget()
		self.top_scroll.setWidget(self.top_widget)
		self.top_layout = Qt.QVBoxLayout(self.top_widget)
		self.top_grid_layout = Qt.QGridLayout()
		self.top_layout.addLayout(self.top_grid_layout)

		self.settings = Qt.QSettings("GNU Radio", "uhd_snr_receiver")
		self.restoreGeometry(self.settings.value("geometry").toByteArray())


		##################################################
		# Variables
		##################################################
		self.sps = sps = 4
		self.nfilts = nfilts = 32
		self.samp_rate = samp_rate = 1e6
		self.rrc_taps = rrc_taps = filter.firdes.root_raised_cosine(nfilts, nfilts, 1.0/float(sps), 0.35, 11*sps*nfilts)
		self.gain = gain = 15
		self.freq = freq = 520e6
		self.fine_freq = fine_freq = -28400

		##################################################
		# Blocks
		##################################################
		self._gain_layout = Qt.QVBoxLayout()
		self._gain_tool_bar = Qt.QToolBar(self)
		self._gain_layout.addWidget(self._gain_tool_bar)
		self._gain_tool_bar.addWidget(Qt.QLabel("RX Gain"+": "))
		self._gain_counter = Qwt.QwtCounter()
		self._gain_counter.setRange(0, 31.5, 0.5)
		self._gain_counter.setNumButtons(2)
		self._gain_counter.setValue(self.gain)
		self._gain_tool_bar.addWidget(self._gain_counter)
		self._gain_counter.valueChanged.connect(self.set_gain)
		self._gain_slider = Qwt.QwtSlider(None, Qt.Qt.Horizontal, Qwt.QwtSlider.BottomScale, Qwt.QwtSlider.BgSlot)
		self._gain_slider.setRange(0, 31.5, 0.5)
		self._gain_slider.setValue(self.gain)
		self._gain_slider.setMinimumWidth(200)
		self._gain_slider.valueChanged.connect(self.set_gain)
		self._gain_layout.addWidget(self._gain_slider)
		self.top_layout.addLayout(self._gain_layout)
		self._freq_layout = Qt.QVBoxLayout()
		self._freq_tool_bar = Qt.QToolBar(self)
		self._freq_layout.addWidget(self._freq_tool_bar)
		self._freq_tool_bar.addWidget(Qt.QLabel("Frequency"+": "))
		self._freq_counter = Qwt.QwtCounter()
		self._freq_counter.setRange(514e6, 526e6, 1e6)
		self._freq_counter.setNumButtons(2)
		self._freq_counter.setValue(self.freq)
		self._freq_tool_bar.addWidget(self._freq_counter)
		self._freq_counter.valueChanged.connect(self.set_freq)
		self._freq_slider = Qwt.QwtSlider(None, Qt.Qt.Horizontal, Qwt.QwtSlider.BottomScale, Qwt.QwtSlider.BgSlot)
		self._freq_slider.setRange(514e6, 526e6, 1e6)
		self._freq_slider.setValue(self.freq)
		self._freq_slider.setMinimumWidth(200)
		self._freq_slider.valueChanged.connect(self.set_freq)
		self._freq_layout.addWidget(self._freq_slider)
		self.top_grid_layout.addLayout(self._freq_layout, 2,0,1,1)
		self._fine_freq_layout = Qt.QVBoxLayout()
		self._fine_freq_tool_bar = Qt.QToolBar(self)
		self._fine_freq_layout.addWidget(self._fine_freq_tool_bar)
		self._fine_freq_tool_bar.addWidget(Qt.QLabel("Fine Frequency"+": "))
		self._fine_freq_counter = Qwt.QwtCounter()
		self._fine_freq_counter.setRange(-50e3, 50e3, 100)
		self._fine_freq_counter.setNumButtons(2)
		self._fine_freq_counter.setValue(self.fine_freq)
		self._fine_freq_tool_bar.addWidget(self._fine_freq_counter)
		self._fine_freq_counter.valueChanged.connect(self.set_fine_freq)
		self._fine_freq_slider = Qwt.QwtSlider(None, Qt.Qt.Horizontal, Qwt.QwtSlider.BottomScale, Qwt.QwtSlider.BgSlot)
		self._fine_freq_slider.setRange(-50e3, 50e3, 100)
		self._fine_freq_slider.setValue(self.fine_freq)
		self._fine_freq_slider.setMinimumWidth(200)
		self._fine_freq_slider.valueChanged.connect(self.set_fine_freq)
		self._fine_freq_layout.addWidget(self._fine_freq_slider)
		self.top_grid_layout.addLayout(self._fine_freq_layout, 2,1,1,1)
		self.uhd_usrp_source_0 = uhd.usrp_source(
			device_addr="",
			stream_args=uhd.stream_args(
				cpu_format="fc32",
				channels=range(1),
			),
		)
		self.uhd_usrp_source_0.set_samp_rate(samp_rate)
		self.uhd_usrp_source_0.set_center_freq(freq + fine_freq, 0)
		self.uhd_usrp_source_0.set_gain(gain, 0)
		self.uhd_usrp_source_0.set_antenna("TX/RX", 0)
		self.qtgui_time_sink_x_0_0 = qtgui.time_sink_c(
			500, #size
			samp_rate, #bw
			"QT GUI Plot", #name
			3 #number of inputs
		)
		self._qtgui_time_sink_x_0_0_win = sip.wrapinstance(self.qtgui_time_sink_x_0_0.pyqwidget(), Qt.QWidget)
		self.top_grid_layout.addWidget(self._qtgui_time_sink_x_0_0_win, 0,0,1,1)
		self.qtgui_freq_sink_x_0 = qtgui.freq_sink_c(
			1024, #size
			firdes.WIN_BLACKMAN_hARRIS, #wintype
			0, #fc
			samp_rate, #bw
			"QT GUI Plot", #name
			1 #number of inputs
		)
		self._qtgui_freq_sink_x_0_win = sip.wrapinstance(self.qtgui_freq_sink_x_0.pyqwidget(), Qt.QWidget)
		self.top_grid_layout.addWidget(self._qtgui_freq_sink_x_0_win, 1,0,1,2)
		self.qtgui_const_sink_x_0 = qtgui.const_sink_c(
			1024, #size
			"QT GUI Plot", #name
			2 #number of inputs
		)
		self._qtgui_const_sink_x_0_win = sip.wrapinstance(self.qtgui_const_sink_x_0.pyqwidget(), Qt.QWidget)
		self.top_grid_layout.addWidget(self._qtgui_const_sink_x_0_win, 0,1,1,1)
		self.gr_multiply_xx_0 = gr.multiply_vcc(1)
		self.gr_agc2_xx_0 = gr.agc2_cc(1e-1, 1e-2, 1.0, 1.0, 0.0)
		self.digital_pfb_clock_sync_xxx_0 = digital.pfb_clock_sync_ccf(sps, 2*3.14/100.0, (rrc_taps), nfilts, nfilts/2, 1.5, 1)
		self.digital_mpsk_snr_est_cc_0_1 = digital.mpsk_snr_est_cc(3, 10000, 0.001)
		self.digital_mpsk_snr_est_cc_0_0 = digital.mpsk_snr_est_cc(2, 10000, 0.001)
		self.digital_mpsk_snr_est_cc_0 = digital.mpsk_snr_est_cc(0, 10000, 0.001)
		self.digital_lms_dd_equalizer_cc_0 = digital.lms_dd_equalizer_cc(15, 0.010, 1, digital.constellation_qpsk().base())
		self.digital_costas_loop_cc_0 = digital.costas_loop_cc(2*3.14/100.0, 4)

		##################################################
		# Connections
		##################################################
		self.connect((self.digital_pfb_clock_sync_xxx_0, 0), (self.digital_mpsk_snr_est_cc_0, 0))
		self.connect((self.gr_multiply_xx_0, 0), (self.qtgui_freq_sink_x_0, 0))
		self.connect((self.digital_lms_dd_equalizer_cc_0, 0), (self.qtgui_const_sink_x_0, 1))
		self.connect((self.gr_agc2_xx_0, 0), (self.gr_multiply_xx_0, 3))
		self.connect((self.gr_agc2_xx_0, 0), (self.gr_multiply_xx_0, 2))
		self.connect((self.gr_agc2_xx_0, 0), (self.gr_multiply_xx_0, 1))
		self.connect((self.gr_agc2_xx_0, 0), (self.gr_multiply_xx_0, 0))
		self.connect((self.gr_agc2_xx_0, 0), (self.digital_pfb_clock_sync_xxx_0, 0))
		self.connect((self.digital_pfb_clock_sync_xxx_0, 0), (self.digital_costas_loop_cc_0, 0))
		self.connect((self.digital_costas_loop_cc_0, 0), (self.digital_lms_dd_equalizer_cc_0, 0))
		self.connect((self.digital_costas_loop_cc_0, 0), (self.qtgui_const_sink_x_0, 0))
		self.connect((self.uhd_usrp_source_0, 0), (self.gr_agc2_xx_0, 0))
		self.connect((self.digital_pfb_clock_sync_xxx_0, 0), (self.digital_mpsk_snr_est_cc_0_0, 0))
		self.connect((self.digital_pfb_clock_sync_xxx_0, 0), (self.digital_mpsk_snr_est_cc_0_1, 0))
		self.connect((self.digital_mpsk_snr_est_cc_0, 0), (self.qtgui_time_sink_x_0_0, 0))
		self.connect((self.digital_mpsk_snr_est_cc_0_0, 0), (self.qtgui_time_sink_x_0_0, 1))
		self.connect((self.digital_mpsk_snr_est_cc_0_1, 0), (self.qtgui_time_sink_x_0_0, 2))
    def __init__(self):
        gr.top_block.__init__(self, "Uhd Snr Receiver")
        Qt.QWidget.__init__(self)
        self.setWindowTitle("Uhd Snr Receiver")
        self.setWindowIcon(Qt.QIcon.fromTheme('gnuradio-grc'))
        self.top_scroll_layout = Qt.QVBoxLayout()
        self.setLayout(self.top_scroll_layout)
        self.top_scroll = Qt.QScrollArea()
        self.top_scroll.setFrameStyle(Qt.QFrame.NoFrame)
        self.top_scroll_layout.addWidget(self.top_scroll)
        self.top_scroll.setWidgetResizable(True)
        self.top_widget = Qt.QWidget()
        self.top_scroll.setWidget(self.top_widget)
        self.top_layout = Qt.QVBoxLayout(self.top_widget)
        self.top_grid_layout = Qt.QGridLayout()
        self.top_layout.addLayout(self.top_grid_layout)

        self.settings = Qt.QSettings("GNU Radio", "uhd_snr_receiver")
        self.restoreGeometry(self.settings.value("geometry").toByteArray())

        ##################################################
        # Variables
        ##################################################
        self.sps = sps = 4
        self.nfilts = nfilts = 32
        self.samp_rate = samp_rate = 1e6
        self.rrc_taps = rrc_taps = filter.firdes.root_raised_cosine(
            nfilts, nfilts, 1.0 / float(sps), 0.35, 11 * sps * nfilts)
        self.gain = gain = 15
        self.freq = freq = 520e6
        self.fine_freq = fine_freq = -28400

        ##################################################
        # Blocks
        ##################################################
        self._gain_layout = Qt.QVBoxLayout()
        self._gain_tool_bar = Qt.QToolBar(self)
        self._gain_layout.addWidget(self._gain_tool_bar)
        self._gain_tool_bar.addWidget(Qt.QLabel("RX Gain" + ": "))
        self._gain_counter = Qwt.QwtCounter()
        self._gain_counter.setRange(0, 31.5, 0.5)
        self._gain_counter.setNumButtons(2)
        self._gain_counter.setValue(self.gain)
        self._gain_tool_bar.addWidget(self._gain_counter)
        self._gain_counter.valueChanged.connect(self.set_gain)
        self._gain_slider = Qwt.QwtSlider(None, Qt.Qt.Horizontal,
                                          Qwt.QwtSlider.BottomScale,
                                          Qwt.QwtSlider.BgSlot)
        self._gain_slider.setRange(0, 31.5, 0.5)
        self._gain_slider.setValue(self.gain)
        self._gain_slider.setMinimumWidth(200)
        self._gain_slider.valueChanged.connect(self.set_gain)
        self._gain_layout.addWidget(self._gain_slider)
        self.top_layout.addLayout(self._gain_layout)
        self._freq_layout = Qt.QVBoxLayout()
        self._freq_tool_bar = Qt.QToolBar(self)
        self._freq_layout.addWidget(self._freq_tool_bar)
        self._freq_tool_bar.addWidget(Qt.QLabel("Frequency" + ": "))
        self._freq_counter = Qwt.QwtCounter()
        self._freq_counter.setRange(514e6, 526e6, 1e6)
        self._freq_counter.setNumButtons(2)
        self._freq_counter.setValue(self.freq)
        self._freq_tool_bar.addWidget(self._freq_counter)
        self._freq_counter.valueChanged.connect(self.set_freq)
        self._freq_slider = Qwt.QwtSlider(None, Qt.Qt.Horizontal,
                                          Qwt.QwtSlider.BottomScale,
                                          Qwt.QwtSlider.BgSlot)
        self._freq_slider.setRange(514e6, 526e6, 1e6)
        self._freq_slider.setValue(self.freq)
        self._freq_slider.setMinimumWidth(200)
        self._freq_slider.valueChanged.connect(self.set_freq)
        self._freq_layout.addWidget(self._freq_slider)
        self.top_grid_layout.addLayout(self._freq_layout, 2, 0, 1, 1)
        self._fine_freq_layout = Qt.QVBoxLayout()
        self._fine_freq_tool_bar = Qt.QToolBar(self)
        self._fine_freq_layout.addWidget(self._fine_freq_tool_bar)
        self._fine_freq_tool_bar.addWidget(Qt.QLabel("Fine Frequency" + ": "))
        self._fine_freq_counter = Qwt.QwtCounter()
        self._fine_freq_counter.setRange(-50e3, 50e3, 100)
        self._fine_freq_counter.setNumButtons(2)
        self._fine_freq_counter.setValue(self.fine_freq)
        self._fine_freq_tool_bar.addWidget(self._fine_freq_counter)
        self._fine_freq_counter.valueChanged.connect(self.set_fine_freq)
        self._fine_freq_slider = Qwt.QwtSlider(None, Qt.Qt.Horizontal,
                                               Qwt.QwtSlider.BottomScale,
                                               Qwt.QwtSlider.BgSlot)
        self._fine_freq_slider.setRange(-50e3, 50e3, 100)
        self._fine_freq_slider.setValue(self.fine_freq)
        self._fine_freq_slider.setMinimumWidth(200)
        self._fine_freq_slider.valueChanged.connect(self.set_fine_freq)
        self._fine_freq_layout.addWidget(self._fine_freq_slider)
        self.top_grid_layout.addLayout(self._fine_freq_layout, 2, 1, 1, 1)
        self.uhd_usrp_source_0 = uhd.usrp_source(
            device_addr="",
            stream_args=uhd.stream_args(
                cpu_format="fc32",
                channels=range(1),
            ),
        )
        self.uhd_usrp_source_0.set_samp_rate(samp_rate)
        self.uhd_usrp_source_0.set_center_freq(freq + fine_freq, 0)
        self.uhd_usrp_source_0.set_gain(gain, 0)
        self.uhd_usrp_source_0.set_antenna("TX/RX", 0)
        self.qtgui_time_sink_x_0_0 = qtgui.time_sink_c(
            500,  #size
            samp_rate,  #bw
            "QT GUI Plot",  #name
            3  #number of inputs
        )
        self._qtgui_time_sink_x_0_0_win = sip.wrapinstance(
            self.qtgui_time_sink_x_0_0.pyqwidget(), Qt.QWidget)
        self.top_grid_layout.addWidget(self._qtgui_time_sink_x_0_0_win, 0, 0,
                                       1, 1)
        self.qtgui_freq_sink_x_0 = qtgui.freq_sink_c(
            1024,  #size
            firdes.WIN_BLACKMAN_hARRIS,  #wintype
            0,  #fc
            samp_rate,  #bw
            "QT GUI Plot",  #name
            1  #number of inputs
        )
        self._qtgui_freq_sink_x_0_win = sip.wrapinstance(
            self.qtgui_freq_sink_x_0.pyqwidget(), Qt.QWidget)
        self.top_grid_layout.addWidget(self._qtgui_freq_sink_x_0_win, 1, 0, 1,
                                       2)
        self.qtgui_const_sink_x_0 = qtgui.const_sink_c(
            1024,  #size
            "QT GUI Plot",  #name
            2  #number of inputs
        )
        self._qtgui_const_sink_x_0_win = sip.wrapinstance(
            self.qtgui_const_sink_x_0.pyqwidget(), Qt.QWidget)
        self.top_grid_layout.addWidget(self._qtgui_const_sink_x_0_win, 0, 1, 1,
                                       1)
        self.gr_multiply_xx_0 = gr.multiply_vcc(1)
        self.gr_agc2_xx_0 = gr.agc2_cc(1e-1, 1e-2, 1.0, 1.0, 0.0)
        self.digital_pfb_clock_sync_xxx_0 = digital.pfb_clock_sync_ccf(
            sps, 2 * 3.14 / 100.0, (rrc_taps), nfilts, nfilts / 2, 1.5, 1)
        self.digital_mpsk_snr_est_cc_0_1 = digital.mpsk_snr_est_cc(
            3, 10000, 0.001)
        self.digital_mpsk_snr_est_cc_0_0 = digital.mpsk_snr_est_cc(
            2, 10000, 0.001)
        self.digital_mpsk_snr_est_cc_0 = digital.mpsk_snr_est_cc(
            0, 10000, 0.001)
        self.digital_lms_dd_equalizer_cc_0 = digital.lms_dd_equalizer_cc(
            15, 0.010, 1,
            digital.constellation_qpsk().base())
        self.digital_costas_loop_cc_0 = digital.costas_loop_cc(
            2 * 3.14 / 100.0, 4)

        ##################################################
        # Connections
        ##################################################
        self.connect((self.digital_pfb_clock_sync_xxx_0, 0),
                     (self.digital_mpsk_snr_est_cc_0, 0))
        self.connect((self.gr_multiply_xx_0, 0), (self.qtgui_freq_sink_x_0, 0))
        self.connect((self.digital_lms_dd_equalizer_cc_0, 0),
                     (self.qtgui_const_sink_x_0, 1))
        self.connect((self.gr_agc2_xx_0, 0), (self.gr_multiply_xx_0, 3))
        self.connect((self.gr_agc2_xx_0, 0), (self.gr_multiply_xx_0, 2))
        self.connect((self.gr_agc2_xx_0, 0), (self.gr_multiply_xx_0, 1))
        self.connect((self.gr_agc2_xx_0, 0), (self.gr_multiply_xx_0, 0))
        self.connect((self.gr_agc2_xx_0, 0),
                     (self.digital_pfb_clock_sync_xxx_0, 0))
        self.connect((self.digital_pfb_clock_sync_xxx_0, 0),
                     (self.digital_costas_loop_cc_0, 0))
        self.connect((self.digital_costas_loop_cc_0, 0),
                     (self.digital_lms_dd_equalizer_cc_0, 0))
        self.connect((self.digital_costas_loop_cc_0, 0),
                     (self.qtgui_const_sink_x_0, 0))
        self.connect((self.uhd_usrp_source_0, 0), (self.gr_agc2_xx_0, 0))
        self.connect((self.digital_pfb_clock_sync_xxx_0, 0),
                     (self.digital_mpsk_snr_est_cc_0_0, 0))
        self.connect((self.digital_pfb_clock_sync_xxx_0, 0),
                     (self.digital_mpsk_snr_est_cc_0_1, 0))
        self.connect((self.digital_mpsk_snr_est_cc_0, 0),
                     (self.qtgui_time_sink_x_0_0, 0))
        self.connect((self.digital_mpsk_snr_est_cc_0_0, 0),
                     (self.qtgui_time_sink_x_0_0, 1))
        self.connect((self.digital_mpsk_snr_est_cc_0_1, 0),
                     (self.qtgui_time_sink_x_0_0, 2))
import scipy
from gnuradio import gr, digital
import cspl

N = 10e5
ntag = 10e1
gr_est = digital.SNR_EST_M2M4

bits = tuple(range(0,255))*100

SNR = 10.0**(10/10.0) # 5dB
scale = scipy.sqrt(SNR)

src = gr.vector_source_b(bits, False)
mod = digital.bpsk_mod()
chn = gr.channel_model(1.0/scale)
snr = digital.mpsk_snr_est_cc(gr_est)
demod = digital.bpsk_demod()
snk = cspl.snr_file_sink_b("data.foo")
tb = gr.top_block()
tb.connect(src, mod, chn, snr, demod, snk)
tb.run()

    def __init__(self, options):
        """
        Hierarchical block for O-QPSK demodulation.
        
        The input is the complex modulated signal at baseband
        and the output is a stream of bytes.
        
        @param sps: samples per symbol
        @type sps: integer
        """ 
        try:
            #self.sps = kwargs.pop('sps')
            self.sps = 2
        except KeyError:
            pass
        
        gr.hier_block2.__init__(self, "oqpsk_demodulator",
                gr.io_signature(1, 1, gr.sizeof_gr_complex), # Input
                gr.io_signature(0, 0, 0))  # Output
        
        # Demodulate FM
        sensitivity = (pi / 2) / self.sps
        #self.fmdemod = gr.quadrature_demod_cf(1.0 / sensitivity)
        self.fmdemod = gr.quadrature_demod_cf(1)
        
        # Low pass the output of fmdemod to allow us to remove
        # the DC offset resulting from frequency offset
        
        alpha = 0.0008/self.sps
        self.freq_offset = gr.single_pole_iir_filter_ff(alpha)
        self.sub = gr.sub_ff()
        
        # recover the clock
        omega = self.sps
        gain_mu=0.03
        mu=0.5
        omega_relative_limit=0.0002
        freq_error=0.0
        
        gain_omega = .25*gain_mu*gain_mu        # critically damped
        
        # Descramble BERT sequence.  A channel error will create 3 incorrect bits
        #self._descrambler = gr.descrambler_bb(0x8A, 0x7F, 31) # CCSDS 7-bit descrambler
        
        self.clock_recovery_f = digital.clock_recovery_mm_ff(omega, gain_omega, mu, gain_mu,
                                                      omega_relative_limit)
    
        self.gr_float_to_complex = gr.float_to_complex(1)
        
        #Create a vector source reference to calculate BER
        self._vector_source_ref = gr.vector_source_b(([1,]), True, 1)
        #create a comparator 
        self.comparator = howto.compare_vector_cci((1, 1, 1, 1 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1), (0,0, 1, 1, 0,0), 5, 0, True)
        
        self._ber = grc_blks2.error_rate(type='BER', 
                                         win_size=1000,
                                         bits_per_symbol=1)
        
        self._slicer = digital.binary_slicer_fb()
        
        
        self.gr_null_sink_f = gr.null_sink(gr.sizeof_float*1)
        
        # Add an SNR probe on the demodulated constellation
        #self._snr_probe = gr.probe_mpsk_snr_c(10.0/symbol_rate)
        self._snr_probe = digital.mpsk_snr_est_cc(0, 10000, 0.001) # 0 at the first mean Simple
        self.gr_null_sink_cc = gr.null_sink(gr.sizeof_gr_complex*1)
        
        self.gr_null_source = gr.null_source(gr.sizeof_float*1)
        
        ###############################
        ###------->                                            -->gr_float_to_complex--->_snr_probe --> gr_null_sink_cc
        ###---->fmdemod -->freq_offset--->sub-->clock_recovery --> _slicer -->_descrambler --> comparator -->_ber--> self.gr_null_sink_f 
        ###'''''''''''''|-->----------------|-->'                      _vector_source_ref-->
        #############################
        # Connect
        
        self.connect(self, self.fmdemod)
        self.connect(self.fmdemod, (self.sub, 0))
        self.connect(self.fmdemod, self.freq_offset, (self.sub, 1))
        
        #self.connect(self.sub, self.clock_recovery_f, self._slicer, self._descrambler, self.comparator, (self._ber, 0))
        self.connect(self.sub, self.clock_recovery_f, self._slicer, self.comparator, (self._ber, 0))
        
#        self.connect(self.clock_recovery_f, (self.gr_float_to_complex, 1))
#        self.connect(self.gr_null_source, (self.gr_float_to_complex, 0))
#    
#        self.connect(self.gr_float_to_complex, self._snr_probe, self.gr_null_sink_cc)
        
        self.connect(self._vector_source_ref, (self._ber,1))
        self.connect(self._ber, self.gr_null_sink_f)