summaryrefslogtreecommitdiff
path: root/gnuradio-examples/python/gmsk2/mpsk_test.py
blob: 1f257d9c5d0d0262db1f4f12df946a3ff9c7a615 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python

from gnuradio import gr, blks
from gnuradio.eng_option import eng_option
from optparse import OptionParser
import random
import struct
from mpsk_pkt import *
import cmath

class my_graph(gr.flow_graph):

    def __init__(self, rx_callback, spb, excess_bw, SNR, freq_error, arity):
        gr.flow_graph.__init__(self)

        fg = self

        # Tuning Parameters
        gain_mu = 0.05/spb

        # transmitter
        self.packet_transmitter = \
          mpsk_mod_pkts(fg, spb=spb, excess_bw=excess_bw, diff=True, arity=arity)

        # ----------------------------------------------------------------
        #                       Channel model
        # ----------------------------------------------------------------

        awgn = gr.noise_source_c(gr.GR_GAUSSIAN, pow(10.0,-SNR/20.0))
        add = gr.add_cc()            # add some noise
        fg.connect(awgn, (add,1))
        radians = 17*pi/180
        phase_rotate = gr.multiply_const_cc(cmath.exp(radians * 1j))

        # ----------------------------------------------------------------

        # channel filter
        rx_filt_taps = gr.firdes.low_pass(1,spb,0.8,0.1,gr.firdes.WIN_HANN)
        #rx_filt_taps = (1,)
        rx_filt = gr.fir_filter_ccf(1,rx_filt_taps)

        # receiver
        self.packet_receiver = \
            mpsk_demod_pkts(fg, callback=rx_callback,
                            excess_bw=excess_bw, arity=arity, diff=True,
                            costas_alpha=.005, gain_mu=gain_mu, spb=spb)
        
        fg.connect (self.packet_transmitter, (add,0))
        fg.connect(add, phase_rotate, rx_filt, self.packet_receiver)


class stats(object):
    def __init__(self):
        self.npkts = 0
        self.nright = 0
        
def main():
    st = stats()
    
    def send_pkt(payload='', eof=False):
        fg.packet_transmitter.send_pkt(payload, eof)

    def rx_callback(ok, payload):
        st.npkts += 1
        if ok:
            st.nright += 1
        if len(payload) <= 16:
            print "ok = %5r  payload = '%s'  %d/%d" % (ok, payload, st.nright, st.npkts)
        else:
            (pktno,) = struct.unpack('!H', payload[0:2])
            print "ok = %5r  pktno = %4d  len(payload) = %4d  %d/%d" % (ok, pktno, len(payload),
                                                                        st.nright, st.npkts)


    parser = OptionParser (option_class=eng_option)
    parser.add_option("-M", "--megabytes", type="eng_float", default=1,
                      help="set megabytes to transmit [default=%default]")
    parser.add_option("-s", "--size", type="eng_float", default=1500,
                      help="set packet size [default=%default]")
    parser.add_option("","--spb", type=int, default=4,
                      help="set samples per baud to SPB [default=%default]")
    parser.add_option("", "--excess-bw", type="eng_float", default=0.4,
                      help="set excess bandwidth for RRC filter [default=%default]")
    parser.add_option("", "--snr", type="eng_float", default=40,
                      help="set SNR in dB for simulation [default=%default]")
    parser.add_option("", "--m-arity", type=int, default=4,
                      help="PSK arity [default=%default]")
    parser.add_option("", "--freq-error", type="eng_float", default=0,
                      help="set frequency error for simulation [default=%default]")

    (options, args) = parser.parse_args ()

    if len(args) != 0:
        parser.print_help()
        sys.exit(1)

    pkt_size = int(options.size)

    fg = my_graph(rx_callback, options.spb, options.excess_bw, options.snr,
                  options.freq_error, options.m_arity)
    fg.start()

    nbytes = int(1e6 * options.megabytes)
    n = 0
    pktno = 0

    send_pkt('Hello World')

    # generate and send packets
    while n < nbytes:
        send_pkt(struct.pack('!H', pktno) + (pkt_size - 2) * chr(pktno & 0xff))
        n += pkt_size
        pktno += 1

    send_pkt('Goodbye World')
    send_pkt(eof=True)             # tell modulator we're not sending any more pkts

    fg.wait()


if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        pass