2015-07-08 11:39:24 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program 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 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program 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 program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __CLOCK_RECOVERY_H__
|
|
|
|
#define __CLOCK_RECOVERY_H__
|
|
|
|
|
2015-09-26 01:13:36 -04:00
|
|
|
#include <cstddef>
|
|
|
|
#include <array>
|
|
|
|
#include <functional>
|
2015-07-08 11:39:24 -04:00
|
|
|
|
2015-09-26 01:13:36 -04:00
|
|
|
#include "linear_resampler.hpp"
|
|
|
|
|
|
|
|
namespace clock_recovery {
|
|
|
|
|
|
|
|
class GardnerTimingErrorDetector {
|
2015-07-08 11:39:24 -04:00
|
|
|
public:
|
2015-09-26 01:13:36 -04:00
|
|
|
static constexpr size_t samples_per_symbol { 2 };
|
2015-07-08 11:39:24 -04:00
|
|
|
|
2015-09-26 01:13:36 -04:00
|
|
|
/*
|
|
|
|
Expects retimed samples at a rate of twice the expected symbol rate.
|
|
|
|
Calculates timing error, sends symbol and error to handler.
|
|
|
|
*/
|
2015-07-08 11:39:24 -04:00
|
|
|
template<typename SymbolHandler>
|
2015-09-26 01:13:36 -04:00
|
|
|
void operator()(
|
2015-07-08 11:39:24 -04:00
|
|
|
const float in,
|
|
|
|
SymbolHandler symbol_handler
|
|
|
|
) {
|
2015-09-26 01:13:36 -04:00
|
|
|
/* NOTE: Algorithm is sensitive to input magnitude. Timing error value
|
|
|
|
* will scale proportionally. Best practice is to use error sign only.
|
|
|
|
*/
|
|
|
|
t[2] = t[1];
|
|
|
|
t[1] = t[0];
|
|
|
|
t[0] = in;
|
2015-07-08 11:39:24 -04:00
|
|
|
|
2015-09-26 01:13:36 -04:00
|
|
|
if( symbol_phase == 0 ) {
|
|
|
|
const auto symbol = t[0];
|
|
|
|
const float lateness = (t[0] - t[2]) * t[1];
|
|
|
|
symbol_handler(symbol, lateness);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
2015-09-26 01:13:36 -04:00
|
|
|
symbol_phase = (symbol_phase + 1) % samples_per_symbol;
|
|
|
|
}
|
2015-07-08 11:39:24 -04:00
|
|
|
|
2015-09-26 01:13:36 -04:00
|
|
|
private:
|
2016-11-26 19:50:44 -05:00
|
|
|
std::array<float, 3> t { };
|
2015-09-26 01:13:36 -04:00
|
|
|
size_t symbol_phase { 0 };
|
|
|
|
};
|
2015-07-08 11:39:24 -04:00
|
|
|
|
2015-09-26 01:13:36 -04:00
|
|
|
class LinearErrorFilter {
|
|
|
|
public:
|
2015-10-02 20:49:33 -04:00
|
|
|
LinearErrorFilter(
|
|
|
|
const float filter_alpha = 0.95f,
|
|
|
|
const float error_weight = -1.0f
|
|
|
|
) : filter_alpha { filter_alpha },
|
|
|
|
error_weight { error_weight }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-09-26 01:13:36 -04:00
|
|
|
float operator()(
|
|
|
|
const float error
|
|
|
|
) {
|
|
|
|
error_filtered = filter_alpha * error_filtered + (1.0f - filter_alpha) * error;
|
|
|
|
return error_filtered * error_weight;
|
|
|
|
}
|
2015-07-08 11:39:24 -04:00
|
|
|
|
2015-09-26 01:13:36 -04:00
|
|
|
private:
|
2015-10-02 20:49:33 -04:00
|
|
|
const float filter_alpha;
|
|
|
|
const float error_weight;
|
2015-09-26 01:13:36 -04:00
|
|
|
float error_filtered { 0.0f };
|
|
|
|
};
|
2015-09-02 22:19:43 -04:00
|
|
|
|
2015-09-26 01:13:36 -04:00
|
|
|
class FixedErrorFilter {
|
|
|
|
public:
|
2015-10-02 20:51:06 -04:00
|
|
|
FixedErrorFilter(
|
2015-11-05 13:19:05 -05:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
FixedErrorFilter(
|
|
|
|
const float weight
|
2015-10-03 01:34:06 -04:00
|
|
|
) : weight_ { weight }
|
2015-10-02 20:51:06 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-09-26 01:13:36 -04:00
|
|
|
float operator()(
|
|
|
|
const float lateness
|
2015-10-02 20:49:59 -04:00
|
|
|
) const {
|
2015-10-03 01:34:06 -04:00
|
|
|
return (lateness < 0.0f) ? weight() : -weight();
|
|
|
|
}
|
|
|
|
|
|
|
|
float weight() const {
|
|
|
|
return weight_;
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-11-05 13:19:05 -05:00
|
|
|
float weight_ { 1.0f / 16.0f };
|
2015-09-26 01:13:36 -04:00
|
|
|
};
|
|
|
|
|
2015-10-03 01:32:58 -04:00
|
|
|
template<typename ErrorFilter>
|
2015-09-26 01:13:36 -04:00
|
|
|
class ClockRecovery {
|
|
|
|
public:
|
2016-02-04 01:47:22 -05:00
|
|
|
using SymbolHandler = std::function<void(const float)>;
|
|
|
|
|
2015-11-10 17:04:50 -05:00
|
|
|
ClockRecovery(
|
|
|
|
const float sampling_rate,
|
|
|
|
const float symbol_rate,
|
|
|
|
ErrorFilter error_filter,
|
2016-02-04 01:47:22 -05:00
|
|
|
SymbolHandler symbol_handler
|
|
|
|
) : symbol_handler { std::move(symbol_handler) }
|
2015-11-10 17:04:50 -05:00
|
|
|
{
|
|
|
|
configure(sampling_rate, symbol_rate, error_filter);
|
|
|
|
}
|
|
|
|
|
2015-09-26 01:13:36 -04:00
|
|
|
ClockRecovery(
|
2016-02-04 01:47:22 -05:00
|
|
|
SymbolHandler symbol_handler
|
|
|
|
) : symbol_handler { std::move(symbol_handler) }
|
2015-09-26 01:13:36 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void configure(
|
|
|
|
const float sampling_rate,
|
2015-11-05 13:19:05 -05:00
|
|
|
const float symbol_rate,
|
|
|
|
ErrorFilter error_filter
|
2015-09-26 01:13:36 -04:00
|
|
|
) {
|
|
|
|
resampler.configure(sampling_rate, symbol_rate * timing_error_detector.samples_per_symbol);
|
2015-11-05 13:19:05 -05:00
|
|
|
error_filter = error_filter;
|
2015-09-26 01:13:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(
|
|
|
|
const float baseband_sample
|
2015-07-08 11:39:24 -04:00
|
|
|
) {
|
2015-09-26 01:13:36 -04:00
|
|
|
resampler(baseband_sample,
|
|
|
|
[this](const float interpolated_sample) {
|
|
|
|
this->resampler_callback(interpolated_sample);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-11-26 19:50:44 -05:00
|
|
|
dsp::interpolation::LinearResampler resampler { };
|
|
|
|
GardnerTimingErrorDetector timing_error_detector { };
|
|
|
|
ErrorFilter error_filter { };
|
2016-02-04 01:47:22 -05:00
|
|
|
const SymbolHandler symbol_handler;
|
2015-09-26 01:13:36 -04:00
|
|
|
|
|
|
|
void resampler_callback(const float interpolated_sample) {
|
|
|
|
timing_error_detector(interpolated_sample,
|
|
|
|
[this](const float symbol, const float lateness) {
|
|
|
|
this->symbol_callback(symbol, lateness);
|
|
|
|
}
|
|
|
|
);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
2015-09-26 01:13:36 -04:00
|
|
|
void symbol_callback(const float symbol, const float lateness) {
|
2016-02-23 16:30:41 -05:00
|
|
|
// NOTE: This check is to avoid std::function nullptr check, which
|
|
|
|
// brings in "_ZSt25__throw_bad_function_callv" and a lot of extra code.
|
|
|
|
// TODO: Make symbol_handler known at compile time.
|
|
|
|
if( symbol_handler) {
|
|
|
|
symbol_handler(symbol);
|
|
|
|
}
|
2015-09-26 01:13:36 -04:00
|
|
|
|
|
|
|
const float adjustment = error_filter(lateness);
|
|
|
|
resampler.advance(adjustment);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-26 01:13:36 -04:00
|
|
|
} /* namespace clock_recovery */
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
#endif/*__CLOCK_RECOVERY_H__*/
|