summaryrefslogtreecommitdiff
path: root/gr-digital/examples
diff options
context:
space:
mode:
Diffstat (limited to 'gr-digital/examples')
-rw-r--r--gr-digital/examples/.gitignore2
-rw-r--r--gr-digital/examples/Makefile.am30
-rwxr-xr-xgr-digital/examples/benchmark_rx.py124
-rwxr-xr-xgr-digital/examples/benchmark_tx.py146
-rw-r--r--gr-digital/examples/receive_path.py140
-rw-r--r--gr-digital/examples/transmit_path.py119
6 files changed, 561 insertions, 0 deletions
diff --git a/gr-digital/examples/.gitignore b/gr-digital/examples/.gitignore
new file mode 100644
index 000000000..b336cc7ce
--- /dev/null
+++ b/gr-digital/examples/.gitignore
@@ -0,0 +1,2 @@
+/Makefile
+/Makefile.in
diff --git a/gr-digital/examples/Makefile.am b/gr-digital/examples/Makefile.am
new file mode 100644
index 000000000..8ed53fe41
--- /dev/null
+++ b/gr-digital/examples/Makefile.am
@@ -0,0 +1,30 @@
+#
+# Copyright 2011 Free Software Foundation, Inc.
+#
+# This file is part of GNU Radio
+#
+# GNU Radio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# GNU Radio is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Radio; see the file COPYING. If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+#
+
+include $(top_srcdir)/Makefile.common
+
+ourdatadir = $(exampledir)/digital
+
+dist_ourdata_SCRIPTS = \
+ transmit_path.py \
+ receive_path.py
+
+
diff --git a/gr-digital/examples/benchmark_rx.py b/gr-digital/examples/benchmark_rx.py
new file mode 100755
index 000000000..9d8734e3d
--- /dev/null
+++ b/gr-digital/examples/benchmark_rx.py
@@ -0,0 +1,124 @@
+#!/usr/bin/env python
+#
+# Copyright 2010,2011 Free Software Foundation, Inc.
+#
+# This file is part of GNU Radio
+#
+# GNU Radio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# GNU Radio is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Radio; see the file COPYING. If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+#
+
+from gnuradio import gr, gru
+from gnuradio import usrp
+from gnuradio import eng_notation
+from gnuradio.eng_option import eng_option
+from optparse import OptionParser
+
+# From gr-digital
+from gnuradio import digital
+
+# from current dir
+from receive_path import receive_path
+
+import random
+import struct
+import sys
+
+#import os
+#print os.getpid()
+#raw_input('Attach and press enter: ')
+
+class my_top_block(gr.top_block):
+ def __init__(self, demodulator, rx_callback, options):
+ gr.top_block.__init__(self)
+
+ # Set up receive path
+ self.rxpath = receive_path(demodulator, rx_callback, options)
+
+ if(options.from_file is not None):
+ self.source = gr.file_source(gr.sizeof_gr_complex, options.from_file)
+ else:
+ self.source = gr.null_source(gr.sizeof_gr_complex)
+
+ self.connect(self.source, self.rxpath)
+
+# /////////////////////////////////////////////////////////////////////////////
+# main
+# /////////////////////////////////////////////////////////////////////////////
+
+global n_rcvd, n_right
+
+def main():
+ global n_rcvd, n_right
+
+ n_rcvd = 0
+ n_right = 0
+
+ def rx_callback(ok, payload):
+ global n_rcvd, n_right
+ (pktno,) = struct.unpack('!H', payload[0:2])
+ n_rcvd += 1
+ if ok:
+ n_right += 1
+
+ print "ok = %5s pktno = %4d n_rcvd = %4d n_right = %4d" % (
+ ok, pktno, n_rcvd, n_right)
+
+ demods = digital.modulation_utils2.type_1_demods()
+
+ # Create Options Parser:
+ parser = OptionParser (option_class=eng_option, conflict_handler="resolve")
+ expert_grp = parser.add_option_group("Expert")
+
+ parser.add_option("-m", "--modulation", type="choice", choices=demods.keys(),
+ default='psk',
+ help="Select modulation from: %s [default=%%default]"
+ % (', '.join(demods.keys()),))
+ parser.add_option("","--from-file", default=None,
+ help="input file of samples to demod")
+
+ receive_path.add_options(parser, expert_grp)
+
+ for mod in demods.values():
+ mod.add_options(expert_grp)
+
+ (options, args) = parser.parse_args ()
+
+ if len(args) != 0:
+ parser.print_help(sys.stderr)
+ sys.exit(1)
+
+ if options.from_file is None:
+ if options.rx_freq is None:
+ sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
+ parser.print_help(sys.stderr)
+ sys.exit(1)
+
+
+ # build the graph
+ tb = my_top_block(demods[options.modulation], rx_callback, options)
+
+ r = gr.enable_realtime_scheduling()
+ if r != gr.RT_OK:
+ print "Warning: Failed to enable realtime scheduling."
+
+ tb.start() # start flow graph
+ tb.wait() # wait for it to finish
+
+if __name__ == '__main__':
+ try:
+ main()
+ except KeyboardInterrupt:
+ pass
diff --git a/gr-digital/examples/benchmark_tx.py b/gr-digital/examples/benchmark_tx.py
new file mode 100755
index 000000000..01902c0e3
--- /dev/null
+++ b/gr-digital/examples/benchmark_tx.py
@@ -0,0 +1,146 @@
+#!/usr/bin/env python
+#
+# Copyright 2010 Free Software Foundation, Inc.
+#
+# This file is part of GNU Radio
+#
+# GNU Radio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# GNU Radio is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Radio; see the file COPYING. If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+#
+
+from gnuradio import gr, gru
+from gnuradio import usrp
+from gnuradio import eng_notation
+from gnuradio.eng_option import eng_option
+from optparse import OptionParser
+
+# From gr-digital
+from gnuradio import digital
+
+# from current dir
+from transmit_path import transmit_path
+
+import random, time, struct, sys
+
+#import os
+#print os.getpid()
+#raw_input('Attach and press enter')
+
+class my_top_block(gr.top_block):
+ def __init__(self, modulator, options):
+ gr.top_block.__init__(self)
+
+ self.txpath = transmit_path(modulator, options)
+
+ if(options.to_file is not None):
+ self.sink = gr.file_sink(gr.sizeof_gr_complex, options.to_file)
+ else:
+ self.sink = gr.null_sink(gr.sizeof_gr_complex)
+
+ self.connect(self.txpath, self.sink)
+
+# /////////////////////////////////////////////////////////////////////////////
+# main
+# /////////////////////////////////////////////////////////////////////////////
+
+def main():
+
+ def send_pkt(payload='', eof=False):
+ return tb.txpath.send_pkt(payload, eof)
+
+ def rx_callback(ok, payload):
+ print "ok = %r, payload = '%s'" % (ok, payload)
+
+ mods = digital.modulation_utils2.type_1_mods()
+
+ parser = OptionParser(option_class=eng_option, conflict_handler="resolve")
+ expert_grp = parser.add_option_group("Expert")
+
+ parser.add_option("-m", "--modulation", type="choice", choices=mods.keys(),
+ default='psk',
+ help="Select modulation from: %s [default=%%default]"
+ % (', '.join(mods.keys()),))
+
+ parser.add_option("-s", "--size", type="eng_float", default=1500,
+ help="set packet size [default=%default]")
+ parser.add_option("-M", "--megabytes", type="eng_float", default=1.0,
+ help="set megabytes to transmit [default=%default]")
+ parser.add_option("","--discontinuous", action="store_true", default=False,
+ help="enable discontinous transmission (bursts of 5 packets)")
+ parser.add_option("","--from-file", default=None,
+ help="use intput file for packet contents")
+ parser.add_option("","--to-file", default=None,
+ help="Output file for modulated samples")
+
+ transmit_path.add_options(parser, expert_grp)
+
+ for mod in mods.values():
+ mod.add_options(expert_grp)
+
+ (options, args) = parser.parse_args ()
+
+ if len(args) != 0:
+ parser.print_help()
+ sys.exit(1)
+
+ if options.to_file is None:
+ if options.tx_freq is None:
+ sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
+ parser.print_help(sys.stderr)
+ sys.exit(1)
+
+ if options.from_file is not None:
+ source_file = open(options.from_file, 'r')
+
+ # build the graph
+ tb = my_top_block(mods[options.modulation], options)
+
+ r = gr.enable_realtime_scheduling()
+ if r != gr.RT_OK:
+ print "Warning: failed to enable realtime scheduling"
+
+ tb.start() # start flow graph
+
+ # generate and send packets
+ nbytes = int(1e6 * options.megabytes)
+ n = 0
+ pktno = 0
+ pkt_size = int(options.size)
+
+ while n < nbytes:
+ if options.from_file is None:
+ data = (pkt_size - 2) * chr(pktno & 0xff)
+ else:
+ data = source_file.read(pkt_size - 2)
+ if data == '':
+ break;
+
+ payload = struct.pack('!H', pktno & 0xffff) + data
+ send_pkt(payload)
+ n += len(payload)
+ sys.stderr.write('.')
+ if options.discontinuous and pktno % 5 == 4:
+ time.sleep(1)
+ pktno += 1
+
+ send_pkt(eof=True)
+
+ tb.wait() # wait for it to finish
+
+if __name__ == '__main__':
+ try:
+ main()
+ except KeyboardInterrupt:
+ pass
diff --git a/gr-digital/examples/receive_path.py b/gr-digital/examples/receive_path.py
new file mode 100644
index 000000000..9bc5f7b8f
--- /dev/null
+++ b/gr-digital/examples/receive_path.py
@@ -0,0 +1,140 @@
+#!/usr/bin/env python
+#
+# Copyright 2005,2006,2007 Free Software Foundation, Inc.
+#
+# This file is part of GNU Radio
+#
+# GNU Radio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# GNU Radio is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Radio; see the file COPYING. If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+#
+
+from gnuradio import gr, gru
+from gnuradio import eng_notation
+from gnuradio import digital
+
+import copy
+import sys
+
+# /////////////////////////////////////////////////////////////////////////////
+# receive path
+# /////////////////////////////////////////////////////////////////////////////
+
+class receive_path(gr.hier_block2):
+ def __init__(self, demod_class, rx_callback, options):
+ gr.hier_block2.__init__(self, "receive_path",
+ gr.io_signature(1, 1, gr.sizeof_gr_complex), # Input signature
+ gr.io_signature(0, 0, 0)) # Output signature
+
+
+ options = copy.copy(options) # make a copy so we can destructively modify
+
+ self._verbose = options.verbose
+ self._bitrate = options.bitrate # desired bit rate
+ self._samples_per_symbol = options.samples_per_symbol # desired samples/symbol
+
+ self._rx_callback = rx_callback # this callback is fired when there's a packet available
+ self._demod_class = demod_class # the demodulator_class we're using
+
+ # Get demod_kwargs
+ demod_kwargs = self._demod_class.extract_kwargs_from_options(options)
+
+ # Design filter to get actual channel we want
+ sw_decim = 1
+ chan_coeffs = gr.firdes.low_pass (1.0, # gain
+ sw_decim * self._samples_per_symbol, # sampling rate
+ 1.0, # midpoint of trans. band
+ 0.5, # width of trans. band
+ gr.firdes.WIN_HANN) # filter type
+ self.channel_filter = gr.fft_filter_ccc(sw_decim, chan_coeffs)
+
+ # receiver
+ self.packet_receiver = \
+ digital.demod_pkts(self._demod_class(**demod_kwargs),
+ access_code=None,
+ callback=self._rx_callback,
+ threshold=-1)
+
+ # Carrier Sensing Blocks
+ alpha = 0.001
+ thresh = 30 # in dB, will have to adjust
+ self.probe = gr.probe_avg_mag_sqrd_c(thresh,alpha)
+
+ # Display some information about the setup
+ if self._verbose:
+ self._print_verbage()
+
+ # connect block input to channel filter
+ self.connect(self, self.channel_filter)
+
+ # connect the channel input filter to the carrier power detector
+ self.connect(self.channel_filter, self.probe)
+
+ # connect channel filter to the packet receiver
+ self.connect(self.channel_filter, self.packet_receiver)
+
+ def bitrate(self):
+ return self._bitrate
+
+ def samples_per_symbol(self):
+ return self._samples_per_symbol
+
+ def carrier_sensed(self):
+ """
+ Return True if we think carrier is present.
+ """
+ #return self.probe.level() > X
+ return self.probe.unmuted()
+
+ def carrier_threshold(self):
+ """
+ Return current setting in dB.
+ """
+ return self.probe.threshold()
+
+ def set_carrier_threshold(self, threshold_in_db):
+ """
+ Set carrier threshold.
+
+ @param threshold_in_db: set detection threshold
+ @type threshold_in_db: float (dB)
+ """
+ self.probe.set_threshold(threshold_in_db)
+
+
+ def add_options(normal, expert):
+ """
+ Adds receiver-specific options to the Options Parser
+ """
+ if not normal.has_option("--bitrate"):
+ normal.add_option("-r", "--bitrate", type="eng_float", default=100e3,
+ help="specify bitrate [default=%default].")
+ normal.add_option("-v", "--verbose", action="store_true", default=False)
+ expert.add_option("-S", "--samples-per-symbol", type="float", default=None,
+ help="set samples/symbol [default=%default]")
+ expert.add_option("", "--log", action="store_true", default=False,
+ help="Log all parts of flow graph to files (CAUTION: lots of data)")
+
+ # Make a static method to call before instantiation
+ add_options = staticmethod(add_options)
+
+
+ def _print_verbage(self):
+ """
+ Prints information about the receive path
+ """
+ print "\nReceive Path:"
+ print "modulation: %s" % (self._demod_class.__name__)
+ print "bitrate: %sb/s" % (eng_notation.num_to_str(self._bitrate))
+ print "samples/symbol: %.4f" % (self._samples_per_symbol)
diff --git a/gr-digital/examples/transmit_path.py b/gr-digital/examples/transmit_path.py
new file mode 100644
index 000000000..91869ad9a
--- /dev/null
+++ b/gr-digital/examples/transmit_path.py
@@ -0,0 +1,119 @@
+#
+# Copyright 2005-2007,2011 Free Software Foundation, Inc.
+#
+# This file is part of GNU Radio
+#
+# GNU Radio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# GNU Radio is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Radio; see the file COPYING. If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+#
+
+from gnuradio import gr, gru
+from gnuradio import eng_notation
+from gnuradio import digital
+
+import copy
+import sys
+
+# /////////////////////////////////////////////////////////////////////////////
+# transmit path
+# /////////////////////////////////////////////////////////////////////////////
+
+class transmit_path(gr.hier_block2):
+ def __init__(self, modulator_class, options):
+ '''
+ See below for what options should hold
+ '''
+ gr.hier_block2.__init__(self, "transmit_path",
+ gr.io_signature(0, 0, 0), # Input signature
+ gr.io_signature(1, 1, gr.sizeof_gr_complex)) # Output signature
+
+ options = copy.copy(options) # make a copy so we can destructively modify
+
+ self._verbose = options.verbose
+ self._tx_amplitude = options.tx_amplitude # digital amplitude sent to USRP
+ self._bitrate = options.bitrate # desired bit rate
+ self._samples_per_symbol = options.samples_per_symbol # desired samples/baud
+
+ self._modulator_class = modulator_class # the modulator_class we are using
+
+ # Get mod_kwargs
+ mod_kwargs = self._modulator_class.extract_kwargs_from_options(options)
+
+ # transmitter
+ modulator = self._modulator_class(**mod_kwargs)
+ self.packet_transmitter = \
+ digital.mod_pkts(modulator,
+ access_code=None,
+ msgq_limit=4,
+ pad_for_usrp=True)
+
+ self.amp = gr.multiply_const_cc(1)
+ self.set_tx_amplitude(self._tx_amplitude)
+
+ # Display some information about the setup
+ if self._verbose:
+ self._print_verbage()
+
+ # Connect components in the flowgraph
+ self.connect(self.packet_transmitter, self.amp, self)
+
+ def set_tx_amplitude(self, ampl):
+ """
+ Sets the transmit amplitude sent to the USRP in volts
+ @param: ampl 0 <= ampl < 1.
+ """
+ self._tx_amplitude = max(0.0, min(ampl, 1))
+ self.amp.set_k(self._tx_amplitude)
+
+ def send_pkt(self, payload='', eof=False):
+ """
+ Calls the transmitter method to send a packet
+ """
+ return self.packet_transmitter.send_pkt(payload, eof)
+
+ def bitrate(self):
+ return self._bitrate
+
+ def samples_per_symbol(self):
+ return self._samples_per_symbol
+
+ def add_options(normal, expert):
+ """
+ Adds transmitter-specific options to the Options Parser
+ """
+ if not normal.has_option('--bitrate'):
+ normal.add_option("-r", "--bitrate", type="eng_float", default=100e3,
+ help="specify bitrate [default=%default].")
+ normal.add_option("", "--tx-amplitude", type="eng_float", default=0.250, metavar="AMPL",
+ help="set transmitter digital amplitude: 0 <= AMPL < 1 [default=%default]")
+ normal.add_option("-v", "--verbose", action="store_true", default=False)
+
+ expert.add_option("-S", "--samples-per-symbol", type="float", default=None,
+ help="set samples/symbol [default=%default]")
+ expert.add_option("", "--log", action="store_true", default=False,
+ help="Log all parts of flow graph to file (CAUTION: lots of data)")
+
+ # Make a static method to call before instantiation
+ add_options = staticmethod(add_options)
+
+ def _print_verbage(self):
+ """
+ Prints information about the transmit path
+ """
+ print "Tx amplitude %s" % (self._tx_amplitude)
+ print "modulation: %s" % (self._modulator_class.__name__)
+ print "bitrate: %sb/s" % (eng_notation.num_to_str(self._bitrate))
+ print "samples/symbol: %.4f" % (self._samples_per_symbol)
+