Sunday, February 12, 2017

PSK Transmitter and Receiver with GUN Radio Python Code


PSK Transmitter and Receiver with Channel Coding



if __name__ == '__main__':
    import ctypes
    import sys
    if sys.platform.startswith('linux'):
        try:
            x11 = ctypes.cdll.LoadLibrary('libX11.so')
            x11.XInitThreads()
        except:
            print "Warning: failed to XInitThreads()"

from PyQt4 import Qt
from gnuradio import blocks
from gnuradio import channels
from gnuradio import digital
from gnuradio import eng_notation
from gnuradio import fec
from gnuradio import filter
from gnuradio import gr
from gnuradio import qtgui
from gnuradio.eng_option import eng_option
from gnuradio.filter import firdes
from optparse import OptionParser
import sip
import sys


class top_block(gr.top_block, Qt.QWidget):

    def __init__(self):
        gr.top_block.__init__(self, "Top Block")
        Qt.QWidget.__init__(self)
        self.setWindowTitle("Top Block")
        try:
            self.setWindowIcon(Qt.QIcon.fromTheme('gnuradio-grc'))
        except:
            pass
        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", "top_block")
        self.restoreGeometry(self.settings.value("geometry").toByteArray())

        ##################################################
        # Variables
        ##################################################
        self.samp_per_bit = samp_per_bit = 2
        self.channel_samp_rate = channel_samp_rate = 1000000/2
        self.gain = gain = 0.1
        self.bit_rate = bit_rate = channel_samp_rate/samp_per_bit
        self.Max_tput = Max_tput = 100000

        ##################################################
        # Blocks
        ##################################################
        self.qtgui_time_sink_x_0_1 = qtgui.time_sink_f(
        1024, #size
        bit_rate, #samp_rate
        "Output Bit stream", #name
        1 #number of inputs
        )
        self.qtgui_time_sink_x_0_1.set_update_time(0.10)
        self.qtgui_time_sink_x_0_1.set_y_axis(-1, 1)
       
        self.qtgui_time_sink_x_0_1.set_y_label("Amplitude", "")
       
        self.qtgui_time_sink_x_0_1.enable_tags(-1, True)
        self.qtgui_time_sink_x_0_1.set_trigger_mode(qtgui.TRIG_MODE_FREE, qtgui.TRIG_SLOPE_POS, 0.0, 0, 0, "")
        self.qtgui_time_sink_x_0_1.enable_autoscale(True)
        self.qtgui_time_sink_x_0_1.enable_grid(False)
        self.qtgui_time_sink_x_0_1.enable_control_panel(True)
       
        if not True:
          self.qtgui_time_sink_x_0_1.disable_legend()
       
        labels = ["", "", "", "", "",
                  "", "", "", "", ""]
        widths = [1, 1, 1, 1, 1,
                  1, 1, 1, 1, 1]
        colors = ["blue", "red", "green", "black", "cyan",
                  "magenta", "yellow", "dark red", "dark green", "blue"]
        styles = [1, 1, 1, 1, 1,
                  1, 1, 1, 1, 1]
        markers = [-1, -1, -1, -1, -1,
                   -1, -1, -1, -1, -1]
        alphas = [1.0, 1.0, 1.0, 1.0, 1.0,
                  1.0, 1.0, 1.0, 1.0, 1.0]
       
        for i in xrange(1):
            if len(labels[i]) == 0:
                self.qtgui_time_sink_x_0_1.set_line_label(i, "Data {0}".format(i))
            else:
                self.qtgui_time_sink_x_0_1.set_line_label(i, labels[i])
            self.qtgui_time_sink_x_0_1.set_line_width(i, widths[i])
            self.qtgui_time_sink_x_0_1.set_line_color(i, colors[i])
            self.qtgui_time_sink_x_0_1.set_line_style(i, styles[i])
            self.qtgui_time_sink_x_0_1.set_line_marker(i, markers[i])
            self.qtgui_time_sink_x_0_1.set_line_alpha(i, alphas[i])
       
        self._qtgui_time_sink_x_0_1_win = sip.wrapinstance(self.qtgui_time_sink_x_0_1.pyqwidget(), Qt.QWidget)
        self.top_layout.addWidget(self._qtgui_time_sink_x_0_1_win)
        self.qtgui_time_sink_x_0 = qtgui.time_sink_f(
        400, #size
        bit_rate, #samp_rate
        "Input Bit stream", #name
        1 #number of inputs
        )
        self.qtgui_time_sink_x_0.set_update_time(0.10)
        self.qtgui_time_sink_x_0.set_y_axis(-1, 1)
       
        self.qtgui_time_sink_x_0.set_y_label("Amplitude", "")
       
        self.qtgui_time_sink_x_0.enable_tags(-1, True)
        self.qtgui_time_sink_x_0.set_trigger_mode(qtgui.TRIG_MODE_FREE, qtgui.TRIG_SLOPE_POS, 0.0, 0, 0, "")
        self.qtgui_time_sink_x_0.enable_autoscale(True)
        self.qtgui_time_sink_x_0.enable_grid(False)
        self.qtgui_time_sink_x_0.enable_control_panel(True)
       
        if not True:
          self.qtgui_time_sink_x_0.disable_legend()
       
        labels = ["", "", "", "", "",
                  "", "", "", "", ""]
        widths = [1, 1, 1, 1, 1,
                  1, 1, 1, 1, 1]
        colors = ["blue", "red", "green", "black", "cyan",
                  "magenta", "yellow", "dark red", "dark green", "blue"]
        styles = [1, 1, 1, 1, 1,
                  1, 1, 1, 1, 1]
        markers = [-1, -1, -1, -1, -1,
                   -1, -1, -1, -1, -1]
        alphas = [1.0, 1.0, 1.0, 1.0, 1.0,
                  1.0, 1.0, 1.0, 1.0, 1.0]
       
        for i in xrange(1):
            if len(labels[i]) == 0:
                self.qtgui_time_sink_x_0.set_line_label(i, "Data {0}".format(i))
            else:
                self.qtgui_time_sink_x_0.set_line_label(i, labels[i])
            self.qtgui_time_sink_x_0.set_line_width(i, widths[i])
            self.qtgui_time_sink_x_0.set_line_color(i, colors[i])
            self.qtgui_time_sink_x_0.set_line_style(i, styles[i])
            self.qtgui_time_sink_x_0.set_line_marker(i, markers[i])
            self.qtgui_time_sink_x_0.set_line_alpha(i, alphas[i])
       
        self._qtgui_time_sink_x_0_win = sip.wrapinstance(self.qtgui_time_sink_x_0.pyqwidget(), Qt.QWidget)
        self.top_layout.addWidget(self._qtgui_time_sink_x_0_win)
        self.qtgui_freq_sink_x_0 = qtgui.freq_sink_c(
        1024, #size
        firdes.WIN_BLACKMAN_hARRIS, #wintype
        0, #fc
        channel_samp_rate, #bw
        "Channel Input", #name
        1 #number of inputs
        )
        self.qtgui_freq_sink_x_0.set_update_time(0.50)
        self.qtgui_freq_sink_x_0.set_y_axis(-140, 10)
        self.qtgui_freq_sink_x_0.set_trigger_mode(qtgui.TRIG_MODE_FREE, 0.0, 0, "")
        self.qtgui_freq_sink_x_0.enable_autoscale(False)
        self.qtgui_freq_sink_x_0.enable_grid(True)
        self.qtgui_freq_sink_x_0.set_fft_average(0.05)
        self.qtgui_freq_sink_x_0.enable_control_panel(False)
       
        if not True:
          self.qtgui_freq_sink_x_0.disable_legend()
       
        if "complex" == "float" or "complex" == "msg_float":
          self.qtgui_freq_sink_x_0.set_plot_pos_half(not True)
       
        labels = ["", "", "", "", "",
                  "", "", "", "", ""]
        widths = [1, 1, 1, 1, 1,
                  1, 1, 1, 1, 1]
        colors = ["blue", "red", "green", "black", "cyan",
                  "magenta", "yellow", "dark red", "dark green", "dark blue"]
        alphas = [1.0, 1.0, 1.0, 1.0, 1.0,
                  1.0, 1.0, 1.0, 1.0, 1.0]
        for i in xrange(1):
            if len(labels[i]) == 0:
                self.qtgui_freq_sink_x_0.set_line_label(i, "Data {0}".format(i))
            else:
                self.qtgui_freq_sink_x_0.set_line_label(i, labels[i])
            self.qtgui_freq_sink_x_0.set_line_width(i, widths[i])
            self.qtgui_freq_sink_x_0.set_line_color(i, colors[i])
            self.qtgui_freq_sink_x_0.set_line_alpha(i, alphas[i])
       
        self._qtgui_freq_sink_x_0_win = sip.wrapinstance(self.qtgui_freq_sink_x_0.pyqwidget(), Qt.QWidget)
        self.top_layout.addWidget(self._qtgui_freq_sink_x_0_win)
        self.interp_fir_filter_xxx_0_0 = filter.interp_fir_filter_fff(8, (1, ))
        self.interp_fir_filter_xxx_0_0.declare_sample_delay(0)
        self.fec_encode_ccsds_27_bb_0 = fec.encode_ccsds_27_bb()
        self.fec_decode_ccsds_27_fb_0 = fec.decode_ccsds_27_fb()
        self.digital_simple_framer_0 = digital.simple_framer(20)
        self.digital_simple_correlator_0 = digital.simple_correlator(20)
        self.digital_psk_mod_0 = digital.psk.psk_mod(
          constellation_points=2,
          mod_code="none",
          differential=False,
          samples_per_symbol=samp_per_bit,
          excess_bw=0.35,
          verbose=False,
          log=False,
          )
        self.digital_psk_demod_0 = digital.psk.psk_demod(
          constellation_points=2,
          differential=False,
          samples_per_symbol=samp_per_bit,
          excess_bw=0.35,
          phase_bw=6.28/100.0,
          timing_bw=6.28/100.0,
          mod_code="none",
          verbose=False,
          log=False,
          )
        self.digital_map_bb_0 = digital.map_bb((-1,1))
        self.digital_costas_loop_cc_0 = digital.costas_loop_cc(2*3.14/100, 2, True)
        self.channels_channel_model_0 = channels.channel_model(
        noise_voltage=0,
        frequency_offset=0.0,
        epsilon=1,
        taps=(1, ),
        noise_seed=0,
        block_tags=False
        )
        self.blocks_throttle_0_1_0_0_0 = blocks.throttle(gr.sizeof_char*1, Max_tput,True)
        self.blocks_throttle_0_1_0 = blocks.throttle(gr.sizeof_gr_complex*1, channel_samp_rate,True)
        self.blocks_multiply_const_vxx_0 = blocks.multiply_const_vcc((gain*(1+1j), ))
        self.blocks_file_source_0 = blocks.file_source(gr.sizeof_char*1, "/Users/gunaratr/Documents/MATLAB/src_wav.bin", False)
        self.blocks_file_sink_0_0_1 = blocks.file_sink(gr.sizeof_char*1, "/Users/gunaratr/Documents/MATLAB/new.dat", False)
        self.blocks_file_sink_0_0_1.set_unbuffered(False)
        self.blocks_char_to_float_0_1_0_0_0_0_1_0_0_0_0 = blocks.char_to_float(1, 1)
        self.blocks_char_to_float_0_1_0_0_0_0_1_0_0 = blocks.char_to_float(1, 1)
        self.blocks_char_to_float_0_1_0_0_0_0_1_0 = blocks.char_to_float(1, 1)
        self.blocks_char_to_float_0_1_0_0_0_0_0 = blocks.char_to_float(1, 1)
        self.blocks_add_const_vxx_0 = blocks.add_const_vff((-1, ))

        ##################################################
        # Connections
        ##################################################
        self.connect((self.blocks_add_const_vxx_0, 0), (self.digital_simple_correlator_0, 0))  
        self.connect((self.blocks_char_to_float_0_1_0_0_0_0_0, 0), (self.fec_decode_ccsds_27_fb_0, 0))  
        self.connect((self.blocks_char_to_float_0_1_0_0_0_0_1_0, 0), (self.interp_fir_filter_xxx_0_0, 0))  
        self.connect((self.blocks_char_to_float_0_1_0_0_0_0_1_0_0, 0), (self.qtgui_time_sink_x_0, 0))  
        self.connect((self.blocks_char_to_float_0_1_0_0_0_0_1_0_0_0_0, 0), (self.qtgui_time_sink_x_0_1, 0))  
        self.connect((self.blocks_file_source_0, 0), (self.blocks_char_to_float_0_1_0_0_0_0_1_0_0, 0))  
        self.connect((self.blocks_file_source_0, 0), (self.blocks_throttle_0_1_0_0_0, 0))  
        self.connect((self.blocks_multiply_const_vxx_0, 0), (self.blocks_throttle_0_1_0, 0))  
        self.connect((self.blocks_throttle_0_1_0, 0), (self.channels_channel_model_0, 0))  
        self.connect((self.blocks_throttle_0_1_0, 0), (self.qtgui_freq_sink_x_0, 0))  
        self.connect((self.blocks_throttle_0_1_0_0_0, 0), (self.fec_encode_ccsds_27_bb_0, 0))  
        self.connect((self.channels_channel_model_0, 0), (self.digital_costas_loop_cc_0, 0))  
        self.connect((self.digital_costas_loop_cc_0, 0), (self.digital_psk_demod_0, 0))  
        self.connect((self.digital_map_bb_0, 0), (self.blocks_char_to_float_0_1_0_0_0_0_0, 0))  
        self.connect((self.digital_psk_demod_0, 0), (self.blocks_char_to_float_0_1_0_0_0_0_1_0, 0))  
        self.connect((self.digital_psk_mod_0, 0), (self.blocks_multiply_const_vxx_0, 0))  
        self.connect((self.digital_simple_correlator_0, 0), (self.digital_map_bb_0, 0))  
        self.connect((self.digital_simple_framer_0, 0), (self.digital_psk_mod_0, 0))  
        self.connect((self.fec_decode_ccsds_27_fb_0, 0), (self.blocks_char_to_float_0_1_0_0_0_0_1_0_0_0_0, 0))  
        self.connect((self.fec_decode_ccsds_27_fb_0, 0), (self.blocks_file_sink_0_0_1, 0))  
        self.connect((self.fec_encode_ccsds_27_bb_0, 0), (self.digital_simple_framer_0, 0))  
        self.connect((self.interp_fir_filter_xxx_0_0, 0), (self.blocks_add_const_vxx_0, 0))  

    def closeEvent(self, event):
        self.settings = Qt.QSettings("GNU Radio", "top_block")
        self.settings.setValue("geometry", self.saveGeometry())
        event.accept()


    def get_samp_per_bit(self):
        return self.samp_per_bit

    def set_samp_per_bit(self, samp_per_bit):
        self.samp_per_bit = samp_per_bit
        self.set_bit_rate(self.channel_samp_rate/self.samp_per_bit)

    def get_channel_samp_rate(self):
        return self.channel_samp_rate

    def set_channel_samp_rate(self, channel_samp_rate):
        self.channel_samp_rate = channel_samp_rate
        self.set_bit_rate(self.channel_samp_rate/self.samp_per_bit)
        self.blocks_throttle_0_1_0.set_sample_rate(self.channel_samp_rate)
        self.qtgui_freq_sink_x_0.set_frequency_range(0, self.channel_samp_rate)

    def get_gain(self):
        return self.gain

    def set_gain(self, gain):
        self.gain = gain
        self.blocks_multiply_const_vxx_0.set_k((self.gain*(1+1j), ))

    def get_bit_rate(self):
        return self.bit_rate

    def set_bit_rate(self, bit_rate):
        self.bit_rate = bit_rate
        self.qtgui_time_sink_x_0.set_samp_rate(self.bit_rate)
        self.qtgui_time_sink_x_0_1.set_samp_rate(self.bit_rate)

    def get_Max_tput(self):
        return self.Max_tput

    def set_Max_tput(self, Max_tput):
        self.Max_tput = Max_tput
        self.blocks_throttle_0_1_0_0_0.set_sample_rate(self.Max_tput)


def main(top_block_cls=top_block, options=None):

    from distutils.version import StrictVersion
    if StrictVersion(Qt.qVersion()) >= StrictVersion("4.5.0"):
        style = gr.prefs().get_string('qtgui', 'style', 'raster')
        Qt.QApplication.setGraphicsSystem(style)
    qapp = Qt.QApplication(sys.argv)

    tb = top_block_cls()
    tb.start()
    tb.show()

    def quitting():
        tb.stop()
        tb.wait()
    qapp.connect(qapp, Qt.SIGNAL("aboutToQuit()"), quitting)
    qapp.exec_()


if __name__ == '__main__':
    main()

No comments:

Post a Comment