3 * Copyright 2006 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
27 #include <gr_costas_loop_cc.h>
28 #include <gr_io_signature.h>
30 #include <gr_sincos.h>
33 #define M_TWOPI (2*M_PI)
35 gr_costas_loop_cc_sptr
36 gr_make_costas_loop_cc (float alpha, float beta,
37 float max_freq, float min_freq,
39 ) throw (std::invalid_argument)
41 return gr_costas_loop_cc_sptr (new gr_costas_loop_cc (alpha, beta,
46 gr_costas_loop_cc::gr_costas_loop_cc (float alpha, float beta,
47 float max_freq, float min_freq,
49 ) throw (std::invalid_argument)
50 : gr_sync_block ("costas_loop_cc",
51 gr_make_io_signature (1, 1, sizeof (gr_complex)),
52 gr_make_io_signature (1, 2, sizeof (gr_complex))),
53 d_alpha(alpha), d_beta(beta),
54 d_max_freq(max_freq), d_min_freq(min_freq),
55 d_phase(0), d_freq((max_freq+min_freq)/2),
56 d_order(order), d_phase_detector(0)
60 d_phase_detector = &gr_costas_loop_cc::phase_detector_2;
64 d_phase_detector = &gr_costas_loop_cc::phase_detector_4;
68 throw std::invalid_argument("order must be 2 or 4");
75 gr_costas_loop_cc::phase_detector_4(gr_complex sample) const
78 return ((sample.real()>0 ? 1.0 : -1.0) * sample.imag() -
79 (sample.imag()>0 ? 1.0 : -1.0) * sample.real());
83 gr_costas_loop_cc::phase_detector_2(gr_complex sample) const
85 return (sample.real()*sample.imag());
89 gr_costas_loop_cc::work (int noutput_items,
90 gr_vector_const_void_star &input_items,
91 gr_vector_void_star &output_items)
93 const gr_complex *iptr = (gr_complex *) input_items[0];
94 gr_complex *optr = (gr_complex *) output_items[0];
95 gr_complex *foptr = (gr_complex *) output_items[1];
97 bool write_foptr = output_items.size() >= 2;
102 for (int i = 0; i < noutput_items; i++){
103 nco_out = gr_expj(-d_phase);
104 optr[i] = iptr[i] * nco_out;
106 error = (*this.*d_phase_detector)(optr[i]);
112 d_freq = d_freq + d_beta * error;
113 d_phase = d_phase + d_freq + d_alpha * error;
115 while(d_phase>M_TWOPI)
117 while(d_phase<-M_TWOPI)
120 if (d_freq > d_max_freq)
122 else if (d_freq < d_min_freq)
126 foptr[i] = gr_complex(d_freq,0);
127 //foptr[i] = gr_complex(error, 0);
130 return noutput_items;