Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_pll_freqdet_cf.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004 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 // WARNING: this file is machine generated.  Edits will be over written
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <gr_pll_freqdet_cf.h>
30 #include <gr_io_signature.h>
31 #include <math.h>
32 #include <gr_math.h>
33
34 #define M_TWOPI (2*M_PI)
35
36 gr_pll_freqdet_cf_sptr
37 gr_make_pll_freqdet_cf (float alpha, float beta, float max_freq, float min_freq)
38 {
39   return gr_pll_freqdet_cf_sptr (new gr_pll_freqdet_cf (alpha, beta, max_freq, min_freq));
40 }
41
42 gr_pll_freqdet_cf::gr_pll_freqdet_cf (float alpha, float beta, float max_freq, float min_freq)
43   : gr_sync_block ("pll_freqdet_cf",
44                    gr_make_io_signature (1, 1, sizeof (gr_complex)),
45                    gr_make_io_signature (1, 1, sizeof (float))),
46     d_alpha(alpha), d_beta(beta), 
47     d_max_freq(max_freq), d_min_freq(min_freq),
48     d_phase(0), d_freq((max_freq+min_freq)/2)
49 {
50 }
51
52 float
53 gr_pll_freqdet_cf::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_freqdet_cf::phase_detector(gr_complex sample,float ref_phase)
65 {
66   float sample_phase;
67   sample_phase = gr_fast_atan2f(sample.imag(),sample.real());
68   return mod_2pi(sample_phase-ref_phase);
69 }
70
71 int
72 gr_pll_freqdet_cf::work (int noutput_items,
73                    gr_vector_const_void_star &input_items,
74                    gr_vector_void_star &output_items)
75 {
76   const gr_complex *iptr = (gr_complex *) input_items[0];
77   float *optr = (float *) output_items[0];
78
79   float error;
80   int   size = noutput_items;
81   
82   while (size-- > 0) {
83     error = phase_detector(*iptr++,d_phase);
84     
85     d_freq = d_freq + d_beta * error;
86     d_phase = mod_2pi(d_phase + d_freq + d_alpha * error);
87     
88     if (d_freq > d_max_freq)
89       d_freq = d_max_freq;
90     else if (d_freq < d_min_freq)
91       d_freq = d_min_freq;
92     *optr++ = d_freq;
93   }
94   return noutput_items;
95 }