Statistics
| Branch: | Tag: | Revision:

gnuradio / gr-digital / lib / costas_loop_cc_impl.cc @ master

History | View | Annotate | Download (8.64 KB)

1
/* -*- c++ -*- */
2
/*
3
 * Copyright 2006,2010-2012 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 "costas_loop_cc_impl.h"
28
#include <gnuradio/io_signature.h>
29
#include <gnuradio/expj.h>
30
#include <gnuradio/sincos.h>
31
#include <gnuradio/math.h>
32
#include <boost/format.hpp>
33

34
namespace gr {
35
  namespace digital {
36

37
    costas_loop_cc::sptr
38
    costas_loop_cc::make(float loop_bw, int order, bool use_snr)
39
    {
40
      return gnuradio::get_initial_sptr
41
        (new costas_loop_cc_impl(loop_bw, order, use_snr));
42
    }
43

44
    costas_loop_cc_impl::costas_loop_cc_impl(float loop_bw, int order, bool use_snr)
45
      : sync_block("costas_loop_cc",
46
                   io_signature::make(1, 1, sizeof(gr_complex)),
47
                   io_signature::make2(1, 2, sizeof(gr_complex), sizeof(float))),
48
        blocks::control_loop(loop_bw, 1.0, -1.0),
49
        d_order(order), d_error(0), d_noise(1.0), d_phase_detector(NULL)
50
    {
51
      // Set up the phase detector to use based on the constellation order
52
      switch(d_order) {
53
      case 2:
54
        if(use_snr)
55
          d_phase_detector = &costas_loop_cc_impl::phase_detector_snr_2;
56
        else
57
          d_phase_detector = &costas_loop_cc_impl::phase_detector_2;
58
        break;
59

60
      case 4:
61
        if(use_snr)
62
          d_phase_detector = &costas_loop_cc_impl::phase_detector_snr_4;
63
        else
64
          d_phase_detector = &costas_loop_cc_impl::phase_detector_4;
65
        break;
66

67
      case 8:
68
        if(use_snr)
69
          d_phase_detector = &costas_loop_cc_impl::phase_detector_snr_8;
70
        else
71
          d_phase_detector = &costas_loop_cc_impl::phase_detector_8;
72
        break;
73

74
      default:
75
        throw std::invalid_argument("order must be 2, 4, or 8");
76
        break;
77
      }
78

79
      message_port_register_in(pmt::mp("noise"));
80
      set_msg_handler(
81
        pmt::mp("noise"),
82
        boost::bind(&costas_loop_cc_impl::handle_set_noise,
83
                    this, _1));
84
    }
85

86
    costas_loop_cc_impl::~costas_loop_cc_impl()
87
    {
88
    }
89

90
    float
91
    costas_loop_cc_impl::phase_detector_8(gr_complex sample) const
92
    {
93
      /* This technique splits the 8PSK constellation into 2 squashed
94
         QPSK constellations, one when I is larger than Q and one
95
         where Q is larger than I. The error is then calculated
96
         proportionally to these squashed constellations by the const
97
         K = sqrt(2)-1.
98

99
         The signal magnitude must be > 1 or K will incorrectly bias
100
         the error value.
101

102
         Ref: Z. Huang, Z. Yi, M. Zhang, K. Wang, "8PSK demodulation for
103
         new generation DVB-S2", IEEE Proc. Int. Conf. Communications,
104
         Circuits and Systems, Vol. 2, pp. 1447 - 1450, 2004.
105
      */
106

107
      float K = (sqrt(2.0) - 1);
108
      if(fabsf(sample.real()) >= fabsf(sample.imag())) {
109
        return ((sample.real()>0 ? 1.0 : -1.0) * sample.imag() -
110
                (sample.imag()>0 ? 1.0 : -1.0) * sample.real() * K);
111
      }
112
      else {
113
        return ((sample.real()>0 ? 1.0 : -1.0) * sample.imag() * K -
114
                (sample.imag()>0 ? 1.0 : -1.0) * sample.real());
115
      }
116
    }
117

118
    float
119
    costas_loop_cc_impl::phase_detector_4(gr_complex sample) const
120
    {
121
      return ((sample.real()>0 ? 1.0 : -1.0) * sample.imag() -
122
              (sample.imag()>0 ? 1.0 : -1.0) * sample.real());
123
    }
124

125
    float
126
    costas_loop_cc_impl::phase_detector_2(gr_complex sample) const
127
    {
128
      return (sample.real()*sample.imag());
129
    }
130

131
    float
132
    costas_loop_cc_impl::phase_detector_snr_8(gr_complex sample) const
133
    {
134
      float K = (sqrt(2.0) - 1);
135
      float snr = abs(sample)*abs(sample) / d_noise;
136
      if(fabsf(sample.real()) >= fabsf(sample.imag())) {
137
        return ((blocks::tanhf_lut(snr*sample.real()) * sample.imag()) -
138
          (blocks::tanhf_lut(snr*sample.imag()) * sample.real() * K));
139
      }
140
      else {
141
        return ((blocks::tanhf_lut(snr*sample.real()) * sample.imag() * K) -
142
          (blocks::tanhf_lut(snr*sample.imag()) * sample.real()));
143
      }
144
    }
145

146
    float
147
    costas_loop_cc_impl::phase_detector_snr_4(gr_complex sample) const
148
    {
149
      float snr = abs(sample)*abs(sample) / d_noise;
150
      return ((blocks::tanhf_lut(snr*sample.real()) * sample.imag()) -
151
              (blocks::tanhf_lut(snr*sample.imag()) * sample.real()));
152
    }
153

154
    float
155
    costas_loop_cc_impl::phase_detector_snr_2(gr_complex sample) const
156
    {
157
      float snr = abs(sample)*abs(sample) / d_noise;
158
      return blocks::tanhf_lut(snr*sample.real()) * sample.imag();
159
    }
160

161
    float
162
    costas_loop_cc_impl::error() const
163
    {
164
      return d_error;
165
    }
166

167
    void
168
    costas_loop_cc_impl::handle_set_noise(pmt::pmt_t msg)
169
    {
170
      if(pmt::is_real(msg)) {
171
        d_noise = pmt::to_double(msg);
172
        d_noise = powf(10.0f, d_noise/10.0f);
173
      }
174
    }
175

176
    int
177
    costas_loop_cc_impl::work(int noutput_items,
178
                              gr_vector_const_void_star &input_items,
179
                              gr_vector_void_star &output_items)
180
    {
181
      const gr_complex *iptr = (gr_complex *) input_items[0];
182
      gr_complex *optr = (gr_complex *) output_items[0];
183
      float *foptr = (float *) output_items[1];
184

185
      bool write_foptr = output_items.size() >= 2;
186

187
      gr_complex nco_out;
188

189
      std::vector<tag_t> tags;
190
      get_tags_in_range(tags, 0, nitems_read(0),
191
                        nitems_read(0)+noutput_items,
192
                        pmt::intern("phase_est"));
193

194
      if(write_foptr) {
195
        for(int i = 0; i < noutput_items; i++) {
196
          if(tags.size() > 0) {
197
            if(tags[0].offset-nitems_read(0) == (size_t)i) {
198
              d_phase = (float)pmt::to_double(tags[0].value);
199
              tags.erase(tags.begin());
200
            }
201
          }
202

203
          nco_out = gr_expj(-d_phase);
204
          optr[i] = iptr[i] * nco_out;
205

206
          d_error = (*this.*d_phase_detector)(optr[i]);
207
          d_error = gr::branchless_clip(d_error, 1.0);
208

209
          advance_loop(d_error);
210
          phase_wrap();
211
          frequency_limit();
212

213
          foptr[i] = d_freq;
214
        }
215
      }
216
      else {
217
        for(int i = 0; i < noutput_items; i++) {
218
          if(tags.size() > 0) {
219
            if(tags[0].offset-nitems_read(0) == (size_t)i) {
220
              d_phase = (float)pmt::to_double(tags[0].value);
221
              tags.erase(tags.begin());
222
            }
223
          }
224

225
          nco_out = gr_expj(-d_phase);
226
          optr[i] = iptr[i] * nco_out;
227

228
          d_error = (*this.*d_phase_detector)(optr[i]);
229
          d_error = gr::branchless_clip(d_error, 1.0);
230

231
          advance_loop(d_error);
232
          phase_wrap();
233
          frequency_limit();
234
        }
235
      }
236

237
      return noutput_items;
238
    }
239

240
    void
241
    costas_loop_cc_impl::setup_rpc()
242
    {
243
#ifdef GR_CTRLPORT
244
      // Getters
245
      add_rpc_variable(
246
          rpcbasic_sptr(new rpcbasic_register_get<costas_loop_cc, float>(
247
              alias(), "error",
248
              &costas_loop_cc::error,
249
              pmt::mp(-2.0f), pmt::mp(2.0f), pmt::mp(0.0f),
250
              "", "Error signal of loop", RPC_PRIVLVL_MIN,
251
              DISPTIME | DISPOPTSTRIP)));
252

253
      add_rpc_variable(
254
          rpcbasic_sptr(new rpcbasic_register_get<control_loop, float>(
255
              alias(), "frequency",
256
              &control_loop::get_frequency,
257
              pmt::mp(0.0f), pmt::mp(2.0f), pmt::mp(0.0f),
258
              "", "Frequency Est.", RPC_PRIVLVL_MIN,
259
              DISPTIME | DISPOPTSTRIP)));
260

261
      add_rpc_variable(
262
          rpcbasic_sptr(new rpcbasic_register_get<control_loop, float>(
263
              alias(), "phase",
264
              &control_loop::get_phase,
265
              pmt::mp(0.0f), pmt::mp(2.0f), pmt::mp(0.0f),
266
              "", "Phase Est.", RPC_PRIVLVL_MIN,
267
              DISPTIME | DISPOPTSTRIP)));
268

269
      add_rpc_variable(
270
          rpcbasic_sptr(new rpcbasic_register_get<control_loop, float>(
271
              alias(), "loop_bw",
272
              &control_loop::get_loop_bandwidth,
273
              pmt::mp(0.0f), pmt::mp(2.0f), pmt::mp(0.0f),
274
              "", "Loop bandwidth", RPC_PRIVLVL_MIN,
275
              DISPTIME | DISPOPTSTRIP)));
276

277
      // Setters
278
      add_rpc_variable(
279
          rpcbasic_sptr(new rpcbasic_register_set<control_loop, float>(
280
              alias(), "loop_bw",
281
              &control_loop::set_loop_bandwidth,
282
              pmt::mp(0.0f), pmt::mp(1.0f), pmt::mp(0.0f),
283
              "", "Loop bandwidth",
284
              RPC_PRIVLVL_MIN, DISPNULL)));
285
#endif /* GR_CTRLPORT */
286
    }
287

288
  } /* namespace digital */
289
} /* namespace gr */