Merge branch 'upstream' into dfsg-orig
[debian/gnuradio] / gr-atsc / src / lib / GrAtscBitTimingLoop3.cc
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
23 #include <GrAtscBitTimingLoop3.h>
24 #include <cmath>
25 #include <cstdio>
26 #include <assert.h>
27
28 using std::abs;
29
30
31 static const int NOUTPUTS = 2;
32
33 GrAtscBitTimingLoop3::GrAtscBitTimingLoop3 (double ratio_of_rx_clock_to_symbol_freq)
34   : VrDecimatingSigProc<float,float> (NOUTPUTS, (int) rint (ratio_of_rx_clock_to_symbol_freq)),
35     d_interp (ratio_of_rx_clock_to_symbol_freq), d_next_input(0),
36     d_rx_clock_to_symbol_freq (ratio_of_rx_clock_to_symbol_freq)
37
38 {
39   assert (ratio_of_rx_clock_to_symbol_freq >= 1.8);     // sanity check
40   
41   history = 1500;       // spare input samples in case we need them.
42 }
43
44 //
45 // We are nominally a 2x decimator, but our actual rate varies slightly
46 // depending on the difference between the transmitter and receiver
47 // sampling clocks.  Hence, we need to compute our input ranges
48 // explictly.
49
50 int
51 GrAtscBitTimingLoop3::forecast(VrSampleRange output,
52                                VrSampleRange inputs[]) {
53   assert (numberInputs == 1);
54   
55   /* dec:1 ratio with history */
56   inputs[0].index = d_next_input;
57   inputs[0].size =
58     ((unsigned long) (output.size * d_rx_clock_to_symbol_freq) + history - 1);
59
60   return 0;
61 }  
62
63
64 int 
65 GrAtscBitTimingLoop3::work (VrSampleRange output, void *ao[],
66                             VrSampleRange inputs[], void *ai[])
67 {
68   iType  *in = ((iType **)ai)[0];
69   oDataType *out_sample = ((oDataType **)ao)[0];
70   oTagType  *out_tag =    ((oTagType **) ao)[1];
71
72   // Force in-order computation of output stream.
73   // This is required because of our slightly variable decimation factor
74   sync (output.index);
75
76   // We are tasked with producing output.size output samples.  
77   // We will consume approximately 2 * output.size input samples.
78
79   int           si = 0;         // source index
80   unsigned int  k;              // output index
81
82   float         interp_sample;
83   int           symbol_index;
84   double        timing_adjustment = 0;
85   bool          seg_locked;
86   oTagType      tag;
87
88   memset (&tag, 0, sizeof (tag));
89
90   for (k = 0; k < output.size; k++){
91
92     if (!d_interp.update (in, inputs[0].size, &si, timing_adjustment, &interp_sample)){
93       fprintf (stderr, "GrAtscBitTimingLoop3: ran short on data...\n");
94       break;
95     }
96       
97     d_sssr.update (interp_sample, &seg_locked, &symbol_index, &timing_adjustment);
98     out_sample[k] = interp_sample;
99     tag.valid = seg_locked;
100     tag.symbol_num = symbol_index;
101     out_tag[k] = tag;
102   }
103
104   d_next_input += si;   // update next_input so forecast can get us what we need
105   return k;
106 }