File: atsc_sync_impl.cc

package info (click to toggle)
gnuradio 3.7.10.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 37,104 kB
  • ctags: 37,270
  • sloc: cpp: 150,156; python: 86,734; xml: 40,886; ansic: 19,461; fortran: 927; sh: 271; lisp: 31; makefile: 14
file content (201 lines) | stat: -rw-r--r-- 7,390 bytes parent folder | download
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
/* -*- c++ -*- */
/*
 * Copyright 2014,2016 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 "atsc_sync_impl.h"
#include "atsc_types.h"
#include <gnuradio/io_signature.h>

namespace gr {
  namespace dtv {

    static const double LOOP_FILTER_TAP = 0.0005;	// 0.0005 works
    static const double ADJUSTMENT_GAIN = 1.0e-5 / (10 * ATSC_DATA_SEGMENT_LENGTH);
    static const int	SYMBOL_INDEX_OFFSET = 3;
    static const int	MIN_SEG_LOCK_CORRELATION_VALUE = 5;
    static const char   SSI_MIN = -16;
    static const char   SSI_MAX =  15;

    atsc_sync::sptr
    atsc_sync::make(float rate)
    {
      return gnuradio::get_initial_sptr
	(new atsc_sync_impl(rate));
    }

    atsc_sync_impl::atsc_sync_impl(float rate)
      : gr::block("dtv_atsc_sync",
                  io_signature::make(1, 1, sizeof(float)),
                  io_signature::make(1, 1, sizeof(atsc_soft_data_segment))),
	d_next_input(0), d_rx_clock_to_symbol_freq(rate/ATSC_SYMBOL_RATE),
	d_si(0)
    {
      d_loop.set_taps(LOOP_FILTER_TAP);
      reset();
    }

    void
    atsc_sync_impl::reset()
    {
      d_w = d_rx_clock_to_symbol_freq;
      d_mu = 0.5;

      d_timing_adjust = 0;
      d_counter = 0;
      d_symbol_index = 0;
      d_seg_locked = false;

      d_sr = 0;

      memset(d_sample_mem, 0,       ATSC_DATA_SEGMENT_LENGTH * sizeof(*d_sample_mem));  // (float)0 = 0x00000000
      memset(d_data_mem,   0,       ATSC_DATA_SEGMENT_LENGTH * sizeof(*d_data_mem));    // (float)0 = 0x00000000
      memset(d_integrator, SSI_MIN, ATSC_DATA_SEGMENT_LENGTH * sizeof(*d_integrator));  // signed char
    }

    atsc_sync_impl::~atsc_sync_impl()
    {
    }

    void
    atsc_sync_impl::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] = static_cast<int>(noutput_items * d_rx_clock_to_symbol_freq * ATSC_DATA_SEGMENT_LENGTH) + 1500 - 1;
    }

    int
    atsc_sync_impl::general_work(int noutput_items,
                                 gr_vector_int &ninput_items,
                                 gr_vector_const_void_star &input_items,
                                 gr_vector_void_star &output_items)
    {
	const float *in = (const float *) input_items[0];
	atsc_soft_data_segment *soft_data_segment_out = (atsc_soft_data_segment *) output_items[0];

	float interp_sample;

	// amount actually consumed
	d_si = 0;

	for (d_output_produced = 0; d_output_produced < noutput_items && (d_si + (int)d_interp.ntaps()) < ninput_items[0];) {
          // First we interpolate a sample from input to work with
          interp_sample = d_interp.interpolate(&in[d_si], d_mu);

          // Apply our timing adjustment slowly over several samples
          d_mu += ADJUSTMENT_GAIN * 1e3 * d_timing_adjust;

          double s = d_mu + d_w;
          double float_incr = floor(s);
          d_mu = s - float_incr;
          d_incr = (int)float_incr;

          assert (d_incr >= 1 && d_incr <= 3);
          d_si += d_incr;

          // Remember the sample at this count position
          d_sample_mem[d_counter] = interp_sample;

          // Is the sample positive or negative?
          int bit = (interp_sample < 0 ? 0 : 1);

          // Put the sign bit into our shift register
          d_sr = ((bit & 1) << 3) | (d_sr >> 1);

          // When +,-,-,+ (0x9, 1001) samples show up we have likely found a segment
          // sync, it is more likely the segment sync will show up at about the same
          // spot every ATSC_DATA_SEGMENT_LENGTH samples so we add some weight
          // to this spot every pass to prevent random +,-,-,+ symbols from
          // confusing our synchronizer
          d_integrator[d_counter] += ((d_sr == 0x9) ? +2 : -1);
          if(d_integrator[d_counter] < SSI_MIN) d_integrator[d_counter] = SSI_MIN;
          if(d_integrator[d_counter] > SSI_MAX) d_integrator[d_counter] = SSI_MAX;

          d_symbol_index++;
          if( d_symbol_index >= ATSC_DATA_SEGMENT_LENGTH )
            d_symbol_index = 0;

          d_counter++;
          if( d_counter >= ATSC_DATA_SEGMENT_LENGTH ) {	// counter just wrapped...
            int best_correlation_value = d_integrator[0];
            int best_correlation_index = 0;

            for(int i = 1; i < ATSC_DATA_SEGMENT_LENGTH; i++)
              if (d_integrator[i] > best_correlation_value) {
                best_correlation_value = d_integrator[i];
                best_correlation_index = i;
              }

            d_seg_locked = best_correlation_value >= MIN_SEG_LOCK_CORRELATION_VALUE;

            // the coefficients are -1,-1,+1,+1
            //d_timing_adjust = d_sample_mem[best_correlation_index - 3] +
            //                   d_sample_mem[best_correlation_index - 2] -
            //                   d_sample_mem[best_correlation_index - 1] -
            //                   d_sample_mem[best_correlation_index];

            //printf( "d_timing_adjust = %f\n", d_timing_adjust );

            int corr_count = best_correlation_index;

            d_timing_adjust = -d_sample_mem[corr_count--];
            if( corr_count < 0 ) corr_count = ATSC_DATA_SEGMENT_LENGTH - 1;
            d_timing_adjust -= d_sample_mem[corr_count--];
            if( corr_count < 0 ) corr_count = ATSC_DATA_SEGMENT_LENGTH - 1;
            d_timing_adjust += d_sample_mem[corr_count--];
            if( corr_count < 0 ) corr_count = ATSC_DATA_SEGMENT_LENGTH - 1;
            d_timing_adjust += d_sample_mem[corr_count--];

            d_symbol_index = SYMBOL_INDEX_OFFSET - 1 - best_correlation_index;
            if (d_symbol_index < 0)
              d_symbol_index += ATSC_DATA_SEGMENT_LENGTH;

            d_counter = 0;
          }

          // If we are locked we can start filling and producing data packets
          // Due to the way we lock the first data packet will almost always be
          // half full, this is OK becouse the fs_checker will not let packets though
          // untill a non-corrupted field packet is found
          if( d_seg_locked ) {
            d_data_mem[d_symbol_index] = interp_sample;

            if( d_symbol_index >= (ATSC_DATA_SEGMENT_LENGTH - 1) )
              {
                for( int i = 0; i < ATSC_DATA_SEGMENT_LENGTH; i++ )
                  soft_data_segment_out[d_output_produced].data[i] = d_data_mem[i];
                d_output_produced++;
              }
          }
	}

	consume_each(d_si);
	return d_output_produced;

    }

  } /* namespace dtv */
} /* namespace gr */