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::set_alpha(float alpha)
95 gr_costas_loop_cc::set_beta(float beta)
101 gr_costas_loop_cc::work (int noutput_items,
102 gr_vector_const_void_star &input_items,
103 gr_vector_void_star &output_items)
105 const gr_complex *iptr = (gr_complex *) input_items[0];
106 gr_complex *optr = (gr_complex *) output_items[0];
107 gr_complex *foptr = (gr_complex *) output_items[1];
109 bool write_foptr = output_items.size() >= 2;
116 for (int i = 0; i < noutput_items; i++){
117 nco_out = gr_expj(-d_phase);
118 optr[i] = iptr[i] * nco_out;
120 error = (*this.*d_phase_detector)(optr[i]);
126 d_freq = d_freq + d_beta * error;
127 d_phase = d_phase + d_freq + d_alpha * error;
129 while(d_phase>M_TWOPI)
131 while(d_phase<-M_TWOPI)
134 if (d_freq > d_max_freq)
136 else if (d_freq < d_min_freq)
139 foptr[i] = gr_complex(d_freq,0);
142 for (int i = 0; i < noutput_items; i++){
143 nco_out = gr_expj(-d_phase);
144 optr[i] = iptr[i] * nco_out;
146 error = (*this.*d_phase_detector)(optr[i]);
152 d_freq = d_freq + d_beta * error;
153 d_phase = d_phase + d_freq + d_alpha * error;
155 while(d_phase>M_TWOPI)
157 while(d_phase<-M_TWOPI)
160 if (d_freq > d_max_freq)
162 else if (d_freq < d_min_freq)
167 return noutput_items;