3 * Copyright 2004 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 2, 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_dd_mpsk_sync_cc.h>
28 #include <gr_io_signature.h>
29 #include <gr_sincos.h>
30 #include <gri_mmse_fir_interpolator_cc.h>
34 #include <gr_complex.h>
36 #define M_TWOPI (2*M_PI)
38 gr_dd_mpsk_sync_cc_sptr
39 gr_make_dd_mpsk_sync_cc (float alpha, float beta, float max_freq, float min_freq, float ref_phase,
40 float omega, float gain_omega, float mu, float gain_mu)
42 return gr_dd_mpsk_sync_cc_sptr (new gr_dd_mpsk_sync_cc (alpha, beta, max_freq, min_freq,ref_phase,
43 omega,gain_omega,mu,gain_mu));
46 gr_dd_mpsk_sync_cc::gr_dd_mpsk_sync_cc (float alpha, float beta, float max_freq, float min_freq,
48 float omega, float gain_omega, float mu, float gain_mu)
49 : gr_block ("dd_mpsk_sync_cc",
50 gr_make_io_signature (1, 1, sizeof (gr_complex)),
51 gr_make_io_signature (1, 1, sizeof (gr_complex))),
52 d_alpha(alpha), d_beta(beta),
53 d_max_freq(max_freq), d_min_freq(min_freq),
54 d_ref_phase(ref_phase),d_omega(omega), d_gain_omega(gain_omega),
55 d_mu(mu), d_gain_mu(gain_mu),
56 d_phase(0), d_freq((max_freq+min_freq)/2), d_last_sample(0),
57 d_interp(new gri_mmse_fir_interpolator_cc()),
61 throw std::out_of_range ("clock rate must be > 0");
62 if (gain_mu < 0 || gain_omega < 0)
63 throw std::out_of_range ("Gains must be non-negative");
65 assert(d_interp->ntaps() <= DLLEN);
67 // zero double length delay line.
68 for (unsigned int i = 0; i < 2 * DLLEN; i++)
69 d_dl[i] = gr_complex(0.0,0.0);
72 gr_dd_mpsk_sync_cc::~gr_dd_mpsk_sync_cc()
78 gr_dd_mpsk_sync_cc::phase_detector(gr_complex sample,float ref_phase)
80 return ((sample.real()>0 ? 1.0 : -1.0) * sample.imag() -
81 (sample.imag()>0 ? 1.0 : -1.0) * sample.real());
85 gr_dd_mpsk_sync_cc::forecast(int noutput_items, gr_vector_int &ninput_items_required)
87 unsigned ninputs = ninput_items_required.size();
88 for (unsigned i=0; i < ninputs; i++)
89 ninput_items_required[i] =
90 (int) ceil((noutput_items * d_omega) + d_interp->ntaps());
93 gr_dd_mpsk_sync_cc::slicer_45deg (gr_complex sample)
100 if(sample.imag() > 0)
104 return gr_complex(real,imag);
108 gr_dd_mpsk_sync_cc::slicer_0deg (gr_complex sample)
111 if( fabs(sample.real()) > fabs(sample.imag()) ) {
112 if(sample.real() > 0)
113 return gr_complex(1.0,0.0);
115 return gr_complex(-1.0,0.0);
118 if(sample.imag() > 0)
119 return gr_complex(0.0, 1.0);
121 return gr_complex(0.0, -1.0);
126 gr_dd_mpsk_sync_cc::general_work (int noutput_items,
127 gr_vector_int &ninput_items,
128 gr_vector_const_void_star &input_items,
129 gr_vector_void_star &output_items)
131 const gr_complex *in = (gr_complex *) input_items[0];
132 gr_complex *out = (gr_complex *) output_items[0];
138 float t_imag, t_real;
142 while (oo < noutput_items) {
144 // generate an output sample by interpolating between the carrier
145 // tracked samples in the delay line. d_mu, the fractional
146 // interpolation amount (in [0.0, 1.0]) is controlled by the
147 // symbol timing loop below.
149 out[oo] = d_interp->interpolate (&d_dl[d_dl_idx], d_mu);
151 error = phase_detector(out[oo], d_ref_phase);
153 d_freq = d_freq + d_beta * error;
154 d_phase = d_phase + 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)
165 mm_val = real(d_last_sample * slicer_0deg(out[oo]) - out[oo] * slicer_0deg(d_last_sample));
166 d_last_sample = out[oo];
168 d_omega = d_omega + d_gain_omega * mm_val;
169 d_mu = d_mu + d_omega + d_gain_mu * mm_val;
173 // Generate more carrier tracked samples for the delay line
176 gr_sincosf(d_phase, &t_imag, &t_real);
177 nco_out = gr_complex(t_real, -t_imag);
178 gr_complex new_sample = in[ii] * nco_out;
180 d_dl[d_dl_idx] = new_sample; // overwrite oldest sample
181 d_dl[(d_dl_idx + DLLEN)] = new_sample; // and second copy
182 d_dl_idx = (d_dl_idx+1) % DLLEN; // point to the new oldest sample
183 d_phase = d_phase + d_freq;
187 printf("%f\t%f\t%f\t%f\t%f\n",d_mu,d_omega,mm_val,d_freq,d_phase);
188 //printf("%f\t%f\t%f\t%f\t%f\t%f\t%f\n",mple).real(),slicer_0deg(d_last_sample).imag(),mm_val,d_omega,d_mu);
191 assert(ii <= ninput_items[0]);
194 return noutput_items;