Updated FSF address in all files. Fixes ticket:51
[debian/gnuradio] / gr-atsc / src / lib / atsc_fpll.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 2, 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_fpll.h>
28 #include <gr_io_signature.h>
29 #include <atsc_consts.h>
30 #include <algorithm>
31 #include "fpll_btloop_coupling.h"
32
33
34 atsc_fpll_sptr
35 atsc_make_fpll()
36 {
37   return atsc_fpll_sptr(new atsc_fpll());
38 }
39
40
41 /*
42  * I strongly suggest that you not mess with these...
43  *
44  * They are strongly coupled into the symbol timing code and
45  * their value also sets the level of the symbols going
46  * into the equalizer and viterbi decoder.
47  */
48 static const float FPLL_AGC_REFERENCE = 2.5 * FPLL_BTLOOP_COUPLING_CONST;
49 static const float FPLL_AGC_RATE = 0.25e-6;
50
51
52
53 atsc_fpll::atsc_fpll()
54   : gr_sync_block("atsc_fpll",
55                   gr_make_io_signature(1, 1, sizeof(float)),
56                   gr_make_io_signature(1, 1, sizeof(float))),
57                   initial_phase(0)
58 {
59   initial_freq = 5.75e6 - 3e6 + 0.31e6 + 5e3; // a_initial_freq;
60   agc.set_rate (FPLL_AGC_RATE);
61   agc.set_reference (FPLL_AGC_REFERENCE);
62   initialize();
63 }
64
65
66 void
67 atsc_fpll::initialize ()
68 {
69   float Fs = 20e6;
70
71   float alpha = 1 - exp(-1.0 / Fs / 5e-6);
72
73   afci.set_taps (alpha);
74   afcq.set_taps (alpha);
75
76   printf("Setting initial_freq: %f\n",initial_freq);
77   nco.set_freq (initial_freq / Fs * 2 * M_PI);
78   nco.set_phase (initial_phase);
79 }
80
81
82 int
83 atsc_fpll::work (int noutput_items,
84                        gr_vector_const_void_star &input_items,
85                        gr_vector_void_star &output_items)
86 {
87   const float *in = (const float *) input_items[0];
88   float *out = (float *) output_items[0];
89
90   for (int k = 0; k < noutput_items; k++){
91
92     float a_cos, a_sin;
93
94     float input = agc.scale (in[k]);
95
96     nco.step ();                // increment phase
97     nco.sincos (&a_sin, &a_cos);  // compute cos and sin
98
99     float I = input * a_sin;
100     float Q = input * a_cos;
101
102     out[k] = I;
103
104     float filtered_I = afci.filter (I);
105     float filtered_Q = afcq.filter (Q);
106
107     // phase detector
108
109     float x = atan2 (filtered_Q, filtered_I);
110
111     // avoid slamming filter with big transitions
112
113     static const float limit = M_PI / 2;
114
115     if (x > limit)
116       x = limit;
117     else if (x < -limit)
118       x = -limit;
119
120     // static const float alpha = 0.037;   // Max value
121     // static const float alpha = 0.005;   // takes about 5k samples to pull in, stddev = 323
122     // static const float alpha = 0.002;   // takes about 15k samples to pull in, stddev =  69
123                                            //  or about 120k samples on noisy data,
124     static const float alpha = 0.001;
125     static const float beta = alpha * alpha / 4;
126
127     nco.adjust_phase (alpha * x);
128     nco.adjust_freq (beta * x);
129
130   }
131
132   return noutput_items;
133 }
134
135
136