2016-07-12 02:12:58 +00:00
|
|
|
//
|
|
|
|
// DigitalPhaseLockedLoop.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 11/07/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "DigitalPhaseLockedLoop.hpp"
|
2016-07-27 20:24:24 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdlib>
|
2016-07-13 00:23:56 +00:00
|
|
|
|
|
|
|
using namespace Storage;
|
|
|
|
|
2016-07-28 15:32:14 +00:00
|
|
|
DigitalPhaseLockedLoop::DigitalPhaseLockedLoop(int clocks_per_bit, int tolerance, size_t length_of_history) :
|
2016-07-13 00:23:56 +00:00
|
|
|
_clocks_per_bit(clocks_per_bit),
|
|
|
|
_tolerance(tolerance),
|
2016-07-28 15:32:14 +00:00
|
|
|
|
|
|
|
_phase(0),
|
|
|
|
_window_length(clocks_per_bit),
|
|
|
|
|
|
|
|
_phase_error_pointer(0)
|
|
|
|
{
|
|
|
|
_phase_error_history.reset(new std::vector<int>(length_of_history, 0));
|
|
|
|
}
|
2016-07-13 00:23:56 +00:00
|
|
|
|
2016-07-14 23:42:01 +00:00
|
|
|
void DigitalPhaseLockedLoop::run_for_cycles(int number_of_cycles)
|
2016-07-13 00:23:56 +00:00
|
|
|
{
|
2016-07-28 15:32:14 +00:00
|
|
|
_phase += number_of_cycles;
|
2016-08-03 01:28:50 +00:00
|
|
|
if(_phase >= _window_length)
|
2016-07-13 00:23:56 +00:00
|
|
|
{
|
2016-08-03 01:28:50 +00:00
|
|
|
int windows_crossed = _phase / _window_length;
|
|
|
|
|
2016-07-28 18:35:39 +00:00
|
|
|
// check whether this triggers any 0s, if anybody cares
|
|
|
|
if(_delegate)
|
2016-07-13 00:23:56 +00:00
|
|
|
{
|
2016-07-28 18:35:39 +00:00
|
|
|
if(_window_was_filled) windows_crossed--;
|
|
|
|
for(int c = 0; c < windows_crossed; c++)
|
|
|
|
_delegate->digital_phase_locked_loop_output_bit(0);
|
2016-07-13 00:23:56 +00:00
|
|
|
}
|
2016-07-28 18:35:39 +00:00
|
|
|
|
|
|
|
_window_was_filled = false;
|
2016-07-28 15:32:14 +00:00
|
|
|
_phase %= _window_length;
|
2016-07-13 00:23:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DigitalPhaseLockedLoop::add_pulse()
|
|
|
|
{
|
2016-07-28 15:32:14 +00:00
|
|
|
if(!_window_was_filled)
|
2016-07-14 11:31:23 +00:00
|
|
|
{
|
2016-07-28 15:32:14 +00:00
|
|
|
if(_delegate) _delegate->digital_phase_locked_loop_output_bit(1);
|
|
|
|
_window_was_filled = true;
|
|
|
|
post_phase_error(_phase - (_window_length >> 1));
|
2016-07-14 23:42:01 +00:00
|
|
|
}
|
2016-07-28 15:32:14 +00:00
|
|
|
}
|
2016-07-14 23:42:01 +00:00
|
|
|
|
2016-07-28 15:32:14 +00:00
|
|
|
void DigitalPhaseLockedLoop::post_phase_error(int error)
|
|
|
|
{
|
|
|
|
// use a simple spring mechanism as a lowpass filter for phase
|
|
|
|
_phase -= (error + 1) >> 1;
|
2016-07-14 23:42:01 +00:00
|
|
|
|
2016-07-28 15:32:14 +00:00
|
|
|
// use the average of the last few errors to affect frequency
|
|
|
|
std::vector<int> *phase_error_history = _phase_error_history.get();
|
|
|
|
size_t phase_error_history_size = phase_error_history->size();
|
2016-07-13 00:23:56 +00:00
|
|
|
|
2016-07-28 15:32:14 +00:00
|
|
|
(*phase_error_history)[_phase_error_pointer] = error;
|
|
|
|
_phase_error_pointer = (_phase_error_pointer + 1)%phase_error_history_size;
|
2016-07-13 00:23:56 +00:00
|
|
|
|
2016-07-28 15:32:14 +00:00
|
|
|
int total_error = 0;
|
|
|
|
for(size_t c = 0; c < phase_error_history_size; c++)
|
2016-07-13 00:23:56 +00:00
|
|
|
{
|
2016-07-28 15:32:14 +00:00
|
|
|
total_error += (*phase_error_history)[c];
|
2016-07-13 00:23:56 +00:00
|
|
|
}
|
2016-07-28 15:32:14 +00:00
|
|
|
int denominator = (int)(phase_error_history_size * 4);
|
|
|
|
_window_length += (total_error + (denominator >> 1)) / denominator;
|
|
|
|
_window_length = std::max(std::min(_window_length, _clocks_per_bit + _tolerance), _clocks_per_bit - _tolerance);
|
2016-07-13 00:23:56 +00:00
|
|
|
}
|