Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_clock_recovery_mm_ff.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_io_signature.h>
28 #include <gr_clock_recovery_mm_ff.h>
29 #include <gri_mmse_fir_interpolator.h>
30 #include <stdexcept>
31
32 #define DEBUG_CR_MM_FF  0               // must be defined as 0 or 1
33
34 // Public constructor
35
36 gr_clock_recovery_mm_ff_sptr 
37 gr_make_clock_recovery_mm_ff(float omega, float gain_omega, float mu, float gain_mu,
38                              float omega_relative_limit)
39 {
40   return gr_clock_recovery_mm_ff_sptr (new gr_clock_recovery_mm_ff (omega,
41                                                                     gain_omega, 
42                                                                     mu,
43                                                                     gain_mu,
44                                                                     omega_relative_limit));
45 }
46
47 gr_clock_recovery_mm_ff::gr_clock_recovery_mm_ff (float omega, float gain_omega, float mu, float gain_mu,
48                                                   float omega_relative_limit)
49   : gr_block ("clock_recovery_mm_ff",
50               gr_make_io_signature (1, 1, sizeof (float)),
51               gr_make_io_signature (1, 1, sizeof (float))),
52     d_mu (mu), d_gain_omega(gain_omega), d_gain_mu(gain_mu),
53     d_last_sample(0), d_interp(new gri_mmse_fir_interpolator()),
54     d_logfile(0), d_omega_relative_limit(omega_relative_limit)
55 {
56   if (omega <  1)
57     throw std::out_of_range ("clock rate must be > 0");
58   if (gain_mu <  0  || gain_omega < 0)
59     throw std::out_of_range ("Gains must be non-negative");
60
61   set_omega(omega);                     // also sets min and max omega
62   set_relative_rate (1.0 / omega);
63
64   if (DEBUG_CR_MM_FF)
65     d_logfile = fopen("cr_mm_ff.dat", "wb");
66 }
67
68 gr_clock_recovery_mm_ff::~gr_clock_recovery_mm_ff ()
69 {
70   delete d_interp;
71
72   if (DEBUG_CR_MM_FF && d_logfile){
73     fclose(d_logfile);
74     d_logfile = 0;
75   }
76 }
77
78 void
79 gr_clock_recovery_mm_ff::forecast(int noutput_items, gr_vector_int &ninput_items_required)
80 {
81   unsigned ninputs = ninput_items_required.size();
82   for (unsigned i=0; i < ninputs; i++)
83     ninput_items_required[i] =
84       (int) ceil((noutput_items * d_omega) + d_interp->ntaps());
85 }
86
87 static inline float
88 slice(float x)
89 {
90   return x < 0 ? -1.0F : 1.0F;
91 }
92
93 /*
94  * This implements the Mueller and Müller (M&M) discrete-time error-tracking synchronizer.
95  *
96  * See "Digital Communication Receivers: Synchronization, Channel
97  * Estimation and Signal Processing" by Heinrich Meyr, Marc Moeneclaey, & Stefan Fechtel.
98  * ISBN 0-471-50275-8.
99  */
100 int
101 gr_clock_recovery_mm_ff::general_work (int noutput_items,
102                                        gr_vector_int &ninput_items,
103                                        gr_vector_const_void_star &input_items,
104                                        gr_vector_void_star &output_items)
105 {
106   const float *in = (const float *) input_items[0];
107   float *out = (float *) output_items[0];
108
109   int   ii = 0;                         // input index
110   int   oo = 0;                         // output index
111   int   ni = ninput_items[0] - d_interp->ntaps(); // don't use more input than this
112   float mm_val;
113
114   while (oo < noutput_items && ii < ni ){
115
116     // produce output sample
117     out[oo] = d_interp->interpolate (&in[ii], d_mu);
118     mm_val = slice(d_last_sample) * out[oo] - slice(out[oo]) * d_last_sample;
119     d_last_sample = out[oo];
120
121     d_omega = d_omega + d_gain_omega * mm_val;
122     d_omega = d_omega_mid + gr_branchless_clip(d_omega-d_omega_mid, d_omega_relative_limit);   // make sure we don't walk away
123     d_mu = d_mu + d_omega + d_gain_mu * mm_val;
124
125     ii += (int) floor(d_mu);
126     d_mu = d_mu - floor(d_mu);
127     oo++;
128
129     if (DEBUG_CR_MM_FF && d_logfile){
130       fwrite(&d_omega, sizeof(d_omega), 1, d_logfile);
131     }
132   }
133
134   consume_each (ii);
135
136   return oo;
137 }