diff options
Diffstat (limited to 'gr-digital')
85 files changed, 5933 insertions, 376 deletions
diff --git a/gr-digital/examples/CMakeLists.txt b/gr-digital/examples/CMakeLists.txt index 9632c1638..2645557cc 100644 --- a/gr-digital/examples/CMakeLists.txt +++ b/gr-digital/examples/CMakeLists.txt @@ -20,18 +20,26 @@ include(GrPython) GR_PYTHON_INSTALL(PROGRAMS - transmit_path.py - receive_path.py - uhd_interface.py - benchmark_tx.py - benchmark_rx.py - tx_voice.py - rx_voice.py + example_costas.py + example_fll.py + example_timing.py run_length.py gen_whitener.py - digital_bert_rx.py - digital_bert_tx.py - tunnel.py DESTINATION ${GR_PKG_DATA_DIR}/examples/digital COMPONENT "digital_python" ) + +GR_PYTHON_INSTALL(PROGRAMS + narrowband/transmit_path.py + narrowband/receive_path.py + narrowband/uhd_interface.py + narrowband/benchmark_tx.py + narrowband/benchmark_rx.py + narrowband/tx_voice.py + narrowband/rx_voice.py + narrowband/digital_bert_rx.py + narrowband/digital_bert_tx.py + narrowband/tunnel.py + DESTINATION ${GR_PKG_DATA_DIR}/examples/digital/narrowband + COMPONENT "digital_python" +) diff --git a/gr-digital/examples/Makefile.am b/gr-digital/examples/Makefile.am index dc8aba6ca..3841bbd0e 100644 --- a/gr-digital/examples/Makefile.am +++ b/gr-digital/examples/Makefile.am @@ -21,24 +21,26 @@ include $(top_srcdir)/Makefile.common -ourdatadir = $(exampledir)/digital +dig_examples_prefix= $(exampledir)/digital -noinst_PYTHON = \ - example_costas.py \ - example_fll.py \ - example_timing.py +basicdatadir = $(dig_examples_prefix) +dist_basicdata_SCRIPTS = \ + example_costas.py \ + example_fll.py \ + example_timing.py \ + run_length.py \ + gen_whitener.py -dist_ourdata_SCRIPTS = \ - transmit_path.py \ - receive_path.py \ - uhd_interface.py \ - benchmark_tx.py \ - benchmark_rx.py \ - tx_voice.py \ - rx_voice.py \ - run_length.py \ - gen_whitener.py \ - digital_bert_rx.py \ - digital_bert_tx.py \ - tunnel.py +nbdatadir = $(dig_examples_prefix)/narrowband +dist_nbdata_SCRIPTS = \ + narrowband/transmit_path.py \ + narrowband/receive_path.py \ + narrowband/uhd_interface.py \ + narrowband/benchmark_tx.py \ + narrowband/benchmark_rx.py \ + narrowband/tx_voice.py \ + narrowband/rx_voice.py \ + narrowband/digital_bert_rx.py \ + narrowband/digital_bert_tx.py \ + narrowband/tunnel.py diff --git a/gr-digital/examples/benchmark_add_channel.py b/gr-digital/examples/narrowband/benchmark_add_channel.py index def1f8267..841833a08 100755 --- a/gr-digital/examples/benchmark_add_channel.py +++ b/gr-digital/examples/narrowband/benchmark_add_channel.py @@ -37,11 +37,9 @@ class my_top_block(gr.top_block): phase_offset = options.phase_offset*(math.pi/180.0) # calculate noise voltage from SNR - # FIXME: normalize to signal amplitude - power_in_signal = abs(1.0)**2 + power_in_signal = abs(options.tx_amplitude)**2 noise_power = power_in_signal/SNR noise_voltage = math.sqrt(noise_power) - print noise_voltage self.src = gr.file_source(gr.sizeof_gr_complex, ifile) #self.throttle = gr.throttle(gr.sizeof_gr_complex, options.sample_rate) @@ -74,6 +72,9 @@ def main(): help="set phase offset (in degrees) between Tx and Rx [default=%default]") parser.add_option("-m", "--use-multipath", action="store_true", default=False, help="Use a multipath channel [default=%default]") + parser.add_option("", "--tx-amplitude", type="eng_float", + default=1.0, + help="tell the simulator the signal amplitude [default=%default]") (options, args) = parser.parse_args () diff --git a/gr-digital/examples/benchmark_rx.py b/gr-digital/examples/narrowband/benchmark_rx.py index c586cc8fe..65aac3638 100755 --- a/gr-digital/examples/benchmark_rx.py +++ b/gr-digital/examples/narrowband/benchmark_rx.py @@ -32,7 +32,6 @@ from gnuradio import digital from receive_path import receive_path from uhd_interface import uhd_receiver -import random import struct import sys @@ -52,8 +51,10 @@ class my_top_block(gr.top_block): options.samples_per_symbol = self.source._sps elif(options.from_file is not None): + sys.stderr.write(("Reading samples from '%s'.\n\n" % (options.from_file))) self.source = gr.file_source(gr.sizeof_gr_complex, options.from_file) else: + sys.stderr.write("No source defined, pulling samples from null source.\n\n") self.source = gr.null_source(gr.sizeof_gr_complex) # Set up receive path @@ -86,7 +87,7 @@ def main(): print "ok = %5s pktno = %4d n_rcvd = %4d n_right = %4d" % ( ok, pktno, n_rcvd, n_right) - demods = digital.modulation_utils2.type_1_demods() + demods = digital.modulation_utils.type_1_demods() # Create Options Parser: parser = OptionParser (option_class=eng_option, conflict_handler="resolve") diff --git a/gr-digital/examples/benchmark_tx.py b/gr-digital/examples/narrowband/benchmark_tx.py index a4396dd98..1fd881981 100755 --- a/gr-digital/examples/benchmark_tx.py +++ b/gr-digital/examples/narrowband/benchmark_tx.py @@ -32,7 +32,7 @@ from gnuradio import digital from transmit_path import transmit_path from uhd_interface import uhd_transmitter -import random, time, struct, sys +import time, struct, sys #import os #print os.getpid() @@ -50,8 +50,10 @@ class my_top_block(gr.top_block): options.samples_per_symbol = self.sink._sps elif(options.to_file is not None): + sys.stderr.write(("Saving samples to '%s'.\n\n" % (options.to_file))) self.sink = gr.file_sink(gr.sizeof_gr_complex, options.to_file) else: + sys.stderr.write("No sink defined, dumping samples to null sink.\n\n") self.sink = gr.null_sink(gr.sizeof_gr_complex) # do this after for any adjustments to the options that may @@ -69,10 +71,7 @@ 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() + mods = digital.modulation_utils.type_1_mods() parser = OptionParser(option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") diff --git a/gr-digital/examples/digital_bert_rx.py b/gr-digital/examples/narrowband/digital_bert_rx.py index 9ee1f5ee8..9878f55e1 100755 --- a/gr-digital/examples/digital_bert_rx.py +++ b/gr-digital/examples/narrowband/digital_bert_rx.py @@ -178,7 +178,7 @@ def get_options(demods): uhd_receiver.add_options(parser) - demods = digital.modulation_utils2.type_1_demods() + demods = digital.modulation_utils.type_1_demods() for mod in demods.values(): mod.add_options(parser) @@ -191,7 +191,7 @@ def get_options(demods): if __name__ == "__main__": - demods = digital.modulation_utils2.type_1_demods() + demods = digital.modulation_utils.type_1_demods() (options, args) = get_options(demods) diff --git a/gr-digital/examples/digital_bert_tx.py b/gr-digital/examples/narrowband/digital_bert_tx.py index 6b544c7f7..96cb338fe 100755 --- a/gr-digital/examples/digital_bert_tx.py +++ b/gr-digital/examples/narrowband/digital_bert_tx.py @@ -122,7 +122,7 @@ def get_options(mods): return (options, args) if __name__ == "__main__": - mods = digital.modulation_utils2.type_1_mods() + mods = digital.modulation_utils.type_1_mods() (options, args) = get_options(mods) diff --git a/gr-digital/examples/receive_path.py b/gr-digital/examples/narrowband/receive_path.py index 1c5e58963..1c5e58963 100644 --- a/gr-digital/examples/receive_path.py +++ b/gr-digital/examples/narrowband/receive_path.py diff --git a/gr-digital/examples/rx_voice.py b/gr-digital/examples/narrowband/rx_voice.py index b58704e16..42d7b893b 100755 --- a/gr-digital/examples/rx_voice.py +++ b/gr-digital/examples/narrowband/rx_voice.py @@ -114,7 +114,7 @@ def main(): print "ok = %r n_rcvd = %4d n_right = %4d" % ( ok, n_rcvd, n_right) - demods = digital.modulation_utils2.type_1_demods() + demods = digital.modulation_utils.type_1_demods() # Create Options Parser: parser = OptionParser (option_class=eng_option, conflict_handler="resolve") diff --git a/gr-digital/examples/transmit_path.py b/gr-digital/examples/narrowband/transmit_path.py index 3af37f330..4d6162ed6 100644 --- a/gr-digital/examples/transmit_path.py +++ b/gr-digital/examples/narrowband/transmit_path.py @@ -19,7 +19,7 @@ # Boston, MA 02110-1301, USA. # -from gnuradio import gr, gru +from gnuradio import gr from gnuradio import eng_notation from gnuradio import digital @@ -96,15 +96,20 @@ class transmit_path(gr.hier_block2): Adds transmitter-specific options to the Options Parser """ if not normal.has_option('--bitrate'): - normal.add_option("-r", "--bitrate", type="eng_float", default=100e3, + 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", + 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) + normal.add_option("-v", "--verbose", action="store_true", + default=False) - expert.add_option("-S", "--samples-per-symbol", type="float", default=2, + expert.add_option("-S", "--samples-per-symbol", type="float", + default=2, help="set samples/symbol [default=%default]") - expert.add_option("", "--log", action="store_true", default=False, + 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 @@ -117,5 +122,5 @@ class transmit_path(gr.hier_block2): 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()) + print "samples/symbol: %.4f" % (self.samples_per_symbol()) print "Differential: %s" % (self.differential()) diff --git a/gr-digital/examples/tunnel.py b/gr-digital/examples/narrowband/tunnel.py index d25594df5..7f40bb1c3 100755 --- a/gr-digital/examples/tunnel.py +++ b/gr-digital/examples/narrowband/tunnel.py @@ -35,7 +35,7 @@ # //////////////////////////////////////////////////////////////////// -from gnuradio import gr, digital, uhd +from gnuradio import gr, digital from gnuradio import eng_notation from gnuradio.eng_option import eng_option from optparse import OptionParser @@ -118,6 +118,14 @@ class my_top_block(gr.top_block): """ return self.rxpath.carrier_sensed() + def set_freq(self, target_freq): + """ + Set the center frequency we're interested in. + """ + + self.sink.set_freq(target_freq) + self.source.set_freq(target_freq) + # //////////////////////////////////////////////////////////////////// # Carrier Sense MAC @@ -189,8 +197,8 @@ class cs_mac(object): def main(): - mods = digital.modulation_utils2.type_1_mods() - demods = digital.modulation_utils2.type_1_demods() + mods = digital.modulation_utils.type_1_mods() + demods = digital.modulation_utils.type_1_demods() parser = OptionParser (option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") diff --git a/gr-digital/examples/tx_voice.py b/gr-digital/examples/narrowband/tx_voice.py index eabb5e3c3..3d767a077 100755 --- a/gr-digital/examples/tx_voice.py +++ b/gr-digital/examples/narrowband/tx_voice.py @@ -102,7 +102,7 @@ def main(): def rx_callback(ok, payload): print "ok = %r, payload = '%s'" % (ok, payload) - mods = digital.modulation_utils2.type_1_mods() + mods = digital.modulation_utils.type_1_mods() parser = OptionParser(option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") diff --git a/gr-digital/examples/uhd_interface.py b/gr-digital/examples/narrowband/uhd_interface.py index 8420f3eec..8420f3eec 100644 --- a/gr-digital/examples/uhd_interface.py +++ b/gr-digital/examples/narrowband/uhd_interface.py diff --git a/gr-digital/examples/ofdm/benchmark_add_channel.py b/gr-digital/examples/ofdm/benchmark_add_channel.py new file mode 100755 index 000000000..01776d209 --- /dev/null +++ b/gr-digital/examples/ofdm/benchmark_add_channel.py @@ -0,0 +1,110 @@ +#!/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 +from gnuradio import eng_notation +from gnuradio.eng_option import eng_option +from optparse import OptionParser + +import random, math, sys + +class my_top_block(gr.top_block): + def __init__(self, ifile, ofile, options): + gr.top_block.__init__(self) + + SNR = 10.0**(options.snr/10.0) + time_offset = options.time_offset + phase_offset = options.phase_offset*(math.pi/180.0) + + # calculate noise voltage from SNR + power_in_signal = abs(options.tx_amplitude)**2 + noise_power = power_in_signal/SNR + noise_voltage = math.sqrt(noise_power) + print "Noise voltage: ", noise_voltage + + frequency_offset = options.frequency_offset / options.fft_length + + self.src = gr.file_source(gr.sizeof_gr_complex, ifile) + #self.throttle = gr.throttle(gr.sizeof_gr_complex, options.sample_rate) + self.channel = gr.channel_model(noise_voltage, frequency_offset, + time_offset, noise_seed=random.randint(0,100000)) + self.phase = gr.multiply_const_cc(complex(math.cos(phase_offset), + math.sin(phase_offset))) + self.snk = gr.file_sink(gr.sizeof_gr_complex, ofile) + + self.connect(self.src, self.channel, self.phase, self.snk) + + +# ///////////////////////////////////////////////////////////////////////////// +# main +# ///////////////////////////////////////////////////////////////////////////// + +def main(): + # Create Options Parser: + usage = "benchmack_add_channel.py [options] <input file> <output file>" + parser = OptionParser (usage=usage, option_class=eng_option, conflict_handler="resolve") + parser.add_option("-n", "--snr", type="eng_float", default=30, + help="set the SNR of the channel in dB [default=%default]") + parser.add_option("", "--seed", action="store_true", default=False, + help="use a random seed for AWGN noise [default=%default]") + parser.add_option("-f", "--frequency-offset", type="eng_float", default=0, + help="set frequency offset introduced by channel [default=%default]") + parser.add_option("-t", "--time-offset", type="eng_float", default=1.0, + help="set timing offset between Tx and Rx [default=%default]") + parser.add_option("-p", "--phase-offset", type="eng_float", default=0, + help="set phase offset (in degrees) between Tx and Rx [default=%default]") + parser.add_option("-m", "--use-multipath", action="store_true", default=False, + help="Use a multipath channel [default=%default]") + parser.add_option("", "--fft-length", type="intx", default=None, + help="set the number of FFT bins [default=%default]") + parser.add_option("", "--tx-amplitude", type="eng_float", + default=1.0, + help="tell the simulator the signal amplitude [default=%default]") + + (options, args) = parser.parse_args () + + if len(args) != 2: + parser.print_help(sys.stderr) + sys.exit(1) + + if options.fft_length is None: + sys.stderr.write("Please enter the FFT length of the OFDM signal.\n") + sys.exit(1) + + ifile = args[0] + ofile = args[1] + + # build the graph + tb = my_top_block(ifile, ofile, 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/ofdm/benchmark_rx.py b/gr-digital/examples/ofdm/benchmark_rx.py new file mode 100755 index 000000000..f2ed3b8ff --- /dev/null +++ b/gr-digital/examples/ofdm/benchmark_rx.py @@ -0,0 +1,122 @@ +#!/usr/bin/env python +# +# Copyright 2006,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, blks2 +from gnuradio import eng_notation +from gnuradio.eng_option import eng_option +from optparse import OptionParser + +from gnuradio import digital + +# from current dir +from receive_path import receive_path +from uhd_interface import uhd_receiver + +import struct, sys + +class my_top_block(gr.top_block): + def __init__(self, callback, options): + gr.top_block.__init__(self) + + if(options.rx_freq is not None): + self.source = uhd_receiver(options.address, + options.bandwidth, + options.rx_freq, options.rx_gain, + options.antenna, options.verbose) + elif(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) + + # Set up receive path + # do this after for any adjustments to the options that may + # occur in the sinks (specifically the UHD sink) + self.rxpath = receive_path(callback, options) + + self.connect(self.source, self.rxpath) + + +# ///////////////////////////////////////////////////////////////////////////// +# main +# ///////////////////////////////////////////////////////////////////////////// + +def main(): + + global n_rcvd, n_right + + n_rcvd = 0 + n_right = 0 + + def rx_callback(ok, payload): + global n_rcvd, n_right + n_rcvd += 1 + (pktno,) = struct.unpack('!H', payload[0:2]) + if ok: + n_right += 1 + print "ok: %r \t pktno: %d \t n_rcvd: %d \t n_right: %d" % (ok, pktno, n_rcvd, n_right) + + if 0: + printlst = list() + for x in payload[2:]: + t = hex(ord(x)).replace('0x', '') + if(len(t) == 1): + t = '0' + t + printlst.append(t) + printable = ''.join(printlst) + + print printable + print "\n" + + parser = OptionParser(option_class=eng_option, conflict_handler="resolve") + expert_grp = parser.add_option_group("Expert") + parser.add_option("","--discontinuous", action="store_true", default=False, + help="enable discontinuous") + parser.add_option("","--from-file", default=None, + help="input file of samples to demod") + + receive_path.add_options(parser, expert_grp) + uhd_receiver.add_options(parser) + digital.ofdm_demod.add_options(parser, expert_grp) + + (options, args) = parser.parse_args () + + 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(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/ofdm/benchmark_tx.py b/gr-digital/examples/ofdm/benchmark_tx.py new file mode 100755 index 000000000..ad3609b52 --- /dev/null +++ b/gr-digital/examples/ofdm/benchmark_tx.py @@ -0,0 +1,121 @@ +#!/usr/bin/env python +# +# Copyright 2005,2006,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 +from gnuradio import eng_notation +from gnuradio.eng_option import eng_option +from optparse import OptionParser +import time, struct, sys + +from gnuradio import digital + +# from current dir +from transmit_path import transmit_path +from uhd_interface import uhd_transmitter + +class my_top_block(gr.top_block): + def __init__(self, options): + gr.top_block.__init__(self) + + if(options.tx_freq is not None): + self.sink = uhd_transmitter(options.address, + options.bandwidth, + options.tx_freq, options.tx_gain, + options.antenna, options.verbose) + elif(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) + + # do this after for any adjustments to the options that may + # occur in the sinks (specifically the UHD sink) + self.txpath = transmit_path(options) + + self.connect(self.txpath, self.sink) + +# ///////////////////////////////////////////////////////////////////////////// +# main +# ///////////////////////////////////////////////////////////////////////////// + +def main(): + + def send_pkt(payload='', eof=False): + return tb.txpath.send_pkt(payload, eof) + + parser = OptionParser(option_class=eng_option, conflict_handler="resolve") + expert_grp = parser.add_option_group("Expert") + parser.add_option("-s", "--size", type="eng_float", default=400, + 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 discontinuous mode") + 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) + digital.ofdm_mod.add_options(parser, expert_grp) + uhd_transmitter.add_options(parser) + + (options, args) = parser.parse_args () + + # build the graph + tb = my_top_block(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/ofdm/gr_plot_ofdm.py b/gr-digital/examples/ofdm/gr_plot_ofdm.py new file mode 100755 index 000000000..b24855148 --- /dev/null +++ b/gr-digital/examples/ofdm/gr_plot_ofdm.py @@ -0,0 +1,278 @@ +#!/usr/bin/env python +# +# Copyright 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. +# + +import math, struct, sys +from optparse import OptionParser +from math import log10 + +try: + import scipy + from scipy import fftpack +except ImportError: + print "Error: Program requires scipy (see: www.scipy.org)." + sys.exit(1) + +try: + from pylab import * + from matplotlib.font_manager import fontManager, FontProperties +except ImportError: + print "Error: Program requires matplotlib (see: matplotlib.sourceforge.net)." + sys.exit(1) + +matplotlib.interactive(True) +matplotlib.use('TkAgg') + +class draw_constellation: + def __init__(self, options): + derot_file = "ofdm_frame_sink_c.dat" + acq_file = "ofdm_frame_acq_c.dat" + fft_file = "ofdm_receiver-fft_out_c.dat" + + self.h_derot_file = open(derot_file, "r") + self.h_acq_file = open(acq_file, "r") + self.h_fft_file = open(fft_file, "r") + + self.occ_tones = options.occ_tones + self.fft_size = options.fft_size + self.symbol = options.start + self.sample_rate = options.sample_rate + + self.axis_font_size = 16 + self.label_font_size = 18 + self.title_font_size = 20 + self.text_size = 22 + + # Setup PLOT + self.fig = figure(1, figsize=(14, 9), facecolor='w') + rcParams['xtick.labelsize'] = self.axis_font_size + rcParams['ytick.labelsize'] = self.axis_font_size + + self.text_sym = figtext(0.05, 0.95, ("Symbol: %s" % self.symbol), weight="heavy", size=self.text_size) + + self.make_plots() + + self.button_left_axes = self.fig.add_axes([0.45, 0.01, 0.05, 0.05], frameon=True) + self.button_left = Button(self.button_left_axes, "<") + self.button_left_callback = self.button_left.on_clicked(self.button_left_click) + + self.button_right_axes = self.fig.add_axes([0.50, 0.01, 0.05, 0.05], frameon=True) + self.button_right = Button(self.button_right_axes, ">") + self.button_right_callback = self.button_right.on_clicked(self.button_right_click) + + self.xlim = self.sp_eq.get_xlim() + + self.manager = get_current_fig_manager() + #connect('draw_event', self.zoom) + connect('key_press_event', self.click) + show() + + def get_data(self): + self.text_sym.set_text("Symbol: %d" % (self.symbol)) + + derot_data = scipy.fromfile(self.h_derot_file, dtype=scipy.complex64, count=self.occ_tones) + acq_data = scipy.fromfile(self.h_acq_file, dtype=scipy.complex64, count=self.occ_tones) + fft_data = scipy.fromfile(self.h_fft_file, dtype=scipy.complex64, count=self.fft_size) + if(len(acq_data) == 0): + print "End of File" + else: + self.acq_data_reals = [r.real for r in acq_data] + self.acq_data_imags = [i.imag for i in acq_data] + self.derot_data_reals = [r.real for r in derot_data] + self.derot_data_imags = [i.imag for i in derot_data] + + self.unequalized_angle = [math.atan2(x.imag, x.real) for x in fft_data] + self.equalized_angle = [math.atan2(x.imag, x.real) for x in acq_data] + self.derot_equalized_angle = [math.atan2(x.imag, x.real) for x in derot_data] + + self.time = [i*(1/self.sample_rate) for i in range(len(acq_data))] + ffttime = [i*(1/self.sample_rate) for i in range(len(fft_data))] + + self.freq = self.get_freq(ffttime, self.sample_rate) + + for i in range(len(fft_data)): + if(abs(fft_data[i]) == 0.0): + fft_data[i] = complex(1e-6,1e-6) + self.fft_data = [20*log10(abs(f)) for f in fft_data] + + def get_freq(self, time, sample_rate, T=1): + N = len(time) + Fs = 1.0 / (max(time) - min(time)) + Fn = 0.5 * sample_rate + freq = [-Fn + i*Fs for i in range(N)] + return freq + + def make_plots(self): + self.h_acq_file.seek(8*self.symbol*self.occ_tones, 0) + self.h_fft_file.seek(8*self.symbol*self.fft_size, 0) + self.h_derot_file.seek(8*self.symbol*self.occ_tones, 0) + + self.get_data() + + # Subplot: constellation of rotated symbols + self.sp_const = self.fig.add_subplot(4,1,1, position=[0.15, 0.55, 0.3, 0.35]) + self.sp_const.set_title(("Constellation"), fontsize=self.title_font_size, fontweight="bold") + self.sp_const.set_xlabel("Inphase", fontsize=self.label_font_size, fontweight="bold") + self.sp_const.set_ylabel("Qaudrature", fontsize=self.label_font_size, fontweight="bold") + self.plot_const = plot(self.acq_data_reals, self.acq_data_imags, 'bo') + self.plot_const += plot(self.derot_data_reals, self.derot_data_imags, 'ro') + self.sp_const.axis([-2, 2, -2, 2]) + + # Subplot: unequalized angle + self.sp_uneq = self.fig.add_subplot(4,2,1, position=[0.575, 0.55, 0.3, 0.35]) + self.sp_uneq.set_title(("Unequalized Angle"), fontsize=self.title_font_size, fontweight="bold") + self.sp_uneq.set_xlabel("Time (s)", fontsize=self.label_font_size, fontweight="bold") + self.sp_uneq.set_ylabel("Angle", fontsize=self.label_font_size, fontweight="bold") + uneqscale = range(len(self.unequalized_angle)) + self.plot_uneq = plot(uneqscale, self.unequalized_angle, 'bo') + + # Subplot: equalized angle + self.sp_eq = self.fig.add_subplot(4,1,2, position=[0.15, 0.1, 0.3, 0.35]) + self.sp_eq.set_title(("Equalized Angle"), fontsize=self.title_font_size, fontweight="bold") + self.sp_eq.set_xlabel("Time (s)", fontsize=self.label_font_size, fontweight="bold") + self.sp_eq.set_ylabel("Angle", fontsize=self.label_font_size, fontweight="bold") + eqscale = range(len(self.equalized_angle)) + self.plot_eq = plot(eqscale, self.equalized_angle, 'bo') + self.plot_eq += plot(eqscale, self.derot_equalized_angle, 'ro', markersize=4) + + # Subplot: FFT + self.sp_fft = self.fig.add_subplot(4,2,2, position=[0.575, 0.1, 0.3, 0.35]) + self.sp_fft.set_title(("FFT"), fontsize=self.title_font_size, fontweight="bold") + self.sp_fft.set_xlabel("Frequency (MHz)", fontsize=self.label_font_size, fontweight="bold") + self.sp_fft.set_ylabel("Power (dBm)", fontsize=self.label_font_size, fontweight="bold") + self.plot_fft = plot(self.freq, self.fft_data, '-bo') + + draw() + + def update_plots(self): + eqscale = range(len(self.equalized_angle)) + uneqscale = range(len(self.unequalized_angle)) + self.plot_eq[0].set_data([eqscale, self.equalized_angle]) + self.plot_eq[1].set_data([eqscale, self.derot_equalized_angle]) + self.plot_uneq[0].set_data([uneqscale, self.unequalized_angle]) + self.sp_eq.set_ylim([-4, 4]) + self.sp_uneq.set_ylim([-4, 4]) + + #self.sp_iq.axis([min(self.time), max(self.time), + # 1.5*min([min(self.acq_data_reals), min(self.acq_data_imags)]), + # 1.5*max([max(self.acq_data_reals), max(self.acq_data_imags)])]) + + self.plot_const[0].set_data([self.acq_data_reals, self.acq_data_imags]) + self.plot_const[1].set_data([self.derot_data_reals, self.derot_data_imags]) + self.sp_const.axis([-2, 2, -2, 2]) + + self.plot_fft[0].set_data([self.freq, self.fft_data]) + + draw() + + def zoom(self, event): + newxlim = self.sp_eq.get_xlim() + if(newxlim != self.xlim): + self.xlim = newxlim + r = self.reals[int(ceil(self.xlim[0])) : int(ceil(self.xlim[1]))] + i = self.imags[int(ceil(self.xlim[0])) : int(ceil(self.xlim[1]))] + + self.plot_const[0].set_data(r, i) + self.sp_const.axis([-2, 2, -2, 2]) + self.manager.canvas.draw() + draw() + + def click(self, event): + forward_valid_keys = [" ", "down", "right"] + backward_valid_keys = ["up", "left"] + + if(find(event.key, forward_valid_keys)): + self.step_forward() + + elif(find(event.key, backward_valid_keys)): + self.step_backward() + + def button_left_click(self, event): + self.step_backward() + + def button_right_click(self, event): + self.step_forward() + + def step_forward(self): + self.symbol += 1 + self.get_data() + self.update_plots() + + def step_backward(self): + # Step back in file position + self.symbol -= 1 + if(self.h_acq_file.tell() >= 16*self.occ_tones): + self.h_acq_file.seek(-16*self.occ_tones, 1) + else: + self.symbol = 0 + self.h_acq_file.seek(-self.h_acq_file.tell(),1) + + + if(self.h_derot_file.tell() >= 16*self.occ_tones): + self.h_derot_file.seek(-16*self.occ_tones, 1) + else: + self.symbol = 0 + self.h_derot_file.seek(-self.h_derot_file.tell(),1) + + + if(self.h_fft_file.tell() >= 16*self.fft_size): + self.h_fft_file.seek(-16*self.fft_size, 1) + else: + self.symbol = 0 + self.h_fft_file.seek(-self.h_fft_file.tell(),1) + + self.get_data() + self.update_plots() + + + +#FIXME: there must be a way to do this with a Python builtin +def find(item_in, list_search): + for l in list_search: + if item_in == l: + return True + return False + +def main(): + usage="%prog: [options]" + + parser = OptionParser(conflict_handler="resolve", usage=usage) + parser.add_option("", "--fft-size", type="int", default=512, + help="Specify the size of the FFT [default=%default]") + parser.add_option("", "--occ-tones", type="int", default=200, + help="Specify the number of occupied tones [default=%default]") + parser.add_option("-s", "--start", type="int", default=0, + help="Specify the starting symbol to plot [default=%default]") + parser.add_option("-R", "--sample-rate", type="float", default=1.0, + help="Set the sampler rate of the data [default=%default]") + + (options, args) = parser.parse_args () + + dc = draw_constellation(options) + +if __name__ == "__main__": + try: + main() + except KeyboardInterrupt: + pass + + + diff --git a/gr-digital/examples/ofdm/ofdm_mod_demod_test.py b/gr-digital/examples/ofdm/ofdm_mod_demod_test.py new file mode 100755 index 000000000..b1521da6d --- /dev/null +++ b/gr-digital/examples/ofdm/ofdm_mod_demod_test.py @@ -0,0 +1,179 @@ +#!/usr/bin/env python +# +# Copyright 2005,2006,2008 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, ofdm_packet_utils +import gnuradio.gr.gr_threading as _threading +from gnuradio import eng_notation +from gnuradio.eng_option import eng_option +from optparse import OptionParser + +import random, time, struct, sys, math, os + +class my_top_block(gr.top_block): + def __init__(self, callback, options): + gr.top_block.__init__(self) + + # hard-coded known symbol + ks1 = known_symbols_4512_1[0:options.occupied_tones] + ks2 = known_symbols_4512_2[0:options.occupied_tones] + + self._rcvd_pktq = gr.msg_queue() + + # accepts messages from the outside world + self.ofdm_mapper = gr.ofdm_bpsk_mapper(4, options.occupied_tones, options.fft_length, ks1, ks2) + self.ofdm_corr = gr.ofdm_correlator(options.occupied_tones, options.fft_length, 0, ks1, ks2) + self.ofdm_framer = gr.ofdm_frame_sink(self._rcvd_pktq, options.occupied_tones) + + if 0: # set to 1 to put the correlator in the path to take over the signalling + self.connect((self.ofdm_mapper, 0), (self.ofdm_corr, 0)) + self.connect((self.ofdm_corr, 0), (self.ofdm_framer, 0)) + self.connect((self.ofdm_corr, 1), (self.ofdm_framer, 1)) + + self.connect((self.ofdm_mapper,0), gr.file_sink(gr.sizeof_gr_complex*options.fft_length, "ofdm_mapper.dat")) + self.connect((self.ofdm_corr,0), gr.file_sink(gr.sizeof_gr_complex*options.occupied_tones, "ofdm_corr.dat")) + self.connect((self.ofdm_corr,1), gr.file_sink(gr.sizeof_char, "ofdm_sig.dat")) + + else: + self.connect((self.ofdm_mapper, 0), (self.ofdm_framer, 0)) + self.connect((self.ofdm_mapper, 1), (self.ofdm_framer, 1)) + + self.connect((self.ofdm_mapper,0), gr.file_sink(gr.sizeof_gr_complex*options.fft_length, "ofdm_mapper.dat")) + self.connect((self.ofdm_mapper,1), gr.file_sink(gr.sizeof_char, "ofdm_sig.dat")) + + self._watcher = _queue_watcher_thread(self._rcvd_pktq, callback) + + def send_pkt(self, payload='', eof=False): + if eof: + msg = gr.message(1) + else: + pkt = ofdm_packet_utils.make_packet(payload, 1, 1, False, whiten=False) + msg = gr.message_from_string(pkt) + self.ofdm_mapper.msgq().insert_tail(msg) + +class _queue_watcher_thread(_threading.Thread): + def __init__(self, rcvd_pktq, callback): + _threading.Thread.__init__(self) + self.setDaemon(1) + self.rcvd_pktq = rcvd_pktq + self.callback = callback + self.keep_running = True + self.start() + + def run(self): + while self.keep_running: + msg = self.rcvd_pktq.delete_head() + ok, payload = ofdm_packet_utils.unmake_packet(msg.to_string(), whiten=False) + if self.callback: + self.callback(ok, payload) + +# ///////////////////////////////////////////////////////////////////////////// +# main +# ///////////////////////////////////////////////////////////////////////////// + +def main(): + ''' Use this program to tie the OFDM modulators straight into the frame sink, with or without + the correlator in between. This is for testing of the modulators and demodulators only without + receiver and sync functionality.''' + + global n_rcvd, n_right + + n_rcvd = 0 + n_right = 0 + + def send_pkt(payload='', eof=False): + return fg.send_pkt(payload, eof) + + def rx_callback(ok, payload): + global n_rcvd, n_right + n_rcvd += 1 + (pktno,) = struct.unpack('!H', payload[0:2]) + if ok: + n_right += 1 + print "ok: %r \t pktno: %d \t n_rcvd: %d \t n_right: %d" % (ok, pktno, n_rcvd, n_right) + + printlst = list() + for x in payload[2:]: + t = hex(ord(x)).replace('0x', '') + if(len(t) == 1): + t = '0' + t + printlst.append(t) + printable = ''.join(printlst) + + print printable + print "\n" + + parser = OptionParser(option_class=eng_option, conflict_handler="resolve") + expert = parser.add_option_group("Expert") + parser.add_option("-s", "--size", type="eng_float", default=1450, + help="set packet size [default=%default]") + parser.add_option("-M", "--megabytes", type="eng_float", default=1.0, + help="set megabytes to transmit [default=%default]") + expert.add_option("", "--fft-length", type="intx", default=512, + help="set the number of FFT bins [default=%default]") + expert.add_option("", "--occupied-tones", type="intx", default=200, + help="set the number of occupied FFT bins [default=%default]") + expert.add_option("", "--cp-length", type="intx", default=128, + help="set the number of bits in the cyclic prefix [default=%default]") + expert.add_option("", "--fft-length", type="intx", default=512, + help="set the number of FFT bins [default=%default]") + expert.add_option("", "--occupied-tones", type="intx", default=200, + help="set the number of occupied FFT bins [default=%default]") + expert.add_option("", "--cp-length", type="intx", default=128, + help="set the number of bits in the cyclic prefix [default=%default]") + + (options, args) = parser.parse_args () + + # build the graph + tb = my_top_block(rx_callback, options) + + 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: + #r = ''.join([chr(random.randint(0,255)) for i in range(pkt_size-2)]) + #pkt_contents = struct.pack('!H', pktno) + r + + pkt_contents = struct.pack('!H', pktno) + (pkt_size - 2) * chr(pktno & 0xff) + + send_pkt(pkt_contents) + n += pkt_size + pktno += 1 + + send_pkt(eof=True) + tb.wait() # wait for it to finish + +known_symbols_4512_1 = [-1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, 1, -1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1, -1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, 1, 1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1, 1, 1, -1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, 1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, -1, -1, 1] + +known_symbols_4512_2 = [1, 1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, -1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, 1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, -1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1] + + +if __name__ == '__main__': + try: + main() + except KeyboardInterrupt: + pass + diff --git a/gr-digital/examples/ofdm/ofdm_sync.m b/gr-digital/examples/ofdm/ofdm_sync.m new file mode 100644 index 000000000..d5df42137 --- /dev/null +++ b/gr-digital/examples/ofdm/ofdm_sync.m @@ -0,0 +1,28 @@ +% + +function [theta, g, g1, k, h] = ofdm_sync(signal,SNR,FFTSIZE,CPLEN) + + rho = SNR/(SNR+1); + + d = abs(signal).^2; + g = [ zeros(1,FFTSIZE) signal(1:max(size(signal))-FFTSIZE) ]; + g1 = conj(g); + f = abs(g).^2; + c = d + f; + moving_sum_taps = rho/2 * ones(1,CPLEN); + b = conv(c,moving_sum_taps); + b = b(1:max(size(signal))); + %b = b(CPLEN:max(size(b))); + + k = g1 .* signal; + + moving_sum_taps2 = ones(1, CPLEN); + h = conv(k,moving_sum_taps2); + h = h(1:max(size(signal))); + %h = h(CPLEN:max(size(h))); + + a = abs(h); + + theta = a-b; + +endfunction diff --git a/gr-digital/examples/ofdm/ofdm_sync_pn.m b/gr-digital/examples/ofdm/ofdm_sync_pn.m new file mode 100644 index 000000000..d93c0ca92 --- /dev/null +++ b/gr-digital/examples/ofdm/ofdm_sync_pn.m @@ -0,0 +1,21 @@ +mf = read_float_binary('ofdm_sync_pn-mf_f.dat'); +theta_pn = read_float_binary('ofdm_sync_pn-theta_f.dat'); +peaks_pn = read_char_binary('ofdm_sync_pn-peaks_b.dat'); +regen_pn = read_char_binary('ofdm_sync_pn-regen_b.dat'); +angle_pn = read_float_binary('ofdm_sync_pn-epsilon_f.dat'); + +plot(mf, 'k') +hold +plot(theta_pn, 'g'); +plot(peaks_pn, 'r'); +plot(regen_pn, 'b'); +xlim([100, 50000]); +ylim([0, 1]) +i = find(peaks_pn); +i(100:200) +hold off + +ipeaks = find(peaks_pn); +dpeaks = diff(ipeaks); +hist(dpeaks, 30) +set(gca, 'FontSize', 30, 'FontWeight', 'Bold'); diff --git a/gr-digital/examples/ofdm/plot_ofdm.m b/gr-digital/examples/ofdm/plot_ofdm.m new file mode 100755 index 000000000..2a649b5f5 --- /dev/null +++ b/gr-digital/examples/ofdm/plot_ofdm.m @@ -0,0 +1,74 @@ +function plot_ofdm(fft_size, occ_tones) + +ofdm = read_complex_binary('ofdm_frame_acq_c.dat'); +ofdm_split = split_vect(ofdm, occ_tones); + +ofdm_derot = read_complex_binary('ofdm_frame_sink_c.dat'); +ofdm_derot_split = split_vect(ofdm_derot, occ_tones); + +fftc = read_complex_binary('fft_out_c.dat'); +fftc_split = split_vect(fftc, fft_size); + +size(ofdm_split) +size(ofdm_derot_split) +disp "DEROTATED SPLIT" +ofdm_derot(1:100) + + +figure(1) +#set(gcf, 'Position', [50 50 1000 600]); + +a = size(ofdm_split); +if nargin == 3 + maxcount = count; + if maxcount > a(1) + error('plot_ofdm:tolong', 'Requested count size exceeds size of vectors'); + end +else + maxcount = a(1); +end + +for i = 1:size(ofdm_split)[0] + x = ofdm_split(i,:); + y = fftc_split(i+1,:); + + subplot(2,2,1); + plot(real(x), imag(x), 'bo'); + #set(gca, 'FontSize', 30, 'FontWeight', 'Bold'); + axis([-1.5, 1.5, -1.5, 1.5]) + #title('I&Q Constellation', 'FontSize', 36); + #xlabel('Inphase', 'FontSize', 32); + #ylabel('Quadrature', 'FontSize', 32); + + subplot(2,2,3); + plot(angle(x*j), 'bo'); + #set(gca, 'FontSize', 30, 'FontWeight', 'Bold'); + axis([0, occ_tones, -3.5, 3.5]) + #title('Equalized Symbol Angle', 'FontSize', 36); + #xlabel('Bin Number (Occ. Tones)', 'FontSize', 32); + #ylabel('Symbol Angle', 'FontSize', 32); + + subplot(2,2,2); + plot(angle(y*j), 'bo'); + #set(gca, 'FontSize', 30, 'FontWeight', 'Bold'); + axis([0, fft_size, -3.5, 3.5]) + #title('Unequalized Symbol Angle', 'FontSize', 36); + #xlabel('Bin Number (FFT Size)', 'FontSize', 32); + #ylabel('Symbol Angle', 'FontSize', 32); + + Y = 20*log10(abs(y) ./ max(abs(y))); + subplot(2,2,4); + plot(Y, 'b-'); + #set(gca, 'FontSize', 30, 'FontWeight', 'Bold'); + axis([0, fft_size, -50, 1]); + #title('Frequency Domain of Unequalized Rx', 'FontSize', 36); + #xlabel('Bin Number (FFT Size)', 'FontSize', 32); + #ylabel('Power (dB)', 'FontSize', 32); + + #N = 20*log10(var(abs(x)-1)); + + disp(sprintf('Symbol Number: %d\n', i)) + #disp(sprintf('\tFreq Error: %f\n', anglesh_pn(1+(i-1)*fft_size))) + pause + +end diff --git a/gr-digital/examples/ofdm/receive_path.py b/gr-digital/examples/ofdm/receive_path.py new file mode 100644 index 000000000..e1c7868a0 --- /dev/null +++ b/gr-digital/examples/ofdm/receive_path.py @@ -0,0 +1,105 @@ +# +# Copyright 2005,2006,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 +from gnuradio import eng_notation +from gnuradio import digital + +import copy +import sys + +# ///////////////////////////////////////////////////////////////////////////// +# receive path +# ///////////////////////////////////////////////////////////////////////////// + +class receive_path(gr.hier_block2): + def __init__(self, rx_callback, options): + + gr.hier_block2.__init__(self, "receive_path", + gr.io_signature(1, 1, gr.sizeof_gr_complex), + gr.io_signature(0, 0, 0)) + + + options = copy.copy(options) # make a copy so we can destructively modify + + self._verbose = options.verbose + self._log = options.log + self._rx_callback = rx_callback # this callback is fired when there's a packet available + + # receiver + self.ofdm_rx = digital.ofdm_demod(options, + callback=self._rx_callback) + + # Carrier Sensing Blocks + alpha = 0.001 + thresh = 30 # in dB, will have to adjust + self.probe = gr.probe_avg_mag_sqrd_c(thresh,alpha) + + self.connect(self, self.ofdm_rx) + self.connect(self.ofdm_rx, self.probe) + + # Display some information about the setup + if self._verbose: + self._print_verbage() + + 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 + """ + normal.add_option("-W", "--bandwidth", type="eng_float", + default=500e3, + help="set symbol bandwidth [default=%default]") + normal.add_option("-v", "--verbose", action="store_true", default=False) + 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 + """ + pass diff --git a/gr-digital/examples/ofdm/transmit_path.py b/gr-digital/examples/ofdm/transmit_path.py new file mode 100644 index 000000000..ec357d617 --- /dev/null +++ b/gr-digital/examples/ofdm/transmit_path.py @@ -0,0 +1,100 @@ +# +# Copyright 2005,2006,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 +from gnuradio import eng_notation +from gnuradio import digital + +import copy +import sys + +# ///////////////////////////////////////////////////////////////////////////// +# transmit path +# ///////////////////////////////////////////////////////////////////////////// + +class transmit_path(gr.hier_block2): + def __init__(self, options): + ''' + See below for what options should hold + ''' + + gr.hier_block2.__init__(self, "transmit_path", + gr.io_signature(0, 0, 0), + gr.io_signature(1, 1, gr.sizeof_gr_complex)) + + options = copy.copy(options) # make a copy so we can destructively modify + + self._verbose = options.verbose # turn verbose mode on/off + self._tx_amplitude = options.tx_amplitude # digital amp sent to radio + + self.ofdm_tx = digital.ofdm_mod(options, + msgq_limit=4, + pad_for_usrp=False) + + 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() + + # Create and setup transmit path flow graph + self.connect(self.ofdm_tx, self.amp, self) + + def set_tx_amplitude(self, ampl): + """ + Sets the transmit amplitude sent to the USRP + @param: ampl 0 <= ampl < 1.0. Try 0.10 + """ + 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.ofdm_tx.send_pkt(payload, eof) + + def add_options(normal, expert): + """ + Adds transmitter-specific options to the Options Parser + """ + normal.add_option("", "--tx-amplitude", type="eng_float", + default=0.1, metavar="AMPL", + help="set transmitter digital amplitude: 0 <= AMPL < 1.0 [default=%default]") + normal.add_option("-W", "--bandwidth", type="eng_float", + default=500e3, + help="set symbol bandwidth [default=%default]") + normal.add_option("-v", "--verbose", action="store_true", + default=False) + 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) + diff --git a/gr-digital/examples/ofdm/tunnel.py b/gr-digital/examples/ofdm/tunnel.py new file mode 100755 index 000000000..74f2667fa --- /dev/null +++ b/gr-digital/examples/ofdm/tunnel.py @@ -0,0 +1,267 @@ +#!/usr/bin/env python +# +# Copyright 2005,2006,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 2, 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. +# + + +# ///////////////////////////////////////////////////////////////////////////// +# +# This code sets up up a virtual ethernet interface (typically gr0), +# and relays packets between the interface and the GNU Radio PHY+MAC +# +# What this means in plain language, is that if you've got a couple +# of USRPs on different machines, and if you run this code on those +# machines, you can talk between them using normal TCP/IP networking. +# +# ///////////////////////////////////////////////////////////////////////////// + + +from gnuradio import gr, digital +from gnuradio import eng_notation +from gnuradio.eng_option import eng_option +from optparse import OptionParser + +# from current dir +from receive_path import receive_path +from transmit_path import transmit_path +from uhd_interface import uhd_transmitter +from uhd_interface import uhd_receiver + +import os, sys +import random, time, struct + +#print os.getpid() +#raw_input('Attach and press enter') + + +# ///////////////////////////////////////////////////////////////////////////// +# +# Use the Universal TUN/TAP device driver to move packets to/from kernel +# +# See /usr/src/linux/Documentation/networking/tuntap.txt +# +# ///////////////////////////////////////////////////////////////////////////// + +# Linux specific... +# TUNSETIFF ifr flags from <linux/tun_if.h> + +IFF_TUN = 0x0001 # tunnel IP packets +IFF_TAP = 0x0002 # tunnel ethernet frames +IFF_NO_PI = 0x1000 # don't pass extra packet info +IFF_ONE_QUEUE = 0x2000 # beats me ;) + +def open_tun_interface(tun_device_filename): + from fcntl import ioctl + + mode = IFF_TAP | IFF_NO_PI + TUNSETIFF = 0x400454ca + + tun = os.open(tun_device_filename, os.O_RDWR) + ifs = ioctl(tun, TUNSETIFF, struct.pack("16sH", "gr%d", mode)) + ifname = ifs[:16].strip("\x00") + return (tun, ifname) + + +# ///////////////////////////////////////////////////////////////////////////// +# the flow graph +# ///////////////////////////////////////////////////////////////////////////// + +class my_top_block(gr.top_block): + def __init__(self, callback, options): + gr.top_block.__init__(self) + + self.source = uhd_receiver(options.address, + options.bandwidth, + options.rx_freq, options.rx_gain, + options.antenna, options.verbose) + + self.sink = uhd_transmitter(options.address, + options.bandwidth, + options.tx_freq, options.tx_gain, + options.antenna, options.verbose) + + self.txpath = transmit_path(options) + self.rxpath = receive_path(callback, options) + + self.connect(self.txpath, self.sink) + self.connect(self.source, self.rxpath) + + def carrier_sensed(self): + """ + Return True if the receive path thinks there's carrier + """ + return self.rxpath.carrier_sensed() + + def set_freq(self, target_freq): + """ + Set the center frequency we're interested in. + """ + self.u_snk.set_freq(target_freq) + self.u_src.set_freq(target_freq) + + +# ///////////////////////////////////////////////////////////////////////////// +# Carrier Sense MAC +# ///////////////////////////////////////////////////////////////////////////// + +class cs_mac(object): + """ + Prototype carrier sense MAC + + Reads packets from the TUN/TAP interface, and sends them to the PHY. + Receives packets from the PHY via phy_rx_callback, and sends them + into the TUN/TAP interface. + + Of course, we're not restricted to getting packets via TUN/TAP, this + is just an example. + """ + def __init__(self, tun_fd, verbose=False): + self.tun_fd = tun_fd # file descriptor for TUN/TAP interface + self.verbose = verbose + self.tb = None # top block (access to PHY) + + def set_flow_graph(self, tb): + self.tb = tb + + def phy_rx_callback(self, ok, payload): + """ + Invoked by thread associated with PHY to pass received packet up. + + @param ok: bool indicating whether payload CRC was OK + @param payload: contents of the packet (string) + """ + if self.verbose: + print "Rx: ok = %r len(payload) = %4d" % (ok, len(payload)) + if ok: + os.write(self.tun_fd, payload) + + def main_loop(self): + """ + Main loop for MAC. + Only returns if we get an error reading from TUN. + + FIXME: may want to check for EINTR and EAGAIN and reissue read + """ + min_delay = 0.001 # seconds + + while 1: + payload = os.read(self.tun_fd, 10*1024) + if not payload: + self.tb.txpath.send_pkt(eof=True) + break + + if self.verbose: + print "Tx: len(payload) = %4d" % (len(payload),) + + delay = min_delay + while self.tb.carrier_sensed(): + sys.stderr.write('B') + time.sleep(delay) + if delay < 0.050: + delay = delay * 2 # exponential back-off + + self.tb.txpath.send_pkt(payload) + + +# ///////////////////////////////////////////////////////////////////////////// +# main +# ///////////////////////////////////////////////////////////////////////////// + +def main(): + + parser = OptionParser (option_class=eng_option, conflict_handler="resolve") + expert_grp = parser.add_option_group("Expert") + + parser.add_option("-m", "--modulation", type="choice", choices=['bpsk', 'qpsk'], + default='bpsk', + help="Select modulation from: bpsk, qpsk [default=%%default]") + + parser.add_option("-v","--verbose", action="store_true", default=False) + expert_grp.add_option("-c", "--carrier-threshold", type="eng_float", default=30, + help="set carrier detect threshold (dB) [default=%default]") + expert_grp.add_option("","--tun-device-filename", default="/dev/net/tun", + help="path to tun device file [default=%default]") + + digital.ofdm_mod.add_options(parser, expert_grp) + digital.ofdm_demod.add_options(parser, expert_grp) + transmit_path.add_options(parser, expert_grp) + receive_path.add_options(parser, expert_grp) + uhd_receiver.add_options(parser) + uhd_transmitter.add_options(parser) + + (options, args) = parser.parse_args () + if len(args) != 0: + parser.print_help(sys.stderr) + sys.exit(1) + + if options.rx_freq is None or 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) + + # open the TUN/TAP interface + (tun_fd, tun_ifname) = open_tun_interface(options.tun_device_filename) + + # Attempt to enable realtime scheduling + r = gr.enable_realtime_scheduling() + if r == gr.RT_OK: + realtime = True + else: + realtime = False + print "Note: failed to enable realtime scheduling" + + # instantiate the MAC + mac = cs_mac(tun_fd, verbose=True) + + + # build the graph (PHY) + tb = my_top_block(mac.phy_rx_callback, options) + + mac.set_flow_graph(tb) # give the MAC a handle for the PHY + + print "modulation: %s" % (options.modulation,) + print "freq: %s" % (eng_notation.num_to_str(options.tx_freq)) + + tb.rxpath.set_carrier_threshold(options.carrier_threshold) + print "Carrier sense threshold:", options.carrier_threshold, "dB" + + print + print "Allocated virtual ethernet interface: %s" % (tun_ifname,) + print "You must now use ifconfig to set its IP address. E.g.," + print + print " $ sudo ifconfig %s 192.168.200.1" % (tun_ifname,) + print + print "Be sure to use a different address in the same subnet for each machine." + print + + + tb.start() # Start executing the flow graph (runs in separate threads) + + mac.main_loop() # don't expect this to return... + + tb.stop() # but if it does, tell flow graph to stop. + tb.wait() # wait for it to finish + + +if __name__ == '__main__': + try: + main() + except KeyboardInterrupt: + pass diff --git a/gr-digital/examples/ofdm/uhd_interface.py b/gr-digital/examples/ofdm/uhd_interface.py new file mode 100644 index 000000000..d03bd7f46 --- /dev/null +++ b/gr-digital/examples/ofdm/uhd_interface.py @@ -0,0 +1,199 @@ +#!/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, uhd +from gnuradio import eng_notation +from gnuradio.eng_option import eng_option +from optparse import OptionParser + +import sys + +def add_freq_option(parser): + """ + Hackery that has the -f / --freq option set both tx_freq and rx_freq + """ + def freq_callback(option, opt_str, value, parser): + parser.values.rx_freq = value + parser.values.tx_freq = value + + if not parser.has_option('--freq'): + parser.add_option('-f', '--freq', type="eng_float", + action="callback", callback=freq_callback, + help="set Tx and/or Rx frequency to FREQ [default=%default]", + metavar="FREQ") + +class uhd_interface: + def __init__(self, istx, address, bandwidth, freq=None, + gain=None, antenna=None): + + if(istx): + self.u = uhd.usrp_sink(device_addr=address, + io_type=uhd.io_type.COMPLEX_FLOAT32, + num_channels=1) + else: + self.u = uhd.usrp_source(device_addr=address, + io_type=uhd.io_type.COMPLEX_FLOAT32, + num_channels=1) + + self._addr = address + self._ant = antenna + self._gain = self.set_gain(gain) + self._freq = self.set_freq(freq) + + self._rate = self.set_sample_rate(bandwidth) + + if(antenna): + self.u.set_antenna(antenna, 0) + + def set_sample_rate(self, bandwidth): + self.u.set_samp_rate(bandwidth) + actual_bw = self.u.get_samp_rate() + + return actual_bw + + def get_sample_rate(self): + return self.u.get_samp_rate() + + def set_gain(self, gain=None): + if gain is None: + # if no gain was specified, use the mid-point in dB + g = self.u.get_gain_range() + gain = float(g.start()+g.stop())/2 + print "\nNo gain specified." + print "Setting gain to %f (from [%f, %f])" % \ + (gain, g.start(), g.stop()) + + self.u.set_gain(gain, 0) + return gain + + def set_freq(self, freq=None): + if(freq is None): + sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") + sys.exit(1) + + r = self.u.set_center_freq(freq, 0) + if r: + return freq + else: + frange = self.u.get_freq_range() + sys.stderr.write(("\nRequested frequency (%f) out or range [%f, %f]\n") % \ + (freq, frange.start(), frange.stop())) + sys.exit(1) + +#-------------------------------------------------------------------# +# TRANSMITTER +#-------------------------------------------------------------------# + +class uhd_transmitter(uhd_interface, gr.hier_block2): + def __init__(self, address, bandwidth, freq=None, gain=None, + antenna=None, verbose=False): + gr.hier_block2.__init__(self, "uhd_transmitter", + gr.io_signature(1,1,gr.sizeof_gr_complex), + gr.io_signature(0,0,0)) + + # Set up the UHD interface as a transmitter + uhd_interface.__init__(self, True, address, bandwidth, + freq, gain, antenna) + + self.connect(self, self.u) + + if(verbose): + self._print_verbage() + + def add_options(parser): + add_freq_option(parser) + parser.add_option("-a", "--address", type="string", default="addr=192.168.10.2", + help="Address of UHD device, [default=%default]") + parser.add_option("-A", "--antenna", type="string", default=None, + help="select Rx Antenna where appropriate") + parser.add_option("", "--tx-freq", type="eng_float", default=None, + help="set transmit frequency to FREQ [default=%default]", + metavar="FREQ") + parser.add_option("", "--tx-gain", type="eng_float", default=None, + help="set transmit gain in dB (default is midpoint)") + parser.add_option("-v", "--verbose", action="store_true", default=False) + + # Make a static method to call before instantiation + add_options = staticmethod(add_options) + + def _print_verbage(self): + """ + Prints information about the UHD transmitter + """ + print "\nUHD Transmitter:" + print "Address: %s" % (self._addr) + print "Freq: %sHz" % (eng_notation.num_to_str(self._freq)) + print "Gain: %f dB" % (self._gain) + print "Sample Rate: %ssps" % (eng_notation.num_to_str(self._rate)) + print "Antenna: %s" % (self._ant) + + + +#-------------------------------------------------------------------# +# RECEIVER +#-------------------------------------------------------------------# + + +class uhd_receiver(uhd_interface, gr.hier_block2): + def __init__(self, address, bandwidth, freq=None, gain=None, + antenna=None, verbose=False): + gr.hier_block2.__init__(self, "uhd_receiver", + gr.io_signature(0,0,0), + gr.io_signature(1,1,gr.sizeof_gr_complex)) + + # Set up the UHD interface as a receiver + uhd_interface.__init__(self, False, address, bandwidth, + freq, gain, antenna) + + self.connect(self.u, self) + + if(verbose): + self._print_verbage() + + def add_options(parser): + add_freq_option(parser) + parser.add_option("-a", "--address", type="string", default="addr=192.168.10.2", + help="Address of UHD device, [default=%default]") + parser.add_option("-A", "--antenna", type="string", default=None, + help="select Rx Antenna where appropriate") + parser.add_option("", "--rx-freq", type="eng_float", default=None, + help="set receive frequency to FREQ [default=%default]", + metavar="FREQ") + parser.add_option("", "--rx-gain", type="eng_float", default=None, + help="set receive gain in dB (default is midpoint)") + if not parser.has_option("--verbose"): + parser.add_option("-v", "--verbose", action="store_true", default=False) + + # Make a static method to call before instantiation + add_options = staticmethod(add_options) + + def _print_verbage(self): + """ + Prints information about the UHD transmitter + """ + print "\nUHD Receiver:" + print "Address: %s" % (self._addr) + print "Freq: %sHz" % (eng_notation.num_to_str(self._freq)) + print "Gain: %f dB" % (self._gain) + print "Sample Rate: %ssps" % (eng_notation.num_to_str(self._rate)) + print "Antenna: %s" % (self._ant) + diff --git a/gr-digital/grc/Makefile.am b/gr-digital/grc/Makefile.am index e4426e721..ef4e38600 100644 --- a/gr-digital/grc/Makefile.am +++ b/gr-digital/grc/Makefile.am @@ -40,4 +40,11 @@ dist_grcblocks_DATA = \ digital_psk_mod.xml \ digital_psk_demod.xml \ digital_qam_mod.xml \ - digital_qam_demod.xml + digital_qam_demod.xml \ + digital_ofdm_mod.xml \ + digital_ofdm_demod.xml \ + digital_ofdm_cyclic_prefixer.xml \ + digital_ofdm_frame_acquisition.xml \ + digital_ofdm_insert_preamble.xml \ + digital_ofdm_sampler.xml \ + digital_ofdm_sync_pn.xml diff --git a/gr-digital/grc/digital_block_tree.xml b/gr-digital/grc/digital_block_tree.xml index 088fe1c10..816df7f45 100644 --- a/gr-digital/grc/digital_block_tree.xml +++ b/gr-digital/grc/digital_block_tree.xml @@ -50,4 +50,14 @@ <block>digital_qam_mod</block> <block>digital_qam_demod</block> </cat> + <cat> + <name>OFDM</name> + <block>digital_ofdm_mod</block> + <block>digital_ofdm_demod</block> + <block>digital_ofdm_cyclic_prefixer</block> + <block>digital_ofdm_frame_acquisition</block> + <block>digital_ofdm_insert_preamble</block> + <block>digital_ofdm_sampler</block> + <block>digital_ofdm_sync_pn</block> + </cat> </cat> diff --git a/gr-digital/grc/digital_ofdm_cyclic_prefixer.xml b/gr-digital/grc/digital_ofdm_cyclic_prefixer.xml new file mode 100644 index 000000000..d5e5d3894 --- /dev/null +++ b/gr-digital/grc/digital_ofdm_cyclic_prefixer.xml @@ -0,0 +1,52 @@ +<?xml version="1.0"?> +<!-- + 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. +--> + +<!-- +################################################### +##OFDM Cyclic Prefixer +################################################### + --> +<block> + <name>OFDM Cyclic Prefixer</name> + <key>digital_ofdm_cyclic_prefixer</key> + <import>from gnuradio import digital</import> + <make>digital.ofdm_cyclic_prefixer($input_size, $output_size)</make> + <param> + <name>Input Size</name> + <key>input_size</key> + <type>int</type> + </param> + <param> + <name>Output Size</name> + <key>output_size</key> + <type>int</type> + </param> + <sink> + <name>in</name> + <type>complex</type> + <vlen>$input_size</vlen> + </sink> + <source> + <name>out</name> + <type>complex</type> + </source> +</block> diff --git a/gr-digital/grc/digital_ofdm_demod.xml b/gr-digital/grc/digital_ofdm_demod.xml new file mode 100644 index 000000000..9f3a83715 --- /dev/null +++ b/gr-digital/grc/digital_ofdm_demod.xml @@ -0,0 +1,143 @@ +<?xml version="1.0"?> +<!-- + 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. +--> + +<!-- +################################################### +##OFDM Demod +################################################### + --> +<block> + <name>OFDM Demod</name> + <key>digital_ofdm_demod</key> + <import>from grc_gnuradio import blks2 as grc_blks2</import> + <import>from gnuradio import digital</import> + <make>grc_blks2.packet_demod_$(type.fcn)(digital.ofdm_demod( + options=grc_blks2.options( + modulation="$modulation", + fft_length=$fft_length, + occupied_tones=$occupied_tones, + cp_length=$cp_length, + snr=$snr, + log=None, + verbose=None, + ), + callback=lambda ok, payload: self.$(id).recv_pkt(ok, payload), + ), +)</make> + <param> + <name>Output Type</name> + <key>type</key> + <value>float</value> + <type>enum</type> + <option> + <name>Complex</name> + <key>complex</key> + <opt>fcn:c</opt> + </option> + <option> + <name>Float</name> + <key>float</key> + <opt>fcn:f</opt> + </option> + <option> + <name>Int</name> + <key>int</key> + <opt>fcn:i</opt> + </option> + <option> + <name>Short</name> + <key>short</key> + <opt>fcn:s</opt> + </option> + <option> + <name>Byte</name> + <key>byte</key> + <opt>fcn:b</opt> + </option> + </param> + <param> + <name>Modulation</name> + <key>modulation</key> + <type>enum</type> + <option> + <name>BPSK</name> + <key>bpsk</key> + </option> + <option> + <name>QPSK</name> + <key>qpsk</key> + </option> + <option> + <name>8PSK</name> + <key>8psk</key> + </option> + <option> + <name>QAM8</name> + <key>qam8</key> + </option> + <option> + <name>QAM16</name> + <key>qam16</key> + </option> + <option> + <name>QAM64</name> + <key>qam64</key> + </option> + <option> + <name>QAM256</name> + <key>qam256</key> + </option> + </param> + <param> + <name>FFT Length</name> + <key>fft_length</key> + <value>512</value> + <type>int</type> + </param> + <param> + <name>Occupied Tones</name> + <key>occupied_tones</key> + <value>200</value> + <type>int</type> + </param> + <param> + <name>Cyclic Prefix Length</name> + <key>cp_length</key> + <value>128</value> + <type>int</type> + </param> + <param> + <name>SNR</name> + <key>snr</key> + <value>10</value> + <type>real</type> + </param> + <sink> + <name>in</name> + <type>complex</type> + </sink> + <source> + <name>out</name> + <type>$type</type> + </source> + <doc>Payload Length: 0 for automatic.</doc> +</block> diff --git a/gr-digital/grc/digital_ofdm_frame_acquisition.xml b/gr-digital/grc/digital_ofdm_frame_acquisition.xml new file mode 100644 index 000000000..2545f18e6 --- /dev/null +++ b/gr-digital/grc/digital_ofdm_frame_acquisition.xml @@ -0,0 +1,77 @@ +<?xml version="1.0"?> +<!-- + 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. +--> + +<!-- +################################################### +##OFDM Frame Acquisition +################################################### + --> +<block> + <name>OFDM Frame Acquisition</name> + <key>digital_ofdm_frame_acquisition</key> + <import>from gnuradio import digital</import> + <make>digital.ofdm_frame_acquisition($occupied_carriers, $fft_length, $cplen, $known_symbol, $max_fft_shift_len)</make> + <param> + <name>Occupied Carriers</name> + <key>occupied_carriers</key> + <type>int</type> + </param> + <param> + <name>FFT Length</name> + <key>fft_length</key> + <type>int</type> + </param> + <param> + <name>CP Length</name> + <key>cplen</key> + <type>int</type> + </param> + <param> + <name>Preamble</name> + <key>known_symbol</key> + <type>complex_vector</type> + </param> + <param> + <name>Max FFT Shift</name> + <key>max_fft_shift_len</key> + <type>int</type> + </param> + <sink> + <name>in</name> + <type>complex</type> + <vlen>$fft_length</vlen> + </sink> + <sink> + <name>flag</name> + <type>byte</type> + <vlen>$fft_length</vlen> + </sink> + <source> + <name>out</name> + <type>complex</type> + <vlen>$occupied_carriers</vlen> + </source> + <source> + <name>flag</name> + <type>byte</type> + </source> +</block> diff --git a/gr-digital/grc/digital_ofdm_insert_preamble.xml b/gr-digital/grc/digital_ofdm_insert_preamble.xml new file mode 100644 index 000000000..33a93058f --- /dev/null +++ b/gr-digital/grc/digital_ofdm_insert_preamble.xml @@ -0,0 +1,57 @@ +<?xml version="1.0"?> +<!-- + 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. +--> + +<!-- +################################################### +##OFDM Insert Preamble +################################################### + --> +<block> + <name>OFDM Insert Preamble</name> + <key>digital_ofdm_insert_preamble</key> + <import>from gnuradio import digital</import> + <make>digital.ofdm_insert_preamble($fft_length, $preamble)</make> + <param> + <name>FFT Length</name> + <key>fft_length</key> + <type>int</type> + </param> + <param> + <name>Preamble</name> + <key>preamble</key> + <type>raw</type> + </param> + <sink> + <name>in</name> + <type>complex</type> + <vlen>$fft_length</vlen> + </sink> + <sink> + <name>flag</name> + <type>byte</type> + </sink> + <source> + <name>out</name> + <type>complex</type> + <vlen>$fft_length</vlen> + </source> +</block> diff --git a/gr-digital/grc/digital_ofdm_mod.xml b/gr-digital/grc/digital_ofdm_mod.xml new file mode 100644 index 000000000..24cb2aa33 --- /dev/null +++ b/gr-digital/grc/digital_ofdm_mod.xml @@ -0,0 +1,156 @@ +<?xml version="1.0"?> +<!-- + 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. +--> + +<!-- +################################################### +##OFDM Mod +################################################### + --> +<block> + <name>OFDM Mod</name> + <key>digital_ofdm_mod</key> + <import>from grc_gnuradio import blks2 as grc_blks2</import> + <import>from gnuradio import digital</import> + <make>grc_blks2.packet_mod_$(type.fcn)(digital.ofdm_mod( + options=grc_blks2.options( + modulation="$modulation", + fft_length=$fft_length, + occupied_tones=$occupied_tones, + cp_length=$cp_length, + pad_for_usrp=$pad_for_usrp, + log=None, + verbose=None, + ), + ), + payload_length=$payload_length, +)</make> + <param> + <name>Input Type</name> + <key>type</key> + <value>float</value> + <type>enum</type> + <option> + <name>Complex</name> + <key>complex</key> + <opt>fcn:c</opt> + </option> + <option> + <name>Float</name> + <key>float</key> + <opt>fcn:f</opt> + </option> + <option> + <name>Int</name> + <key>int</key> + <opt>fcn:i</opt> + </option> + <option> + <name>Short</name> + <key>short</key> + <opt>fcn:s</opt> + </option> + <option> + <name>Byte</name> + <key>byte</key> + <opt>fcn:b</opt> + </option> + </param> + <param> + <name>Modulation</name> + <key>modulation</key> + <type>enum</type> + <option> + <name>BPSK</name> + <key>bpsk</key> + </option> + <option> + <name>QPSK</name> + <key>qpsk</key> + </option> + <option> + <name>8PSK</name> + <key>8psk</key> + </option> + <option> + <name>QAM8</name> + <key>qam8</key> + </option> + <option> + <name>QAM16</name> + <key>qam16</key> + </option> + <option> + <name>QAM64</name> + <key>qam64</key> + </option> + <option> + <name>QAM256</name> + <key>qam256</key> + </option> + </param> + <param> + <name>FFT Length</name> + <key>fft_length</key> + <value>512</value> + <type>int</type> + </param> + <param> + <name>Occupied Tones</name> + <key>occupied_tones</key> + <value>200</value> + <type>int</type> + </param> + <param> + <name>Cyclic Prefix Length</name> + <key>cp_length</key> + <value>128</value> + <type>int</type> + </param> + <param> + <name>Pad for USRP</name> + <key>pad_for_usrp</key> + <type>enum</type> + <option> + <name>Yes</name> + <key>True</key> + </option> + <option> + <name>No</name> + <key>False</key> + </option> + </param> + <param> + <name>Payload Length</name> + <key>payload_length</key> + <value>0</value> + <type>int</type> + </param> + <sink> + <name>in</name> + <type>$type</type> + </sink> + <source> + <name>out</name> + <type>complex</type> + </source> + <doc>Payload Length: 0 for automatic.</doc> +</block> diff --git a/gr-digital/grc/digital_ofdm_sampler.xml b/gr-digital/grc/digital_ofdm_sampler.xml new file mode 100644 index 000000000..1be0d11f3 --- /dev/null +++ b/gr-digital/grc/digital_ofdm_sampler.xml @@ -0,0 +1,67 @@ +<?xml version="1.0"?> +<!-- + 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. +--> + +<!-- +################################################### +##OFDM Sampler +################################################### + --> +<block> + <name>OFDM Sampler</name> + <key>digital_ofdm_sampler</key> + <import>from gnuradio import digital</import> + <make>digital.ofdm_sampler($fft_length, $symbol_length, $timeout)</make> + <param> + <name>FFT Length</name> + <key>fft_length</key> + <type>int</type> + </param> + <param> + <name>Symbol Length</name> + <key>symbol_length</key> + <type>int</type> + </param> + <param> + <name>Timeout</name> + <key>timeout</key> + <type>int</type> + <value>1000</value> + </param> + <sink> + <name>in</name> + <type>complex</type> + </sink> + <sink> + <name>flag</name> + <type>byte</type> + </sink> + <source> + <name>out</name> + <type>complex</type> + <vlen>$fft_length</vlen> + </source> + <source> + <name>flag</name> + <type>byte</type> + <vlen>$fft_length</vlen> + </source> +</block> diff --git a/gr-digital/grc/digital_ofdm_sync_pn.xml b/gr-digital/grc/digital_ofdm_sync_pn.xml new file mode 100644 index 000000000..ac0bbfe88 --- /dev/null +++ b/gr-digital/grc/digital_ofdm_sync_pn.xml @@ -0,0 +1,61 @@ +<?xml version="1.0"?> +<!-- + 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. +--> + +<!-- +################################################### +##OFDM Synchronizer - PN based +################################################### + --> +<block> + <name>OFDM Sync PN</name> + <key>digital_ofdm_sync_pn</key> + <import>from gnuradio import digital</import> + <make>digital.ofdm_sync_pn($fft_length, $cp_length, $logging)</make> + <param> + <name>FFT Length</name> + <key>fft_length</key> + <type>int</type> + </param> + <param> + <name>CP Length</name> + <key>cp_length</key> + <type>int</type> + </param> + <param> + <name>Logging</name> + <key>logging</key> + <type>bool</type> + <value>False</value> + </param> + <sink> + <name>in</name> + <type>complex</type> + </sink> + <source> + <name>fine freq</name> + <type>float</type> + </source> + <source> + <name>timing sig</name> + <type>byte</type> + </source> +</block> diff --git a/gr-digital/grc/digital_psk_mod.xml b/gr-digital/grc/digital_psk_mod.xml index 34ed42c97..cafcf4e50 100644 --- a/gr-digital/grc/digital_psk_mod.xml +++ b/gr-digital/grc/digital_psk_mod.xml @@ -30,7 +30,7 @@ <name>PSK Mod</name> <key>digital_psk_mod</key> <import>from gnuradio import digital</import> - <make>digital.psk2.psk_mod( + <make>digital.psk.psk_mod( constellation_points=$constellation_points, mod_code=$mod_code, differential=$differential, diff --git a/gr-digital/include/CMakeLists.txt b/gr-digital/include/CMakeLists.txt index f5d4d76b6..cf20bd1e7 100644 --- a/gr-digital/include/CMakeLists.txt +++ b/gr-digital/include/CMakeLists.txt @@ -37,6 +37,12 @@ install(FILES digital_kurtotic_equalizer_cc.h digital_metric_type.h digital_mpsk_receiver_cc.h + digital_ofdm_cyclic_prefixer.h + digital_ofdm_frame_acquisition.h + digital_ofdm_frame_sink.h + digital_ofdm_insert_preamble.h + digital_ofdm_mapper_bcv.h + digital_ofdm_sampler.h digital_gmskmod_bc.h digital_cpmmod_bc.h DESTINATION ${GR_INCLUDE_DIR}/gnuradio diff --git a/gr-digital/include/Makefile.am b/gr-digital/include/Makefile.am index 346d036ce..8ce3a94e8 100644 --- a/gr-digital/include/Makefile.am +++ b/gr-digital/include/Makefile.am @@ -39,6 +39,12 @@ grinclude_HEADERS = \ digital_kurtotic_equalizer_cc.h \ digital_metric_type.h \ digital_mpsk_receiver_cc.h \ + digital_ofdm_cyclic_prefixer.h \ + digital_ofdm_frame_acquisition.h \ + digital_ofdm_frame_sink.h \ + digital_ofdm_insert_preamble.h \ + digital_ofdm_mapper_bcv.h \ + digital_ofdm_sampler.h \ digital_gmskmod_bc.h \ digital_cpmmod_bc.h diff --git a/gr-digital/include/digital_clock_recovery_mm_cc.h b/gr-digital/include/digital_clock_recovery_mm_cc.h index f8d973765..a2577d537 100644 --- a/gr-digital/include/digital_clock_recovery_mm_cc.h +++ b/gr-digital/include/digital_clock_recovery_mm_cc.h @@ -44,12 +44,15 @@ digital_make_clock_recovery_mm_cc (float omega, float gain_omega, * \ingroup sync_blk * \ingroup digital * - * This implements the Mueller and Müller (M&M) discrete-time error-tracking synchronizer. + * This implements the Mueller and Müller (M&M) discrete-time + * error-tracking synchronizer. + * * The complex version here is based on: * Modified Mueller and Muller clock recovery circuit * Based: - * G. R. Danesfahani, T.G. Jeans, "Optimisation of modified Mueller and Muller - * algorithm," Electronics Letters, Vol. 31, no. 13, 22 June 1995, pp. 1032 - 1033. + * G. R. Danesfahani, T.G. Jeans, "Optimisation of modified Mueller + * and Muller algorithm," Electronics Letters, Vol. 31, no. 13, 22 + * June 1995, pp. 1032 - 1033. */ class DIGITAL_API digital_clock_recovery_mm_cc : public gr_block { diff --git a/gr-digital/include/digital_ofdm_cyclic_prefixer.h b/gr-digital/include/digital_ofdm_cyclic_prefixer.h new file mode 100644 index 000000000..1b9682bb3 --- /dev/null +++ b/gr-digital/include/digital_ofdm_cyclic_prefixer.h @@ -0,0 +1,59 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004-2006,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. + */ + +#ifndef INCLUDED_DIGITAL_OFDM_CYCLIC_PREFIXER_H +#define INCLUDED_DIGITAL_OFDM_CYCLIC_PREFIXER_H + +#include <digital_api.h> +#include <gr_sync_interpolator.h> +#include <stdio.h> + +class digital_ofdm_cyclic_prefixer; +typedef boost::shared_ptr<digital_ofdm_cyclic_prefixer> digital_ofdm_cyclic_prefixer_sptr; + +DIGITAL_API digital_ofdm_cyclic_prefixer_sptr +digital_make_ofdm_cyclic_prefixer (size_t input_size, size_t output_size); + + +/*! + * \brief adds a cyclic prefix vector to an input size long ofdm + * symbol(vector) and converts vector to a stream output_size long. + * \ingroup ofdm_blk + */ +class DIGITAL_API digital_ofdm_cyclic_prefixer : public gr_sync_interpolator +{ + friend DIGITAL_API digital_ofdm_cyclic_prefixer_sptr + digital_make_ofdm_cyclic_prefixer (size_t input_size, size_t output_size); + + protected: + digital_ofdm_cyclic_prefixer (size_t input_size, size_t output_size); + + public: + int work (int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + private: + size_t d_input_size; + size_t d_output_size; +}; + +#endif /* INCLUDED_DIGITAL_OFDM_CYCLIC_PREFIXER_H */ diff --git a/gr-digital/include/digital_ofdm_frame_acquisition.h b/gr-digital/include/digital_ofdm_frame_acquisition.h new file mode 100644 index 000000000..9c2f60233 --- /dev/null +++ b/gr-digital/include/digital_ofdm_frame_acquisition.h @@ -0,0 +1,116 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,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. + */ + +#ifndef INCLUDED_DIGITAL_OFDM_FRAME_ACQUISITION_H +#define INCLUDED_DIGITAL_OFDM_FRAME_ACQUISITION_H + +#include <digital_api.h> +#include <gr_block.h> +#include <vector> + +class digital_ofdm_frame_acquisition; +typedef boost::shared_ptr<digital_ofdm_frame_acquisition> digital_ofdm_frame_acquisition_sptr; + +digital_ofdm_frame_acquisition_sptr +DIGITAL_API digital_make_ofdm_frame_acquisition (unsigned int occupied_carriers, unsigned int fft_length, + unsigned int cplen, + const std::vector<gr_complex> &known_symbol, + unsigned int max_fft_shift_len=10); + +/*! + * \brief take a vector of complex constellation points in from an FFT + * and performs a correlation and equalization. + * \ingroup demodulation_blk + * \ingroup ofdm_blk + * + * This block takes the output of an FFT of a received OFDM symbol and finds the + * start of a frame based on two known symbols. It also looks at the surrounding + * bins in the FFT output for the correlation in case there is a large frequency + * shift in the data. This block assumes that the fine frequency shift has already + * been corrected and that the samples fall in the middle of one FFT bin. + * + * It then uses one of those known + * symbols to estimate the channel response over all subcarriers and does a simple + * 1-tap equalization on all subcarriers. This corrects for the phase and amplitude + * distortion caused by the channel. + */ + +class DIGITAL_API digital_ofdm_frame_acquisition : public gr_block +{ + /*! + * \brief Build an OFDM correlator and equalizer. + * \param occupied_carriers The number of subcarriers with data in the received symbol + * \param fft_length The size of the FFT vector (occupied_carriers + unused carriers) + * \param cplen The length of the cycle prefix + * \param known_symbol A vector of complex numbers representing a known symbol at the + * start of a frame (usually a BPSK PN sequence) + * \param max_fft_shift_len Set's the maximum distance you can look between bins for correlation + */ + friend DIGITAL_API digital_ofdm_frame_acquisition_sptr + digital_make_ofdm_frame_acquisition (unsigned int occupied_carriers, unsigned int fft_length, + unsigned int cplen, + const std::vector<gr_complex> &known_symbol, + unsigned int max_fft_shift_len); + +protected: + digital_ofdm_frame_acquisition (unsigned int occupied_carriers, unsigned int fft_length, + unsigned int cplen, + const std::vector<gr_complex> &known_symbol, + unsigned int max_fft_shift_len); + + private: + unsigned char slicer(gr_complex x); + void correlate(const gr_complex *symbol, int zeros_on_left); + void calculate_equalizer(const gr_complex *symbol, int zeros_on_left); + gr_complex coarse_freq_comp(int freq_delta, int count); + + unsigned int d_occupied_carriers; // !< \brief number of subcarriers with data + unsigned int d_fft_length; // !< \brief length of FFT vector + unsigned int d_cplen; // !< \brief length of cyclic prefix in samples + unsigned int d_freq_shift_len; // !< \brief number of surrounding bins to look at for correlation + std::vector<gr_complex> d_known_symbol; // !< \brief known symbols at start of frame + std::vector<float> d_known_phase_diff; // !< \brief factor used in correlation from known symbol + std::vector<float> d_symbol_phase_diff; // !< \brief factor used in correlation from received symbol + std::vector<gr_complex> d_hestimate; // !< channel estimate + int d_coarse_freq; // !< \brief search distance in number of bins + unsigned int d_phase_count; // !< \brief accumulator for coarse freq correction + float d_snr_est; // !< an estimation of the signal to noise ratio + + gr_complex *d_phase_lut; // !< look-up table for coarse frequency compensation + + void forecast(int noutput_items, gr_vector_int &ninput_items_required); + + public: + /*! + * \brief Return an estimate of the SNR of the channel + */ + float snr() { return d_snr_est; } + + ~digital_ofdm_frame_acquisition(void); + int general_work(int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); +}; + + +#endif diff --git a/gr-digital/include/digital_ofdm_frame_sink.h b/gr-digital/include/digital_ofdm_frame_sink.h new file mode 100644 index 000000000..5785d4be7 --- /dev/null +++ b/gr-digital/include/digital_ofdm_frame_sink.h @@ -0,0 +1,127 @@ +/* -*- c++ -*- */ +/* + * Copyright 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. + */ + +#ifndef INCLUDED_DIGITAL_OFDM_FRAME_SINK_H +#define INCLUDED_DIGITAL_OFDM_FRAME_SINK_H + +#include <digital_api.h> +#include <gr_sync_block.h> +#include <gr_msg_queue.h> + +class digital_ofdm_frame_sink; +typedef boost::shared_ptr<digital_ofdm_frame_sink> digital_ofdm_frame_sink_sptr; + +DIGITAL_API digital_ofdm_frame_sink_sptr +digital_make_ofdm_frame_sink (const std::vector<gr_complex> &sym_position, + const std::vector<unsigned char> &sym_value_out, + gr_msg_queue_sptr target_queue, unsigned int occupied_tones, + float phase_gain=0.25, float freq_gain=0.25*0.25/4.0); + +/*! + * \brief Takes an OFDM symbol in, demaps it into bits of 0's and 1's, packs + * them into packets, and sends to to a message queue sink. + * \ingroup sink_blk + * \ingroup ofdm_blk + * + * NOTE: The mod input parameter simply chooses a pre-defined demapper/slicer. Eventually, + * we want to be able to pass in a reference to an object to do the demapping and slicing + * for a given modulation type. + */ +class DIGITAL_API digital_ofdm_frame_sink : public gr_sync_block +{ + friend DIGITAL_API digital_ofdm_frame_sink_sptr + digital_make_ofdm_frame_sink (const std::vector<gr_complex> &sym_position, + const std::vector<unsigned char> &sym_value_out, + gr_msg_queue_sptr target_queue, unsigned int occupied_tones, + float phase_gain, float freq_gain); + + private: + enum state_t {STATE_SYNC_SEARCH, STATE_HAVE_SYNC, STATE_HAVE_HEADER}; + + static const int MAX_PKT_LEN = 4096; + static const int HEADERBYTELEN = 4; + + gr_msg_queue_sptr d_target_queue; // where to send the packet when received + state_t d_state; + unsigned int d_header; // header bits + int d_headerbytelen_cnt; // how many so far + + unsigned char *d_bytes_out; // hold the current bytes produced by the demapper + + unsigned int d_occupied_carriers; + unsigned int d_byte_offset; + unsigned int d_partial_byte; + + unsigned char d_packet[MAX_PKT_LEN]; // assembled payload + int d_packetlen; // length of packet + int d_packet_whitener_offset; // offset into whitener string to use + int d_packetlen_cnt; // how many so far + + gr_complex * d_derotated_output; // Pointer to output stream to send deroated symbols out + + std::vector<gr_complex> d_sym_position; + std::vector<unsigned char> d_sym_value_out; + std::vector<gr_complex> d_dfe; + unsigned int d_nbits; + + unsigned char d_resid; + unsigned int d_nresid; + float d_phase; + float d_freq; + float d_phase_gain; + float d_freq_gain; + float d_eq_gain; + + std::vector<int> d_subcarrier_map; + + protected: + digital_ofdm_frame_sink(const std::vector<gr_complex> &sym_position, + const std::vector<unsigned char> &sym_value_out, + gr_msg_queue_sptr target_queue, unsigned int occupied_tones, + float phase_gain, float freq_gain); + + void enter_search(); + void enter_have_sync(); + void enter_have_header(); + + bool header_ok() + { + // confirm that two copies of header info are identical + return ((d_header >> 16) ^ (d_header & 0xffff)) == 0; + } + + unsigned char slicer(const gr_complex x); + unsigned int demapper(const gr_complex *in, + unsigned char *out); + + bool set_sym_value_out(const std::vector<gr_complex> &sym_position, + const std::vector<unsigned char> &sym_value_out); + + public: + ~digital_ofdm_frame_sink(); + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); +}; + +#endif /* INCLUDED_GR_OFDM_FRAME_SINK_H */ diff --git a/gr-digital/include/digital_ofdm_insert_preamble.h b/gr-digital/include/digital_ofdm_insert_preamble.h new file mode 100644 index 000000000..6f9dae5d6 --- /dev/null +++ b/gr-digital/include/digital_ofdm_insert_preamble.h @@ -0,0 +1,105 @@ +/* -*- c++ -*- */ +/* + * Copyright 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 this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef INCLUDED_DIGITAL_OFDM_INSERT_PREAMBLE_H +#define INCLUDED_DIGITAL_OFDM_INSERT_PREAMBLE_H + +#include <digital_api.h> +#include <gr_block.h> +#include <vector> + +class digital_ofdm_insert_preamble; +typedef boost::shared_ptr<digital_ofdm_insert_preamble> digital_ofdm_insert_preamble_sptr; + +DIGITAL_API digital_ofdm_insert_preamble_sptr +digital_make_ofdm_insert_preamble(int fft_length, + const std::vector<std::vector<gr_complex> > &preamble); + +/*! + * \brief insert "pre-modulated" preamble symbols before each payload. + * \ingroup sync_blk + * \ingroup ofdm_blk + * + * <pre> + * input 1: stream of vectors of gr_complex [fft_length] + * These are the modulated symbols of the payload. + * + * input 2: stream of char. The LSB indicates whether the corresponding + * symbol on input 1 is the first symbol of the payload or not. + * It's a 1 if the corresponding symbol is the first symbol, + * otherwise 0. + * + * N.B., this implies that there must be at least 1 symbol in the payload. + * + * + * output 1: stream of vectors of gr_complex [fft_length] + * These include the preamble symbols and the payload symbols. + * + * output 2: stream of char. The LSB indicates whether the corresponding + * symbol on input 1 is the first symbol of a packet (i.e., the + * first symbol of the preamble.) It's a 1 if the corresponding + * symbol is the first symbol, otherwise 0. + * </pre> + * + * \param fft_length length of each symbol in samples. + * \param preamble vector of symbols that represent the pre-modulated preamble. + */ + +class DIGITAL_API digital_ofdm_insert_preamble : public gr_block +{ + friend DIGITAL_API digital_ofdm_insert_preamble_sptr + digital_make_ofdm_insert_preamble(int fft_length, + const std::vector<std::vector<gr_complex> > &preamble); + +protected: + digital_ofdm_insert_preamble(int fft_length, + const std::vector<std::vector<gr_complex> > &preamble); + +private: + enum state_t { + ST_IDLE, + ST_PREAMBLE, + ST_FIRST_PAYLOAD, + ST_PAYLOAD + }; + + int d_fft_length; + const std::vector<std::vector<gr_complex> > d_preamble; + state_t d_state; + int d_nsymbols_output; + int d_pending_flag; + + void enter_idle(); + void enter_preamble(); + void enter_first_payload(); + void enter_payload(); + + +public: + ~digital_ofdm_insert_preamble(); + + int general_work (int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); +}; + +#endif /* INCLUDED_DIGITAL_OFDM_INSERT_PREAMBLE_H */ diff --git a/gr-digital/include/digital_ofdm_mapper_bcv.h b/gr-digital/include/digital_ofdm_mapper_bcv.h new file mode 100644 index 000000000..daed1eab2 --- /dev/null +++ b/gr-digital/include/digital_ofdm_mapper_bcv.h @@ -0,0 +1,88 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,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. + */ + +#ifndef INCLUDED_DIGITAL_OFDM_MAPPER_BCV_H +#define INCLUDED_DIGITAL_OFDM_MAPPER_BCV_H + +#include <digital_api.h> +#include <gr_sync_block.h> +#include <gr_message.h> +#include <gr_msg_queue.h> + +class digital_ofdm_mapper_bcv; +typedef boost::shared_ptr<digital_ofdm_mapper_bcv> digital_ofdm_mapper_bcv_sptr; + +DIGITAL_API digital_ofdm_mapper_bcv_sptr +digital_make_ofdm_mapper_bcv (const std::vector<gr_complex> &constellation, unsigned msgq_limit, + unsigned occupied_carriers, unsigned int fft_length); + +/*! + * \brief take a stream of bytes in and map to a vector of complex + * constellation points suitable for IFFT input to be used in an ofdm + * modulator. Abstract class must be subclassed with specific mapping. + * \ingroup modulation_blk + * \ingroup ofdm_blk + */ + +class DIGITAL_API digital_ofdm_mapper_bcv : public gr_sync_block +{ + friend DIGITAL_API digital_ofdm_mapper_bcv_sptr + digital_make_ofdm_mapper_bcv (const std::vector<gr_complex> &constellation, unsigned msgq_limit, + unsigned occupied_carriers, unsigned int fft_length); +protected: + digital_ofdm_mapper_bcv (const std::vector<gr_complex> &constellation, unsigned msgq_limit, + unsigned occupied_carriers, unsigned int fft_length); + + private: + std::vector<gr_complex> d_constellation; + gr_msg_queue_sptr d_msgq; + gr_message_sptr d_msg; + unsigned d_msg_offset; + bool d_eof; + + unsigned int d_occupied_carriers; + unsigned int d_fft_length; + unsigned int d_bit_offset; + int d_pending_flag; + + unsigned long d_nbits; + unsigned char d_msgbytes; + + unsigned char d_resid; + unsigned int d_nresid; + + std::vector<int> d_subcarrier_map; + + int randsym(); + + public: + ~digital_ofdm_mapper_bcv(void); + + gr_msg_queue_sptr msgq() const { return d_msgq; } + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + +}; + +#endif diff --git a/gr-digital/include/digital_ofdm_sampler.h b/gr-digital/include/digital_ofdm_sampler.h new file mode 100644 index 000000000..9c54e4e77 --- /dev/null +++ b/gr-digital/include/digital_ofdm_sampler.h @@ -0,0 +1,68 @@ +/* -*- c++ -*- */ +/* + * Copyright 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. + */ + +#ifndef INCLUDED_DIGITAL_OFDM_SAMPLER_H +#define INCLUDED_DIGITAL_OFDM_SAMPLER_H + +#include <digital_api.h> +#include <gr_sync_block.h> + +class digital_ofdm_sampler; +typedef boost::shared_ptr<digital_ofdm_sampler> digital_ofdm_sampler_sptr; + +DIGITAL_API digital_ofdm_sampler_sptr digital_make_ofdm_sampler (unsigned int fft_length, + unsigned int symbol_length, + unsigned int timeout=1000); + +/*! + * \brief does the rest of the OFDM stuff + * \ingroup ofdm_blk + */ +class DIGITAL_API digital_ofdm_sampler : public gr_block +{ + friend DIGITAL_API digital_ofdm_sampler_sptr digital_make_ofdm_sampler (unsigned int fft_length, + unsigned int symbol_length, + unsigned int timeout); + + digital_ofdm_sampler (unsigned int fft_length, + unsigned int symbol_length, + unsigned int timeout); + + private: + enum state_t {STATE_NO_SIG, STATE_PREAMBLE, STATE_FRAME}; + + state_t d_state; + unsigned int d_timeout_max; + unsigned int d_timeout; + unsigned int d_fft_length; + unsigned int d_symbol_length; + + public: + void forecast (int noutput_items, gr_vector_int &ninput_items_required); + + int general_work (int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); +}; + +#endif diff --git a/gr-digital/lib/CMakeLists.txt b/gr-digital/lib/CMakeLists.txt index c100453c2..9417dc355 100644 --- a/gr-digital/lib/CMakeLists.txt +++ b/gr-digital/lib/CMakeLists.txt @@ -46,6 +46,12 @@ list(APPEND gr_digital_sources digital_lms_dd_equalizer_cc.cc digital_kurtotic_equalizer_cc.cc digital_mpsk_receiver_cc.cc + digital_ofdm_cyclic_prefixer.cc + digital_ofdm_frame_acquisition.cc + digital_ofdm_frame_sink.cc + digital_ofdm_insert_preamble.cc + digital_ofdm_mapper_bcv.cc + digital_ofdm_sampler.cc digital_gmskmod_bc.cc digital_cpmmod_bc.cc ) diff --git a/gr-digital/lib/Makefile.am b/gr-digital/lib/Makefile.am index 17baf2101..2860974ca 100644 --- a/gr-digital/lib/Makefile.am +++ b/gr-digital/lib/Makefile.am @@ -41,6 +41,12 @@ libgnuradio_digital_la_SOURCES = \ digital_lms_dd_equalizer_cc.cc \ digital_kurtotic_equalizer_cc.cc \ digital_mpsk_receiver_cc.cc \ + digital_ofdm_cyclic_prefixer.cc \ + digital_ofdm_frame_acquisition.cc \ + digital_ofdm_frame_sink.cc \ + digital_ofdm_insert_preamble.cc \ + digital_ofdm_mapper_bcv.cc \ + digital_ofdm_sampler.cc \ digital_gmskmod_bc.cc \ digital_cpmmod_bc.cc diff --git a/gr-digital/lib/digital_ofdm_cyclic_prefixer.cc b/gr-digital/lib/digital_ofdm_cyclic_prefixer.cc new file mode 100644 index 000000000..192af2591 --- /dev/null +++ b/gr-digital/lib/digital_ofdm_cyclic_prefixer.cc @@ -0,0 +1,70 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2006,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. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <digital_ofdm_cyclic_prefixer.h> +#include <gr_io_signature.h> + +digital_ofdm_cyclic_prefixer_sptr +digital_make_ofdm_cyclic_prefixer (size_t input_size, size_t output_size) +{ + return gnuradio::get_initial_sptr(new digital_ofdm_cyclic_prefixer (input_size, + output_size)); +} + +digital_ofdm_cyclic_prefixer::digital_ofdm_cyclic_prefixer (size_t input_size, + size_t output_size) + : gr_sync_interpolator ("ofdm_cyclic_prefixer", + gr_make_io_signature (1, 1, input_size*sizeof(gr_complex)), + gr_make_io_signature (1, 1, sizeof(gr_complex)), + output_size), + d_input_size(input_size), + d_output_size(output_size) + +{ +} + +int +digital_ofdm_cyclic_prefixer::work (int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) +{ + gr_complex *in = (gr_complex *) input_items[0]; + gr_complex *out = (gr_complex *) output_items[0]; + size_t cp_size = d_output_size - d_input_size; + unsigned int i=0, j=0; + + j = cp_size; + for(i=0; i < d_input_size; i++,j++) { + out[j] = in[i]; + } + + j = d_input_size - cp_size; + for(i=0; i < cp_size; i++, j++) { + out[i] = in[j]; + } + + return d_output_size; +} diff --git a/gr-digital/lib/digital_ofdm_frame_acquisition.cc b/gr-digital/lib/digital_ofdm_frame_acquisition.cc new file mode 100644 index 000000000..93b58aeca --- /dev/null +++ b/gr-digital/lib/digital_ofdm_frame_acquisition.cc @@ -0,0 +1,210 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006-2008,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. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <digital_ofdm_frame_acquisition.h> +#include <gr_io_signature.h> +#include <gr_expj.h> +#include <gr_math.h> +#include <cstdio> + +#define VERBOSE 0 +#define M_TWOPI (2*M_PI) +#define MAX_NUM_SYMBOLS 1000 + +digital_ofdm_frame_acquisition_sptr +digital_make_ofdm_frame_acquisition (unsigned int occupied_carriers, + unsigned int fft_length, + unsigned int cplen, + const std::vector<gr_complex> &known_symbol, + unsigned int max_fft_shift_len) +{ + return gnuradio::get_initial_sptr(new digital_ofdm_frame_acquisition (occupied_carriers, fft_length, cplen, + known_symbol, max_fft_shift_len)); +} + +digital_ofdm_frame_acquisition::digital_ofdm_frame_acquisition (unsigned occupied_carriers, + unsigned int fft_length, + unsigned int cplen, + const std::vector<gr_complex> &known_symbol, + unsigned int max_fft_shift_len) + : gr_block ("ofdm_frame_acquisition", + gr_make_io_signature2 (2, 2, sizeof(gr_complex)*fft_length, sizeof(char)*fft_length), + gr_make_io_signature2 (2, 2, sizeof(gr_complex)*occupied_carriers, sizeof(char))), + d_occupied_carriers(occupied_carriers), + d_fft_length(fft_length), + d_cplen(cplen), + d_freq_shift_len(max_fft_shift_len), + d_known_symbol(known_symbol), + d_coarse_freq(0), + d_phase_count(0) +{ + d_symbol_phase_diff.resize(d_fft_length); + d_known_phase_diff.resize(d_occupied_carriers); + d_hestimate.resize(d_occupied_carriers); + + unsigned int i = 0, j = 0; + + std::fill(d_known_phase_diff.begin(), d_known_phase_diff.end(), 0); + for(i = 0; i < d_known_symbol.size()-2; i+=2) { + d_known_phase_diff[i] = norm(d_known_symbol[i] - d_known_symbol[i+2]); + } + + d_phase_lut = new gr_complex[(2*d_freq_shift_len+1) * MAX_NUM_SYMBOLS]; + for(i = 0; i <= 2*d_freq_shift_len; i++) { + for(j = 0; j < MAX_NUM_SYMBOLS; j++) { + d_phase_lut[j + i*MAX_NUM_SYMBOLS] = gr_expj(-M_TWOPI*d_cplen/d_fft_length*(i-d_freq_shift_len)*j); + } + } +} + +digital_ofdm_frame_acquisition::~digital_ofdm_frame_acquisition(void) +{ + delete [] d_phase_lut; +} + +void +digital_ofdm_frame_acquisition::forecast (int noutput_items, gr_vector_int &ninput_items_required) +{ + unsigned ninputs = ninput_items_required.size (); + for (unsigned i = 0; i < ninputs; i++) + ninput_items_required[i] = 1; +} + +gr_complex +digital_ofdm_frame_acquisition::coarse_freq_comp(int freq_delta, int symbol_count) +{ + // return gr_complex(cos(-M_TWOPI*freq_delta*d_cplen/d_fft_length*symbol_count), + // sin(-M_TWOPI*freq_delta*d_cplen/d_fft_length*symbol_count)); + + return gr_expj(-M_TWOPI*freq_delta*d_cplen/d_fft_length*symbol_count); + + //return d_phase_lut[MAX_NUM_SYMBOLS * (d_freq_shift_len + freq_delta) + symbol_count]; +} + +void +digital_ofdm_frame_acquisition::correlate(const gr_complex *symbol, int zeros_on_left) +{ + unsigned int i,j; + + std::fill(d_symbol_phase_diff.begin(), d_symbol_phase_diff.end(), 0); + for(i = 0; i < d_fft_length-2; i++) { + d_symbol_phase_diff[i] = norm(symbol[i] - symbol[i+2]); + } + + // sweep through all possible/allowed frequency offsets and select the best + int index = 0; + float max = 0, sum=0; + for(i = zeros_on_left - d_freq_shift_len; i < zeros_on_left + d_freq_shift_len; i++) { + sum = 0; + for(j = 0; j < d_occupied_carriers; j++) { + sum += (d_known_phase_diff[j] * d_symbol_phase_diff[i+j]); + } + if(sum > max) { + max = sum; + index = i; + } + } + + // set the coarse frequency offset relative to the edge of the occupied tones + d_coarse_freq = index - zeros_on_left; +} + +void +digital_ofdm_frame_acquisition::calculate_equalizer(const gr_complex *symbol, int zeros_on_left) +{ + unsigned int i=0; + + // Set first tap of equalizer + d_hestimate[0] = d_known_symbol[0] / + (coarse_freq_comp(d_coarse_freq,1)*symbol[zeros_on_left+d_coarse_freq]); + + // set every even tap based on known symbol + // linearly interpolate between set carriers to set zero-filled carriers + // FIXME: is this the best way to set this? + for(i = 2; i < d_occupied_carriers; i+=2) { + d_hestimate[i] = d_known_symbol[i] / + (coarse_freq_comp(d_coarse_freq,1)*(symbol[i+zeros_on_left+d_coarse_freq])); + d_hestimate[i-1] = (d_hestimate[i] + d_hestimate[i-2]) / gr_complex(2.0, 0.0); + } + + // with even number of carriers; last equalizer tap is wrong + if(!(d_occupied_carriers & 1)) { + d_hestimate[d_occupied_carriers-1] = d_hestimate[d_occupied_carriers-2]; + } + + if(VERBOSE) { + fprintf(stderr, "Equalizer setting:\n"); + for(i = 0; i < d_occupied_carriers; i++) { + gr_complex sym = coarse_freq_comp(d_coarse_freq,1)*symbol[i+zeros_on_left+d_coarse_freq]; + gr_complex output = sym * d_hestimate[i]; + fprintf(stderr, "sym: %+.4f + j%+.4f ks: %+.4f + j%+.4f eq: %+.4f + j%+.4f ==> %+.4f + j%+.4f\n", + sym .real(), sym.imag(), + d_known_symbol[i].real(), d_known_symbol[i].imag(), + d_hestimate[i].real(), d_hestimate[i].imag(), + output.real(), output.imag()); + } + fprintf(stderr, "\n"); + } +} + +int +digital_ofdm_frame_acquisition::general_work(int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) +{ + const gr_complex *symbol = (const gr_complex *)input_items[0]; + const char *signal_in = (const char *)input_items[1]; + + gr_complex *out = (gr_complex *) output_items[0]; + char *signal_out = (char *) output_items[1]; + + int unoccupied_carriers = d_fft_length - d_occupied_carriers; + int zeros_on_left = (int)ceil(unoccupied_carriers/2.0); + + if(signal_in[0]) { + d_phase_count = 1; + correlate(symbol, zeros_on_left); + calculate_equalizer(symbol, zeros_on_left); + signal_out[0] = 1; + } + else { + signal_out[0] = 0; + } + + for(unsigned int i = 0; i < d_occupied_carriers; i++) { + out[i] = d_hestimate[i]*coarse_freq_comp(d_coarse_freq,d_phase_count) + *symbol[i+zeros_on_left+d_coarse_freq]; + } + + d_phase_count++; + if(d_phase_count == MAX_NUM_SYMBOLS) { + d_phase_count = 1; + } + + consume_each(1); + return 1; +} diff --git a/gr-digital/lib/digital_ofdm_frame_sink.cc b/gr-digital/lib/digital_ofdm_frame_sink.cc new file mode 100644 index 000000000..f8fb1bbb1 --- /dev/null +++ b/gr-digital/lib/digital_ofdm_frame_sink.cc @@ -0,0 +1,405 @@ +/* -*- c++ -*- */ +/* + * Copyright 2007,2008,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. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <digital_ofdm_frame_sink.h> +#include <gr_io_signature.h> +#include <gr_expj.h> +#include <gr_math.h> +#include <math.h> +#include <cstdio> +#include <stdexcept> +#include <iostream> +#include <string.h> + +#define VERBOSE 0 + +inline void +digital_ofdm_frame_sink::enter_search() +{ + if (VERBOSE) + fprintf(stderr, "@ enter_search\n"); + + d_state = STATE_SYNC_SEARCH; + +} + +inline void +digital_ofdm_frame_sink::enter_have_sync() +{ + if (VERBOSE) + fprintf(stderr, "@ enter_have_sync\n"); + + d_state = STATE_HAVE_SYNC; + + // clear state of demapper + d_byte_offset = 0; + d_partial_byte = 0; + + d_header = 0; + d_headerbytelen_cnt = 0; + + // Resetting PLL + d_freq = 0.0; + d_phase = 0.0; + fill(d_dfe.begin(), d_dfe.end(), gr_complex(1.0,0.0)); +} + +inline void +digital_ofdm_frame_sink::enter_have_header() +{ + d_state = STATE_HAVE_HEADER; + + // header consists of two 16-bit shorts in network byte order + // payload length is lower 12 bits + // whitener offset is upper 4 bits + d_packetlen = (d_header >> 16) & 0x0fff; + d_packet_whitener_offset = (d_header >> 28) & 0x000f; + d_packetlen_cnt = 0; + + if (VERBOSE) + fprintf(stderr, "@ enter_have_header (payload_len = %d) (offset = %d)\n", + d_packetlen, d_packet_whitener_offset); +} + + +unsigned char digital_ofdm_frame_sink::slicer(const gr_complex x) +{ + unsigned int table_size = d_sym_value_out.size(); + unsigned int min_index = 0; + float min_euclid_dist = norm(x - d_sym_position[0]); + float euclid_dist = 0; + + for (unsigned int j = 1; j < table_size; j++){ + euclid_dist = norm(x - d_sym_position[j]); + if (euclid_dist < min_euclid_dist){ + min_euclid_dist = euclid_dist; + min_index = j; + } + } + return d_sym_value_out[min_index]; +} + +unsigned int digital_ofdm_frame_sink::demapper(const gr_complex *in, + unsigned char *out) +{ + unsigned int i=0, bytes_produced=0; + gr_complex carrier; + + carrier=gr_expj(d_phase); + + gr_complex accum_error = 0.0; + //while(i < d_occupied_carriers) { + while(i < d_subcarrier_map.size()) { + if(d_nresid > 0) { + d_partial_byte |= d_resid; + d_byte_offset += d_nresid; + d_nresid = 0; + d_resid = 0; + } + + //while((d_byte_offset < 8) && (i < d_occupied_carriers)) { + while((d_byte_offset < 8) && (i < d_subcarrier_map.size())) { + //gr_complex sigrot = in[i]*carrier*d_dfe[i]; + gr_complex sigrot = in[d_subcarrier_map[i]]*carrier*d_dfe[i]; + + if(d_derotated_output != NULL){ + d_derotated_output[i] = sigrot; + } + + unsigned char bits = slicer(sigrot); + + gr_complex closest_sym = d_sym_position[bits]; + + accum_error += sigrot * conj(closest_sym); + + // FIX THE FOLLOWING STATEMENT + if (norm(sigrot)> 0.001) d_dfe[i] += d_eq_gain*(closest_sym/sigrot-d_dfe[i]); + + i++; + + if((8 - d_byte_offset) >= d_nbits) { + d_partial_byte |= bits << (d_byte_offset); + d_byte_offset += d_nbits; + } + else { + d_nresid = d_nbits-(8-d_byte_offset); + int mask = ((1<<(8-d_byte_offset))-1); + d_partial_byte |= (bits & mask) << d_byte_offset; + d_resid = bits >> (8-d_byte_offset); + d_byte_offset += (d_nbits - d_nresid); + } + //printf("demod symbol: %.4f + j%.4f bits: %x partial_byte: %x byte_offset: %d resid: %x nresid: %d\n", + // in[i-1].real(), in[i-1].imag(), bits, d_partial_byte, d_byte_offset, d_resid, d_nresid); + } + + if(d_byte_offset == 8) { + //printf("demod byte: %x \n\n", d_partial_byte); + out[bytes_produced++] = d_partial_byte; + d_byte_offset = 0; + d_partial_byte = 0; + } + } + //std::cerr << "accum_error " << accum_error << std::endl; + + float angle = arg(accum_error); + + d_freq = d_freq - d_freq_gain*angle; + d_phase = d_phase + d_freq - d_phase_gain*angle; + if (d_phase >= 2*M_PI) d_phase -= 2*M_PI; + if (d_phase <0) d_phase += 2*M_PI; + + //if(VERBOSE) + // std::cerr << angle << "\t" << d_freq << "\t" << d_phase << "\t" << std::endl; + + return bytes_produced; +} + + +digital_ofdm_frame_sink_sptr +digital_make_ofdm_frame_sink(const std::vector<gr_complex> &sym_position, + const std::vector<unsigned char> &sym_value_out, + gr_msg_queue_sptr target_queue, unsigned int occupied_carriers, + float phase_gain, float freq_gain) +{ + return gnuradio::get_initial_sptr(new digital_ofdm_frame_sink(sym_position, sym_value_out, + target_queue, occupied_carriers, + phase_gain, freq_gain)); +} + + +digital_ofdm_frame_sink::digital_ofdm_frame_sink(const std::vector<gr_complex> &sym_position, + const std::vector<unsigned char> &sym_value_out, + gr_msg_queue_sptr target_queue, unsigned int occupied_carriers, + float phase_gain, float freq_gain) + : gr_sync_block ("ofdm_frame_sink", + gr_make_io_signature2 (2, 2, sizeof(gr_complex)*occupied_carriers, sizeof(char)), + gr_make_io_signature (1, 1, sizeof(gr_complex)*occupied_carriers)), + d_target_queue(target_queue), d_occupied_carriers(occupied_carriers), + d_byte_offset(0), d_partial_byte(0), + d_resid(0), d_nresid(0),d_phase(0),d_freq(0),d_phase_gain(phase_gain),d_freq_gain(freq_gain), + d_eq_gain(0.05) +{ + std::string carriers = "FE7F"; + + // A bit hacky to fill out carriers to occupied_carriers length + int diff = (d_occupied_carriers - 4*carriers.length()); + while(diff > 7) { + carriers.insert(0, "f"); + carriers.insert(carriers.length(), "f"); + diff -= 8; + } + + // if there's extras left to be processed + // divide remaining to put on either side of current map + // all of this is done to stick with the concept of a carrier map string that + // can be later passed by the user, even though it'd be cleaner to just do this + // on the carrier map itself + int diff_left=0; + int diff_right=0; + + // dictionary to convert from integers to ascii hex representation + char abc[16] = {'0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + if(diff > 0) { + char c[2] = {0,0}; + + diff_left = (int)ceil((float)diff/2.0f); // number of carriers to put on the left side + c[0] = abc[(1 << diff_left) - 1]; // convert to bits and move to ASCI integer + carriers.insert(0, c); + + diff_right = diff - diff_left; // number of carriers to put on the right side + c[0] = abc[0xF^((1 << diff_right) - 1)]; // convert to bits and move to ASCI integer + carriers.insert(carriers.length(), c); + } + + // It seemed like such a good idea at the time... + // because we are only dealing with the occupied_carriers + // at this point, the diff_left in the following compensates + // for any offset from the 0th carrier introduced + unsigned int i,j,k; + for(i = 0; i < (d_occupied_carriers/4)+diff_left; i++) { + char c = carriers[i]; + for(j = 0; j < 4; j++) { + k = (strtol(&c, NULL, 16) >> (3-j)) & 0x1; + if(k) { + d_subcarrier_map.push_back(4*i + j - diff_left); + } + } + } + + // make sure we stay in the limit currently imposed by the occupied_carriers + if(d_subcarrier_map.size() > d_occupied_carriers) { + throw std::invalid_argument("digital_ofdm_mapper_bcv: subcarriers allocated exceeds size of occupied carriers"); + } + + d_bytes_out = new unsigned char[d_occupied_carriers]; + d_dfe.resize(occupied_carriers); + fill(d_dfe.begin(), d_dfe.end(), gr_complex(1.0,0.0)); + + set_sym_value_out(sym_position, sym_value_out); + + enter_search(); +} + +digital_ofdm_frame_sink::~digital_ofdm_frame_sink () +{ + delete [] d_bytes_out; +} + +bool +digital_ofdm_frame_sink::set_sym_value_out(const std::vector<gr_complex> &sym_position, + const std::vector<unsigned char> &sym_value_out) +{ + if (sym_position.size() != sym_value_out.size()) + return false; + + if (sym_position.size()<1) + return false; + + d_sym_position = sym_position; + d_sym_value_out = sym_value_out; + d_nbits = (unsigned long)ceil(log10(float(d_sym_value_out.size())) / log10(2.0)); + + return true; +} + + +int +digital_ofdm_frame_sink::work (int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) +{ + const gr_complex *in = (const gr_complex *) input_items[0]; + const char *sig = (const char *) input_items[1]; + unsigned int j = 0; + unsigned int bytes=0; + + // If the output is connected, send it the derotated symbols + if(output_items.size() >= 1) + d_derotated_output = (gr_complex *)output_items[0]; + else + d_derotated_output = NULL; + + if (VERBOSE) + fprintf(stderr,">>> Entering state machine\n"); + + switch(d_state) { + + case STATE_SYNC_SEARCH: // Look for flag indicating beginning of pkt + if (VERBOSE) + fprintf(stderr,"SYNC Search, noutput=%d\n", noutput_items); + + if (sig[0]) { // Found it, set up for header decode + enter_have_sync(); + } + break; + + case STATE_HAVE_SYNC: + // only demod after getting the preamble signal; otherwise, the + // equalizer taps will screw with the PLL performance + bytes = demapper(&in[0], d_bytes_out); + + if (VERBOSE) { + if(sig[0]) + printf("ERROR -- Found SYNC in HAVE_SYNC\n"); + fprintf(stderr,"Header Search bitcnt=%d, header=0x%08x\n", + d_headerbytelen_cnt, d_header); + } + + j = 0; + while(j < bytes) { + d_header = (d_header << 8) | (d_bytes_out[j] & 0xFF); + j++; + + if (++d_headerbytelen_cnt == HEADERBYTELEN) { + + if (VERBOSE) + fprintf(stderr, "got header: 0x%08x\n", d_header); + + // we have a full header, check to see if it has been received properly + if (header_ok()){ + enter_have_header(); + + if (VERBOSE) + printf("\nPacket Length: %d\n", d_packetlen); + + while((j < bytes) && (d_packetlen_cnt < d_packetlen)) { + d_packet[d_packetlen_cnt++] = d_bytes_out[j++]; + } + + if(d_packetlen_cnt == d_packetlen) { + gr_message_sptr msg = + gr_make_message(0, d_packet_whitener_offset, 0, d_packetlen); + memcpy(msg->msg(), d_packet, d_packetlen_cnt); + d_target_queue->insert_tail(msg); // send it + msg.reset(); // free it up + + enter_search(); + } + } + else { + enter_search(); // bad header + } + } + } + break; + + case STATE_HAVE_HEADER: + bytes = demapper(&in[0], d_bytes_out); + + if (VERBOSE) { + if(sig[0]) + printf("ERROR -- Found SYNC in HAVE_HEADER at %d, length of %d\n", d_packetlen_cnt, d_packetlen); + fprintf(stderr,"Packet Build\n"); + } + + j = 0; + while(j < bytes) { + d_packet[d_packetlen_cnt++] = d_bytes_out[j++]; + + if (d_packetlen_cnt == d_packetlen){ // packet is filled + // build a message + // NOTE: passing header field as arg1 is not scalable + gr_message_sptr msg = + gr_make_message(0, d_packet_whitener_offset, 0, d_packetlen_cnt); + memcpy(msg->msg(), d_packet, d_packetlen_cnt); + + d_target_queue->insert_tail(msg); // send it + msg.reset(); // free it up + + enter_search(); + break; + } + } + break; + + default: + assert(0); + + } // switch + + return 1; +} diff --git a/gr-digital/lib/digital_ofdm_insert_preamble.cc b/gr-digital/lib/digital_ofdm_insert_preamble.cc new file mode 100644 index 000000000..a46133643 --- /dev/null +++ b/gr-digital/lib/digital_ofdm_insert_preamble.cc @@ -0,0 +1,187 @@ +/* -*- c++ -*- */ +/* + * Copyright 2007,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 this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <digital_ofdm_insert_preamble.h> +#include <gr_io_signature.h> +#include <stdexcept> +#include <iostream> +#include <string.h> + +digital_ofdm_insert_preamble_sptr +digital_make_ofdm_insert_preamble(int fft_length, + const std::vector<std::vector<gr_complex> > &preamble) +{ + return gnuradio::get_initial_sptr(new digital_ofdm_insert_preamble(fft_length, + preamble)); +} + +digital_ofdm_insert_preamble::digital_ofdm_insert_preamble + (int fft_length, + const std::vector<std::vector<gr_complex> > &preamble) + : gr_block("ofdm_insert_preamble", + gr_make_io_signature2(2, 2, + sizeof(gr_complex)*fft_length, + sizeof(char)), + gr_make_io_signature2(1, 2, + sizeof(gr_complex)*fft_length, + sizeof(char))), + d_fft_length(fft_length), + d_preamble(preamble), + d_state(ST_IDLE), + d_nsymbols_output(0), + d_pending_flag(0) +{ + // sanity check preamble symbols + for (size_t i = 0; i < d_preamble.size(); i++){ + if (d_preamble[i].size() != (size_t) d_fft_length) + throw std::invalid_argument("digital_ofdm_insert_preamble: invalid length for preamble symbol"); + } + + enter_idle(); +} + + +digital_ofdm_insert_preamble::~digital_ofdm_insert_preamble() +{ +} + +int +digital_ofdm_insert_preamble::general_work (int noutput_items, + gr_vector_int &ninput_items_v, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) +{ + int ninput_items = std::min(ninput_items_v[0], ninput_items_v[1]); + const gr_complex *in_sym = (const gr_complex *) input_items[0]; + const unsigned char *in_flag = (const unsigned char *) input_items[1]; + + gr_complex *out_sym = (gr_complex *) output_items[0]; + unsigned char *out_flag = 0; + if (output_items.size() == 2) + out_flag = (unsigned char *) output_items[1]; + + + int no = 0; // number items output + int ni = 0; // number items read from input + + +#define write_out_flag() \ + do { if (out_flag) \ + out_flag[no] = d_pending_flag; \ + d_pending_flag = 0; \ + } while(0) + + + while (no < noutput_items && ni < ninput_items){ + switch(d_state){ + case ST_IDLE: + if (in_flag[ni] & 0x1) // this is first symbol of new payload + enter_preamble(); + else + ni++; // eat one input symbol + break; + + case ST_PREAMBLE: + assert(in_flag[ni] & 0x1); + if (d_nsymbols_output >= (int) d_preamble.size()){ + // we've output all the preamble + enter_first_payload(); + } + else { + memcpy(&out_sym[no * d_fft_length], + &d_preamble[d_nsymbols_output][0], + d_fft_length*sizeof(gr_complex)); + + write_out_flag(); + no++; + d_nsymbols_output++; + } + break; + + case ST_FIRST_PAYLOAD: + // copy first payload symbol from input to output + memcpy(&out_sym[no * d_fft_length], + &in_sym[ni * d_fft_length], + d_fft_length * sizeof(gr_complex)); + + write_out_flag(); + no++; + ni++; + enter_payload(); + break; + + case ST_PAYLOAD: + if (in_flag[ni] & 0x1){ // this is first symbol of a new payload + enter_preamble(); + break; + } + + // copy a symbol from input to output + memcpy(&out_sym[no * d_fft_length], + &in_sym[ni * d_fft_length], + d_fft_length * sizeof(gr_complex)); + + write_out_flag(); + no++; + ni++; + break; + + default: + std::cerr << "digital_ofdm_insert_preamble: (can't happen) invalid state, resetting\n"; + enter_idle(); + } + } + + consume_each(ni); + return no; +} + +void +digital_ofdm_insert_preamble::enter_idle() +{ + d_state = ST_IDLE; + d_nsymbols_output = 0; + d_pending_flag = 0; +} + +void +digital_ofdm_insert_preamble::enter_preamble() +{ + d_state = ST_PREAMBLE; + d_nsymbols_output = 0; + d_pending_flag = 1; +} + +void +digital_ofdm_insert_preamble::enter_first_payload() +{ + d_state = ST_FIRST_PAYLOAD; +} + +void +digital_ofdm_insert_preamble::enter_payload() +{ + d_state = ST_PAYLOAD; +} diff --git a/gr-digital/lib/digital_ofdm_mapper_bcv.cc b/gr-digital/lib/digital_ofdm_mapper_bcv.cc new file mode 100644 index 000000000..cf3d08703 --- /dev/null +++ b/gr-digital/lib/digital_ofdm_mapper_bcv.cc @@ -0,0 +1,241 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006-2008,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. + */ + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <digital_ofdm_mapper_bcv.h> +#include <gr_io_signature.h> +#include <stdexcept> +#include <string.h> + +digital_ofdm_mapper_bcv_sptr +digital_make_ofdm_mapper_bcv (const std::vector<gr_complex> &constellation, unsigned int msgq_limit, + unsigned int occupied_carriers, unsigned int fft_length) +{ + return gnuradio::get_initial_sptr(new digital_ofdm_mapper_bcv (constellation, msgq_limit, + occupied_carriers, fft_length)); +} + +// Consumes 1 packet and produces as many OFDM symbols of fft_length to hold the full packet +digital_ofdm_mapper_bcv::digital_ofdm_mapper_bcv (const std::vector<gr_complex> &constellation, unsigned int msgq_limit, + unsigned int occupied_carriers, unsigned int fft_length) + : gr_sync_block ("ofdm_mapper_bcv", + gr_make_io_signature (0, 0, 0), + gr_make_io_signature2 (1, 2, sizeof(gr_complex)*fft_length, sizeof(char))), + d_constellation(constellation), + d_msgq(gr_make_msg_queue(msgq_limit)), d_msg_offset(0), d_eof(false), + d_occupied_carriers(occupied_carriers), + d_fft_length(fft_length), + d_bit_offset(0), + d_pending_flag(0), + d_resid(0), + d_nresid(0) +{ + if (!(d_occupied_carriers <= d_fft_length)) + throw std::invalid_argument("digital_ofdm_mapper_bcv: occupied carriers must be <= fft_length"); + + // this is not the final form of this solution since we still use the occupied_tones concept, + // which would get us into trouble if the number of carriers we seek is greater than the occupied carriers. + // Eventually, we will get rid of the occupied_carriers concept. + std::string carriers = "FE7F"; + + // A bit hacky to fill out carriers to occupied_carriers length + int diff = (d_occupied_carriers - 4*carriers.length()); + while(diff > 7) { + carriers.insert(0, "f"); + carriers.insert(carriers.length(), "f"); + diff -= 8; + } + + // if there's extras left to be processed + // divide remaining to put on either side of current map + // all of this is done to stick with the concept of a carrier map string that + // can be later passed by the user, even though it'd be cleaner to just do this + // on the carrier map itself + int diff_left=0; + int diff_right=0; + + // dictionary to convert from integers to ascii hex representation + char abc[16] = {'0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + if(diff > 0) { + char c[2] = {0,0}; + + diff_left = (int)ceil((float)diff/2.0f); // number of carriers to put on the left side + c[0] = abc[(1 << diff_left) - 1]; // convert to bits and move to ASCI integer + carriers.insert(0, c); + + diff_right = diff - diff_left; // number of carriers to put on the right side + c[0] = abc[0xF^((1 << diff_right) - 1)]; // convert to bits and move to ASCI integer + carriers.insert(carriers.length(), c); + } + + // find out how many zeros to pad on the sides; the difference between the fft length and the subcarrier + // mapping size in chunks of four. This is the number to pack on the left and this number plus any + // residual nulls (if odd) will be packed on the right. + diff = (d_fft_length/4 - carriers.length())/2; + + unsigned int i,j,k; + for(i = 0; i < carriers.length(); i++) { + char c = carriers[i]; // get the current hex character from the string + for(j = 0; j < 4; j++) { // walk through all four bits + k = (strtol(&c, NULL, 16) >> (3-j)) & 0x1; // convert to int and extract next bit + if(k) { // if bit is a 1, + d_subcarrier_map.push_back(4*(i+diff) + j); // use this subcarrier + } + } + } + + // make sure we stay in the limit currently imposed by the occupied_carriers + if(d_subcarrier_map.size() > d_occupied_carriers) { + throw std::invalid_argument("digital_ofdm_mapper_bcv: subcarriers allocated exceeds size of occupied carriers"); + } + + d_nbits = (unsigned long)ceil(log10(float(d_constellation.size())) / log10(2.0)); +} + +digital_ofdm_mapper_bcv::~digital_ofdm_mapper_bcv(void) +{ +} + +int digital_ofdm_mapper_bcv::randsym() +{ + return (rand() % d_constellation.size()); +} + +int +digital_ofdm_mapper_bcv::work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) +{ + gr_complex *out = (gr_complex *)output_items[0]; + + unsigned int i=0; + + //printf("OFDM BPSK Mapper: ninput_items: %d noutput_items: %d\n", ninput_items[0], noutput_items); + + if(d_eof) { + return -1; + } + + if(!d_msg) { + d_msg = d_msgq->delete_head(); // block, waiting for a message + d_msg_offset = 0; + d_bit_offset = 0; + d_pending_flag = 1; // new packet, write start of packet flag + + if((d_msg->length() == 0) && (d_msg->type() == 1)) { + d_msg.reset(); + return -1; // We're done; no more messages coming. + } + } + + char *out_flag = 0; + if(output_items.size() == 2) + out_flag = (char *) output_items[1]; + + + // Build a single symbol: + // Initialize all bins to 0 to set unused carriers + memset(out, 0, d_fft_length*sizeof(gr_complex)); + + i = 0; + unsigned char bits = 0; + //while((d_msg_offset < d_msg->length()) && (i < d_occupied_carriers)) { + while((d_msg_offset < d_msg->length()) && (i < d_subcarrier_map.size())) { + + // need new data to process + if(d_bit_offset == 0) { + d_msgbytes = d_msg->msg()[d_msg_offset]; + //printf("mod message byte: %x\n", d_msgbytes); + } + + if(d_nresid > 0) { + // take the residual bits, fill out nbits with info from the new byte, and put them in the symbol + d_resid |= (((1 << d_nresid)-1) & d_msgbytes) << (d_nbits - d_nresid); + bits = d_resid; + + out[d_subcarrier_map[i]] = d_constellation[bits]; + i++; + + d_bit_offset += d_nresid; + d_nresid = 0; + d_resid = 0; + //printf("mod bit(r): %x resid: %x nresid: %d bit_offset: %d\n", + // bits, d_resid, d_nresid, d_bit_offset); + } + else { + if((8 - d_bit_offset) >= d_nbits) { // test to make sure we can fit nbits + // take the nbits number of bits at a time from the byte to add to the symbol + bits = ((1 << d_nbits)-1) & (d_msgbytes >> d_bit_offset); + d_bit_offset += d_nbits; + + out[d_subcarrier_map[i]] = d_constellation[bits]; + i++; + } + else { // if we can't fit nbits, store them for the next + // saves d_nresid bits of this message where d_nresid < d_nbits + unsigned int extra = 8-d_bit_offset; + d_resid = ((1 << extra)-1) & (d_msgbytes >> d_bit_offset); + d_bit_offset += extra; + d_nresid = d_nbits - extra; + } + + } + + if(d_bit_offset == 8) { + d_bit_offset = 0; + d_msg_offset++; + } + } + + // Ran out of data to put in symbol + if (d_msg_offset == d_msg->length()) { + if(d_nresid > 0) { + d_resid |= 0x00; + bits = d_resid; + d_nresid = 0; + d_resid = 0; + } + + //while(i < d_occupied_carriers) { // finish filling out the symbol + while(i < d_subcarrier_map.size()) { // finish filling out the symbol + out[d_subcarrier_map[i]] = d_constellation[randsym()]; + + i++; + } + + if (d_msg->type() == 1) // type == 1 sets EOF + d_eof = true; + d_msg.reset(); // finished packet, free message + assert(d_bit_offset == 0); + } + + if (out_flag) + out_flag[0] = d_pending_flag; + d_pending_flag = 0; + + return 1; // produced symbol +} diff --git a/gr-digital/lib/digital_ofdm_sampler.cc b/gr-digital/lib/digital_ofdm_sampler.cc new file mode 100644 index 000000000..cab8c2ba9 --- /dev/null +++ b/gr-digital/lib/digital_ofdm_sampler.cc @@ -0,0 +1,133 @@ +/* -*- c++ -*- */ +/* + * Copyright 2007,2008,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. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <digital_ofdm_sampler.h> +#include <gr_io_signature.h> +#include <gr_expj.h> +#include <cstdio> + +digital_ofdm_sampler_sptr +digital_make_ofdm_sampler (unsigned int fft_length, + unsigned int symbol_length, + unsigned int timeout) +{ + return gnuradio::get_initial_sptr(new digital_ofdm_sampler (fft_length, symbol_length, timeout)); +} + +digital_ofdm_sampler::digital_ofdm_sampler (unsigned int fft_length, + unsigned int symbol_length, + unsigned int timeout) + : gr_block ("ofdm_sampler", + gr_make_io_signature2 (2, 2, sizeof (gr_complex), sizeof(char)), + gr_make_io_signature2 (2, 2, sizeof (gr_complex)*fft_length, sizeof(char)*fft_length)), + d_state(STATE_NO_SIG), d_timeout_max(timeout), d_fft_length(fft_length), d_symbol_length(symbol_length) +{ + set_relative_rate(1.0/(double) fft_length); // buffer allocator hint +} + +void +digital_ofdm_sampler::forecast (int noutput_items, gr_vector_int &ninput_items_required) +{ + // FIXME do we need more + //int nreqd = (noutput_items-1) * d_symbol_length + d_fft_length; + int nreqd = d_symbol_length + d_fft_length; + unsigned ninputs = ninput_items_required.size (); + for (unsigned i = 0; i < ninputs; i++) + ninput_items_required[i] = nreqd; +} + + +int +digital_ofdm_sampler::general_work (int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) +{ + const gr_complex *iptr = (const gr_complex *) input_items[0]; + const char *trigger = (const char *) input_items[1]; + + gr_complex *optr = (gr_complex *) output_items[0]; + char *outsig = (char *) output_items[1]; + + //FIXME: we only process a single OFDM symbol at a time; after the preamble, we can + // process a few at a time as long as we always look out for the next preamble. + + unsigned int index=d_fft_length; // start one fft length into the input so we can always look back this far + + outsig[0] = 0; // set output to no signal by default + + // Search for a preamble trigger signal during the next symbol length + while((d_state != STATE_PREAMBLE) && (index <= (d_symbol_length+d_fft_length))) { + if(trigger[index]) { + outsig[0] = 1; // tell the next block there is a preamble coming + d_state = STATE_PREAMBLE; + } + else + index++; + } + + unsigned int i, pos, ret; + switch(d_state) { + case(STATE_PREAMBLE): + // When we found a preamble trigger, get it and set the symbol boundary here + for(i = (index - d_fft_length + 1); i <= index; i++) { + *optr++ = iptr[i]; + } + + d_timeout = d_timeout_max; // tell the system to expect at least this many symbols for a frame + d_state = STATE_FRAME; + consume_each(index - d_fft_length + 1); // consume up to one fft_length away to keep the history + ret = 1; + break; + + case(STATE_FRAME): + // use this state when we have processed a preamble and are getting the rest of the frames + //FIXME: we could also have a power squelch system here to enter STATE_NO_SIG if no power is received + + // skip over fft length history and cyclic prefix + pos = d_symbol_length; // keeps track of where we are in the input buffer + while(pos < d_symbol_length + d_fft_length) { + *optr++ = iptr[pos++]; + } + + if(d_timeout-- == 0) { + printf("TIMEOUT\n"); + d_state = STATE_NO_SIG; + } + + consume_each(d_symbol_length); // jump up by 1 fft length and the cyclic prefix length + ret = 1; + break; + + case(STATE_NO_SIG): + default: + consume_each(index-d_fft_length); // consume everything we've gone through so far leaving the fft length history + ret = 0; + break; + } + + return ret; +} diff --git a/gr-digital/python/CMakeLists.txt b/gr-digital/python/CMakeLists.txt index 7f810f14d..905626b8b 100644 --- a/gr-digital/python/CMakeLists.txt +++ b/gr-digital/python/CMakeLists.txt @@ -30,11 +30,17 @@ GR_PYTHON_INSTALL( crc.py generic_mod_demod.py gmsk.py - modulation_utils2.py + modulation_utils.py + ofdm.py + ofdm_packet_utils.py + ofdm_receiver.py + ofdm_sync_fixed.py + ofdm_sync_ml.py + ofdm_sync_pnac.py + ofdm_sync_pn.py packet_utils.py pkt.py psk.py - psk2.py qam.py qpsk.py DESTINATION ${GR_PYTHON_DIR}/gnuradio/digital @@ -66,6 +72,6 @@ foreach(py_qa_test_file ${py_qa_test_files}) ${CMAKE_BINARY_DIR}/gr-digital/swig ) set(GR_TEST_TARGET_DEPS gruel gnuradio-core gnuradio-digital) - GR_ADD_TEST(${py_qa_test_name} ${PYTHON_EXECUTABLE} ${py_qa_test_file}) + GR_ADD_TEST(${py_qa_test_name} ${PYTHON_EXECUTABLE} ${PYTHON_DASH_B} ${py_qa_test_file}) endforeach(py_qa_test_file) endif(ENABLE_TESTING) diff --git a/gr-digital/python/Makefile.am b/gr-digital/python/Makefile.am index cd98fe2d4..ead6f7dfd 100644 --- a/gr-digital/python/Makefile.am +++ b/gr-digital/python/Makefile.am @@ -44,7 +44,8 @@ noinst_PYTHON = \ qa_crc32.py \ qa_fll_band_edge.py \ qa_lms_equalizer.py \ - qa_mpsk_receiver.py + qa_mpsk_receiver.py \ + qa_ofdm_insert_preamble.py digital_PYTHON = \ __init__.py \ @@ -53,11 +54,17 @@ digital_PYTHON = \ crc.py \ generic_mod_demod.py \ gmsk.py \ - modulation_utils2.py \ + modulation_utils.py \ + ofdm.py \ + ofdm_packet_utils.py \ + ofdm_receiver.py \ + ofdm_sync_fixed.py \ + ofdm_sync_ml.py \ + ofdm_sync_pnac.py \ + ofdm_sync_pn.py \ packet_utils.py \ pkt.py \ psk.py \ - psk2.py \ qam.py \ qpsk.py endif diff --git a/gr-digital/python/__init__.py b/gr-digital/python/__init__.py index ca46dcfdb..7c76183c9 100644 --- a/gr-digital/python/__init__.py +++ b/gr-digital/python/__init__.py @@ -26,12 +26,21 @@ utilities, and examples for doing digital modulation and demodulation. # The presence of this file turns this directory into a Python package from digital_swig import * -from psk2 import * +from psk import * +from qam import * from bpsk import * from qpsk import * -from qam import * from gmsk import * +from cpm import * from pkt import * from crc import * -from packet_utils import * -from modulation_utils2 import * +from modulation_utils import * +from ofdm import * +from ofdm_receiver import * +from ofdm_sync_fixed import * +from ofdm_sync_ml import * +from ofdm_sync_pnac import * +from ofdm_sync_pn import * + +import packet_utils +import ofdm_packet_utils diff --git a/gr-digital/python/bpsk.py b/gr-digital/python/bpsk.py index 58a8289a5..0d8f05c4c 100644 --- a/gr-digital/python/bpsk.py +++ b/gr-digital/python/bpsk.py @@ -29,7 +29,7 @@ from cmath import exp from gnuradio import gr from gnuradio.digital.generic_mod_demod import generic_mod, generic_demod import digital_swig -import modulation_utils2 +import modulation_utils # Default number of points in constellation. _def_constellation_points = 2 @@ -163,9 +163,9 @@ class dbpsk_demod(generic_demod): # # Add these to the mod/demod registry # -modulation_utils2.add_type_1_mod('bpsk', bpsk_mod) -modulation_utils2.add_type_1_demod('bpsk', bpsk_demod) -modulation_utils2.add_type_1_constellation('bpsk', bpsk_constellation) -modulation_utils2.add_type_1_mod('dbpsk', dbpsk_mod) -modulation_utils2.add_type_1_demod('dbpsk', dbpsk_demod) -modulation_utils2.add_type_1_constellation('dbpsk', dbpsk_constellation) +modulation_utils.add_type_1_mod('bpsk', bpsk_mod) +modulation_utils.add_type_1_demod('bpsk', bpsk_demod) +modulation_utils.add_type_1_constellation('bpsk', bpsk_constellation) +modulation_utils.add_type_1_mod('dbpsk', dbpsk_mod) +modulation_utils.add_type_1_demod('dbpsk', dbpsk_demod) +modulation_utils.add_type_1_constellation('dbpsk', dbpsk_constellation) diff --git a/gr-digital/python/cpm.py b/gr-digital/python/cpm.py index 8f593cd51..05032336d 100644 --- a/gr-digital/python/cpm.py +++ b/gr-digital/python/cpm.py @@ -2,7 +2,7 @@ # CPM modulation and demodulation. # # -# Copyright 2005,2006,2007 Free Software Foundation, Inc. +# Copyright 2005-2007,2011 Free Software Foundation, Inc. # # This file is part of GNU Radio # @@ -24,12 +24,12 @@ # See gnuradio-examples/python/digital for examples -from gnuradio import gr -from gnuradio import modulation_utils +from gnuradio import gr, blks2 from math import pi import numpy -from pprint import pprint -import inspect + +import digital_swig +import modulation_utils # default values (used in __init__ and add_options) _def_samples_per_symbol = 2 @@ -97,7 +97,7 @@ class cpm_mod(gr.hier_block2): @type debug: bool """ - gr.hier_block2.__init__("cpm_mod", + gr.hier_block2.__init__(self, "cpm_mod", gr.io_signature(1, 1, gr.sizeof_char), # Input signature gr.io_signature(1, 1, gr.sizeof_gr_complex)) # Output signature @@ -116,14 +116,14 @@ class cpm_mod(gr.hier_block2): self._generic_taps=numpy.array(generic_taps) - if not isinstance(samples_per_symbol, int) or samples_per_symbol < 2: - raise TypeError, ("samples_per_symbol must be an integer >= 2, is %r" % (samples_per_symbol,)) + if samples_per_symbol < 2: + raise TypeError, ("samples_per_symbol must be >= 2, is %r" % (samples_per_symbol,)) self.nsymbols = 2**bits_per_symbol - self.sym_alphabet=numpy.arange(-(self.nsymbols-1),self.nsymbols,2) + self.sym_alphabet = numpy.arange(-(self.nsymbols-1),self.nsymbols,2).tolist() - self.ntaps = self._symbols_per_pulse * samples_per_symbol + self.ntaps = int(self._symbols_per_pulse * samples_per_symbol) sensitivity = 2 * pi * h_numerator / h_denominator / samples_per_symbol # Unpack Bytes into bits_per_symbol groups @@ -153,7 +153,7 @@ class cpm_mod(gr.hier_block2): else: raise TypeError, ("cpm_type must be an integer in {0,1,2,3}, is %r" % (cpm_type,)) - self.filter = gr.interp_fir_filter_fff(samples_per_symbol, self.taps) + self.filter = blks2.pfb_arb_resampler_fff(samples_per_symbol, self.taps) # FM modulation self.fmmod = gr.frequency_modulator_fc(sensitivity) @@ -167,26 +167,26 @@ class cpm_mod(gr.hier_block2): # Connect self.connect(self, self.B2s, self.pam, self.filter, self.fmmod, self) - #def samples_per_symbol(self): - #return self._samples_per_symbol + def samples_per_symbol(self): + return self._samples_per_symbol - #def bits_per_symbol(self): - #return self._bits_per_symbol + def bits_per_symbol(self): + return self._bits_per_symbol - #def h_numerator(self): - #return self._h_numerator + def h_numerator(self): + return self._h_numerator - #def h_denominator(self): - #return self._h_denominator + def h_denominator(self): + return self._h_denominator - #def cpm_type(self): - #return self._cpm_type + def cpm_type(self): + return self._cpm_type - #def bt(self): - #return self._bt + def bt(self): + return self._bt - #def symbols_per_pulse(self): - #return self._symbols_per_pulse + def symbols_per_pulse(self): + return self._symbols_per_pulse def _print_verbage(self): @@ -228,7 +228,7 @@ class cpm_mod(gr.hier_block2): Given command line options, create dictionary suitable for passing to __init__ """ return modulation_utils.extract_kwargs_from_options(cpm_mod.__init__, - ('self',), options) + ('self',), options) extract_kwargs_from_options=staticmethod(extract_kwargs_from_options) @@ -240,8 +240,6 @@ class cpm_mod(gr.hier_block2): # Not yet implemented # - - # # Add these to the mod/demod registry # diff --git a/gr-digital/python/generic_mod_demod.py b/gr-digital/python/generic_mod_demod.py index dec96e455..ae876e108 100644 --- a/gr-digital/python/generic_mod_demod.py +++ b/gr-digital/python/generic_mod_demod.py @@ -26,7 +26,7 @@ Generic modulation and demodulation. """ from gnuradio import gr -from modulation_utils2 import extract_kwargs_from_options_for_class +from modulation_utils import extract_kwargs_from_options_for_class from utils import mod_codes import digital_swig import math diff --git a/gr-digital/python/gmsk.py b/gr-digital/python/gmsk.py index c7a50f422..70fa197e3 100644 --- a/gr-digital/python/gmsk.py +++ b/gr-digital/python/gmsk.py @@ -26,7 +26,7 @@ from gnuradio import gr import digital_swig -import modulation_utils2 +import modulation_utils from math import pi import numpy @@ -151,7 +151,7 @@ class gmsk_mod(gr.hier_block2): """ Given command line options, create dictionary suitable for passing to __init__ """ - return modulation_utils2.extract_kwargs_from_options(gmsk_mod.__init__, + return modulation_utils.extract_kwargs_from_options(gmsk_mod.__init__, ('self',), options) extract_kwargs_from_options=staticmethod(extract_kwargs_from_options) @@ -265,12 +265,12 @@ class gmsk_demod(gr.hier_block2): """ Given command line options, create dictionary suitable for passing to __init__ """ - return modulation_utils2.extract_kwargs_from_options(gmsk_demod.__init__, + return modulation_utils.extract_kwargs_from_options(gmsk_demod.__init__, ('self',), options) extract_kwargs_from_options=staticmethod(extract_kwargs_from_options) # # Add these to the mod/demod registry # -modulation_utils2.add_type_1_mod('gmsk', gmsk_mod) -modulation_utils2.add_type_1_demod('gmsk', gmsk_demod) +modulation_utils.add_type_1_mod('gmsk', gmsk_mod) +modulation_utils.add_type_1_demod('gmsk', gmsk_demod) diff --git a/gr-digital/python/modulation_utils2.py b/gr-digital/python/modulation_utils.py index cb3a9812d..cb3a9812d 100644 --- a/gr-digital/python/modulation_utils2.py +++ b/gr-digital/python/modulation_utils.py diff --git a/gr-digital/python/ofdm.py b/gr-digital/python/ofdm.py index e05f074f4..9f57920ef 100644 --- a/gr-digital/python/ofdm.py +++ b/gr-digital/python/ofdm.py @@ -21,32 +21,13 @@ # import math -from gnuradio import gr, ofdm_packet_utils, modulation_utils2 +from gnuradio import gr +import digital_swig +import ofdm_packet_utils +from ofdm_receiver import ofdm_receiver import gnuradio.gr.gr_threading as _threading import psk, qam -from gnuradio.blks2impl.ofdm_receiver import ofdm_receiver - -def _add_common_options(normal, expert): - """ - Adds OFDM-specific options to the Options Parser that are common - both to the modulator and demodulator. - """ - mods_list = ", ".join(modulation_utils2.type_1_constellations().keys()) - print dir(modulation_utils2) - print "MODS LIST: ", mods_list - print modulation_utils2.type_1_mods() - normal.add_option("-m", "--modulation", type="string", default="psk", - help="set modulation type (" + mods_list + ") [default=%default]") - normal.add_option("-c", "--constellation-points", type="int", default=2, - help="set number of constellation points [default=%default]") - expert.add_option("", "--fft-length", type="intx", default=512, - help="set the number of FFT bins [default=%default]") - expert.add_option("", "--occupied-tones", type="intx", default=200, - help="set the number of occupied FFT bins [default=%default]") - expert.add_option("", "--cp-length", type="intx", default=128, - help="set the number of bits in the cyclic prefix [default=%default]") - # ///////////////////////////////////////////////////////////////////////////// # mod/demod with packets as i/o # ///////////////////////////////////////////////////////////////////////////// @@ -81,9 +62,6 @@ class ofdm_mod(gr.hier_block2): self._occupied_tones = options.occupied_tones self._cp_length = options.cp_length - print (options) - arity = options.constellation_points - win = [] #[1 for i in range(self._fft_length)] # Use freq domain to get doubled-up known symbol for correlation in time domain @@ -104,15 +82,31 @@ class ofdm_mod(gr.hier_block2): symbol_length = options.fft_length + options.cp_length - print modulation_utils2.type_1_constellations - const = modulation_utils2.type_1_constellations()[self._modulation](arity).points() - - self._pkt_input = gr.ofdm_mapper_bcv(const, msgq_limit, - options.occupied_tones, options.fft_length) + mods = {"bpsk": 2, "qpsk": 4, "8psk": 8, "qam8": 8, "qam16": 16, "qam64": 64, "qam256": 256} + arity = mods[self._modulation] - self.preambles = gr.ofdm_insert_preamble(self._fft_length, padded_preambles) + rot = 1 + if self._modulation == "qpsk": + rot = (0.707+0.707j) + + # FIXME: pass the constellation objects instead of just the points + if(self._modulation.find("psk") >= 0): + constel = psk.psk_constellation(arity) + rotated_const = map(lambda pt: pt * rot, constel.points()) + elif(self._modulation.find("qam") >= 0): + constel = qam.qam_constellation(arity) + rotated_const = map(lambda pt: pt * rot, constel.points()) + #print rotated_const + self._pkt_input = digital_swig.ofdm_mapper_bcv(rotated_const, + msgq_limit, + options.occupied_tones, + options.fft_length) + + self.preambles = digital_swig.ofdm_insert_preamble(self._fft_length, + padded_preambles) self.ifft = gr.fft_vcc(self._fft_length, False, win, True) - self.cp_adder = gr.ofdm_cyclic_prefixer(self._fft_length, symbol_length) + self.cp_adder = digital_swig.ofdm_cyclic_prefixer(self._fft_length, + symbol_length) self.scale = gr.multiply_const_cc(1.0 / math.sqrt(self._fft_length)) self.connect((self._pkt_input, 0), (self.preambles, 0)) @@ -143,7 +137,9 @@ class ofdm_mod(gr.hier_block2): msg = gr.message(1) # tell self._pkt_input we're not sending any more packets else: # print "original_payload =", string_to_hex_list(payload) - pkt = ofdm_packet_utils.make_packet(payload, 1, 1, self._pad_for_usrp, whitening=True) + pkt = ofdm_packet_utils.make_packet(payload, 1, 1, + self._pad_for_usrp, + whitening=True) #print "pkt =", string_to_hex_list(pkt) msg = gr.message_from_string(pkt) @@ -153,10 +149,14 @@ class ofdm_mod(gr.hier_block2): """ Adds OFDM-specific options to the Options Parser """ - _add_common_options(normal, expert) - for mod in modulation_utils2.type_1_mods().values(): - mod.add_options(expert) - + normal.add_option("-m", "--modulation", type="string", default="bpsk", + help="set modulation type (bpsk, qpsk, 8psk, qam{16,64}) [default=%default]") + expert.add_option("", "--fft-length", type="intx", default=512, + help="set the number of FFT bins [default=%default]") + expert.add_option("", "--occupied-tones", type="intx", default=200, + help="set the number of occupied FFT bins [default=%default]") + expert.add_option("", "--cp-length", type="intx", default=128, + help="set the number of bits in the cyclic prefix [default=%default]") # Make a static method to call before instantiation add_options = staticmethod(add_options) @@ -205,9 +205,6 @@ class ofdm_demod(gr.hier_block2): self._cp_length = options.cp_length self._snr = options.snr - arity = options.constellation_points - print("con points is %s" % options.constellation_points) - # Use freq domain to get doubled-up known symbol for correlation in time domain zeros_on_left = int(math.ceil((self._fft_length - self._occupied_tones)/2.0)) ksfreq = known_symbols_4512_3[0:self._occupied_tones] @@ -217,20 +214,36 @@ class ofdm_demod(gr.hier_block2): # hard-coded known symbols preambles = (ksfreq,) - + symbol_length = self._fft_length + self._cp_length - self.ofdm_recv = ofdm_receiver(self._fft_length, self._cp_length, - self._occupied_tones, self._snr, preambles, + self.ofdm_recv = ofdm_receiver(self._fft_length, + self._cp_length, + self._occupied_tones, + self._snr, preambles, options.log) - constell = modulation_utils2.type_1_constellations()[self._modulation](arity) + mods = {"bpsk": 2, "qpsk": 4, "8psk": 8, "qam8": 8, "qam16": 16, "qam64": 64, "qam256": 256} + arity = mods[self._modulation] + + rot = 1 + if self._modulation == "qpsk": + rot = (0.707+0.707j) + + # FIXME: pass the constellation objects instead of just the points + if(self._modulation.find("psk") >= 0): + constel = psk.psk_constellation(arity) + rotated_const = map(lambda pt: pt * rot, constel.points()) + elif(self._modulation.find("qam") >= 0): + constel = qam.qam_constellation(arity) + rotated_const = map(lambda pt: pt * rot, constel.points()) + #print rotated_const phgain = 0.25 frgain = phgain*phgain / 4.0 - self.ofdm_demod = gr.ofdm_frame_sink2(constell.base(), - self._rcvd_pktq, - self._occupied_tones, - phgain, frgain) + self.ofdm_demod = digital_swig.ofdm_frame_sink(rotated_const, range(arity), + self._rcvd_pktq, + self._occupied_tones, + phgain, frgain) self.connect(self, self.ofdm_recv) self.connect((self.ofdm_recv, 0), (self.ofdm_demod, 0)) @@ -241,9 +254,12 @@ class ofdm_demod(gr.hier_block2): self.connect(self.ofdm_recv.chan_filt, self) if options.log: - self.connect(self.ofdm_demod, gr.file_sink(gr.sizeof_gr_complex*self._occupied_tones, "ofdm_frame_sink_c.dat")) + self.connect(self.ofdm_demod, + gr.file_sink(gr.sizeof_gr_complex*self._occupied_tones, + "ofdm_frame_sink_c.dat")) else: - self.connect(self.ofdm_demod, gr.null_sink(gr.sizeof_gr_complex*self._occupied_tones)) + self.connect(self.ofdm_demod, + gr.null_sink(gr.sizeof_gr_complex*self._occupied_tones)) if options.verbose: self._print_verbage() @@ -254,9 +270,16 @@ class ofdm_demod(gr.hier_block2): """ Adds OFDM-specific options to the Options Parser """ - _add_common_options(normal, expert) - for mod in modulation_utils2.type_1_mods().values(): - mod.add_options(expert) + normal.add_option("-m", "--modulation", type="string", default="bpsk", + help="set modulation type (bpsk or qpsk) [default=%default]") + expert.add_option("", "--fft-length", type="intx", default=512, + help="set the number of FFT bins [default=%default]") + expert.add_option("", "--occupied-tones", type="intx", default=200, + help="set the number of occupied FFT bins [default=%default]") + expert.add_option("", "--cp-length", type="intx", default=128, + help="set the number of bits in the cyclic prefix [default=%default]") + expert.add_option("", "--snr", type="float", default=30.0, + help="SNR estimate [default=%default]") # Make a static method to call before instantiation add_options = staticmethod(add_options) diff --git a/gr-digital/python/ofdm_packet_utils.py b/gr-digital/python/ofdm_packet_utils.py new file mode 100644 index 000000000..d0000e6db --- /dev/null +++ b/gr-digital/python/ofdm_packet_utils.py @@ -0,0 +1,454 @@ +# +# Copyright 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. +# + +import struct +import numpy +from gnuradio import gru +import crc + +def conv_packed_binary_string_to_1_0_string(s): + """ + '\xAF' --> '10101111' + """ + r = [] + for ch in s: + x = ord(ch) + for i in range(7,-1,-1): + t = (x >> i) & 0x1 + r.append(t) + + return ''.join(map(lambda x: chr(x + ord('0')), r)) + +def conv_1_0_string_to_packed_binary_string(s): + """ + '10101111' -> ('\xAF', False) + + Basically the inverse of conv_packed_binary_string_to_1_0_string, + but also returns a flag indicating if we had to pad with leading zeros + to get to a multiple of 8. + """ + if not is_1_0_string(s): + raise ValueError, "Input must be a string containing only 0's and 1's" + + # pad to multiple of 8 + padded = False + rem = len(s) % 8 + if rem != 0: + npad = 8 - rem + s = '0' * npad + s + padded = True + + assert len(s) % 8 == 0 + + r = [] + i = 0 + while i < len(s): + t = 0 + for j in range(8): + t = (t << 1) | (ord(s[i + j]) - ord('0')) + r.append(chr(t)) + i += 8 + return (''.join(r), padded) + + +def is_1_0_string(s): + if not isinstance(s, str): + return False + for ch in s: + if not ch in ('0', '1'): + return False + return True + +def string_to_hex_list(s): + return map(lambda x: hex(ord(x)), s) + + +def whiten(s, o): + sa = numpy.fromstring(s, numpy.uint8) + z = sa ^ random_mask_vec8[o:len(sa)+o] + return z.tostring() + +def dewhiten(s, o): + return whiten(s, o) # self inverse + + +def make_header(payload_len, whitener_offset=0): + # Upper nibble is offset, lower 12 bits is len + val = ((whitener_offset & 0xf) << 12) | (payload_len & 0x0fff) + #print "offset =", whitener_offset, " len =", payload_len, " val=", val + return struct.pack('!HH', val, val) + +def make_packet(payload, samples_per_symbol, bits_per_symbol, + pad_for_usrp=True, whitener_offset=0, whitening=True): + """ + Build a packet, given access code, payload, and whitener offset + + @param payload: packet payload, len [0, 4096] + @param samples_per_symbol: samples per symbol (needed for padding calculation) + @type samples_per_symbol: int + @param bits_per_symbol: (needed for padding calculation) + @type bits_per_symbol: int + @param whitener_offset offset into whitener string to use [0-16) + @param whitening: Turn whitener on or off + @type whitening: bool + + Packet will have access code at the beginning, followed by length, payload + and finally CRC-32. + """ + + if not whitener_offset >=0 and whitener_offset < 16: + raise ValueError, "whitener_offset must be between 0 and 15, inclusive (%i)" % (whitener_offset,) + + payload_with_crc = crc.gen_and_append_crc32(payload) + #print "outbound crc =", string_to_hex_list(payload_with_crc[-4:]) + + L = len(payload_with_crc) + MAXLEN = len(random_mask_tuple) + if L > MAXLEN: + raise ValueError, "len(payload) must be in [0, %d]" % (MAXLEN,) + + pkt_hd = make_header(L, whitener_offset) + pkt_dt = ''.join((payload_with_crc, '\x55')) + packet_length = len(pkt_hd) + len(pkt_dt) + + if pad_for_usrp: + usrp_packing = _npadding_bytes(packet_length, samples_per_symbol, bits_per_symbol) * '\x55' + pkt_dt = pkt_dt + usrp_packing + + if(whitening): + pkt = pkt_hd + whiten(pkt_dt, whitener_offset) + else: + pkt = pkt_hd + pkt_dt + + #print "make_packet: len(pkt) =", len(pkt) + + return pkt + +def _npadding_bytes(pkt_byte_len, samples_per_symbol, bits_per_symbol): + """ + Generate sufficient padding such that each packet ultimately ends + up being a multiple of 512 bytes when sent across the USB. We + send 4-byte samples across the USB (16-bit I and 16-bit Q), thus + we want to pad so that after modulation the resulting packet + is a multiple of 128 samples. + + @param ptk_byte_len: len in bytes of packet, not including padding. + @param samples_per_symbol: samples per bit (1 bit / symbolwidth GMSK) + @type samples_per_symbol: int + @param bits_per_symbol: bits per symbol (log2(modulation order)) + @type bits_per_symbol: int + + @returns number of bytes of padding to append. + """ + modulus = 128 + byte_modulus = gru.lcm(modulus/8, samples_per_symbol) * bits_per_symbol / samples_per_symbol + r = pkt_byte_len % byte_modulus + if r == 0: + return 0 + return byte_modulus - r + + +def unmake_packet(whitened_payload_with_crc, whitener_offset=0, dewhitening=1): + """ + Return (ok, payload) + + @param whitened_payload_with_crc: string + @param whitener_offset offset into whitener string to use [0-16) + @param dewhitening: Turn whitener on or off + @type dewhitening: bool + """ + + if dewhitening: + payload_with_crc = dewhiten(whitened_payload_with_crc, whitener_offset) + else: + payload_with_crc = whitened_payload_with_crc + + ok, payload = crc.check_crc32(payload_with_crc) + + if 0: + print "payload_with_crc =", string_to_hex_list(payload_with_crc) + print "ok = %r, len(payload) = %d" % (ok, len(payload)) + print "payload =", string_to_hex_list(payload) + + return ok, payload + + +# FYI, this PN code is the output of a 15-bit LFSR +random_mask_tuple = ( + 255, 63, 0, 16, 0, 12, 0, 5, 192, 3, 16, 1, 204, 0, 85, 192, + 63, 16, 16, 12, 12, 5, 197, 195, 19, 17, 205, 204, 85, 149, 255, 47, + 0, 28, 0, 9, 192, 6, 208, 2, 220, 1, 153, 192, 106, 208, 47, 28, + 28, 9, 201, 198, 214, 210, 222, 221, 152, 89, 170, 186, 255, 51, 0, 21, + 192, 15, 16, 4, 12, 3, 69, 193, 243, 16, 69, 204, 51, 21, 213, 207, + 31, 20, 8, 15, 70, 132, 50, 227, 85, 137, 255, 38, 192, 26, 208, 11, + 28, 7, 73, 194, 182, 209, 182, 220, 118, 217, 230, 218, 202, 219, 23, 27, + 78, 139, 116, 103, 103, 106, 170, 175, 63, 60, 16, 17, 204, 12, 85, 197, + 255, 19, 0, 13, 192, 5, 144, 3, 44, 1, 221, 192, 89, 144, 58, 236, + 19, 13, 205, 197, 149, 147, 47, 45, 220, 29, 153, 201, 170, 214, 255, 30, + 192, 8, 80, 6, 188, 2, 241, 193, 132, 80, 99, 124, 41, 225, 222, 200, + 88, 86, 186, 190, 243, 48, 69, 212, 51, 31, 85, 200, 63, 22, 144, 14, + 236, 4, 77, 195, 117, 145, 231, 44, 74, 157, 247, 41, 134, 158, 226, 232, + 73, 142, 182, 228, 118, 203, 102, 215, 106, 222, 175, 24, 124, 10, 161, 199, + 56, 82, 146, 189, 173, 177, 189, 180, 113, 183, 100, 118, 171, 102, 255, 106, + 192, 47, 16, 28, 12, 9, 197, 198, 211, 18, 221, 205, 153, 149, 170, 239, + 63, 12, 16, 5, 204, 3, 21, 193, 207, 16, 84, 12, 63, 69, 208, 51, + 28, 21, 201, 207, 22, 212, 14, 223, 68, 88, 51, 122, 149, 227, 47, 9, + 220, 6, 217, 194, 218, 209, 155, 28, 107, 73, 239, 118, 204, 38, 213, 218, + 223, 27, 24, 11, 74, 135, 119, 34, 166, 153, 186, 234, 243, 15, 5, 196, + 3, 19, 65, 205, 240, 85, 132, 63, 35, 80, 25, 252, 10, 193, 199, 16, + 82, 140, 61, 165, 209, 187, 28, 115, 73, 229, 246, 203, 6, 215, 66, 222, + 177, 152, 116, 106, 167, 111, 58, 172, 19, 61, 205, 209, 149, 156, 111, 41, + 236, 30, 205, 200, 85, 150, 191, 46, 240, 28, 68, 9, 243, 70, 197, 242, + 211, 5, 157, 195, 41, 145, 222, 236, 88, 77, 250, 181, 131, 55, 33, 214, + 152, 94, 234, 184, 79, 50, 180, 21, 183, 79, 54, 180, 22, 247, 78, 198, + 180, 82, 247, 125, 134, 161, 162, 248, 121, 130, 162, 225, 185, 136, 114, 230, + 165, 138, 251, 39, 3, 90, 129, 251, 32, 67, 88, 49, 250, 148, 67, 47, + 113, 220, 36, 89, 219, 122, 219, 99, 27, 105, 203, 110, 215, 108, 94, 173, + 248, 125, 130, 161, 161, 184, 120, 114, 162, 165, 185, 187, 50, 243, 85, 133, + 255, 35, 0, 25, 192, 10, 208, 7, 28, 2, 137, 193, 166, 208, 122, 220, + 35, 25, 217, 202, 218, 215, 27, 30, 139, 72, 103, 118, 170, 166, 255, 58, + 192, 19, 16, 13, 204, 5, 149, 195, 47, 17, 220, 12, 89, 197, 250, 211, + 3, 29, 193, 201, 144, 86, 236, 62, 205, 208, 85, 156, 63, 41, 208, 30, + 220, 8, 89, 198, 186, 210, 243, 29, 133, 201, 163, 22, 249, 206, 194, 212, + 81, 159, 124, 104, 33, 238, 152, 76, 106, 181, 239, 55, 12, 22, 133, 206, + 227, 20, 73, 207, 118, 212, 38, 223, 90, 216, 59, 26, 147, 75, 45, 247, + 93, 134, 185, 162, 242, 249, 133, 130, 227, 33, 137, 216, 102, 218, 170, 219, + 63, 27, 80, 11, 124, 7, 97, 194, 168, 81, 190, 188, 112, 113, 228, 36, + 75, 91, 119, 123, 102, 163, 106, 249, 239, 2, 204, 1, 149, 192, 111, 16, + 44, 12, 29, 197, 201, 147, 22, 237, 206, 205, 148, 85, 175, 127, 60, 32, + 17, 216, 12, 90, 133, 251, 35, 3, 89, 193, 250, 208, 67, 28, 49, 201, + 212, 86, 223, 126, 216, 32, 90, 152, 59, 42, 147, 95, 45, 248, 29, 130, + 137, 161, 166, 248, 122, 194, 163, 17, 185, 204, 114, 213, 229, 159, 11, 40, + 7, 94, 130, 184, 97, 178, 168, 117, 190, 167, 48, 122, 148, 35, 47, 89, + 220, 58, 217, 211, 26, 221, 203, 25, 151, 74, 238, 183, 12, 118, 133, 230, + 227, 10, 201, 199, 22, 210, 142, 221, 164, 89, 187, 122, 243, 99, 5, 233, + 195, 14, 209, 196, 92, 83, 121, 253, 226, 193, 137, 144, 102, 236, 42, 205, + 223, 21, 152, 15, 42, 132, 31, 35, 72, 25, 246, 138, 198, 231, 18, 202, + 141, 151, 37, 174, 155, 60, 107, 81, 239, 124, 76, 33, 245, 216, 71, 26, + 178, 139, 53, 167, 87, 58, 190, 147, 48, 109, 212, 45, 159, 93, 168, 57, + 190, 146, 240, 109, 132, 45, 163, 93, 185, 249, 178, 194, 245, 145, 135, 44, + 98, 157, 233, 169, 142, 254, 228, 64, 75, 112, 55, 100, 22, 171, 78, 255, + 116, 64, 39, 112, 26, 164, 11, 59, 71, 83, 114, 189, 229, 177, 139, 52, + 103, 87, 106, 190, 175, 48, 124, 20, 33, 207, 88, 84, 58, 191, 83, 48, + 61, 212, 17, 159, 76, 104, 53, 238, 151, 12, 110, 133, 236, 99, 13, 233, + 197, 142, 211, 36, 93, 219, 121, 155, 98, 235, 105, 143, 110, 228, 44, 75, + 93, 247, 121, 134, 162, 226, 249, 137, 130, 230, 225, 138, 200, 103, 22, 170, + 142, 255, 36, 64, 27, 112, 11, 100, 7, 107, 66, 175, 113, 188, 36, 113, + 219, 100, 91, 107, 123, 111, 99, 108, 41, 237, 222, 205, 152, 85, 170, 191, + 63, 48, 16, 20, 12, 15, 69, 196, 51, 19, 85, 205, 255, 21, 128, 15, + 32, 4, 24, 3, 74, 129, 247, 32, 70, 152, 50, 234, 149, 143, 47, 36, + 28, 27, 73, 203, 118, 215, 102, 222, 170, 216, 127, 26, 160, 11, 56, 7, + 82, 130, 189, 161, 177, 184, 116, 114, 167, 101, 186, 171, 51, 63, 85, 208, + 63, 28, 16, 9, 204, 6, 213, 194, 223, 17, 152, 12, 106, 133, 239, 35, + 12, 25, 197, 202, 211, 23, 29, 206, 137, 148, 102, 239, 106, 204, 47, 21, + 220, 15, 25, 196, 10, 211, 71, 29, 242, 137, 133, 166, 227, 58, 201, 211, + 22, 221, 206, 217, 148, 90, 239, 123, 12, 35, 69, 217, 243, 26, 197, 203, + 19, 23, 77, 206, 181, 148, 119, 47, 102, 156, 42, 233, 223, 14, 216, 4, + 90, 131, 123, 33, 227, 88, 73, 250, 182, 195, 54, 209, 214, 220, 94, 217, + 248, 90, 194, 187, 17, 179, 76, 117, 245, 231, 7, 10, 130, 135, 33, 162, + 152, 121, 170, 162, 255, 57, 128, 18, 224, 13, 136, 5, 166, 131, 58, 225, + 211, 8, 93, 198, 185, 146, 242, 237, 133, 141, 163, 37, 185, 219, 50, 219, + 85, 155, 127, 43, 96, 31, 104, 8, 46, 134, 156, 98, 233, 233, 142, 206, + 228, 84, 75, 127, 119, 96, 38, 168, 26, 254, 139, 0, 103, 64, 42, 176, + 31, 52, 8, 23, 70, 142, 178, 228, 117, 139, 103, 39, 106, 154, 175, 43, + 60, 31, 81, 200, 60, 86, 145, 254, 236, 64, 77, 240, 53, 132, 23, 35, + 78, 153, 244, 106, 199, 111, 18, 172, 13, 189, 197, 177, 147, 52, 109, 215, + 109, 158, 173, 168, 125, 190, 161, 176, 120, 116, 34, 167, 89, 186, 186, 243, + 51, 5, 213, 195, 31, 17, 200, 12, 86, 133, 254, 227, 0, 73, 192, 54, + 208, 22, 220, 14, 217, 196, 90, 211, 123, 29, 227, 73, 137, 246, 230, 198, + 202, 210, 215, 29, 158, 137, 168, 102, 254, 170, 192, 127, 16, 32, 12, 24, + 5, 202, 131, 23, 33, 206, 152, 84, 106, 191, 111, 48, 44, 20, 29, 207, + 73, 148, 54, 239, 86, 204, 62, 213, 208, 95, 28, 56, 9, 210, 134, 221, + 162, 217, 185, 154, 242, 235, 5, 143, 67, 36, 49, 219, 84, 91, 127, 123, + 96, 35, 104, 25, 238, 138, 204, 103, 21, 234, 143, 15, 36, 4, 27, 67, + 75, 113, 247, 100, 70, 171, 114, 255, 101, 128, 43, 32, 31, 88, 8, 58, + 134, 147, 34, 237, 217, 141, 154, 229, 171, 11, 63, 71, 80, 50, 188, 21, + 177, 207, 52, 84, 23, 127, 78, 160, 52, 120, 23, 98, 142, 169, 164, 126, + 251, 96, 67, 104, 49, 238, 148, 76, 111, 117, 236, 39, 13, 218, 133, 155, + 35, 43, 89, 223, 122, 216, 35, 26, 153, 203, 42, 215, 95, 30, 184, 8, + 114, 134, 165, 162, 251, 57, 131, 82, 225, 253, 136, 65, 166, 176, 122, 244, + 35, 7, 89, 194, 186, 209, 179, 28, 117, 201, 231, 22, 202, 142, 215, 36, + 94, 155, 120, 107, 98, 175, 105, 188, 46, 241, 220, 68, 89, 243, 122, 197, + 227, 19, 9, 205, 198, 213, 146, 223, 45, 152, 29, 170, 137, 191, 38, 240, + 26, 196, 11, 19, 71, 77, 242, 181, 133, 183, 35, 54, 153, 214, 234, 222, + 207, 24, 84, 10, 191, 71, 48, 50, 148, 21, 175, 79, 60, 52, 17, 215, + 76, 94, 181, 248, 119, 2, 166, 129, 186, 224, 115, 8, 37, 198, 155, 18, + 235, 77, 143, 117, 164, 39, 59, 90, 147, 123, 45, 227, 93, 137, 249, 166, + 194, 250, 209, 131, 28, 97, 201, 232, 86, 206, 190, 212, 112, 95, 100, 56, + 43, 82, 159, 125, 168, 33, 190, 152, 112, 106, 164, 47, 59, 92, 19, 121, + 205, 226, 213, 137, 159, 38, 232, 26, 206, 139, 20, 103, 79, 106, 180, 47, + 55, 92, 22, 185, 206, 242, 212, 69, 159, 115, 40, 37, 222, 155, 24, 107, + 74, 175, 119, 60, 38, 145, 218, 236, 91, 13, 251, 69, 131, 115, 33, 229, + 216, 75, 26, 183, 75, 54, 183, 86, 246, 190, 198, 240, 82, 196, 61, 147, + 81, 173, 252, 125, 129, 225, 160, 72, 120, 54, 162, 150, 249, 174, 194, 252, + 81, 129, 252, 96, 65, 232, 48, 78, 148, 52, 111, 87, 108, 62, 173, 208, + 125, 156, 33, 169, 216, 126, 218, 160, 91, 56, 59, 82, 147, 125, 173, 225, + 189, 136, 113, 166, 164, 122, 251, 99, 3, 105, 193, 238, 208, 76, 92, 53, + 249, 215, 2, 222, 129, 152, 96, 106, 168, 47, 62, 156, 16, 105, 204, 46, + 213, 220, 95, 25, 248, 10, 194, 135, 17, 162, 140, 121, 165, 226, 251, 9, + 131, 70, 225, 242, 200, 69, 150, 179, 46, 245, 220, 71, 25, 242, 138, 197, + 167, 19, 58, 141, 211, 37, 157, 219, 41, 155, 94, 235, 120, 79, 98, 180, + 41, 183, 94, 246, 184, 70, 242, 178, 197, 181, 147, 55, 45, 214, 157, 158, + 233, 168, 78, 254, 180, 64, 119, 112, 38, 164, 26, 251, 75, 3, 119, 65, + 230, 176, 74, 244, 55, 7, 86, 130, 190, 225, 176, 72, 116, 54, 167, 86, + 250, 190, 195, 48, 81, 212, 60, 95, 81, 248, 60, 66, 145, 241, 172, 68, + 125, 243, 97, 133, 232, 99, 14, 169, 196, 126, 211, 96, 93, 232, 57, 142, + 146, 228, 109, 139, 109, 167, 109, 186, 173, 179, 61, 181, 209, 183, 28, 118, + 137, 230, 230, 202, 202, 215, 23, 30, 142, 136, 100, 102, 171, 106, 255, 111, + 0, 44, 0, 29, 192, 9, 144, 6, 236, 2, 205, 193, 149, 144, 111, 44, + 44, 29, 221, 201, 153, 150, 234, 238, 207, 12, 84, 5, 255, 67, 0, 49, + 192, 20, 80, 15, 124, 4, 33, 195, 88, 81, 250, 188, 67, 49, 241, 212, + 68, 95, 115, 120, 37, 226, 155, 9, 171, 70, 255, 114, 192, 37, 144, 27, + 44, 11, 93, 199, 121, 146, 162, 237, 185, 141, 178, 229, 181, 139, 55, 39, + 86, 154, 190, 235, 48, 79, 84, 52, 63, 87, 80, 62, 188, 16, 113, 204, + 36, 85, 219, 127, 27, 96, 11, 104, 7, 110, 130, 172, 97, 189, 232, 113, + 142, 164, 100, 123, 107, 99, 111, 105, 236, 46, 205, 220, 85, 153, 255, 42, + 192, 31, 16, 8, 12, 6, 133, 194, 227, 17, 137, 204, 102, 213, 234, 223, + 15, 24, 4, 10, 131, 71, 33, 242, 152, 69, 170, 179, 63, 53, 208, 23, + 28, 14, 137, 196, 102, 211, 106, 221, 239, 25, 140, 10, 229, 199, 11, 18, + 135, 77, 162, 181, 185, 183, 50, 246, 149, 134, 239, 34, 204, 25, 149, 202, + 239, 23, 12, 14, 133, 196, 99, 19, 105, 205, 238, 213, 140, 95, 37, 248, + 27, 2, 139, 65, 167, 112, 122, 164, 35, 59, 89, 211, 122, 221, 227, 25, + 137, 202, 230, 215, 10, 222, 135, 24, 98, 138, 169, 167, 62, 250, 144, 67, + 44, 49, 221, 212, 89, 159, 122, 232, 35, 14, 153, 196, 106, 211, 111, 29, + 236, 9, 141, 198, 229, 146, 203, 45, 151, 93, 174, 185, 188, 114, 241, 229, + 132, 75, 35, 119, 89, 230, 186, 202, 243, 23, 5, 206, 131, 20, 97, 207, + 104, 84, 46, 191, 92, 112, 57, 228, 18, 203, 77, 151, 117, 174, 167, 60, + 122, 145, 227, 44, 73, 221, 246, 217, 134, 218, 226, 219, 9, 155, 70, 235, + 114, 207, 101, 148, 43, 47, 95, 92, 56, 57, 210, 146, 221, 173, 153, 189, + 170, 241, 191, 4, 112, 3, 100, 1, 235, 64, 79, 112, 52, 36, 23, 91, + 78, 187, 116, 115, 103, 101, 234, 171, 15, 63, 68, 16, 51, 76, 21, 245, + 207, 7, 20, 2, 143, 65, 164, 48, 123, 84, 35, 127, 89, 224, 58, 200, + 19, 22, 141, 206, 229, 148, 75, 47, 119, 92, 38, 185, 218, 242, 219, 5, + 155, 67, 43, 113, 223, 100, 88, 43, 122, 159, 99, 40, 41, 222, 158, 216, + 104, 90, 174, 187, 60, 115, 81, 229, 252, 75, 1, 247, 64, 70, 176, 50, + 244, 21, 135, 79, 34, 180, 25, 183, 74, 246, 183, 6, 246, 130, 198, 225, + 146, 200, 109, 150, 173, 174, 253, 188, 65, 177, 240, 116, 68, 39, 115, 90, + 165, 251, 59, 3, 83, 65, 253, 240, 65, 132, 48, 99, 84, 41, 255, 94, + 192, 56, 80, 18, 188, 13, 177, 197, 180, 83, 55, 125, 214, 161, 158, 248, + 104, 66, 174, 177, 188, 116, 113, 231, 100, 74, 171, 119, 63, 102, 144, 42, + 236, 31, 13, 200, 5, 150, 131, 46, 225, 220, 72, 89, 246, 186, 198, 243, + 18, 197, 205, 147, 21, 173, 207, 61, 148, 17, 175, 76, 124, 53, 225, 215, + 8, 94, 134, 184, 98, 242, 169, 133, 190, 227, 48, 73, 212, 54, 223, 86, + 216, 62, 218, 144, 91, 44, 59, 93, 211, 121, 157, 226, 233, 137, 142, 230, + 228, 74, 203, 119, 23, 102, 142, 170, 228, 127, 11, 96, 7, 104, 2, 174, + 129, 188, 96, 113, 232, 36, 78, 155, 116, 107, 103, 111, 106, 172, 47, 61, + 220, 17, 153, 204, 106, 213, 239, 31, 12, 8, 5, 198, 131, 18, 225, 205, + 136, 85, 166, 191, 58, 240, 19, 4, 13, 195, 69, 145, 243, 44, 69, 221, + 243, 25, 133, 202, 227, 23, 9, 206, 134, 212, 98, 223, 105, 152, 46, 234, + 156, 79, 41, 244, 30, 199, 72, 82, 182, 189, 182, 241, 182, 196, 118, 211, + 102, 221, 234, 217, 143, 26, 228, 11, 11, 71, 71, 114, 178, 165, 181, 187, + 55, 51, 86, 149, 254, 239, 0, 76, 0, 53, 192, 23, 16, 14, 140, 4, + 101, 195, 107, 17, 239, 76, 76, 53, 245, 215, 7, 30, 130, 136, 97, 166, + 168, 122, 254, 163, 0, 121, 192, 34, 208, 25, 156, 10, 233, 199, 14, 210, + 132, 93, 163, 121, 185, 226, 242, 201, 133, 150, 227, 46, 201, 220, 86, 217, + 254, 218, 192, 91, 16, 59, 76, 19, 117, 205, 231, 21, 138, 143, 39, 36, + 26, 155, 75, 43, 119, 95, 102, 184, 42, 242, 159, 5, 168, 3, 62, 129, + 208, 96, 92, 40, 57, 222, 146, 216, 109, 154, 173, 171, 61, 191, 81, 176, + 60, 116, 17, 231, 76, 74, 181, 247, 55, 6, 150, 130, 238, 225, 140, 72, + 101, 246, 171, 6, 255, 66, 192, 49, 144, 20, 108, 15, 109, 196, 45, 147, + 93, 173, 249, 189, 130, 241, 161, 132, 120, 99, 98, 169, 233, 190, 206, 240, + 84, 68, 63, 115, 80, 37, 252, 27, 1, 203, 64, 87, 112, 62, 164, 16, + 123, 76, 35, 117, 217, 231, 26, 202, 139, 23, 39, 78, 154, 180, 107, 55, + 111, 86, 172, 62, 253, 208, 65, 156, 48, 105, 212, 46, 223, 92, 88, 57, + 250, 146, 195, 45, 145, 221, 172, 89, 189, 250, 241, 131, 4, 97, 195, 104, + 81, 238, 188, 76, 113, 245, 228, 71, 11, 114, 135, 101, 162, 171, 57, 191, + 82, 240, 61, 132, 17, 163, 76, 121, 245, 226, 199, 9, 146, 134, 237, 162, + 205, 185, 149, 178, 239, 53, 140, 23, 37, 206, 155, 20, 107, 79, 111, 116, + 44, 39, 93, 218, 185, 155, 50, 235, 85, 143, 127, 36, 32, 27, 88, 11, + 122, 135, 99, 34, 169, 217, 190, 218, 240, 91, 4, 59, 67, 83, 113, 253, + 228, 65, 139, 112, 103, 100, 42, 171, 95, 63, 120, 16, 34, 140, 25, 165, + 202, 251, 23, 3, 78, 129, 244, 96, 71, 104, 50, 174, 149, 188, 111, 49, + 236, 20, 77, 207, 117, 148, 39, 47, 90, 156, 59, 41, 211, 94, 221, 248, + 89, 130, 186, 225, 179, 8, 117, 198, 167, 18, 250, 141, 131, 37, 161, 219, + 56, 91, 82, 187, 125, 179, 97, 181, 232, 119, 14, 166, 132, 122, 227, 99, + 9, 233, 198, 206, 210, 212, 93, 159, 121, 168, 34, 254, 153, 128, 106, 224, + 47, 8, 28, 6, 137, 194, 230, 209, 138, 220, 103, 25, 234, 138, 207, 39, + 20, 26, 143, 75, 36, 55, 91, 86, 187, 126, 243, 96, 69, 232, 51, 14, + 149, 196, 111, 19, 108, 13, 237, 197, 141, 147, 37, 173, 219, 61, 155, 81, + 171, 124, 127, 97, 224, 40, 72, 30, 182, 136, 118, 230, 166, 202, 250, 215, + 3, 30, 129, 200, 96, 86, 168, 62, 254, 144, 64, 108, 48, 45, 212, 29, + 159, 73, 168, 54, 254, 150, 192, 110, 208, 44, 92, 29, 249, 201, 130, 214, + 225, 158, 200, 104, 86, 174, 190, 252, 112, 65, 228, 48, 75, 84, 55, 127, + 86, 160, 62, 248, 16, 66, 140, 49, 165, 212, 123, 31, 99, 72, 41, 246, + 158, 198, 232, 82, 206, 189, 148, 113, 175, 100, 124, 43, 97, 223, 104, 88, + 46, 186, 156, 115, 41, 229, 222, 203, 24, 87, 74, 190, 183, 48, 118, 148, + 38, 239, 90, 204, 59, 21, 211, 79, 29, 244, 9, 135, 70, 226, 178, 201, + 181, 150, 247, 46, 198, 156, 82, 233, 253, 142, 193, 164, 80, 123, 124, 35, + 97, 217, 232, 90, 206, 187, 20, 115, 79, 101, 244, 43, 7, 95, 66, 184, + 49, 178, 148, 117, 175, 103, 60, 42, 145, 223, 44, 88, 29, 250, 137, 131, + 38, 225, 218, 200, 91, 22, 187, 78, 243, 116, 69, 231, 115, 10, 165, 199, + 59, 18, 147, 77, 173, 245, 189, 135, 49, 162, 148, 121, 175, 98, 252, 41, + 129, 222, 224, 88, 72, 58, 182, 147, 54, 237, 214, 205, 158, 213, 168, 95, + 62, 184, 16, 114, 140, 37, 165, 219, 59, 27, 83, 75, 125, 247, 97, 134, + 168, 98, 254, 169, 128, 126, 224, 32, 72, 24, 54, 138, 150, 231, 46, 202, + 156, 87, 41, 254, 158, 192, 104, 80, 46, 188, 28, 113, 201, 228, 86, 203, + 126, 215, 96, 94, 168, 56, 126, 146, 160, 109, 184, 45, 178, 157, 181, 169, + 183, 62, 246, 144, 70, 236, 50, 205, 213, 149, 159, 47, 40, 28, 30, 137, + 200, 102, 214, 170, 222, 255, 24, 64, 10, 176, 7, 52, 2, 151, 65, 174, + 176, 124, 116, 33, 231, 88, 74, 186, 183, 51, 54, 149, 214, 239, 30, 204, + 8, 85, 198, 191, 18, 240, 13, 132, 5, 163, 67, 57, 241, 210, 196, 93, + 147, 121, 173, 226, 253, 137, 129, 166, 224, 122, 200, 35, 22, 153, 206, 234, + 212, 79, 31, 116, 8, 39, 70, 154, 178, 235, 53, 143, 87, 36, 62, 155, + 80, 107, 124, 47, 97, 220, 40, 89, 222, 186, 216, 115, 26, 165, 203, 59, + 23, 83, 78, 189, 244, 113, 135, 100, 98, 171, 105, 191, 110, 240, 44, 68, + 29, 243, 73, 133, 246, 227, 6, 201, 194, 214, 209, 158, 220, 104, 89, 238, + 186, 204, 115, 21, 229, 207, 11, 20, 7, 79, 66, 180, 49, 183, 84, 118, + 191, 102, 240, 42, 196, 31, 19, 72, 13, 246, 133, 134, 227, 34, 201, 217, + 150, 218, 238, 219, 12, 91, 69, 251, 115, 3, 101, 193, 235, 16, 79, 76, + 52, 53, 215, 87, 30, 190, 136, 112, 102, 164, 42, 251, 95, 3, 120, 1, + 226, 128, 73, 160, 54, 248, 22, 194, 142, 209, 164, 92, 123, 121, 227, 98, + 201, 233, 150, 206, 238, 212, 76, 95, 117, 248, 39, 2, 154, 129, 171, 32, + 127, 88, 32, 58, 152, 19, 42, 141, 223, 37, 152, 27, 42, 139, 95, 39, + 120, 26, 162, 139, 57, 167, 82, 250, 189, 131, 49, 161, 212, 120, 95, 98, + 184, 41, 178, 158, 245, 168, 71, 62, 178, 144, 117, 172, 39, 61, 218, 145, + 155, 44, 107, 93, 239, 121, 140, 34, 229, 217, 139, 26, 231, 75, 10, 183, + 71, 54, 178, 150, 245, 174, 199, 60, 82, 145, 253, 172, 65, 189, 240, 113, + 132, 36, 99, 91, 105, 251, 110, 195, 108, 81, 237, 252, 77, 129, 245, 160, + 71, 56, 50, 146, 149, 173, 175, 61, 188, 17, 177, 204, 116, 85, 231, 127, + 10, 160, 7, 56, 2, 146, 129, 173, 160, 125, 184, 33, 178, 152, 117, 170, + 167, 63, 58, 144, 19, 44, 13, 221, 197, 153, 147, 42, 237, 223, 13, 152, + 5, 170, 131, 63, 33, 208, 24, 92, 10, 185, 199, 50, 210, 149, 157, 175, + 41, 188, 30, 241, 200, 68, 86, 179, 126, 245, 224, 71, 8, 50, 134, 149, + 162, 239, 57, 140, 18, 229, 205, 139, 21, 167, 79, 58, 180, 19, 55, 77, + 214, 181, 158, 247, 40, 70, 158, 178, 232, 117, 142, 167, 36, 122, 155, 99, + 43, 105, 223, 110, 216, 44, 90, 157, 251, 41, 131, 94, 225, 248, 72, 66, + 182, 177, 182, 244, 118, 199, 102, 210, 170, 221, 191, 25, 176, 10, 244, 7, + 7, 66, 130, 177, 161, 180, 120, 119, 98, 166, 169, 186, 254, 243, 0, 69, + 192, 51, 16, 21, 204, 15, 21, 196, 15, 19, 68, 13, 243, 69, 133, 243, + 35, 5, 217, 195, 26, 209, 203, 28, 87, 73, 254, 182, 192, 118, 208, 38, + 220, 26, 217, 203, 26, 215, 75, 30, 183, 72, 118, 182, 166, 246, 250, 198, + 195, 18, 209, 205, 156, 85, 169, 255, 62, 192, 16, 80, 12, 60, 5, 209, + 195, 28, 81, 201, 252, 86, 193, 254, 208, 64, 92, 48, 57, 212, 18, 223, + 77, 152, 53, 170, 151, 63, 46, 144, 28, 108, 9, 237, 198, 205, 146, 213, + 173, 159, 61, 168, 17, 190, 140, 112, 101, 228, 43, 11, 95, 71, 120, 50, + 162, 149, 185, 175, 50, 252, 21, 129, 207, 32, 84, 24, 63, 74, 144, 55, + 44, 22, 157, 206, 233, 148, 78, 239, 116, 76, 39, 117, 218, 167, 27, 58, + 139, 83, 39, 125, 218, 161, 155, 56, 107, 82, 175, 125, 188, 33, 177, 216, + 116, 90, 167, 123, 58, 163, 83, 57, 253, 210, 193, 157, 144, 105, 172, 46, + 253, 220, 65, 153, 240, 106, 196, 47, 19, 92, 13, 249, 197, 130, 211, 33, + 157, 216, 105, 154, 174, 235, 60, 79, 81, 244, 60, 71, 81, 242, 188, 69, + 177, 243, 52, 69, 215, 115, 30, 165, 200, 123, 22, 163, 78, 249, 244, 66, + 199, 113, 146, 164, 109, 187, 109, 179, 109, 181, 237, 183, 13, 182, 133, 182, + 227, 54, 201, 214, 214, 222, 222, 216, 88, 90, 186, 187, 51, 51, 255, 63 ) + +random_mask_vec8 = numpy.array(random_mask_tuple, numpy.uint8) + diff --git a/gr-digital/python/ofdm_receiver.py b/gr-digital/python/ofdm_receiver.py new file mode 100644 index 000000000..9d4d6e559 --- /dev/null +++ b/gr-digital/python/ofdm_receiver.py @@ -0,0 +1,151 @@ +#!/usr/bin/env python +# +# Copyright 2006, 2007, 2008 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. +# + +import math +from numpy import fft +from gnuradio import gr + +import digital_swig +from ofdm_sync_pn import ofdm_sync_pn +from ofdm_sync_fixed import ofdm_sync_fixed +from ofdm_sync_pnac import ofdm_sync_pnac +from ofdm_sync_ml import ofdm_sync_ml + +class ofdm_receiver(gr.hier_block2): + """ + Performs receiver synchronization on OFDM symbols. + + The receiver performs channel filtering as well as symbol, frequency, and phase synchronization. + The synchronization routines are available in three flavors: preamble correlator (Schmidl and Cox), + modifid preamble correlator with autocorrelation (not yet working), and cyclic prefix correlator + (Van de Beeks). + """ + + def __init__(self, fft_length, cp_length, occupied_tones, snr, ks, logging=False): + """ + Hierarchical block for receiving OFDM symbols. + + The input is the complex modulated signal at baseband. + Synchronized packets are sent back to the demodulator. + + @param fft_length: total number of subcarriers + @type fft_length: int + @param cp_length: length of cyclic prefix as specified in subcarriers (<= fft_length) + @type cp_length: int + @param occupied_tones: number of subcarriers used for data + @type occupied_tones: int + @param snr: estimated signal to noise ratio used to guide cyclic prefix synchronizer + @type snr: float + @param ks: known symbols used as preambles to each packet + @type ks: list of lists + @param logging: turn file logging on or off + @type logging: bool + """ + + gr.hier_block2.__init__(self, "ofdm_receiver", + gr.io_signature(1, 1, gr.sizeof_gr_complex), # Input signature + gr.io_signature2(2, 2, gr.sizeof_gr_complex*occupied_tones, gr.sizeof_char)) # Output signature + + bw = (float(occupied_tones) / float(fft_length)) / 2.0 + tb = bw*0.08 + chan_coeffs = gr.firdes.low_pass (1.0, # gain + 1.0, # sampling rate + bw+tb, # midpoint of trans. band + tb, # width of trans. band + gr.firdes.WIN_HAMMING) # filter type + self.chan_filt = gr.fft_filter_ccc(1, chan_coeffs) + + win = [1 for i in range(fft_length)] + + zeros_on_left = int(math.ceil((fft_length - occupied_tones)/2.0)) + ks0 = fft_length*[0,] + ks0[zeros_on_left : zeros_on_left + occupied_tones] = ks[0] + + ks0 = fft.ifftshift(ks0) + ks0time = fft.ifft(ks0) + # ADD SCALING FACTOR + ks0time = ks0time.tolist() + + SYNC = "pn" + if SYNC == "ml": + nco_sensitivity = -1.0/fft_length # correct for fine frequency + self.ofdm_sync = ofdm_sync_ml(fft_length, + cp_length, + snr, + ks0time, + logging) + elif SYNC == "pn": + nco_sensitivity = -2.0/fft_length # correct for fine frequency + self.ofdm_sync = ofdm_sync_pn(fft_length, + cp_length, + logging) + elif SYNC == "pnac": + nco_sensitivity = -2.0/fft_length # correct for fine frequency + self.ofdm_sync = ofdm_sync_pnac(fft_length, + cp_length, + ks0time, + logging) + # for testing only; do not user over the air + # remove filter and filter delay for this + elif SYNC == "fixed": + self.chan_filt = gr.multiply_const_cc(1.0) + nsymbols = 18 # enter the number of symbols per packet + freq_offset = 0.0 # if you use a frequency offset, enter it here + nco_sensitivity = -2.0/fft_length # correct for fine frequency + self.ofdm_sync = ofdm_sync_fixed(fft_length, + cp_length, + nsymbols, + freq_offset, + logging) + + # Set up blocks + + self.nco = gr.frequency_modulator_fc(nco_sensitivity) # generate a signal proportional to frequency error of sync block + self.sigmix = gr.multiply_cc() + self.sampler = digital_swig.ofdm_sampler(fft_length, fft_length+cp_length) + self.fft_demod = gr.fft_vcc(fft_length, True, win, True) + self.ofdm_frame_acq = digital_swig.ofdm_frame_acquisition(occupied_tones, + fft_length, + cp_length, ks[0]) + + self.connect(self, self.chan_filt) # filter the input channel + self.connect(self.chan_filt, self.ofdm_sync) # into the synchronization alg. + self.connect((self.ofdm_sync,0), self.nco, (self.sigmix,1)) # use sync freq. offset output to derotate input signal + self.connect(self.chan_filt, (self.sigmix,0)) # signal to be derotated + self.connect(self.sigmix, (self.sampler,0)) # sample off timing signal detected in sync alg + self.connect((self.ofdm_sync,1), (self.sampler,1)) # timing signal to sample at + + self.connect((self.sampler,0), self.fft_demod) # send derotated sampled signal to FFT + self.connect(self.fft_demod, (self.ofdm_frame_acq,0)) # find frame start and equalize signal + self.connect((self.sampler,1), (self.ofdm_frame_acq,1)) # send timing signal to signal frame start + self.connect((self.ofdm_frame_acq,0), (self,0)) # finished with fine/coarse freq correction, + self.connect((self.ofdm_frame_acq,1), (self,1)) # frame and symbol timing, and equalization + + if logging: + self.connect(self.chan_filt, gr.file_sink(gr.sizeof_gr_complex, "ofdm_receiver-chan_filt_c.dat")) + self.connect(self.fft_demod, gr.file_sink(gr.sizeof_gr_complex*fft_length, "ofdm_receiver-fft_out_c.dat")) + self.connect(self.ofdm_frame_acq, + gr.file_sink(gr.sizeof_gr_complex*occupied_tones, "ofdm_receiver-frame_acq_c.dat")) + self.connect((self.ofdm_frame_acq,1), gr.file_sink(1, "ofdm_receiver-found_corr_b.dat")) + self.connect(self.sampler, gr.file_sink(gr.sizeof_gr_complex*fft_length, "ofdm_receiver-sampler_c.dat")) + self.connect(self.sigmix, gr.file_sink(gr.sizeof_gr_complex, "ofdm_receiver-sigmix_c.dat")) + self.connect(self.nco, gr.file_sink(gr.sizeof_gr_complex, "ofdm_receiver-nco_c.dat")) diff --git a/gr-digital/python/ofdm_sync_fixed.py b/gr-digital/python/ofdm_sync_fixed.py new file mode 100644 index 000000000..9bac789bf --- /dev/null +++ b/gr-digital/python/ofdm_sync_fixed.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python +# +# Copyright 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. +# + +import math +from gnuradio import gr + +class ofdm_sync_fixed(gr.hier_block2): + def __init__(self, fft_length, cp_length, nsymbols, freq_offset, logging=False): + + gr.hier_block2.__init__(self, "ofdm_sync_fixed", + gr.io_signature(1, 1, gr.sizeof_gr_complex), # Input signature + gr.io_signature2(2, 2, gr.sizeof_float, gr.sizeof_char)) # Output signature + + # Use a fixed trigger point instead of sync block + symbol_length = fft_length + cp_length + pkt_length = nsymbols*symbol_length + data = (pkt_length)*[0,] + data[(symbol_length)-1] = 1 + self.peak_trigger = gr.vector_source_b(data, True) + + # Use a pre-defined frequency offset + foffset = (pkt_length)*[math.pi*freq_offset,] + self.frequency_offset = gr.vector_source_f(foffset, True) + + self.connect(self, gr.null_sink(gr.sizeof_gr_complex)) + self.connect(self.frequency_offset, (self,0)) + self.connect(self.peak_trigger, (self,1)) + + if logging: + self.connect(self.peak_trigger, gr.file_sink(gr.sizeof_char, "ofdm_sync_fixed-peaks_b.dat")) + diff --git a/gr-digital/python/ofdm_sync_ml.py b/gr-digital/python/ofdm_sync_ml.py new file mode 100644 index 000000000..7c75d7f1d --- /dev/null +++ b/gr-digital/python/ofdm_sync_ml.py @@ -0,0 +1,165 @@ +#!/usr/bin/env python +# +# Copyright 2007,2008 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. +# + +import math +from gnuradio import gr + +class ofdm_sync_ml(gr.hier_block2): + def __init__(self, fft_length, cp_length, snr, kstime, logging): + ''' Maximum Likelihood OFDM synchronizer: + J. van de Beek, M. Sandell, and P. O. Borjesson, "ML Estimation + of Time and Frequency Offset in OFDM Systems," IEEE Trans. + Signal Processing, vol. 45, no. 7, pp. 1800-1805, 1997. + ''' + + gr.hier_block2.__init__(self, "ofdm_sync_ml", + gr.io_signature(1, 1, gr.sizeof_gr_complex), # Input signature + gr.io_signature2(2, 2, gr.sizeof_float, gr.sizeof_char)) # Output signature + + self.input = gr.add_const_cc(0) + + SNR = 10.0**(snr/10.0) + rho = SNR / (SNR + 1.0) + symbol_length = fft_length + cp_length + + # ML Sync + + # Energy Detection from ML Sync + + self.connect(self, self.input) + + # Create a delay line + self.delay = gr.delay(gr.sizeof_gr_complex, fft_length) + self.connect(self.input, self.delay) + + # magnitude squared blocks + self.magsqrd1 = gr.complex_to_mag_squared() + self.magsqrd2 = gr.complex_to_mag_squared() + self.adder = gr.add_ff() + + moving_sum_taps = [rho/2 for i in range(cp_length)] + self.moving_sum_filter = gr.fir_filter_fff(1,moving_sum_taps) + + self.connect(self.input,self.magsqrd1) + self.connect(self.delay,self.magsqrd2) + self.connect(self.magsqrd1,(self.adder,0)) + self.connect(self.magsqrd2,(self.adder,1)) + self.connect(self.adder,self.moving_sum_filter) + + + # Correlation from ML Sync + self.conjg = gr.conjugate_cc(); + self.mixer = gr.multiply_cc(); + + movingsum2_taps = [1.0 for i in range(cp_length)] + self.movingsum2 = gr.fir_filter_ccf(1,movingsum2_taps) + + # Correlator data handler + self.c2mag = gr.complex_to_mag() + self.angle = gr.complex_to_arg() + self.connect(self.input,(self.mixer,1)) + self.connect(self.delay,self.conjg,(self.mixer,0)) + self.connect(self.mixer,self.movingsum2,self.c2mag) + self.connect(self.movingsum2,self.angle) + + # ML Sync output arg, need to find maximum point of this + self.diff = gr.sub_ff() + self.connect(self.c2mag,(self.diff,0)) + self.connect(self.moving_sum_filter,(self.diff,1)) + + #ML measurements input to sampler block and detect + self.f2c = gr.float_to_complex() + self.pk_detect = gr.peak_detector_fb(0.2, 0.25, 30, 0.0005) + self.sample_and_hold = gr.sample_and_hold_ff() + + # use the sync loop values to set the sampler and the NCO + # self.diff = theta + # self.angle = epsilon + + self.connect(self.diff, self.pk_detect) + + # The DPLL corrects for timing differences between CP correlations + use_dpll = 0 + if use_dpll: + self.dpll = gr.dpll_bb(float(symbol_length),0.01) + self.connect(self.pk_detect, self.dpll) + self.connect(self.dpll, (self.sample_and_hold,1)) + else: + self.connect(self.pk_detect, (self.sample_and_hold,1)) + + self.connect(self.angle, (self.sample_and_hold,0)) + + ################################ + # correlate against known symbol + # This gives us the same timing signal as the PN sync block only on the preamble + # we don't use the signal generated from the CP correlation because we don't want + # to readjust the timing in the middle of the packet or we ruin the equalizer settings. + kstime = [k.conjugate() for k in kstime] + kstime.reverse() + self.kscorr = gr.fir_filter_ccc(1, kstime) + self.corrmag = gr.complex_to_mag_squared() + self.div = gr.divide_ff() + + # The output signature of the correlation has a few spikes because the rest of the + # system uses the repeated preamble symbol. It needs to work that generically if + # anyone wants to use this against a WiMAX-like signal since it, too, repeats. + # The output theta of the correlator above is multiplied with this correlation to + # identify the proper peak and remove other products in this cross-correlation + self.threshold_factor = 0.1 + self.slice = gr.threshold_ff(self.threshold_factor, self.threshold_factor, 0) + self.f2b = gr.float_to_char() + self.b2f = gr.char_to_float() + self.mul = gr.multiply_ff() + + # Normalize the power of the corr output by the energy. This is not really needed + # and could be removed for performance, but it makes for a cleaner signal. + # if this is removed, the threshold value needs adjustment. + self.connect(self.input, self.kscorr, self.corrmag, (self.div,0)) + self.connect(self.moving_sum_filter, (self.div,1)) + + self.connect(self.div, (self.mul,0)) + self.connect(self.pk_detect, self.b2f, (self.mul,1)) + self.connect(self.mul, self.slice) + + # Set output signals + # Output 0: fine frequency correction value + # Output 1: timing signal + self.connect(self.sample_and_hold, (self,0)) + self.connect(self.slice, self.f2b, (self,1)) + + + if logging: + self.connect(self.moving_sum_filter, gr.file_sink(gr.sizeof_float, "ofdm_sync_ml-energy_f.dat")) + self.connect(self.diff, gr.file_sink(gr.sizeof_float, "ofdm_sync_ml-theta_f.dat")) + self.connect(self.angle, gr.file_sink(gr.sizeof_float, "ofdm_sync_ml-epsilon_f.dat")) + self.connect(self.corrmag, gr.file_sink(gr.sizeof_float, "ofdm_sync_ml-corrmag_f.dat")) + self.connect(self.kscorr, gr.file_sink(gr.sizeof_gr_complex, "ofdm_sync_ml-kscorr_c.dat")) + self.connect(self.div, gr.file_sink(gr.sizeof_float, "ofdm_sync_ml-div_f.dat")) + self.connect(self.mul, gr.file_sink(gr.sizeof_float, "ofdm_sync_ml-mul_f.dat")) + self.connect(self.slice, gr.file_sink(gr.sizeof_float, "ofdm_sync_ml-slice_f.dat")) + self.connect(self.pk_detect, gr.file_sink(gr.sizeof_char, "ofdm_sync_ml-peaks_b.dat")) + if use_dpll: + self.connect(self.dpll, gr.file_sink(gr.sizeof_char, "ofdm_sync_ml-dpll_b.dat")) + + self.connect(self.sample_and_hold, gr.file_sink(gr.sizeof_float, "ofdm_sync_ml-sample_and_hold_f.dat")) + self.connect(self.input, gr.file_sink(gr.sizeof_gr_complex, "ofdm_sync_ml-input_c.dat")) + diff --git a/gr-digital/python/ofdm_sync_pn.py b/gr-digital/python/ofdm_sync_pn.py new file mode 100644 index 000000000..05b1de2e1 --- /dev/null +++ b/gr-digital/python/ofdm_sync_pn.py @@ -0,0 +1,123 @@ +#!/usr/bin/env python +# +# Copyright 2007,2008 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. +# + +import math +from numpy import fft +from gnuradio import gr + +class ofdm_sync_pn(gr.hier_block2): + def __init__(self, fft_length, cp_length, logging=False): + """ + OFDM synchronization using PN Correlation: + T. M. Schmidl and D. C. Cox, "Robust Frequency and Timing + Synchonization for OFDM," IEEE Trans. Communications, vol. 45, + no. 12, 1997. + """ + + gr.hier_block2.__init__(self, "ofdm_sync_pn", + gr.io_signature(1, 1, gr.sizeof_gr_complex), # Input signature + gr.io_signature2(2, 2, gr.sizeof_float, gr.sizeof_char)) # Output signature + + self.input = gr.add_const_cc(0) + + # PN Sync + + # Create a delay line + self.delay = gr.delay(gr.sizeof_gr_complex, fft_length/2) + + # Correlation from ML Sync + self.conjg = gr.conjugate_cc(); + self.corr = gr.multiply_cc(); + + # Create a moving sum filter for the corr output + if 1: + moving_sum_taps = [1.0 for i in range(fft_length//2)] + self.moving_sum_filter = gr.fir_filter_ccf(1,moving_sum_taps) + else: + moving_sum_taps = [complex(1.0,0.0) for i in range(fft_length//2)] + self.moving_sum_filter = gr.fft_filter_ccc(1,moving_sum_taps) + + # Create a moving sum filter for the input + self.inputmag2 = gr.complex_to_mag_squared() + movingsum2_taps = [1.0 for i in range(fft_length//2)] + + if 1: + self.inputmovingsum = gr.fir_filter_fff(1,movingsum2_taps) + else: + self.inputmovingsum = gr.fft_filter_fff(1,movingsum2_taps) + + self.square = gr.multiply_ff() + self.normalize = gr.divide_ff() + + # Get magnitude (peaks) and angle (phase/freq error) + self.c2mag = gr.complex_to_mag_squared() + self.angle = gr.complex_to_arg() + + self.sample_and_hold = gr.sample_and_hold_ff() + + #ML measurements input to sampler block and detect + self.sub1 = gr.add_const_ff(-1) + self.pk_detect = gr.peak_detector_fb(0.20, 0.20, 30, 0.001) + #self.pk_detect = gr.peak_detector2_fb(9) + + self.connect(self, self.input) + + # Calculate the frequency offset from the correlation of the preamble + self.connect(self.input, self.delay) + self.connect(self.input, (self.corr,0)) + self.connect(self.delay, self.conjg) + self.connect(self.conjg, (self.corr,1)) + self.connect(self.corr, self.moving_sum_filter) + self.connect(self.moving_sum_filter, self.c2mag) + self.connect(self.moving_sum_filter, self.angle) + self.connect(self.angle, (self.sample_and_hold,0)) + + # Get the power of the input signal to normalize the output of the correlation + self.connect(self.input, self.inputmag2, self.inputmovingsum) + self.connect(self.inputmovingsum, (self.square,0)) + self.connect(self.inputmovingsum, (self.square,1)) + self.connect(self.square, (self.normalize,1)) + self.connect(self.c2mag, (self.normalize,0)) + + # Create a moving sum filter for the corr output + matched_filter_taps = [1.0/cp_length for i in range(cp_length)] + self.matched_filter = gr.fir_filter_fff(1,matched_filter_taps) + self.connect(self.normalize, self.matched_filter) + + self.connect(self.matched_filter, self.sub1, self.pk_detect) + #self.connect(self.matched_filter, self.pk_detect) + self.connect(self.pk_detect, (self.sample_and_hold,1)) + + # Set output signals + # Output 0: fine frequency correction value + # Output 1: timing signal + self.connect(self.sample_and_hold, (self,0)) + self.connect(self.pk_detect, (self,1)) + + if logging: + self.connect(self.matched_filter, gr.file_sink(gr.sizeof_float, "ofdm_sync_pn-mf_f.dat")) + self.connect(self.normalize, gr.file_sink(gr.sizeof_float, "ofdm_sync_pn-theta_f.dat")) + self.connect(self.angle, gr.file_sink(gr.sizeof_float, "ofdm_sync_pn-epsilon_f.dat")) + self.connect(self.pk_detect, gr.file_sink(gr.sizeof_char, "ofdm_sync_pn-peaks_b.dat")) + self.connect(self.sample_and_hold, gr.file_sink(gr.sizeof_float, "ofdm_sync_pn-sample_and_hold_f.dat")) + self.connect(self.input, gr.file_sink(gr.sizeof_gr_complex, "ofdm_sync_pn-input_c.dat")) + diff --git a/gr-digital/python/ofdm_sync_pnac.py b/gr-digital/python/ofdm_sync_pnac.py new file mode 100644 index 000000000..10a125964 --- /dev/null +++ b/gr-digital/python/ofdm_sync_pnac.py @@ -0,0 +1,125 @@ +#!/usr/bin/env python +# +# Copyright 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. +# + +import math +from numpy import fft +from gnuradio import gr + +class ofdm_sync_pnac(gr.hier_block2): + def __init__(self, fft_length, cp_length, kstime, logging=False): + """ + OFDM synchronization using PN Correlation and initial cross-correlation: + F. Tufvesson, O. Edfors, and M. Faulkner, "Time and Frequency Synchronization for OFDM using + PN-Sequency Preambles," IEEE Proc. VTC, 1999, pp. 2203-2207. + + This implementation is meant to be a more robust version of the Schmidl and Cox receiver design. + By correlating against the preamble and using that as the input to the time-delayed correlation, + this circuit produces a very clean timing signal at the end of the preamble. The timing is + more accurate and does not have the problem associated with determining the timing from the + plateau structure in the Schmidl and Cox. + + This implementation appears to require that the signal is received with a normalized power or signal + scalling factor to reduce ambiguities intorduced from partial correlation of the cyclic prefix and + the peak detection. A better peak detection block might fix this. + + Also, the cross-correlation falls apart as the frequency offset gets larger and completely fails + when an integer offset is introduced. Another thing to look at. + """ + + gr.hier_block2.__init__(self, "ofdm_sync_pnac", + gr.io_signature(1, 1, gr.sizeof_gr_complex), # Input signature + gr.io_signature2(2, 2, gr.sizeof_float, gr.sizeof_char)) # Output signature + + + self.input = gr.add_const_cc(0) + + symbol_length = fft_length + cp_length + + # PN Sync with cross-correlation input + + # cross-correlate with the known symbol + kstime = [k.conjugate() for k in kstime[0:fft_length//2]] + kstime.reverse() + self.crosscorr_filter = gr.fir_filter_ccc(1, kstime) + + # Create a delay line + self.delay = gr.delay(gr.sizeof_gr_complex, fft_length/2) + + # Correlation from ML Sync + self.conjg = gr.conjugate_cc(); + self.corr = gr.multiply_cc(); + + # Create a moving sum filter for the input + self.mag = gr.complex_to_mag_squared() + movingsum_taps = (fft_length//1)*[1.0,] + self.power = gr.fir_filter_fff(1,movingsum_taps) + + # Get magnitude (peaks) and angle (phase/freq error) + self.c2mag = gr.complex_to_mag_squared() + self.angle = gr.complex_to_arg() + self.compare = gr.sub_ff() + + self.sample_and_hold = gr.sample_and_hold_ff() + + #ML measurements input to sampler block and detect + self.threshold = gr.threshold_ff(0,0,0) # threshold detection might need to be tweaked + self.peaks = gr.float_to_char() + + self.connect(self, self.input) + + # Cross-correlate input signal with known preamble + self.connect(self.input, self.crosscorr_filter) + + # use the output of the cross-correlation as input time-shifted correlation + self.connect(self.crosscorr_filter, self.delay) + self.connect(self.crosscorr_filter, (self.corr,0)) + self.connect(self.delay, self.conjg) + self.connect(self.conjg, (self.corr,1)) + self.connect(self.corr, self.c2mag) + self.connect(self.corr, self.angle) + self.connect(self.angle, (self.sample_and_hold,0)) + + # Get the power of the input signal to compare against the correlation + self.connect(self.crosscorr_filter, self.mag, self.power) + + # Compare the power to the correlator output to determine timing peak + # When the peak occurs, it peaks above zero, so the thresholder detects this + self.connect(self.c2mag, (self.compare,0)) + self.connect(self.power, (self.compare,1)) + self.connect(self.compare, self.threshold) + self.connect(self.threshold, self.peaks, (self.sample_and_hold,1)) + + # Set output signals + # Output 0: fine frequency correction value + # Output 1: timing signal + self.connect(self.sample_and_hold, (self,0)) + self.connect(self.peaks, (self,1)) + + if logging: + self.connect(self.compare, gr.file_sink(gr.sizeof_float, "ofdm_sync_pnac-compare_f.dat")) + self.connect(self.c2mag, gr.file_sink(gr.sizeof_float, "ofdm_sync_pnac-theta_f.dat")) + self.connect(self.power, gr.file_sink(gr.sizeof_float, "ofdm_sync_pnac-inputpower_f.dat")) + self.connect(self.angle, gr.file_sink(gr.sizeof_float, "ofdm_sync_pnac-epsilon_f.dat")) + self.connect(self.threshold, gr.file_sink(gr.sizeof_float, "ofdm_sync_pnac-threshold_f.dat")) + self.connect(self.peaks, gr.file_sink(gr.sizeof_char, "ofdm_sync_pnac-peaks_b.dat")) + self.connect(self.sample_and_hold, gr.file_sink(gr.sizeof_float, "ofdm_sync_pnac-sample_and_hold_f.dat")) + self.connect(self.input, gr.file_sink(gr.sizeof_gr_complex, "ofdm_sync_pnac-input_c.dat")) diff --git a/gr-digital/python/psk.py b/gr-digital/python/psk.py index acedf3b69..58f6787f0 100644 --- a/gr-digital/python/psk.py +++ b/gr-digital/python/psk.py @@ -1,5 +1,5 @@ # -# Copyright 2005,2006 Free Software Foundation, Inc. +# Copyright 2005,2006,2011 Free Software Foundation, Inc. # # This file is part of GNU Radio # @@ -19,76 +19,104 @@ # Boston, MA 02110-1301, USA. # -from math import pi, sqrt, log10 -import math, cmath - -# The following algorithm generates Gray coded constellations for M-PSK for M=[2,4,8] -def make_gray_constellation(m): - # number of bits/symbol (log2(M)) - k = int(log10(m) / log10(2.0)) - - coeff = 1 - const_map = [] - bits = [0]*3 - for i in range(m): - # get a vector of the k bits to use in this mapping - bits[3-k:3] = [((i&(0x01 << k-j-1)) >> k-j-1) for j in range(k)] - - theta = -(2*bits[0]-1)*(2*pi/m)*(bits[0]+abs(bits[1]-bits[2])+2*bits[1]) - re = math.cos(theta) - im = math.sin(theta) - const_map.append(complex(re, im)) # plug it into the constellation +""" +PSK modulation and demodulation. +""" + +from math import pi, log +from cmath import exp + +import digital_swig +import modulation_utils +from utils import mod_codes, gray_code +from generic_mod_demod import generic_mod, generic_demod + +# Default number of points in constellation. +_def_constellation_points = 4 +# The default encoding (e.g. gray-code, set-partition) +_def_mod_code = mod_codes.GRAY_CODE + +def create_encodings(mod_code, arity): + post_diff_code = None + if mod_code not in mod_codes.codes: + raise ValueError('That modulation code does not exist.') + if mod_code == mod_codes.GRAY_CODE: + pre_diff_code = gray_code.gray_code(arity) + elif mod_code == mod_codes.SET_PARTITION_CODE: + pre_diff_code = set_partition_code.set_partition_code(arity) + elif mod_code == mod_codes.NO_CODE: + pre_diff_code = [] + else: + raise ValueError('That modulation code is not implemented for this constellation.') + return (pre_diff_code, post_diff_code) - # return the constellation; by default, it is normalized - return const_map - -# This makes a constellation that increments around the unit circle -def make_constellation(m): - return [cmath.exp(i * 2 * pi / m * 1j) for i in range(m)] - -# Common definition of constellations for Tx and Rx -constellation = { - 2 : make_constellation(2), # BPSK - 4 : make_constellation(4), # QPSK - 8 : make_constellation(8) # 8PSK - } - -gray_constellation = { - 2 : make_gray_constellation(2), # BPSK - 4 : make_gray_constellation(4), # QPSK - 8 : make_gray_constellation(8) # 8PSK - } - -# ----------------------- -# Do Gray code -# ----------------------- -# binary to gray coding -- constellation does Gray coding -binary_to_gray = { - 2 : range(2), - 4 : [0,1,3,2], - 8 : [0, 1, 3, 2, 7, 6, 4, 5] - } - -# gray to binary -gray_to_binary = { - 2 : range(2), - 4 : [0,1,3,2], - 8 : [0, 1, 3, 2, 6, 7, 5, 4] - } - -# ----------------------- -# Don't Gray code -# ----------------------- -# identity mapping -binary_to_ungray = { - 2 : range(2), - 4 : range(4), - 8 : range(8) - } - -# identity mapping -ungray_to_binary = { - 2 : range(2), - 4 : range(4), - 8 : range(8) - } +# ///////////////////////////////////////////////////////////////////////////// +# PSK constellation +# ///////////////////////////////////////////////////////////////////////////// + +def psk_constellation(m=_def_constellation_points, mod_code=_def_mod_code): + """ + Creates a PSK constellation object. + """ + k = log(m) / log(2.0) + if (k != int(k)): + raise StandardError('Number of constellation points must be a power of two.') + points = [exp(2*pi*(0+1j)*i/m) for i in range(0,m)] + pre_diff_code, post_diff_code = create_encodings(mod_code, m) + if post_diff_code is not None: + inverse_post_diff_code = mod_codes.invert_code(post_diff_code) + points = [points[x] for x in inverse_post_diff_code] + constellation = digital_swig.constellation_psk(points, pre_diff_code, m) + return constellation + +# ///////////////////////////////////////////////////////////////////////////// +# PSK modulator +# ///////////////////////////////////////////////////////////////////////////// + +class psk_mod(generic_mod): + + def __init__(self, constellation_points=_def_constellation_points, + mod_code=_def_mod_code, + *args, **kwargs): + + """ + Hierarchical block for RRC-filtered PSK modulation. + + The input is a byte stream (unsigned char) and the + output is the complex modulated signal at baseband. + + See generic_mod block for list of parameters. + """ + + constellation = psk_constellation(constellation_points, mod_code) + super(psk_mod, self).__init__(constellation, *args, **kwargs) + +# ///////////////////////////////////////////////////////////////////////////// +# PSK demodulator +# +# ///////////////////////////////////////////////////////////////////////////// + +class psk_demod(generic_demod): + + def __init__(self, constellation_points=_def_constellation_points, + mod_code=_def_mod_code, + *args, **kwargs): + + """ + Hierarchical block for RRC-filtered PSK modulation. + + The input is a byte stream (unsigned char) and the + output is the complex modulated signal at baseband. + + See generic_demod block for list of parameters. + """ + + constellation = psk_constellation(constellation_points, mod_code) + super(psk_demod, self).__init__(constellation, *args, **kwargs) + +# +# Add these to the mod/demod registry +# +modulation_utils.add_type_1_mod('psk', psk_mod) +modulation_utils.add_type_1_demod('psk', psk_demod) +modulation_utils.add_type_1_constellation('psk', psk_constellation) diff --git a/gr-digital/python/psk2.py b/gr-digital/python/psk2.py deleted file mode 100644 index 82781e63b..000000000 --- a/gr-digital/python/psk2.py +++ /dev/null @@ -1,122 +0,0 @@ -# -# Copyright 2005,2006,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. -# - -""" -PSK modulation and demodulation. -""" - -from math import pi, log -from cmath import exp - -import digital_swig -import modulation_utils2 -from utils import mod_codes, gray_code -from generic_mod_demod import generic_mod, generic_demod - -# Default number of points in constellation. -_def_constellation_points = 4 -# The default encoding (e.g. gray-code, set-partition) -_def_mod_code = mod_codes.GRAY_CODE - -def create_encodings(mod_code, arity): - post_diff_code = None - if mod_code not in mod_codes.codes: - raise ValueError('That modulation code does not exist.') - if mod_code == mod_codes.GRAY_CODE: - pre_diff_code = gray_code.gray_code(arity) - elif mod_code == mod_codes.SET_PARTITION_CODE: - pre_diff_code = set_partition_code.set_partition_code(arity) - elif mod_code == mod_codes.NO_CODE: - pre_diff_code = [] - else: - raise ValueError('That modulation code is not implemented for this constellation.') - return (pre_diff_code, post_diff_code) - -# ///////////////////////////////////////////////////////////////////////////// -# PSK constellation -# ///////////////////////////////////////////////////////////////////////////// - -def psk_constellation(m=_def_constellation_points, mod_code=_def_mod_code): - """ - Creates a PSK constellation object. - """ - k = log(m) / log(2.0) - if (k != int(k)): - raise StandardError('Number of constellation points must be a power of two.') - points = [exp(2*pi*(0+1j)*i/m) for i in range(0,m)] - pre_diff_code, post_diff_code = create_encodings(mod_code, m) - if post_diff_code is not None: - inverse_post_diff_code = mod_codes.invert_code(post_diff_code) - points = [points[x] for x in inverse_post_diff_code] - constellation = digital_swig.constellation_psk(points, pre_diff_code, m) - return constellation - -# ///////////////////////////////////////////////////////////////////////////// -# PSK modulator -# ///////////////////////////////////////////////////////////////////////////// - -class psk_mod(generic_mod): - - def __init__(self, constellation_points=_def_constellation_points, - mod_code=_def_mod_code, - *args, **kwargs): - - """ - Hierarchical block for RRC-filtered PSK modulation. - - The input is a byte stream (unsigned char) and the - output is the complex modulated signal at baseband. - - See generic_mod block for list of parameters. - """ - - constellation = psk_constellation(constellation_points, mod_code) - super(psk_mod, self).__init__(constellation, *args, **kwargs) - -# ///////////////////////////////////////////////////////////////////////////// -# PSK demodulator -# -# ///////////////////////////////////////////////////////////////////////////// - -class psk_demod(generic_demod): - - def __init__(self, constellation_points=_def_constellation_points, - mod_code=_def_mod_code, - *args, **kwargs): - - """ - Hierarchical block for RRC-filtered PSK modulation. - - The input is a byte stream (unsigned char) and the - output is the complex modulated signal at baseband. - - See generic_demod block for list of parameters. - """ - - constellation = psk_constellation(constellation_points, mod_code) - super(psk_demod, self).__init__(constellation, *args, **kwargs) - -# -# Add these to the mod/demod registry -# -modulation_utils2.add_type_1_mod('psk', psk_mod) -modulation_utils2.add_type_1_demod('psk', psk_demod) -modulation_utils2.add_type_1_constellation('psk', psk_constellation) diff --git a/gr-digital/python/qa_clock_recovery_mm.py b/gr-digital/python/qa_clock_recovery_mm.py index 5ef86eda0..f4c345b03 100755 --- a/gr-digital/python/qa_clock_recovery_mm.py +++ b/gr-digital/python/qa_clock_recovery_mm.py @@ -21,7 +21,7 @@ # from gnuradio import gr, gr_unittest -import digital_swig, psk2 +import digital_swig import random, cmath class test_clock_recovery_mm(gr_unittest.TestCase): diff --git a/gr-digital/python/qa_constellation.py b/gr-digital/python/qa_constellation.py index 264ff7de6..b17d2a0fc 100755 --- a/gr-digital/python/qa_constellation.py +++ b/gr-digital/python/qa_constellation.py @@ -28,7 +28,7 @@ from utils import mod_codes import digital_swig # import from local folder -import psk2 +import psk import qam tested_mod_codes = (mod_codes.NO_CODE, mod_codes.GRAY_CODE) @@ -65,7 +65,7 @@ def threed_constell(): return digital_swig.constellation_calcdist(points, [], rot_sym, dim) tested_constellation_info = ( - (psk2.psk_constellation, + (psk.psk_constellation, {'m': (2, 4, 8, 16, 32, 64), 'mod_code': tested_mod_codes, }, True, None), diff --git a/gr-digital/python/qa_costas_loop_cc.py b/gr-digital/python/qa_costas_loop_cc.py index 124159dba..75fdbc2f8 100755 --- a/gr-digital/python/qa_costas_loop_cc.py +++ b/gr-digital/python/qa_costas_loop_cc.py @@ -21,7 +21,7 @@ # from gnuradio import gr, gr_unittest -import digital_swig, psk2 +import digital_swig, psk import random, cmath class test_costas_loop_cc(gr_unittest.TestCase): @@ -125,7 +125,7 @@ class test_costas_loop_cc(gr_unittest.TestCase): self.test = digital_swig.costas_loop_cc(natfreq, order) rot = cmath.exp(-cmath.pi/8.0j) # rotate to match Costas rotation - const = psk2.psk_constellation(order) + const = psk.psk_constellation(order) data = [random.randint(0,7) for i in xrange(100)] data = [2*rot*const.points()[d] for d in data] diff --git a/gr-digital/python/qa_mpsk_receiver.py b/gr-digital/python/qa_mpsk_receiver.py index 6531e59f7..e1f16ee67 100755 --- a/gr-digital/python/qa_mpsk_receiver.py +++ b/gr-digital/python/qa_mpsk_receiver.py @@ -21,7 +21,7 @@ # from gnuradio import gr, gr_unittest -import digital_swig, psk2 +import digital_swig import random, cmath class test_mpsk_receiver(gr_unittest.TestCase): diff --git a/gr-digital/python/qa_ofdm_insert_preamble.py b/gr-digital/python/qa_ofdm_insert_preamble.py new file mode 100755 index 000000000..c45893fa3 --- /dev/null +++ b/gr-digital/python/qa_ofdm_insert_preamble.py @@ -0,0 +1,180 @@ +#!/usr/bin/env python +# +# Copyright 2007,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, gr_unittest +from pprint import pprint +import digital_swig + +class test_ofdm_insert_preamble (gr_unittest.TestCase): + + def setUp (self): + self.tb = gr.top_block () + + def tearDown (self): + self.tb = None + + def helper(self, v0, v1, fft_length, preamble): + tb = self.tb + src0 = gr.vector_source_c(v0) + src1 = gr.vector_source_b(v1) + + s2v = gr.stream_to_vector(gr.sizeof_gr_complex, fft_length) + + # print "len(v) = %d" % (len(v)) + + op = digital_swig.ofdm_insert_preamble(fft_length, preamble) + + v2s = gr.vector_to_stream(gr.sizeof_gr_complex, fft_length) + dst0 = gr.vector_sink_c() + dst1 = gr.vector_sink_b() + + tb.connect(src0, s2v, (op, 0)) + tb.connect(src1, (op, 1)) + tb.connect((op, 0), v2s, dst0) + tb.connect((op, 1), dst1) + + tb.run() + r0 = dst0.data() + r0v = [] + for i in range(len(r0)//fft_length): + r0v.append(r0[i*fft_length:(i+1)*fft_length]) + + r1 = dst1.data() + self.assertEqual(len(r0v), len(r1)) + return (r1, r0v) + + def check_match(self, actual, expected_list): + lst = [] + map(lambda x: lst.append(x), expected_list) + self.assertEqual(actual, lst) + + + # ---------------------------------------------------------------- + + def test_000(self): + # no preamble, 1 symbol payloads + + preamble = () + fft_length = 8 + npayloads = 8 + v = [] + p = [] + for i in range(npayloads): + t = fft_length*[(i + i*1j)] + p.append(tuple(t)) + v += t + + p = tuple(p) + + r = self.helper(v, npayloads*[1], fft_length, preamble) + # pprint(r) + + self.assertEqual(r[0], tuple(npayloads*[1])) + self.check_match(r[1], (p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7])) + + + def test_001(self): + # 1 symbol preamble, 1 symbol payloads + preamble = ((100, 101, 102, 103, 104, 105, 106, 107),) + p0 = preamble[0] + fft_length = 8 + npayloads = 8 + v = [] + p = [] + for i in range(npayloads): + t = fft_length*[(i + i*1j)] + p.append(tuple(t)) + v += t + + + r = self.helper(v, npayloads*[1], fft_length, preamble) + + self.assertEqual(r[0], tuple(npayloads*[1, 0])) + self.check_match(r[1], (p0, p[0], + p0, p[1], + p0, p[2], + p0, p[3], + p0, p[4], + p0, p[5], + p0, p[6], + p0, p[7])) + + def test_002(self): + # 2 symbol preamble, 1 symbol payloads + preamble = ((100, 101, 102, 103, 104, 105, 106, 107), + (200, 201, 202, 203, 204, 205, 206, 207)) + p0 = preamble[0] + p1 = preamble[1] + + fft_length = 8 + npayloads = 8 + v = [] + p = [] + for i in range(npayloads): + t = fft_length*[(i + i*1j)] + p.append(tuple(t)) + v += t + + r = self.helper(v, npayloads*[1], fft_length, preamble) + + self.assertEqual(r[0], tuple(npayloads*[1, 0, 0])) + self.check_match(r[1], (p0, p1, p[0], + p0, p1, p[1], + p0, p1, p[2], + p0, p1, p[3], + p0, p1, p[4], + p0, p1, p[5], + p0, p1, p[6], + p0, p1, p[7])) + + + def xtest_003_preamble(self): + # 2 symbol preamble, 2 symbol payloads + preamble = ((100, 101, 102, 103, 104, 105, 106, 107), + (200, 201, 202, 203, 204, 205, 206, 207)) + p0 = preamble[0] + p1 = preamble[1] + + fft_length = 8 + npayloads = 8 + v = [] + p = [] + for i in range(npayloads * 2): + t = fft_length*[(i + i*1j)] + p.append(tuple(t)) + v += t + + r = self.helper(v, npayloads*[1, 0], fft_length, preamble) + + self.assertEqual(r[0], tuple(npayloads*[1, 0, 0, 0])) + self.check_match(r[1], (p0, p1, p[0], p[1], + p0, p1, p[2], p[3], + p0, p1, p[4], p[5], + p0, p1, p[6], p[7], + p0, p1, p[8], p[9], + p0, p1, p[10], p[11], + p0, p1, p[12], p[13], + p0, p1, p[14], p[15])) + + +if __name__ == '__main__': + gr_unittest.run(test_ofdm_insert_preamble, "test_ofdm_insert_preamble.xml") diff --git a/gr-digital/python/qam.py b/gr-digital/python/qam.py index a5a2e6c2c..5b1f7683b 100644 --- a/gr-digital/python/qam.py +++ b/gr-digital/python/qam.py @@ -29,7 +29,8 @@ from gnuradio import gr from generic_mod_demod import generic_mod, generic_demod from utils.gray_code import gray_code from utils import mod_codes -import modulation_utils2 +import modulation_utils +import digital_swig # Default number of points in constellation. _def_constellation_points = 16 @@ -164,7 +165,8 @@ def qam_constellation(constellation_points=_def_constellation_points, # No pre-diff code # Should add one so that we can gray-code the quadrant bits too. pre_diff_code = [] - constellation = gr.constellation_rect(points, pre_diff_code, 4, side, side, width, width) + constellation = digital_swig.constellation_rect(points, pre_diff_code, 4, + side, side, width, width) return constellation # ///////////////////////////////////////////////////////////////////////////// @@ -222,6 +224,6 @@ class qam_demod(generic_demod): # # Add these to the mod/demod registry # -modulation_utils2.add_type_1_mod('qam', qam_mod) -modulation_utils2.add_type_1_demod('qam', qam_demod) -modulation_utils2.add_type_1_constellation('qam', qam_constellation) +modulation_utils.add_type_1_mod('qam', qam_mod) +modulation_utils.add_type_1_demod('qam', qam_demod) +modulation_utils.add_type_1_constellation('qam', qam_constellation) diff --git a/gr-digital/python/qpsk.py b/gr-digital/python/qpsk.py index 481b7cb5b..be21fd76f 100644 --- a/gr-digital/python/qpsk.py +++ b/gr-digital/python/qpsk.py @@ -28,7 +28,7 @@ Demodulation is not included since the generic_mod_demod from gnuradio import gr from gnuradio.digital.generic_mod_demod import generic_mod, generic_demod import digital_swig -import modulation_utils2 +import modulation_utils # Default number of points in constellation. _def_constellation_points = 4 @@ -167,10 +167,10 @@ class dqpsk_demod(generic_demod): # # Add these to the mod/demod registry # -modulation_utils2.add_type_1_mod('qpsk', qpsk_mod) -modulation_utils2.add_type_1_demod('qpsk', qpsk_demod) -modulation_utils2.add_type_1_constellation('qpsk', qpsk_constellation) -modulation_utils2.add_type_1_mod('dqpsk', dqpsk_mod) -modulation_utils2.add_type_1_demod('dqpsk', dqpsk_demod) -modulation_utils2.add_type_1_constellation('dqpsk', dqpsk_constellation) +modulation_utils.add_type_1_mod('qpsk', qpsk_mod) +modulation_utils.add_type_1_demod('qpsk', qpsk_demod) +modulation_utils.add_type_1_constellation('qpsk', qpsk_constellation) +modulation_utils.add_type_1_mod('dqpsk', dqpsk_mod) +modulation_utils.add_type_1_demod('dqpsk', dqpsk_demod) +modulation_utils.add_type_1_constellation('dqpsk', dqpsk_constellation) diff --git a/gr-digital/swig/CMakeLists.txt b/gr-digital/swig/CMakeLists.txt index 3df218d6e..4f4e51e33 100644 --- a/gr-digital/swig/CMakeLists.txt +++ b/gr-digital/swig/CMakeLists.txt @@ -54,6 +54,12 @@ install( digital_lms_dd_equalizer_cc.i digital_kurtotic_equalizer_cc.i digital_mpsk_receiver_cc.i + digital_ofdm_cyclic_prefixer.i + digital_ofdm_frame_acquisition.i + digital_ofdm_frame_sink.i + digital_ofdm_insert_preamble.i + digital_ofdm_mapper_bcv.i + digital_ofdm_sampler.i digital_gmskmod_bc.i digital_cpmmod_bc.i DESTINATION ${GR_INCLUDE_DIR}/gnuradio/swig diff --git a/gr-digital/swig/Makefile.am b/gr-digital/swig/Makefile.am index abc24be19..1afa44743 100644 --- a/gr-digital/swig/Makefile.am +++ b/gr-digital/swig/Makefile.am @@ -71,6 +71,12 @@ digital_swig_swiginclude_headers = \ digital_lms_dd_equalizer_cc.i \ digital_kurtotic_equalizer_cc.i \ digital_mpsk_receiver_cc.i \ + digital_ofdm_cyclic_prefixer.i \ + digital_ofdm_frame_acquisition.i \ + digital_ofdm_frame_sink.i \ + digital_ofdm_insert_preamble.i \ + digital_ofdm_mapper_bcv.i \ + digital_ofdm_sampler.i \ digital_gmskmod_bc.i \ digital_cpmmod_bc.i diff --git a/gr-digital/swig/digital_ofdm_cyclic_prefixer.i b/gr-digital/swig/digital_ofdm_cyclic_prefixer.i new file mode 100644 index 000000000..56d1629a8 --- /dev/null +++ b/gr-digital/swig/digital_ofdm_cyclic_prefixer.i @@ -0,0 +1,34 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,2009,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. + */ + +GR_SWIG_BLOCK_MAGIC(digital,ofdm_cyclic_prefixer) + +digital_ofdm_cyclic_prefixer_sptr +digital_make_ofdm_cyclic_prefixer (size_t input_size, size_t output_size); + +class digital_ofdm_cyclic_prefixer : public gr_sync_interpolator +{ + protected: + digital_ofdm_cyclic_prefixer (size_t input_size, size_t output_size); + + public: +}; diff --git a/gr-digital/swig/digital_ofdm_frame_acquisition.i b/gr-digital/swig/digital_ofdm_frame_acquisition.i new file mode 100644 index 000000000..b61297bde --- /dev/null +++ b/gr-digital/swig/digital_ofdm_frame_acquisition.i @@ -0,0 +1,49 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,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. + */ + +#include <vector> + +GR_SWIG_BLOCK_MAGIC(digital,ofdm_frame_acquisition); + +digital_ofdm_frame_acquisition_sptr +digital_make_ofdm_frame_acquisition (unsigned int occupied_carriers, + unsigned int fft_length, + unsigned int cplen, + const std::vector<gr_complex> &known_symbol, + unsigned int max_fft_shift_len=4); + +class digital_ofdm_frame_acquisition : public gr_sync_decimator +{ + protected: + digital_ofdm_frame_acquisition (unsigned int occupied_carriers, + unsigned int fft_length, + unsigned int cplen, + const std::vector<gr_complex> &known_symbol, + unsigned int max_fft_shift_len); + + public: + float snr() { return d_snr_est; } + int general_work(int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); +}; diff --git a/gr-digital/swig/digital_ofdm_frame_sink.i b/gr-digital/swig/digital_ofdm_frame_sink.i new file mode 100644 index 000000000..cd3fa1422 --- /dev/null +++ b/gr-digital/swig/digital_ofdm_frame_sink.i @@ -0,0 +1,41 @@ +/* -*- c++ -*- */ +/* + * Copyright 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. + */ + +GR_SWIG_BLOCK_MAGIC(digital,ofdm_frame_sink); + +digital_ofdm_frame_sink_sptr +digital_make_ofdm_frame_sink(const std::vector<gr_complex> &sym_position, + const std::vector<unsigned char> &sym_value_out, + gr_msg_queue_sptr target_queue, unsigned int occupied_tones, + float phase_gain=0.25, float freq_gain=0.25*0.25/4); + +class digital_ofdm_frame_sink : public gr_sync_block +{ + protected: + digital_ofdm_frame_sink(const std::vector<gr_complex> &sym_position, + const std::vector<unsigned char> &sym_value_out, + gr_msg_queue_sptr target_queue, unsigned int occupied_tones, + float phase_gain, float freq_gain); + + public: + ~digital_ofdm_frame_sink(); +}; diff --git a/gr-digital/swig/digital_ofdm_insert_preamble.i b/gr-digital/swig/digital_ofdm_insert_preamble.i new file mode 100644 index 000000000..5f7b16369 --- /dev/null +++ b/gr-digital/swig/digital_ofdm_insert_preamble.i @@ -0,0 +1,35 @@ +/* -*- c++ -*- */ +/* + * Copyright 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. + */ + +GR_SWIG_BLOCK_MAGIC(digital,ofdm_insert_preamble); + +digital_ofdm_insert_preamble_sptr +digital_make_ofdm_insert_preamble(int fft_length, + const std::vector<std::vector<gr_complex> > &preamble); + + +class digital_ofdm_insert_preamble : public gr_block +{ + protected: + digital_ofdm_insert_preamble(int fft_length, + const std::vector<std::vector<gr_complex> > &preamble); +}; diff --git a/gr-digital/swig/digital_ofdm_mapper_bcv.i b/gr-digital/swig/digital_ofdm_mapper_bcv.i new file mode 100644 index 000000000..4e9aaba7d --- /dev/null +++ b/gr-digital/swig/digital_ofdm_mapper_bcv.i @@ -0,0 +1,46 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,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. + */ + +GR_SWIG_BLOCK_MAGIC(digital,ofdm_mapper_bcv); + +digital_ofdm_mapper_bcv_sptr +digital_make_ofdm_mapper_bcv (const std::vector<gr_complex> &constellation, + unsigned int msgq_limit, + unsigned int bits_per_symbol, + unsigned int fft_length) throw(std::exception); + + +class digital_ofdm_mapper_bcv : public gr_sync_block +{ + protected: + digital_ofdm_mapper_bcv (const std::vector<gr_complex> &constellation, + unsigned int msgq_limit, + unsigned int bits_per_symbol, + unsigned int fft_length); + + public: + gr_msg_queue_sptr msgq(); + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); +}; diff --git a/gr-digital/swig/digital_ofdm_sampler.i b/gr-digital/swig/digital_ofdm_sampler.i new file mode 100644 index 000000000..91056c320 --- /dev/null +++ b/gr-digital/swig/digital_ofdm_sampler.i @@ -0,0 +1,35 @@ +/* -*- c++ -*- */ +/* + * Copyright 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. + */ + +GR_SWIG_BLOCK_MAGIC(digital,ofdm_sampler) + + digital_ofdm_sampler_sptr digital_make_ofdm_sampler (unsigned int fft_length, + unsigned int symbol_length, + unsigned int timeout=1000); + +class digital_ofdm_sampler : public gr_sync_block +{ + private: + digital_ofdm_sampler (unsigned int fft_length, + unsigned int symbol_length, + unsigned int timeout); +}; diff --git a/gr-digital/swig/digital_swig.i b/gr-digital/swig/digital_swig.i index 2e3a3242e..3e3a63b61 100644 --- a/gr-digital/swig/digital_swig.i +++ b/gr-digital/swig/digital_swig.i @@ -38,6 +38,12 @@ #include "digital_kurtotic_equalizer_cc.h" #include "digital_lms_dd_equalizer_cc.h" #include "digital_mpsk_receiver_cc.h" +#include "digital_ofdm_cyclic_prefixer.h" +#include "digital_ofdm_frame_acquisition.h" +#include "digital_ofdm_frame_sink.h" +#include "digital_ofdm_insert_preamble.h" +#include "digital_ofdm_mapper_bcv.h" +#include "digital_ofdm_sampler.h" #include "digital_cpmmod_bc.h" #include "digital_gmskmod_bc.h" %} @@ -56,6 +62,12 @@ %include "digital_kurtotic_equalizer_cc.i" %include "digital_lms_dd_equalizer_cc.i" %include "digital_mpsk_receiver_cc.i" +%include "digital_ofdm_cyclic_prefixer.i" +%include "digital_ofdm_frame_acquisition.i" +%include "digital_ofdm_frame_sink.i" +%include "digital_ofdm_insert_preamble.i" +%include "digital_ofdm_mapper_bcv.i" +%include "digital_ofdm_sampler.i" %include "digital_cpmmod_bc.i" %include "digital_gmskmod_bc.i" |