hamnet70-gnuradio-legacy/gr-hamnet70/lib/qam_phase_tracker_impl.cc

133 lines
3.5 KiB
C++
Raw Normal View History

/* -*- c++ -*- */
/*
* Copyright 2019 Thomas Kolb.
*
* This 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.
*
* This software 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 this software; 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 <gnuradio/io_signature.h>
#include <gnuradio/expj.h>
#include <gnuradio/math.h>
#include "qam_phase_tracker_impl.h"
namespace gr {
namespace hamnet70 {
qam_phase_tracker::sptr
qam_phase_tracker::make(const std::vector<gr_complex> &symbols, float alpha, const std::string &start_tag)
{
return gnuradio::get_initial_sptr
(new qam_phase_tracker_impl(symbols, alpha, start_tag));
}
/*
* The private constructor
*/
qam_phase_tracker_impl::qam_phase_tracker_impl(const std::vector<gr_complex> &symbols, float alpha, const std::string &start_tag)
: gr::sync_block("qam_phase_tracker",
gr::io_signature::make(1, 1, sizeof(gr_complex)),
gr::io_signature::make(1, 1, sizeof(gr_complex))),
d_alpha(alpha),
d_startTag(pmt::intern(start_tag)),
d_avgPhaseOffset(0.0f)
{
// use only symbols in the upper right quadrant
for(auto s : symbols) {
if(s.real() >= 0 && s.imag() >= 0) {
d_refSymbols.push_back(s);
d_refPhase.push_back(gr::fast_atan2f(s));
}
}
}
/*
* Our virtual destructor.
*/
qam_phase_tracker_impl::~qam_phase_tracker_impl()
{
}
int
qam_phase_tracker_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];
for(int i = 0; i < noutput_items; i++) {
out[i] = in[i] * gr_expj(-d_avgPhaseOffset);
std::vector<tag_t> tags;
get_tags_in_window(tags, 0, i, i+1, d_startTag);
if(tags.size() > 0) {
// start tag found on this item -> reset average
d_avgPhaseOffset = 0;
}
// update average phase offset from current symbol
gr_complex rotated = out[i];
float re = rotated.real();
float im = rotated.imag();
// normalize to positive quadrant
if(re < 0 && im > 0) {
rotated *= gr_expj(-M_PI/2);
} else if(re < 0 && im < 0) {
rotated *= -1;
} else if(im < 0) { // && re > 0
rotated *= gr_expj(M_PI/2);
} // else already ok
// find closest reference symbol
float min_dist = 1e9;
size_t closest_idx;
for(size_t r = 0; r < d_refSymbols.size(); r++) {
float d = std::norm(d_refSymbols[r] - rotated);
if(d < min_dist) {
closest_idx = r;
min_dist = d;
}
}
float phase = gr::fast_atan2f(rotated);
float delta_phase = phase - d_refPhase[closest_idx];
if(delta_phase > M_PI/4 || delta_phase < -M_PI/4) {
continue;
}
d_avgPhaseOffset += d_alpha * delta_phase;
}
// Tell runtime system how many output items we produced.
return noutput_items;
}
} /* namespace hamnet70 */
} /* namespace gr */