Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_pll_carriertracking_cc.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 <gr_pll_carriertracking_cc.h>
28 #include <gr_io_signature.h>
29 #include <gr_sincos.h>
30 #include <math.h>
31 #include <gr_math.h>
32
33 #define M_TWOPI (2*M_PI)
34
35 gr_pll_carriertracking_cc_sptr
36 gr_make_pll_carriertracking_cc (float alpha, float beta, float max_freq, float min_freq)
37 {
38   return gr_pll_carriertracking_cc_sptr (new gr_pll_carriertracking_cc (alpha, beta, max_freq, min_freq));
39 }
40
41 gr_pll_carriertracking_cc::gr_pll_carriertracking_cc (float alpha, float beta, float max_freq, float min_freq)
42   : gr_sync_block ("pll_carriertracking_cc",
43                    gr_make_io_signature (1, 1, sizeof (gr_complex)),
44                    gr_make_io_signature (1, 1, sizeof (gr_complex))),
45     d_alpha(alpha), d_beta(beta), 
46     d_max_freq(max_freq), d_min_freq(min_freq),
47     d_phase(0), d_freq((max_freq+min_freq)/2),
48     d_locksig(0),d_lock_threshold(0),d_squelch_enable(false)
49 {
50 }
51
52 float
53 gr_pll_carriertracking_cc::mod_2pi (float in)
54 {
55   if(in>M_PI)
56     return in-M_TWOPI;
57   else if(in<-M_PI)
58     return in+M_TWOPI;
59   else
60     return in;
61 }
62
63 float
64 gr_pll_carriertracking_cc::phase_detector(gr_complex sample,float ref_phase)
65 {
66   float sample_phase;
67   //  sample_phase = atan2(sample.imag(),sample.real());
68   sample_phase = gr_fast_atan2f(sample.imag(),sample.real());
69   return mod_2pi(sample_phase-ref_phase);
70 }
71
72 bool
73 gr_pll_carriertracking_cc::lock_detector(void)
74 {
75     return (fabs(d_locksig) > d_lock_threshold);
76 }
77
78 bool
79 gr_pll_carriertracking_cc::squelch_enable(bool set_squelch)
80 {
81     return d_squelch_enable = set_squelch;
82 }
83
84 float
85 gr_pll_carriertracking_cc::set_lock_threshold(float threshold)
86 {
87     return d_lock_threshold = threshold;
88 }
89
90 int
91 gr_pll_carriertracking_cc::work (int noutput_items,
92                                  gr_vector_const_void_star &input_items,
93                                  gr_vector_void_star &output_items)
94 {
95   const gr_complex *iptr = (gr_complex *) input_items[0];
96   gr_complex *optr = (gr_complex *) output_items[0];
97
98   float error;
99   float t_imag, t_real;
100   
101   for (int i = 0; i < noutput_items; i++){
102     error = phase_detector(iptr[i],d_phase);
103     
104     d_freq = d_freq + d_beta * error;
105     d_phase = mod_2pi(d_phase + d_freq + d_alpha * error);
106     
107     if (d_freq > d_max_freq)
108       d_freq = d_max_freq;
109     else if (d_freq < d_min_freq)
110       d_freq = d_min_freq;
111     gr_sincosf(d_phase,&t_imag,&t_real);
112     optr[i] = iptr[i] * gr_complex(t_real,-t_imag);
113     d_locksig = d_locksig * (1.0 - d_alpha) + d_alpha*(iptr[i].real() * t_real + iptr[i].imag() * t_imag);
114     
115     if ((d_squelch_enable) && !lock_detector())
116       optr[i] = 0;
117   }
118   return noutput_items;
119 }