Statistics
| Branch: | Tag: | Revision:

root / gnuradio-core / src / lib / general / gr_pll_carriertracking_cc.cc @ 4defc0e6

History | View | Annotate | Download (3.1 kB)

1
/* -*- c++ -*- */
2
/*
3
 * Copyright 2006,2010,2011 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_pll_carriertracking_cc.h>
28
#include <gr_io_signature.h>
29
#include <gr_sincos.h>
30
#include <math.h>
31
#include <gr_math.h>
32
33
#ifndef M_TWOPI
34
#define M_TWOPI (2.0f*M_PI)
35
#endif
36
37
gr_pll_carriertracking_cc_sptr
38
gr_make_pll_carriertracking_cc (float loop_bw, float max_freq, float min_freq)
39
{
40
  return gnuradio::get_initial_sptr(new gr_pll_carriertracking_cc (loop_bw, max_freq, min_freq));
41
}
42
43
gr_pll_carriertracking_cc::gr_pll_carriertracking_cc (float loop_bw,
44
                                                      float max_freq,
45
                                                      float min_freq)
46
  : gr_sync_block ("pll_carriertracking_cc",
47
                   gr_make_io_signature (1, 1, sizeof (gr_complex)),
48
                   gr_make_io_signature (1, 1, sizeof (gr_complex))),
49
    gri_control_loop(loop_bw, max_freq, min_freq),
50
    d_locksig(0), d_lock_threshold(0), d_squelch_enable(false)
51
{
52
}
53
54
float
55
gr_pll_carriertracking_cc::mod_2pi (float in)
56
{
57
  if(in>M_PI)
58
    return in-M_TWOPI;
59
  else if(in<-M_PI)
60
    return in+M_TWOPI;
61
  else
62
    return in;
63
}
64
65
float
66
gr_pll_carriertracking_cc::phase_detector(gr_complex sample,float ref_phase)
67
{
68
  float sample_phase;
69
  //  sample_phase = atan2(sample.imag(),sample.real());
70
  sample_phase = gr_fast_atan2f(sample.imag(),sample.real());
71
  return mod_2pi(sample_phase-ref_phase);
72
}
73
74
bool
75
gr_pll_carriertracking_cc::lock_detector(void)
76
{
77
    return (fabsf(d_locksig) > d_lock_threshold);
78
}
79
80
bool
81
gr_pll_carriertracking_cc::squelch_enable(bool set_squelch)
82
{
83
    return d_squelch_enable = set_squelch;
84
}
85
86
float
87
gr_pll_carriertracking_cc::set_lock_threshold(float threshold)
88
{
89
    return d_lock_threshold = threshold;
90
}
91
92
int
93
gr_pll_carriertracking_cc::work (int noutput_items,
94
                                 gr_vector_const_void_star &input_items,
95
                                 gr_vector_void_star &output_items)
96
{
97
  const gr_complex *iptr = (gr_complex *) input_items[0];
98
  gr_complex *optr = (gr_complex *) output_items[0];
99
100
  float error;
101
  float t_imag, t_real;
102
  
103
  for (int i = 0; i < noutput_items; i++){
104
    gr_sincosf(d_phase, &t_imag, &t_real);
105
    optr[i] = iptr[i] * gr_complex(t_real, -t_imag);
106
107
    error = phase_detector(iptr[i],d_phase);
108
109
    advance_loop(error);
110
    phase_wrap();
111
    frequency_limit();
112
113
    d_locksig = d_locksig * (1.0 - d_alpha) + \
114
      d_alpha*(iptr[i].real() * t_real + iptr[i].imag() * t_imag);
115
    
116
    if ((d_squelch_enable) && !lock_detector())
117
      optr[i] = 0;
118
  }
119
  return noutput_items;
120
}