diff options
author | Tom Rondeau <trondeau@vt.edu> | 2013-02-20 12:20:29 -0500 |
---|---|---|
committer | Tom Rondeau <trondeau@vt.edu> | 2013-02-20 12:20:29 -0500 |
commit | 7cbeddfd5a0cffba51a3946a8dd44b9838e42361 (patch) | |
tree | f1236ba0a17387471d1f665aec6e41883d775a42 /gr-digital | |
parent | f4762c567bf5ec85b998935ee330d7c0e986dc57 (diff) | |
parent | 2e2ed85be902b5ddae91bf22d9051554b1bdfd0b (diff) |
Merge branch 'master' into next
Conflicts:
gr-digital/include/CMakeLists.txt
gr-digital/lib/CMakeLists.txt
gr-digital/swig/CMakeLists.txt
gr-digital/swig/digital_swig.i
Diffstat (limited to 'gr-digital')
-rw-r--r-- | gr-digital/grc/digital_simple_correlator.xml | 25 | ||||
-rw-r--r-- | gr-digital/include/digital/simple_correlator.h | 48 | ||||
-rw-r--r-- | gr-digital/include/digital_simple_correlator.h | 107 | ||||
-rw-r--r-- | gr-digital/lib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | gr-digital/lib/simple_correlator_impl.cc | 237 | ||||
-rw-r--r-- | gr-digital/lib/simple_correlator_impl.h | 102 | ||||
-rwxr-xr-x | gr-digital/python/qa_simple_correlator.py | 62 | ||||
-rw-r--r-- | gr-digital/swig/digital_simple_correlator.i | 31 | ||||
-rw-r--r-- | gr-digital/swig/digital_swig.i | 3 |
9 files changed, 616 insertions, 0 deletions
diff --git a/gr-digital/grc/digital_simple_correlator.xml b/gr-digital/grc/digital_simple_correlator.xml new file mode 100644 index 000000000..3b70e59b1 --- /dev/null +++ b/gr-digital/grc/digital_simple_correlator.xml @@ -0,0 +1,25 @@ +<?xml version="1.0"?> +<!-- +################################################### +##Simple Correlator +################################################### + --> +<block> + <name>Simple Correlator</name> + <key>digital_simple_correlator</key> + <import>from gnuradio import digital</import> + <make>digital.simple_correlator($payload_bytesize)</make> + <param> + <name>Payload Byte Size</name> + <key>payload_bytesize</key> + <type>int</type> + </param> + <sink> + <name>in</name> + <type>float</type> + </sink> + <source> + <name>out</name> + <type>byte</type> + </source> +</block> diff --git a/gr-digital/include/digital/simple_correlator.h b/gr-digital/include/digital/simple_correlator.h new file mode 100644 index 000000000..b75dc2476 --- /dev/null +++ b/gr-digital/include/digital/simple_correlator.h @@ -0,0 +1,48 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2013 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_GR_SIMPLE_CORRELATOR_H +#define INCLUDED_GR_SIMPLE_CORRELATOR_H + +#include <digital/api.h> +#include <gr_block.h> + +namespace gr { + namespace digital { + + /*! + * \brief inverse of simple_framer (more or less) + * \ingroup sync_blk + */ + class DIGITAL_API simple_correlator : virtual public gr_block + { + public: + // gr::digital::simple_correlator::sptr + typedef boost::shared_ptr<simple_correlator> sptr; + + static sptr make(int payload_bytesize); + }; + + } /* namespace digital */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_SIMPLE_CORRELATOR_H */ diff --git a/gr-digital/include/digital_simple_correlator.h b/gr-digital/include/digital_simple_correlator.h new file mode 100644 index 000000000..716c6995c --- /dev/null +++ b/gr-digital/include/digital_simple_correlator.h @@ -0,0 +1,107 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2013 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_GR_SIMPLE_CORRELATOR_H +#define INCLUDED_GR_SIMPLE_CORRELATOR_H + +#include <digital_api.h> +#include <gr_block.h> + +class digital_simple_correlator; +typedef boost::shared_ptr<digital_simple_correlator> digital_simple_correlator_sptr; + +DIGITAL_API digital_simple_correlator_sptr digital_make_simple_correlator(int payload_bytesize); + +/*! + * \brief inverse of simple_framer (more or less) + * \ingroup sync_blk + */ +class DIGITAL_API digital_simple_correlator : public gr_block +{ + private: + static const int OVERSAMPLE = 8; + enum state_t { ST_LOOKING, ST_UNDER_THRESHOLD, ST_LOCKED }; + + int d_payload_bytesize; + state_t d_state; + unsigned int d_osi; // over sample index [0,OVERSAMPLE-1] + unsigned int d_transition_osi; // first index where Hamming dist < thresh + unsigned int d_center_osi; // center of bit + unsigned long long int d_shift_reg[OVERSAMPLE]; + int d_bblen; // length of bitbuf + unsigned char *d_bitbuf; // demodulated bits + unsigned char *d_pktbuf; // temp packet buf + int d_bbi; // bitbuf index + + static const int AVG_PERIOD = 512; // must be power of 2 (for freq offset correction) + int d_avbi; + float d_avgbuf[AVG_PERIOD]; + float d_avg; + float d_accum; + +#ifdef DEBUG_SIMPLE_CORRELATOR + FILE *d_debug_fp; // binary log file +#endif + + friend GR_CORE_API digital_simple_correlator_sptr + digital_make_simple_correlator(int payload_bytesize); + digital_simple_correlator(int payload_bytesize); + + inline int slice(float x) + { + return x >= d_avg ? 1 : 0; + } + + void update_avg(float x); + + void enter_locked(); + void enter_under_threshold(); + void enter_looking(); + + static int add_index(int a, int b) + { + int t = a + b; + if(t >= OVERSAMPLE) + t -= OVERSAMPLE; + assert(t >= 0 && t < OVERSAMPLE); + return t; + } + + static int sub_index(int a, int b) + { + int t = a - b; + if(t < 0) + t += OVERSAMPLE; + assert(t >= 0 && t < OVERSAMPLE); + return t; + } + + public: + ~digital_simple_correlator(); + + 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_GR_SIMPLE_CORRELATOR_H */ diff --git a/gr-digital/lib/CMakeLists.txt b/gr-digital/lib/CMakeLists.txt index de99ae8c0..b4c267e8c 100644 --- a/gr-digital/lib/CMakeLists.txt +++ b/gr-digital/lib/CMakeLists.txt @@ -152,6 +152,7 @@ list(APPEND digital_sources probe_mpsk_snr_est_c_impl.cc scrambler_bb_impl.cc simple_framer_impl.cc + simple_correlator_impl.cc ) list(APPEND digital_libs diff --git a/gr-digital/lib/simple_correlator_impl.cc b/gr-digital/lib/simple_correlator_impl.cc new file mode 100644 index 000000000..bc03d7019 --- /dev/null +++ b/gr-digital/lib/simple_correlator_impl.cc @@ -0,0 +1,237 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2010,2013 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 "simple_correlator_impl.h" +#include <digital/simple_framer_sync.h> +#include <gr_io_signature.h> +#include <blocks/count_bits.h> +#include <assert.h> +#include <stdexcept> +#include <string.h> +#include <cstdio> + +namespace gr { + namespace digital { + + static const int THRESHOLD = 3; + + simple_correlator::sptr + simple_correlator::make(int payload_bytesize) + { + return gnuradio::get_initial_sptr + (new simple_correlator_impl(payload_bytesize)); + } + + simple_correlator_impl::simple_correlator_impl(int payload_bytesize) + : gr_block("simple_correlator", + gr_make_io_signature(1, 1, sizeof(float)), + gr_make_io_signature(1, 1, sizeof(unsigned char))), + d_payload_bytesize(payload_bytesize), + d_state(ST_LOOKING), d_osi(0), + d_bblen((payload_bytesize + GRSF_PAYLOAD_OVERHEAD) * GRSF_BITS_PER_BYTE), + d_bitbuf(new unsigned char[d_bblen]), + d_pktbuf(new unsigned char[d_bblen/GRSF_BITS_PER_BYTE]), + d_bbi(0) + { + d_avbi = 0; + d_accum = 0.0; + d_avg = 0.0; + for(int i = 0; i < AVG_PERIOD; i++) + d_avgbuf[i] = 0.0; + +#ifdef DEBUG_SIMPLE_CORRELATOR + d_debug_fp = fopen("corr.log", "w"); +#endif + enter_looking(); + } + + simple_correlator_impl::~simple_correlator_impl() + { +#ifdef DEBUG_SIMPLE_CORRELATOR + fclose(d_debug_fp); +#endif + delete [] d_bitbuf; + delete [] d_pktbuf; + } + + void + simple_correlator_impl::enter_looking() + { + fflush(stdout); + // fprintf(stderr, ">>> enter_looking\n"); + d_state = ST_LOOKING; + for(int i = 0; i < OVERSAMPLE; i++) + d_shift_reg[i] = 0; + d_osi = 0; + + d_avbi = 0; + d_avg = d_avg * 0.5; + d_accum = 0; + for(int i = 0; i < AVG_PERIOD; i++) + d_avgbuf[i] = 0.0; + } + + void + simple_correlator_impl::enter_under_threshold() + { + fflush(stdout); + // fprintf(stderr, ">>> enter_under_threshold\n"); + d_state = ST_UNDER_THRESHOLD; + d_transition_osi = d_osi; + } + + void + simple_correlator_impl::enter_locked() + { + d_state = ST_LOCKED; + int delta = sub_index(d_osi, d_transition_osi); + d_center_osi = add_index(d_transition_osi, delta/2); + //d_center_osi = add_index(d_center_osi, 3); // FIXME + d_bbi = 0; + fflush(stdout); + // fprintf(stderr, ">>> enter_locked d_center_osi = %d\n", d_center_osi); + + d_avg = std::max(-1.0, std::min(1.0, d_accum * (1.0/AVG_PERIOD))); + // fprintf(stderr, ">>> enter_locked d_avg = %g\n", d_avg); + } + + static void + packit(unsigned char *pktbuf, const unsigned char *bitbuf, int bitcount) + { + for(int i = 0; i < bitcount; i += 8) { + int t = bitbuf[i+0] & 0x1; + t = (t << 1) | (bitbuf[i+1] & 0x1); + t = (t << 1) | (bitbuf[i+2] & 0x1); + t = (t << 1) | (bitbuf[i+3] & 0x1); + t = (t << 1) | (bitbuf[i+4] & 0x1); + t = (t << 1) | (bitbuf[i+5] & 0x1); + t = (t << 1) | (bitbuf[i+6] & 0x1); + t = (t << 1) | (bitbuf[i+7] & 0x1); + *pktbuf++ = t; + } + } + + void + simple_correlator_impl::update_avg(float x) + { + d_accum -= d_avgbuf[d_avbi]; + d_avgbuf[d_avbi] = x; + d_accum += x; + d_avbi = (d_avbi + 1) & (AVG_PERIOD-1); + } + + int + simple_correlator_impl::general_work(int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + const float *in = (const float*)input_items[0]; + unsigned char *out = (unsigned char*)output_items[0]; + + int n = 0; + int nin = ninput_items[0]; + int decision; + int hamming_dist; + +#ifdef DEBUG_SIMPLE_CORRELATOR + struct debug_data { + float raw_data; + float sampled; + float enter_locked; + } debug_data; +#endif + + while(n < nin) { + +#ifdef DEBUG_SIMPLE_CORRELATOR + debug_data.raw_data = in[n]; + debug_data.sampled = 0.0; + debug_data.enter_locked = 0.0; +#endif + + switch(d_state) { + case ST_LOCKED: + if(d_osi == d_center_osi) { + +#ifdef DEBUG_SIMPLE_CORRELATOR + debug_data.sampled = 1.0; +#endif + decision = slice(in[n]); + + d_bitbuf[d_bbi] = decision; + d_bbi++; + if(d_bbi >= d_bblen) { + // printf("got whole packet\n"); + packit(d_pktbuf, d_bitbuf, d_bbi); + printf("seqno %3d\n", d_pktbuf[0]); + memcpy(out, &d_pktbuf[GRSF_PAYLOAD_OVERHEAD], d_payload_bytesize); + enter_looking(); + consume_each(n + 1); + return d_payload_bytesize; + } + } + break; + + case ST_LOOKING: + case ST_UNDER_THRESHOLD: + update_avg(in[n]); + decision = slice(in[n]); + d_shift_reg[d_osi] = (d_shift_reg[d_osi] << 1) | decision; + + hamming_dist = gr::blocks::count_bits64(d_shift_reg[d_osi] ^ GRSF_SYNC); + // printf("%2d %d\n", hamming_dist, d_osi); + + if(d_state == ST_LOOKING && hamming_dist <= THRESHOLD) { + // We're seeing a good PN code, remember location + enter_under_threshold(); + } + else if(d_state == ST_UNDER_THRESHOLD && hamming_dist > THRESHOLD) { + // no longer seeing good PN code, compute center of goodness + enter_locked(); +#ifdef DEBUG_SIMPLE_CORRELATOR + debug_data.enter_locked = 1.0; +#endif + } + break; + default: + assert(0); + } + +#ifdef DEBUG_SIMPLE_CORRELATOR + fwrite(&debug_data, sizeof(debug_data), 1, d_debug_fp); +#endif + + d_osi = add_index(d_osi, 1); + n++; + } + + consume_each(n); + return 0; + } + + } /* namespace digital */ +} /* namespace gr */ diff --git a/gr-digital/lib/simple_correlator_impl.h b/gr-digital/lib/simple_correlator_impl.h new file mode 100644 index 000000000..fe324131f --- /dev/null +++ b/gr-digital/lib/simple_correlator_impl.h @@ -0,0 +1,102 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2013 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_GR_SIMPLE_CORRELATOR_IMPL_H +#define INCLUDED_GR_SIMPLE_CORRELATOR_IMPL_H + +#include <digital/simple_correlator.h> + +//#define DEBUG_SIMPLE_CORRELATOR + +namespace gr { + namespace digital { + + class simple_correlator_impl : public simple_correlator + { + private: + static const int OVERSAMPLE = 8; + enum state_t { ST_LOOKING, ST_UNDER_THRESHOLD, ST_LOCKED }; + + int d_payload_bytesize; + state_t d_state; + unsigned int d_osi; // over sample index [0,OVERSAMPLE-1] + unsigned int d_transition_osi; // first index where Hamming dist < thresh + unsigned int d_center_osi; // center of bit + unsigned long long int d_shift_reg[OVERSAMPLE]; + int d_bblen; // length of bitbuf + unsigned char *d_bitbuf; // demodulated bits + unsigned char *d_pktbuf; // temp packet buf + int d_bbi; // bitbuf index + + static const int AVG_PERIOD = 512; // must be power of 2 (for freq offset correction) + int d_avbi; + float d_avgbuf[AVG_PERIOD]; + float d_avg; + float d_accum; + +#ifdef DEBUG_SIMPLE_CORRELATOR + FILE *d_debug_fp; // binary log file +#endif + + inline int slice (float x) + { + return x >= d_avg ? 1 : 0; + } + + void update_avg(float x); + + void enter_locked (); + void enter_under_threshold (); + void enter_looking (); + + static int add_index (int a, int b) + { + int t = a + b; + if(t >= OVERSAMPLE) + t -= OVERSAMPLE; + assert(t >= 0 && t < OVERSAMPLE); + return t; + } + + static int sub_index (int a, int b) + { + int t = a - b; + if(t < 0) + t += OVERSAMPLE; + assert(t >= 0 && t < OVERSAMPLE); + return t; + } + + public: + simple_correlator_impl(int payload_bytesize); + ~simple_correlator_impl(); + + int general_work(int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace digital */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_SIMPLE_CORRELATOR_IMPL_H */ diff --git a/gr-digital/python/qa_simple_correlator.py b/gr-digital/python/qa_simple_correlator.py new file mode 100755 index 000000000..124201a55 --- /dev/null +++ b/gr-digital/python/qa_simple_correlator.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python +# +# Copyright 2013 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 +import digital_swig as digital + +class test_simple_correlator(gr_unittest.TestCase): + + def setUp(self): + self.tb = gr.top_block() + + def tearDown(self): + self.tb = None + + def test_00(self): + expected_result = ( + 0x00, 0x11, 0x22, 0x33, + 0x44, 0x55, 0x66, 0x77, + 0x88, 0x99, 0xaa, 0xbb, + 0xcc, 0xdd, 0xee, 0xff) + + # Filter taps to expand the data to oversample by 8 + # Just using a RRC for some basic filter shape + taps = gr.firdes.root_raised_cosine(8, 8, 1.0, 0.5, 21) + + src = gr.vector_source_b(expected_result) + frame = digital.simple_framer(4) + unpack = gr.packed_to_unpacked_bb(1, gr.GR_MSB_FIRST) + expand = gr.interp_fir_filter_fff(8, taps) + b2f = gr.char_to_float() + mult2 = gr.multiply_const_ff(2) + sub1 = gr.add_const_ff(-1) + op = digital.simple_correlator(4) + dst = gr.vector_sink_b() + self.tb.connect(src, frame, unpack, b2f, mult2, sub1, expand) + self.tb.connect(expand, op, dst) + self.tb.run() + result_data = dst.data() + + self.assertEqual(expected_result, result_data) + +if __name__ == '__main__': + gr_unittest.run(test_simple_correlator, "test_simple_correlator.xml") diff --git a/gr-digital/swig/digital_simple_correlator.i b/gr-digital/swig/digital_simple_correlator.i new file mode 100644 index 000000000..5ad5ca26c --- /dev/null +++ b/gr-digital/swig/digital_simple_correlator.i @@ -0,0 +1,31 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2013 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,simple_correlator); + +digital_simple_correlator_sptr digital_make_simple_correlator(int payload_bytesize); + +class digital_simple_correlator : public gr_block +{ + private: + digital_simple_correlator(int payload_bytesize); +}; diff --git a/gr-digital/swig/digital_swig.i b/gr-digital/swig/digital_swig.i index 9cb78dd0d..155efddf7 100644 --- a/gr-digital/swig/digital_swig.i +++ b/gr-digital/swig/digital_swig.i @@ -85,6 +85,7 @@ #include "digital/probe_mpsk_snr_est_c.h" #include "digital/scrambler_bb.h" #include "digital/simple_framer.h" +#include "digital/simple_correlator.h" %} %include "digital/metric_type.h" @@ -136,6 +137,7 @@ %include "digital/probe_mpsk_snr_est_c.h" %include "digital/scrambler_bb.h" %include "digital/simple_framer.h" +%include "digital/simple_correlator.h" GR_SWIG_BLOCK_MAGIC2(digital, additive_scrambler_bb); GR_SWIG_BLOCK_MAGIC2(digital, binary_slicer_fb); @@ -181,6 +183,7 @@ GR_SWIG_BLOCK_MAGIC2(digital, probe_density_b); GR_SWIG_BLOCK_MAGIC2(digital, probe_mpsk_snr_est_c); GR_SWIG_BLOCK_MAGIC2(digital, scrambler_bb); GR_SWIG_BLOCK_MAGIC2(digital, simple_framer); +GR_SWIG_BLOCK_MAGIC2(digital, simple_correlator); GR_SWIG_BLOCK_MAGIC_FACTORY(digital, cpmmod_bc, gmskmod_bc); |