d4141efc7d3f740a21f6a637e350598a79e7e811
[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 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 #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 #include <cstdio>
34
35 #include <gr_complex.h>
36
37 #define M_TWOPI (2*M_PI)
38
39 gr_dd_mpsk_sync_cc_sptr
40 gr_make_dd_mpsk_sync_cc (float alpha, float beta, float max_freq, float min_freq, float ref_phase,
41                          float omega, float gain_omega, float mu, float gain_mu)
42 {
43     return gr_dd_mpsk_sync_cc_sptr (new gr_dd_mpsk_sync_cc (alpha, beta, max_freq, min_freq,ref_phase,
44                                                             omega,gain_omega,mu,gain_mu));
45 }
46
47 gr_dd_mpsk_sync_cc::gr_dd_mpsk_sync_cc (float alpha, float beta, float max_freq, float min_freq,
48                                         float ref_phase,
49                                         float omega, float gain_omega, float mu, float gain_mu)
50     : gr_block ("dd_mpsk_sync_cc",
51                 gr_make_io_signature (1, 1, sizeof (gr_complex)),
52                 gr_make_io_signature (1, 1, sizeof (gr_complex))),
53       d_alpha(alpha), d_beta(beta), 
54       d_max_freq(max_freq), d_min_freq(min_freq),
55       d_ref_phase(ref_phase),d_omega(omega), d_gain_omega(gain_omega), 
56       d_mu(mu), d_gain_mu(gain_mu),
57       d_phase(0), d_freq((max_freq+min_freq)/2), d_last_sample(0), 
58       d_interp(new gri_mmse_fir_interpolator_cc()),
59       d_dl_idx(0)
60 {
61     if (omega <= 0.0)
62         throw std::out_of_range ("clock rate must be > 0");
63     if (gain_mu <  0  || gain_omega < 0)
64         throw std::out_of_range ("Gains must be non-negative");
65
66     assert(d_interp->ntaps() <= DLLEN);
67
68     // zero double length delay line.
69     for (unsigned int i = 0; i < 2 * DLLEN; i++)
70       d_dl[i] = gr_complex(0.0,0.0);
71 }
72
73 gr_dd_mpsk_sync_cc::~gr_dd_mpsk_sync_cc()
74 {
75     delete d_interp;
76 }
77
78 float
79 gr_dd_mpsk_sync_cc::phase_detector(gr_complex sample,float ref_phase)
80 {
81   return ((sample.real()>0 ? 1.0 : -1.0) * sample.imag() -
82           (sample.imag()>0 ? 1.0 : -1.0) * sample.real());
83 }
84
85 void
86 gr_dd_mpsk_sync_cc::forecast(int noutput_items, gr_vector_int &ninput_items_required)
87 {
88   unsigned ninputs = ninput_items_required.size();
89   for (unsigned i=0; i < ninputs; i++)
90     ninput_items_required[i] =
91       (int) ceil((noutput_items * d_omega) + d_interp->ntaps());
92 }
93 gr_complex
94 gr_dd_mpsk_sync_cc::slicer_45deg (gr_complex sample)
95 {
96   float real,imag;
97   if(sample.real() > 0)
98     real=1;
99   else
100     real=-1;
101   if(sample.imag() > 0)
102     imag = 1;
103   else
104     imag = -1;
105   return gr_complex(real,imag);
106 }
107
108 gr_complex
109 gr_dd_mpsk_sync_cc::slicer_0deg (gr_complex sample)
110 {
111   gr_complex out;
112   if( fabs(sample.real()) > fabs(sample.imag()) ) {
113     if(sample.real() > 0)
114       return gr_complex(1.0,0.0);
115     else
116       return gr_complex(-1.0,0.0);
117   }
118   else {
119     if(sample.imag() > 0)
120       return gr_complex(0.0, 1.0);
121     else
122       return gr_complex(0.0, -1.0);
123   }
124 }
125
126 int
127 gr_dd_mpsk_sync_cc::general_work (int noutput_items, 
128                                   gr_vector_int &ninput_items,
129                                   gr_vector_const_void_star &input_items,
130                                   gr_vector_void_star &output_items)
131 {
132   const gr_complex *in = (gr_complex *) input_items[0];
133   gr_complex *out = (gr_complex *) output_items[0];
134   
135   int ii, oo;
136   ii = 0; oo = 0;
137   
138   float error;
139   float t_imag, t_real;
140   gr_complex nco_out;
141   float mm_val;
142
143   while (oo < noutput_items) {
144     // 
145     // generate an output sample by interpolating between the carrier
146     // tracked samples in the delay line.  d_mu, the fractional
147     // interpolation amount (in [0.0, 1.0]) is controlled by the
148     // symbol timing loop below.
149     //
150     out[oo] = d_interp->interpolate (&d_dl[d_dl_idx], d_mu);
151
152     error = phase_detector(out[oo], d_ref_phase);
153     
154     d_freq = d_freq + d_beta * error;
155     d_phase = d_phase + d_alpha * error;
156     while(d_phase>M_TWOPI)
157       d_phase -= M_TWOPI;
158     while(d_phase<-M_TWOPI)
159       d_phase += M_TWOPI;
160       
161     if (d_freq > d_max_freq)
162       d_freq = d_max_freq;
163     else if (d_freq < d_min_freq)
164       d_freq = d_min_freq;
165       
166     mm_val = real(d_last_sample * slicer_0deg(out[oo]) - out[oo] * slicer_0deg(d_last_sample));
167     d_last_sample = out[oo];
168
169     d_omega = d_omega + d_gain_omega * mm_val;
170     d_mu = d_mu + d_omega + d_gain_mu * mm_val;
171     
172     while(d_mu >= 1.0) {
173       //
174       // Generate more carrier tracked samples for the delay line
175       //
176       d_mu -= 1.0;
177       gr_sincosf(d_phase, &t_imag, &t_real);
178       nco_out = gr_complex(t_real, -t_imag);
179       gr_complex new_sample = in[ii] * nco_out;
180
181       d_dl[d_dl_idx] = new_sample;              // overwrite oldest sample
182       d_dl[(d_dl_idx + DLLEN)] = new_sample;    // and second copy
183       d_dl_idx = (d_dl_idx+1) % DLLEN;          // point to the new oldest sample
184       d_phase = d_phase + d_freq;
185       ii++;
186     }
187     oo++;
188     printf("%f\t%f\t%f\t%f\t%f\n",d_mu,d_omega,mm_val,d_freq,d_phase);
189     //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);
190   }
191
192   assert(ii <= ninput_items[0]);
193
194   consume_each (ii);
195   return noutput_items;
196 }