1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
/* -*- c++ -*- */
/*
* Copyright 2005,2006,2010,2011 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* GNU Radio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gr_io_signature.h>
#include <gr_prefs.h>
#include <digital_clock_recovery_mm_cc.h>
#include <gri_mmse_fir_interpolator_cc.h>
#include <stdexcept>
#include <cstdio>
// Public constructor
static const int FUDGE = 16;
digital_clock_recovery_mm_cc_sptr
digital_make_clock_recovery_mm_cc(float omega, float gain_omega,
float mu, float gain_mu,
float omega_relative_limit)
{
return gnuradio::get_initial_sptr(new digital_clock_recovery_mm_cc (omega,
gain_omega,
mu,
gain_mu,
omega_relative_limit));
}
digital_clock_recovery_mm_cc::digital_clock_recovery_mm_cc (float omega, float gain_omega,
float mu, float gain_mu,
float omega_relative_limit)
: gr_block ("clock_recovery_mm_cc",
gr_make_io_signature (1, 1, sizeof (gr_complex)),
gr_make_io_signature (1, 2, sizeof (gr_complex))),
d_mu (mu), d_omega(omega), d_gain_omega(gain_omega),
d_omega_relative_limit(omega_relative_limit),
d_gain_mu(gain_mu), d_last_sample(0), d_interp(new gri_mmse_fir_interpolator_cc()),
d_verbose(gr_prefs::singleton()->get_bool("clock_recovery_mm_cc", "verbose", false)),
d_p_2T(0), d_p_1T(0), d_p_0T(0), d_c_2T(0), d_c_1T(0), d_c_0T(0)
{
if (omega <= 0.0)
throw std::out_of_range ("clock rate must be > 0");
if (gain_mu < 0 || gain_omega < 0)
throw std::out_of_range ("Gains must be non-negative");
set_omega(omega); // also sets min and max omega
set_relative_rate (1.0 / omega);
set_history(3); // ensure 2 extra input sample is available
}
digital_clock_recovery_mm_cc::~digital_clock_recovery_mm_cc ()
{
delete d_interp;
}
void
digital_clock_recovery_mm_cc::forecast(int noutput_items, gr_vector_int &ninput_items_required)
{
unsigned ninputs = ninput_items_required.size();
for (unsigned i=0; i < ninputs; i++)
ninput_items_required[i] =
(int) ceil((noutput_items * d_omega) + d_interp->ntaps()) + FUDGE;
}
gr_complex
digital_clock_recovery_mm_cc::slicer_0deg (gr_complex sample)
{
float real=0, imag=0;
if(sample.real() > 0)
real = 1;
if(sample.imag() > 0)
imag = 1;
return gr_complex(real,imag);
}
gr_complex
digital_clock_recovery_mm_cc::slicer_45deg (gr_complex sample)
{
float real= -1, imag = -1;
if(sample.real() > 0)
real=1;
if(sample.imag() > 0)
imag = 1;
return gr_complex(real,imag);
}
/*
Modified Mueller and Muller clock recovery circuit
Based:
G. R. Danesfahani, T.G. Jeans, "Optimisation of modified Mueller and Muller
algorithm," Electronics Letters, Vol. 31, no. 13, 22 June 1995, pp. 1032 - 1033.
*/
int
digital_clock_recovery_mm_cc::general_work (int noutput_items,
gr_vector_int &ninput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items)
{
const gr_complex *in = (const gr_complex *) input_items[0];
gr_complex *out = (gr_complex *) output_items[0];
gr_complex *foptr = (gr_complex *) output_items[1];
bool write_foptr = output_items.size() >= 2;
int ii = 0; // input index
int oo = 0; // output index
int ni = ninput_items[0] - d_interp->ntaps() - FUDGE; // don't use more input than this
assert(d_mu >= 0.0);
assert(d_mu <= 1.0);
float mm_val=0;
gr_complex u, x, y;
// This loop writes the error to the second output, if it exists
if (write_foptr) {
while(oo < noutput_items && ii < ni) {
d_p_2T = d_p_1T;
d_p_1T = d_p_0T;
d_p_0T = d_interp->interpolate (&in[ii], d_mu);
d_c_2T = d_c_1T;
d_c_1T = d_c_0T;
d_c_0T = slicer_0deg(d_p_0T);
x = (d_c_0T - d_c_2T) * conj(d_p_1T);
y = (d_p_0T - d_p_2T) * conj(d_c_1T);
u = y - x;
mm_val = u.real();
out[oo++] = d_p_0T;
// limit mm_val
mm_val = gr_branchless_clip(mm_val,1.0);
d_omega = d_omega + d_gain_omega * mm_val;
d_omega = d_omega_mid + gr_branchless_clip(d_omega-d_omega_mid, d_omega_relative_limit); // make sure we don't walk away
d_mu = d_mu + d_omega + d_gain_mu * mm_val;
ii += (int)floor(d_mu);
d_mu -= floor(d_mu);
// write the error signal to the second output
foptr[oo-1] = gr_complex(d_mu,0);
if (ii < 0) // clamp it. This should only happen with bogus input
ii = 0;
}
}
// This loop does not write to the second output (ugly, but faster)
else {
while(oo < noutput_items && ii < ni) {
d_p_2T = d_p_1T;
d_p_1T = d_p_0T;
d_p_0T = d_interp->interpolate (&in[ii], d_mu);
d_c_2T = d_c_1T;
d_c_1T = d_c_0T;
d_c_0T = slicer_0deg(d_p_0T);
x = (d_c_0T - d_c_2T) * conj(d_p_1T);
y = (d_p_0T - d_p_2T) * conj(d_c_1T);
u = y - x;
mm_val = u.real();
out[oo++] = d_p_0T;
// limit mm_val
mm_val = gr_branchless_clip(mm_val,1.0);
d_omega = d_omega + d_gain_omega * mm_val;
d_omega = d_omega_mid + gr_branchless_clip(d_omega-d_omega_mid, d_omega_relative_limit); // make sure we don't walk away
d_mu = d_mu + d_omega + d_gain_mu * mm_val;
ii += (int)floor(d_mu);
d_mu -= floor(d_mu);
if(d_verbose) {
printf("%f\t%f\n", d_omega, d_mu);
}
if (ii < 0) // clamp it. This should only happen with bogus input
ii = 0;
}
}
if (ii > 0){
if (ii > ninput_items[0]){
fprintf(stderr, "gr_clock_recovery_mm_cc: ii > ninput_items[0] (%d > %d)\n",
ii, ninput_items[0]);
assert(0);
}
consume_each (ii);
}
return oo;
}
|