3 * Copyright 2002 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.
23 #include <atsci_sssr.h>
28 #include <atsci_diag_output.h>
32 * ----------------------------------------------------------------
33 * Segment Integrator Pre-Processor
35 * Compute weight passed to integrator based on
36 * correlation result and ncorr_gain2
40 sipp (bool digcorr_output)
43 return +2; // positive correlation
45 return -1; // no correlation
49 * ----------------------------------------------------------------
50 * Segment Sync Integrator...
53 static const int SSI_MIN = -16;
54 static const int SSI_MAX = 15;
57 sssr::seg_sync_integrator::reset ()
59 for (int i = 0; i < ATSC_DATA_SEGMENT_LENGTH; i++)
60 d_integrator[i] = SSI_MIN;
64 sssr::seg_sync_integrator::update (int weight, int index)
66 int t = d_integrator[index] + weight;
67 t = std::max (t, SSI_MIN);
68 t = std::min (t, SSI_MAX);
69 d_integrator[index] = t;
75 sssr::seg_sync_integrator::find_max (int *v)
77 int best_value = d_integrator[0];
80 for (int i = 1; i < ATSC_DATA_SEGMENT_LENGTH; i++)
81 if (d_integrator[i] > best_value){
82 best_value = d_integrator[i];
91 * ----------------------------------------------------------------
92 * Segment Sync and Symbol recovery
95 static const int SYMBOL_INDEX_OFFSET = 3;
96 static const int MIN_SEG_LOCK_CORRELATION_VALUE = 5;
98 atsci_sssr::atsci_sssr ()
102 if (_SSSR_DIAG_OUTPUT_){
103 const char *file = "sssr.ssout";
104 if ((d_debug_fp = fopen (file, "wb")) == 0){
111 atsci_sssr::~atsci_sssr ()
120 d_correlator.reset ();
121 d_integrator.reset ();
122 d_quad_filter.reset ();
124 for (int i = 0; i < ATSC_DATA_SEGMENT_LENGTH; i++)
125 d_quad_output[i] = 0;
130 d_seg_locked = false;
134 atsci_sssr::update (sssr::sample_t sample_in, // input
135 bool *seg_locked, // are we seeing segment syncs?
136 int *symbol_index, // 0..831
137 double *timing_adjust) // how much to adjust timing
139 double qo = d_quad_filter.update (sample_in);
140 d_quad_output[d_counter] = qo;
142 int bit = gr_signbit (sample_in) ^ 1; // slice on sign: + => 1, - => 0
143 int corr_out = d_correlator.update (bit);
144 int weight = sipp (corr_out);
145 int corr_value = d_integrator.update (weight, d_counter);
146 int best_correlation_index = -1;
148 incr_symbol_index ();
149 if (incr_counter ()){ // counter just wrapped...
150 int best_correlation_value;
151 best_correlation_index = d_integrator.find_max (&best_correlation_value);
152 d_seg_locked = best_correlation_value >= MIN_SEG_LOCK_CORRELATION_VALUE;
153 d_timing_adjust = d_quad_output[best_correlation_index];
155 d_symbol_index = SYMBOL_INDEX_OFFSET - 1 - best_correlation_index;
156 if (d_symbol_index < 0)
157 d_symbol_index += ATSC_DATA_SEGMENT_LENGTH;
160 *seg_locked = d_seg_locked;
161 *symbol_index = d_symbol_index;
162 *timing_adjust = d_timing_adjust;
164 if (_SSSR_DIAG_OUTPUT_){
167 iodata[1] = corr_value;
169 iodata[3] = d_counter;
170 iodata[4] = d_symbol_index;
171 iodata[5] = best_correlation_index;
172 iodata[6] = d_timing_adjust;
174 if (fwrite (iodata, sizeof (iodata), 1, d_debug_fp) != 1){
175 perror ("fwrite: sssr");
183 * ----------------------------------------------------------------
184 * Interpolator control for Seg & Symbol Sync Recovery
187 static const double LOOP_FILTER_TAP = 0.00025; // 0.0005 works
188 static const double ADJUSTMENT_GAIN = 1.0e-5 / (10 * ATSC_DATA_SEGMENT_LENGTH);
190 atsci_interpolator::atsci_interpolator (double nominal_ratio_of_rx_clock_to_symbol_freq)
192 // Tweaked ratio from 1.8 to 1.78 to support input rate of 19.2MHz
193 assert (nominal_ratio_of_rx_clock_to_symbol_freq >= 1.78);
194 d_nominal_ratio_of_rx_clock_to_symbol_freq =
195 nominal_ratio_of_rx_clock_to_symbol_freq;
197 d_loop.set_taps (LOOP_FILTER_TAP);
201 if (_SSSR_DIAG_OUTPUT_){
202 const char *file = "interp.ssout";
203 if ((d_debug_fp = fopen (file, "wb")) == 0){
211 atsci_interpolator::~atsci_interpolator ()
218 atsci_interpolator::reset ()
220 d_w = d_nominal_ratio_of_rx_clock_to_symbol_freq;
227 atsci_interpolator::update (
228 const sssr::sample_t input_samples[], // I: vector of samples
229 int nsamples, // I: total number of samples
230 int *index, // I/O: current input index
231 double timing_adjustment, // I: how much to bump timing
232 sssr::sample_t *output_sample)
234 if (*index + (int) ntaps () > nsamples)
237 // FIXME Confirm that this is right. I think it is. It was (1-d_mu)
238 *output_sample = d_interp.interpolate (&input_samples[*index], d_mu);
240 double filter_out = 0;
244 filter_out = d_loop.filter (timing_adjustment);
245 d_w = d_w + ADJUSTMENT_GAIN * filter_out * 1e-3;
249 filter_out = d_loop.filter (timing_adjustment);
250 d_mu = d_mu + ADJUSTMENT_GAIN * 10e3 * filter_out;
254 static const double alpha = 0.01;
255 static const double beta = alpha * alpha / 16;
257 double x = ADJUSTMENT_GAIN * 10e3 * timing_adjustment;
259 d_mu = d_mu + alpha * x; // conceptually "phase"
260 d_w = d_w + beta * x; // conceptually "frequency"
264 double s = d_mu + d_w;
265 double float_incr = floor (s);
266 d_mu = s - float_incr;
267 d_incr = (int) float_incr;
269 assert (d_incr >= 1 && d_incr <= 3);
272 if (_SSSR_DIAG_OUTPUT_){
274 iodata[0] = timing_adjustment;
275 iodata[1] = filter_out;
279 iodata[5] = *output_sample;
280 if (fwrite (iodata, sizeof (iodata), 1, d_debug_fp) != 1){
281 perror ("fwrite: interpolate");