Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_dd_mpsk_sync_cc.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 2, 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_dd_mpsk_sync_cc.h>
28 #include <gr_io_signature.h>
29 #include <gr_sincos.h>
30 #include <gri_mmse_fir_interpolator_cc.h>
31 #include <math.h>
32 #include <stdexcept>
33
34 #include <gr_complex.h>
35
36 #define M_TWOPI (2*M_PI)
37
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)
41 {
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));
44 }
45
46 gr_dd_mpsk_sync_cc::gr_dd_mpsk_sync_cc (float alpha, float beta, float max_freq, float min_freq,
47                                         float ref_phase,
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()),
58       d_dl_idx(0)
59 {
60     if (omega <= 0.0)
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");
64
65     assert(d_interp->ntaps() <= DLLEN);
66
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);
70 }
71
72 gr_dd_mpsk_sync_cc::~gr_dd_mpsk_sync_cc()
73 {
74     delete d_interp;
75 }
76
77 float
78 gr_dd_mpsk_sync_cc::phase_detector(gr_complex sample,float ref_phase)
79 {
80   return ((sample.real()>0 ? 1.0 : -1.0) * sample.imag() -
81           (sample.imag()>0 ? 1.0 : -1.0) * sample.real());
82 }
83
84 void
85 gr_dd_mpsk_sync_cc::forecast(int noutput_items, gr_vector_int &ninput_items_required)
86 {
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());
91 }
92 gr_complex
93 gr_dd_mpsk_sync_cc::slicer_45deg (gr_complex sample)
94 {
95   float real,imag;
96   if(sample.real() > 0)
97     real=1;
98   else
99     real=-1;
100   if(sample.imag() > 0)
101     imag = 1;
102   else
103     imag = -1;
104   return gr_complex(real,imag);
105 }
106
107 gr_complex
108 gr_dd_mpsk_sync_cc::slicer_0deg (gr_complex sample)
109 {
110   gr_complex out;
111   if( fabs(sample.real()) > fabs(sample.imag()) ) {
112     if(sample.real() > 0)
113       return gr_complex(1.0,0.0);
114     else
115       return gr_complex(-1.0,0.0);
116   }
117   else {
118     if(sample.imag() > 0)
119       return gr_complex(0.0, 1.0);
120     else
121       return gr_complex(0.0, -1.0);
122   }
123 }
124
125 int
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)
130 {
131   const gr_complex *in = (gr_complex *) input_items[0];
132   gr_complex *out = (gr_complex *) output_items[0];
133   
134   int ii, oo;
135   ii = 0; oo = 0;
136   
137   float error;
138   float t_imag, t_real;
139   gr_complex nco_out;
140   float mm_val;
141
142   while (oo < noutput_items) {
143     // 
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.
148     //
149     out[oo] = d_interp->interpolate (&d_dl[d_dl_idx], d_mu);
150
151     error = phase_detector(out[oo], d_ref_phase);
152     
153     d_freq = d_freq + d_beta * error;
154     d_phase = d_phase + d_alpha * error;
155     while(d_phase>M_TWOPI)
156       d_phase -= M_TWOPI;
157     while(d_phase<-M_TWOPI)
158       d_phase += M_TWOPI;
159       
160     if (d_freq > d_max_freq)
161       d_freq = d_max_freq;
162     else if (d_freq < d_min_freq)
163       d_freq = d_min_freq;
164       
165     mm_val = real(d_last_sample * slicer_0deg(out[oo]) - out[oo] * slicer_0deg(d_last_sample));
166     d_last_sample = out[oo];
167
168     d_omega = d_omega + d_gain_omega * mm_val;
169     d_mu = d_mu + d_omega + d_gain_mu * mm_val;
170     
171     while(d_mu >= 1.0) {
172       //
173       // Generate more carrier tracked samples for the delay line
174       //
175       d_mu -= 1.0;
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;
179
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;
184       ii++;
185     }
186     oo++;
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);
189   }
190
191   assert(ii <= ninput_items[0]);
192
193   consume_each (ii);
194   return noutput_items;
195 }