Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_simple_correlator.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifndef INCLUDED_GR_SIMPLE_CORRELATOR_H
24 #define INCLUDED_GR_SIMPLE_CORRELATOR_H
25
26 #include <gr_block.h>
27 #include <assert.h>
28
29 //#define       DEBUG_SIMPLE_CORRELATOR
30
31 class gr_simple_correlator;
32 typedef boost::shared_ptr<gr_simple_correlator> gr_simple_correlator_sptr;
33
34 gr_simple_correlator_sptr gr_make_simple_correlator (int payload_bytesize);
35
36 /*!
37  * \brief inverse of gr_simple_framer (more or less)
38  * \ingroup sync_blk
39  */
40 class gr_simple_correlator : public gr_block
41 {
42   static const int OVERSAMPLE = 8;
43   enum state_t { ST_LOOKING, ST_UNDER_THRESHOLD, ST_LOCKED };
44   
45   int            d_payload_bytesize;
46   state_t        d_state;
47   unsigned int   d_osi;                         // over sample index [0,OVERSAMPLE-1]
48   unsigned int   d_transition_osi;              // first index where Hamming dist < thresh
49   unsigned int   d_center_osi;                  // center of bit
50   unsigned long long int d_shift_reg[OVERSAMPLE];
51   int            d_bblen;                       // length of bitbuf
52   unsigned char *d_bitbuf;                      // demodulated bits
53   int            d_bbi;                         // bitbuf index
54
55   static const int AVG_PERIOD = 512;            // must be power of 2 (for freq offset correction)
56   int   d_avbi;
57   float d_avgbuf[AVG_PERIOD];
58   float d_avg;
59   float d_accum;
60
61 #ifdef DEBUG_SIMPLE_CORRELATOR
62   FILE          *d_debug_fp;                    // binary log file
63 #endif
64
65   friend gr_simple_correlator_sptr gr_make_simple_correlator (int payload_bytesize);
66   gr_simple_correlator (int payload_bytesize);
67
68
69   inline int slice (float x)
70   {
71     return x >= d_avg ? 1 : 0;
72   }
73
74   void update_avg(float x);
75
76   void enter_locked ();
77   void enter_under_threshold ();
78   void enter_looking ();
79   
80   static int add_index (int a, int b)
81   {
82     int t = a + b;
83     if (t >= OVERSAMPLE)
84       t -= OVERSAMPLE;
85     assert (t >= 0 && t < OVERSAMPLE);
86     return t;
87   }
88
89   static int sub_index (int a, int b)
90   {
91     int t = a - b;
92     if (t < 0)
93       t += OVERSAMPLE;
94     assert (t >= 0 && t < OVERSAMPLE);
95     return t;
96   }
97
98   
99  public:
100   ~gr_simple_correlator ();
101
102   int general_work (int noutput_items,
103                     gr_vector_int &ninput_items,
104                     gr_vector_const_void_star &input_items,
105                     gr_vector_void_star &output_items);
106 };
107
108
109 #endif /* INCLUDED_GR_SIMPLE_CORRELATOR_H */