portapack-mayhem/firmware/baseband/clock_recovery.hpp

179 lines
4.1 KiB
C++
Raw Normal View History

2015-07-08 15:39:24 +00: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__
#include <cstddef>
#include <array>
#include <functional>
2015-07-08 15:39:24 +00:00
#include "linear_resampler.hpp"
namespace clock_recovery {
class GardnerTimingErrorDetector {
2015-07-08 15:39:24 +00:00
public:
static constexpr size_t samples_per_symbol { 2 };
2015-07-08 15:39:24 +00: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 15:39:24 +00:00
template<typename SymbolHandler>
void operator()(
2015-07-08 15:39:24 +00:00
const float in,
SymbolHandler symbol_handler
) {
/* 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 15:39:24 +00: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 15:39:24 +00:00
}
symbol_phase = (symbol_phase + 1) % samples_per_symbol;
}
2015-07-08 15:39:24 +00:00
private:
std::array<float, 3> t { { 0.0f, 0.0f, 0.0f } };
size_t symbol_phase { 0 };
};
2015-07-08 15:39:24 +00:00
class LinearErrorFilter {
public:
2015-10-03 00:49:33 +00:00
LinearErrorFilter(
const float filter_alpha = 0.95f,
const float error_weight = -1.0f
) : filter_alpha { filter_alpha },
error_weight { error_weight }
{
}
float operator()(
const float error
) {
error_filtered = filter_alpha * error_filtered + (1.0f - filter_alpha) * error;
return error_filtered * error_weight;
}
2015-07-08 15:39:24 +00:00
private:
2015-10-03 00:49:33 +00:00
const float filter_alpha;
const float error_weight;
float error_filtered { 0.0f };
};
class FixedErrorFilter {
public:
2015-10-03 00:51:06 +00:00
FixedErrorFilter(
) {
}
FixedErrorFilter(
const float weight
2015-10-03 05:34:06 +00:00
) : weight_ { weight }
2015-10-03 00:51:06 +00:00
{
}
float operator()(
const float lateness
2015-10-03 00:49:59 +00:00
) const {
2015-10-03 05:34:06 +00:00
return (lateness < 0.0f) ? weight() : -weight();
}
float weight() const {
return weight_;
2015-07-08 15:39:24 +00:00
}
private:
float weight_ { 1.0f / 16.0f };
};
template<typename ErrorFilter>
class ClockRecovery {
public:
ClockRecovery(
const float sampling_rate,
const float symbol_rate,
ErrorFilter error_filter,
std::function<void(const float)> symbol_handler
) : symbol_handler { symbol_handler }
{
configure(sampling_rate, symbol_rate, error_filter);
}
ClockRecovery(
std::function<void(const float)> symbol_handler
) : symbol_handler { symbol_handler }
{
}
void configure(
const float sampling_rate,
const float symbol_rate,
ErrorFilter error_filter
) {
resampler.configure(sampling_rate, symbol_rate * timing_error_detector.samples_per_symbol);
error_filter = error_filter;
}
void operator()(
const float baseband_sample
2015-07-08 15:39:24 +00:00
) {
resampler(baseband_sample,
[this](const float interpolated_sample) {
this->resampler_callback(interpolated_sample);
}
);
}
private:
dsp::interpolation::LinearResampler resampler;
GardnerTimingErrorDetector timing_error_detector;
ErrorFilter error_filter;
std::function<void(const float)> symbol_handler;
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 15:39:24 +00:00
}
void symbol_callback(const float symbol, const float lateness) {
symbol_handler(symbol);
const float adjustment = error_filter(lateness);
resampler.advance(adjustment);
2015-07-08 15:39:24 +00:00
}
};
} /* namespace clock_recovery */
2015-07-08 15:39:24 +00:00
#endif/*__CLOCK_RECOVERY_H__*/