summaryrefslogtreecommitdiff
path: root/gr-digital/lib/lms_dd_equalizer_cc_impl.cc
blob: 530b3aa6fdd56a748691e29b6356c5438107af2d (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
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
/* -*- c++ -*- */
/*
 * Copyright 2011,2012 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 "lms_dd_equalizer_cc_impl.h"
#include <gnuradio/io_signature.h>
#include <gnuradio/misc.h>
#include <volk/volk.h>

namespace gr {
  namespace digital {

    using namespace filter::kernel;

    lms_dd_equalizer_cc::sptr
    lms_dd_equalizer_cc::make(int num_taps, float mu, int sps,
			      constellation_sptr cnst)
    {
      return gnuradio::get_initial_sptr
	(new lms_dd_equalizer_cc_impl(num_taps, mu, sps, cnst));
    }

    lms_dd_equalizer_cc_impl::lms_dd_equalizer_cc_impl(int num_taps, float mu,
						       int sps,
						       constellation_sptr cnst)
      : sync_decimator("lms_dd_equalizer_cc",
			  io_signature::make(1, 1, sizeof(gr_complex)),
			  io_signature::make(1, 1, sizeof(gr_complex)),
			  sps),
	fir_filter_ccc(sps, std::vector<gr_complex>(num_taps, gr_complex(0,0))),
	d_new_taps(num_taps, gr_complex(0,0)),
	d_updated(false), d_cnst(cnst)
    {
      set_gain(mu);
      if(num_taps > 0)
	d_new_taps[0] = 1.0;
      fir_filter_ccc::set_taps(d_new_taps);

      const int alignment_multiple =
	volk_get_alignment() / sizeof(gr_complex);
      set_alignment(std::max(1,alignment_multiple));

      set_history(num_taps);
    }

    lms_dd_equalizer_cc_impl::~lms_dd_equalizer_cc_impl()
    {
    }

    void
    lms_dd_equalizer_cc_impl::set_taps(const std::vector<gr_complex> &taps)
    {
      d_new_taps = taps;
      d_updated = true;
    }

    std::vector<gr_complex>
    lms_dd_equalizer_cc_impl::taps() const
    {
      return d_taps;
    }

    gr_complex
    lms_dd_equalizer_cc_impl::error(const gr_complex &out)
    { 
      gr_complex decision, error;
      d_cnst->map_to_points(d_cnst->decision_maker(&out), &decision);
      error = decision - out;
      return error;
    }

    void
    lms_dd_equalizer_cc_impl::update_tap(gr_complex &tap, const gr_complex &in) 
    {
      tap += d_mu*conj(in)*d_error;
    }

    int
    lms_dd_equalizer_cc_impl::work(int noutput_items,
				   gr_vector_const_void_star &input_items,
				   gr_vector_void_star &output_items)
    {
      const gr_complex *in = (const gr_complex *)input_items[0];
      gr_complex *out = (gr_complex *)output_items[0];

      if(d_updated) {
	d_taps = d_new_taps;
	set_history(d_taps.size());
	d_updated = false;
	return 0;		     // history requirements may have changed.
      }

      int j = 0;
      size_t k, l = d_taps.size();
      for(int i = 0; i < noutput_items; i++) {
	out[i] = filter(&in[j]);

	// Adjust taps
	d_error = error(out[i]);
	for(k = 0; k < l; k++) {
	  // Update tap locally from error.
	  update_tap(d_taps[k], in[j+k]);

	  // Update aligned taps in filter object.
	  fir_filter_ccc::update_tap(d_taps[k], k);
	}

	j += decimation();
      }

      return noutput_items;
    }

  } /* namespace digital */
} /* namespace gr */