lots of example and other useful code for use with the softronics msdd6000
[debian/gnuradio] / gr-msdd6000 / src / python-examples / ofdm / benchmark_ofdm_rx.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2006, 2007 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
7 # GNU Radio is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3, or (at your option)
10 # any later version.
11
12 # GNU Radio is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Radio; see the file COPYING.  If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
21
22
23 from gnuradio import gr, blks2
24 from gnuradio import msdd
25 from gnuradio import eng_notation
26 from gnuradio.eng_option import eng_option
27 from optparse import OptionParser
28
29 import struct, sys
30
31 # from current dir
32 from receive_path import receive_path
33
34 class my_top_block(gr.top_block):
35     def __init__(self, address, callback, options):
36         gr.top_block.__init__(self)
37
38         self._address = address
39         self._rx_freq = options.rx_freq         # receiver's center frequency
40         self._rx_gain = options.rx_gain         # receiver's gain
41         self._decim   = options.decim           # Decimating rate for the USRP (prelim)
42
43         if self._rx_freq is None:
44             sys.stderr.write("-f FREQ or --freq FREQ or --rx-freq FREQ must be specified\n")
45             raise SystemExit
46
47         # Set up USRP source
48         self._setup_source()
49
50         #taps = gr.firdes.low_pass(1, 1, 0.4, 0.2)
51         #self.resample = gr.rational_resampler_base_ccf(5, 8, taps)
52         self.resample = blks2.rational_resampler_ccf(5, 8)
53
54         # Set up receive path
55         self.rxpath = receive_path(callback, options)
56
57         self.connect(self.src, self.resample, self.rxpath)
58         #self.connect(self.src, gr.file_sink(gr.sizeof_gr_complex, "receive.dat"))
59         #self.connect(self.resample, gr.file_sink(gr.sizeof_gr_complex, "resampled.dat"))
60         
61     def _setup_source(self):
62         # build graph
63         self._port = 10001
64         self.src = msdd.source_c(0, 1, self._address, self._port)
65         self.src.set_decim_rate(self._decim)
66         self.src.set_desired_packet_size(0, 1460)
67
68         self.set_gain(self._rx_gain)
69         self.set_freq(self._rx_freq)
70
71     def set_freq(self, target_freq):
72         """
73         Set the center frequency we're interested in.
74
75         @param target_freq: frequency in Hz
76         @rypte: bool
77         """
78         r = self.src.set_rx_freq(0, target_freq)
79         if r:
80             return True
81         return False
82
83     def set_gain(self, gain):
84         """
85         Sets the analog gain in the USRP
86         """
87         return self.src.set_pga(0, gain)
88
89     def decim(self):
90         return self._decim
91
92     def add_options(normal, expert):
93         """
94         Adds usrp-specific options to the Options Parser
95         """
96         add_freq_option(normal)
97         normal.add_option("", "--rx-gain", type="eng_float", default=32, metavar="GAIN",
98                           help="set receiver gain in dB [default=%default].")
99         normal.add_option("-v", "--verbose", action="store_true", default=False)
100
101         expert.add_option("", "--rx-freq", type="eng_float", default=None,
102                           help="set Rx frequency to FREQ [default=%default]", metavar="FREQ")
103         expert.add_option("-d", "--decim", type="intx", default=128,
104                           help="set fpga decimation rate to DECIM [default=%default]")
105         expert.add_option("", "--snr", type="eng_float", default=30,
106                           help="set the SNR of the channel in dB [default=%default]")
107    
108     # Make a static method to call before instantiation
109     add_options = staticmethod(add_options)
110
111 def add_freq_option(parser):
112     """
113     Hackery that has the -f / --freq option set both tx_freq and rx_freq
114     """
115     def freq_callback(option, opt_str, value, parser):
116         parser.values.rx_freq = value
117         parser.values.tx_freq = value
118
119     if not parser.has_option('--freq'):
120         parser.add_option('-f', '--freq', type="eng_float",
121                           action="callback", callback=freq_callback,
122                           help="set Tx and/or Rx frequency to FREQ [default=%default]",
123                           metavar="FREQ")
124
125 # /////////////////////////////////////////////////////////////////////////////
126 #                                   main
127 # /////////////////////////////////////////////////////////////////////////////
128
129 def main():
130
131     global n_rcvd, n_right
132         
133     n_rcvd = 0
134     n_right = 0
135
136     def rx_callback(ok, payload):
137         global n_rcvd, n_right
138         n_rcvd += 1
139         (pktno,) = struct.unpack('!H', payload[0:2])
140         if ok:
141             n_right += 1
142         print "ok: %r \t pktno: %d \t n_rcvd: %d \t n_right: %d" % (ok, pktno, n_rcvd, n_right)
143
144         if 0:
145             printlst = list()
146             for x in payload[2:]:
147                 t = hex(ord(x)).replace('0x', '')
148                 if(len(t) == 1):
149                     t = '0' + t
150                 printlst.append(t)
151             printable = ''.join(printlst)
152
153             print printable
154             print "\n"
155
156     usage = "usage: %prog [options] host"
157     parser = OptionParser(usage=usage, option_class=eng_option, conflict_handler="resolve")
158     expert_grp = parser.add_option_group("Expert")
159     parser.add_option("","--discontinuous", action="store_true", default=False,
160                       help="enable discontinuous")
161
162     my_top_block.add_options(parser, expert_grp)
163     receive_path.add_options(parser, expert_grp)
164     blks2.ofdm_mod.add_options(parser, expert_grp)
165     blks2.ofdm_demod.add_options(parser, expert_grp)
166
167     (options, args) = parser.parse_args ()
168     address = args[0]
169
170     # build the graph
171     tb = my_top_block(address, rx_callback, options)
172
173     #r = gr.enable_realtime_scheduling()
174     #if r != gr.RT_OK:
175     #    print "Warning: failed to enable realtime scheduling"
176
177     tb.start()                      # start flow graph
178     tb.wait()                       # wait for it to finish
179
180 if __name__ == '__main__':
181     try:
182         main()
183     except KeyboardInterrupt:
184         pass