1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2013 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_IMPL_H
24 #define INCLUDED_GR_SIMPLE_CORRELATOR_IMPL_H
25 
26 #include <gnuradio/digital/simple_correlator.h>
27 
28 //#define	DEBUG_SIMPLE_CORRELATOR
29 
30 namespace gr {
31 namespace digital {
32 
33 class simple_correlator_impl : public simple_correlator
34 {
35 private:
36     static const int OVERSAMPLE = 8;
37     enum state_t { ST_LOOKING, ST_UNDER_THRESHOLD, ST_LOCKED };
38 
39     int d_payload_bytesize;
40     state_t d_state;
41     unsigned int d_osi;            // over sample index [0,OVERSAMPLE-1]
42     unsigned int d_transition_osi; // first index where Hamming dist < thresh
43     unsigned int d_center_osi;     // center of bit
44     unsigned long long int d_shift_reg[OVERSAMPLE];
45     int d_bblen;             // length of bitbuf
46     unsigned char* d_bitbuf; // demodulated bits
47     unsigned char* d_pktbuf; // temp packet buf
48     int d_bbi;               // bitbuf index
49 
50     static const int AVG_PERIOD = 512; // must be power of 2 (for freq offset correction)
51     int d_avbi;
52     float d_avgbuf[AVG_PERIOD];
53     float d_avg;
54     float d_accum;
55 
56 #ifdef DEBUG_SIMPLE_CORRELATOR
57     FILE* d_debug_fp; // binary log file
58 #endif
59 
slice(float x)60     inline int slice(float x) { return x >= d_avg ? 1 : 0; }
61 
62     void update_avg(float x);
63 
64     void enter_locked();
65     void enter_under_threshold();
66     void enter_looking();
67 
add_index(int a,int b)68     static int add_index(int a, int b)
69     {
70         int t = a + b;
71         if (t >= OVERSAMPLE)
72             t -= OVERSAMPLE;
73         assert(t >= 0 && t < OVERSAMPLE);
74         return t;
75     }
76 
sub_index(int a,int b)77     static int sub_index(int a, int b)
78     {
79         int t = a - b;
80         if (t < 0)
81             t += OVERSAMPLE;
82         assert(t >= 0 && t < OVERSAMPLE);
83         return t;
84     }
85 
86 public:
87     simple_correlator_impl(int payload_bytesize);
88     ~simple_correlator_impl();
89 
90     int general_work(int noutput_items,
91                      gr_vector_int& ninput_items,
92                      gr_vector_const_void_star& input_items,
93                      gr_vector_void_star& output_items);
94 };
95 
96 } /* namespace digital */
97 } /* namespace gr */
98 
99 #endif /* INCLUDED_GR_SIMPLE_CORRELATOR_IMPL_H */
100