Merge commit 'v3.3.0' into upstream
[debian/gnuradio] / gr-atsc / src / lib / atsc_bit_timing_loop.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006 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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <atsc_bit_timing_loop.h>
28 #include <gr_io_signature.h>
29 #include <atsc_consts.h>
30 #include <string.h>
31
32 // Input rate changed from 20MHz to 19.2 to support usrp at 3 * 6.4MHz
33 float input_rate = 19.2e6;
34 double ratio_of_rx_clock_to_symbol_freq = input_rate / ATSC_SYMBOL_RATE;
35
36
37 atsc_bit_timing_loop_sptr
38 atsc_make_bit_timing_loop()
39 {
40   return atsc_bit_timing_loop_sptr(new atsc_bit_timing_loop());
41 }
42
43
44 atsc_bit_timing_loop::atsc_bit_timing_loop()
45   : gr_block("atsc_bit_timing_loop",
46                   gr_make_io_signature(1, 1, sizeof(float)),
47                   gr_make_io_signature(2, 2, sizeof(float))),
48                   d_interp(ratio_of_rx_clock_to_symbol_freq), d_next_input(0),
49                   d_rx_clock_to_symbol_freq (ratio_of_rx_clock_to_symbol_freq),
50                   d_si(0)
51 {
52   reset();
53 }
54
55 void
56 atsc_bit_timing_loop::forecast (int noutput_items, gr_vector_int &ninput_items_required)
57 {
58   unsigned ninputs = ninput_items_required.size();
59   for (unsigned i = 0; i < ninputs; i++)
60     ninput_items_required[i] = static_cast<int>(noutput_items * d_rx_clock_to_symbol_freq) + 1500 - 1;
61
62 }
63
64 int
65 atsc_bit_timing_loop::general_work (int noutput_items,
66                                  gr_vector_int &ninput_items,
67                                  gr_vector_const_void_star &input_items,
68                                  gr_vector_void_star &output_items)
69 {
70   int   r = work (noutput_items, input_items, output_items);
71   if (r > 0)
72     consume_each (d_si);
73   return r;
74 }
75
76
77 int
78 atsc_bit_timing_loop::work (int noutput_items,
79                        gr_vector_const_void_star &input_items,
80                        gr_vector_void_star &output_items)
81 {
82   const float *in = (const float *) input_items[0];
83   float *out_sample = (float *) output_items[0];
84   atsc::syminfo *out_tag = (atsc::syminfo *) output_items[1];
85
86   assert(sizeof(float) == sizeof(atsc::syminfo));
87
88   // We are tasked with producing output.size output samples.
89   // We will consume approximately 2 * output.size input samples.
90
91   int  k;              // output index
92
93   float         interp_sample;
94   int           symbol_index;
95   double        timing_adjustment = 0;
96   bool          seg_locked;
97   atsc::syminfo    tag;
98   // ammount requested in forecast
99   unsigned long input_size = noutput_items * d_rx_clock_to_symbol_freq + 1500 -1;
100
101   memset (&tag, 0, sizeof (tag));
102
103   // ammount actually consumed
104   d_si = 0;
105   
106   for (k = 0; k < noutput_items; k++){
107     if (!d_interp.update (in, input_size, &d_si, timing_adjustment, &interp_sample)){
108       fprintf (stderr, "GrAtscBitTimingLoop3: ran short on data...\n");
109       break;
110     }
111
112     d_sssr.update (interp_sample, &seg_locked, &symbol_index, &timing_adjustment);
113     out_sample[k] = interp_sample;
114     tag.valid = seg_locked;
115     tag.symbol_num = symbol_index;
116     out_tag[k] = tag;
117
118   }
119
120   return k;
121 }