summaryrefslogtreecommitdiff
path: root/gr-noaa/lib/noaa_hrpt_sync_fb.cc
blob: 9c655b0d98fef5dbb8dced85c79e821b452f749c (plain)
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
/* -*- c++ -*- */
/*
 * Copyright 2009 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 <noaa_hrpt_sync_fb.h>
#include <gr_io_signature.h>

inline int signum(float f)
{
  return f >= 0.0 ? 1 : -1;
}

noaa_hrpt_sync_fb_sptr
noaa_make_hrpt_sync_fb(float alpha, float beta, float sps, float max_offset)
{
  return gnuradio::get_initial_sptr(new noaa_hrpt_sync_fb(alpha, beta, sps, max_offset));
}

noaa_hrpt_sync_fb::noaa_hrpt_sync_fb(float alpha, float beta, float sps, float max_offset)
  : gr_block("noaa_hrpt_sync_fb",
	     gr_make_io_signature(1, 1, sizeof(float)),
	     gr_make_io_signature(1, 1, sizeof(char))),
    d_alpha(alpha), d_beta(beta), 
    d_sps(sps), d_max_offset(max_offset),
    d_phase(0.0), d_freq(1.0/sps),
    d_last_sign(1)
{
}

int
noaa_hrpt_sync_fb::general_work(int noutput_items,
				gr_vector_int &ninput_items,
				gr_vector_const_void_star &input_items,
				gr_vector_void_star &output_items)
{
  int ninputs = ninput_items[0];
  const float *in = (const float *)input_items[0];
  char *out = (char *)output_items[0];

  int i = 0, j = 0;
  while (i < ninputs && j < noutput_items) {
    float sample = in[i++];
    int sign = signum(sample);
    d_phase += d_freq;

    // Train on zero crossings in center region of symbol
    if (sign != d_last_sign) {
      float phase_err = 0.0;
      if (d_phase > 0.25 && d_phase < 0.75)
	phase_err = d_phase-0.5;
      else if (d_phase >= 0.75)
	phase_err = d_phase - 1.0;
      else
	phase_err = d_phase;

      d_phase -= phase_err*d_alpha;        // 1st order phase adjustment
      d_freq -= phase_err*d_beta;          // 2nd order frequency adjustment

      d_last_sign = sign;
    }

    if (d_phase > 1.0) {
      if (sample < 0.0)
	out[j++] = 1;
      else
	out[j++] = 0;
      d_phase -= 1.0;
    }
  }

  consume_each(i);
  return j;
}