d6efd3409894ec0108e87004ea44ea045b43152b
[debian/gnuradio] / gr-atsc / src / lib / atsci_sliding_correlator.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002 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 #include <string>
23 #include <string.h>
24
25 #ifndef _ATSC_SLIDING_CORRELATOR_H_
26 #define _ATSC_SLIDING_CORRELATOR_H_
27
28 extern const unsigned char atsc_pn511[511];
29 extern const unsigned char atsc_pn63[63];
30
31 /*!
32  * \brief look for the PN 511 field sync pattern
33  */
34 class atsci_sliding_correlator {
35  public:
36
37   atsci_sliding_correlator ();
38   ~atsci_sliding_correlator (){};
39
40   //! input hard decision bit, return correlation (0,511)
41   // Result is the number of wrong bits.  
42   // E.g., 0 -> perfect match; 511 -> all bits are wrong
43
44   int input_bit (int bit);
45
46   //! input sample, return correlation (0,511)
47   // Result is the number of wrong bits.  
48   // E.g., 0 -> perfect match; 511 -> all bits are wrong
49
50   int input_int (int sample){
51     return input_bit (sample < 0 ? 0 : 1);
52   }
53
54   //! input sample, return correlation (0,511)
55   // Result is the number of wrong bits.  
56   // E.g., 0 -> perfect match; 511 -> all bits are wrong
57
58   int input_float (float sample){
59     return input_bit (sample < 0 ? 0 : 1);
60   }
61
62   void reset () { input.reset (); }
63   
64  private:
65
66   typedef unsigned long srblock;
67   static const int bits_per_char = 8;
68   static const int srblock_bitsize = sizeof (srblock) * bits_per_char;
69   static const int NSRBLOCKS = (511 + srblock_bitsize - 1) / srblock_bitsize;
70
71   class shift_reg {
72   public:
73     shift_reg ()  { reset (); }
74     void reset () { memset (d, 0, sizeof (d)); }
75     void shift_in (int bit);
76     srblock     d[NSRBLOCKS];
77   };
78
79   shift_reg     mask;           // pattern we're looking for
80   shift_reg     input;          // current input window
81   shift_reg     and_mask;       // bits to consider
82 };
83
84 #endif /* _ATSC_SLIDING_CORRELATOR_H_ */