2019-04-25 06:50:21 +00:00
|
|
|
/*!
|
|
|
|
* \file exponential_smoother.cc
|
|
|
|
* \brief Class that implements an exponential smoother
|
|
|
|
* \authors Carles Fernandez, 2019 cfernandez@cttc.es
|
|
|
|
*
|
|
|
|
* Class that implements a first-order exponential smoother.
|
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
2019-04-25 06:50:21 +00:00
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
|
2019-04-25 06:50:21 +00:00
|
|
|
* This file is part of GNSS-SDR.
|
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* Copyright (C) 2010-2020 (see AUTHORS file for a list of contributors)
|
2020-02-08 00:20:02 +00:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2019-04-25 06:50:21 +00:00
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
2019-04-25 06:50:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "exponential_smoother.h"
|
|
|
|
#include <iterator>
|
|
|
|
#include <numeric>
|
|
|
|
|
|
|
|
Exponential_Smoother::Exponential_Smoother()
|
|
|
|
{
|
|
|
|
alpha_ = 0.001;
|
|
|
|
old_value_ = 0.0;
|
2020-07-09 22:37:55 +00:00
|
|
|
one_minus_alpha_ = 1.0F - alpha_;
|
2019-04-25 13:30:11 +00:00
|
|
|
samples_for_initialization_ = 200;
|
2019-04-25 06:50:21 +00:00
|
|
|
initializing_ = true;
|
|
|
|
init_counter_ = 0;
|
2019-04-25 13:30:11 +00:00
|
|
|
min_value_ = 25.0;
|
|
|
|
offset_ = 12.0;
|
2019-04-25 06:50:21 +00:00
|
|
|
init_buffer_.reserve(samples_for_initialization_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Exponential_Smoother::set_alpha(float alpha)
|
|
|
|
{
|
|
|
|
alpha_ = alpha;
|
|
|
|
if (alpha_ < 0)
|
|
|
|
{
|
|
|
|
alpha_ = 0;
|
|
|
|
}
|
|
|
|
if (alpha_ > 1)
|
|
|
|
{
|
|
|
|
alpha_ = 1;
|
|
|
|
}
|
2020-07-09 22:37:55 +00:00
|
|
|
one_minus_alpha_ = 1.0F - alpha_;
|
2019-04-25 06:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Exponential_Smoother::set_offset(float offset)
|
|
|
|
{
|
|
|
|
offset_ = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Exponential_Smoother::set_samples_for_initialization(int num_samples)
|
|
|
|
{
|
|
|
|
int ns = num_samples;
|
|
|
|
if (ns <= 0)
|
|
|
|
{
|
|
|
|
ns = 1;
|
|
|
|
}
|
2019-07-25 19:37:57 +00:00
|
|
|
samples_for_initialization_ = ns;
|
2019-04-25 06:50:21 +00:00
|
|
|
init_buffer_.reserve(samples_for_initialization_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Exponential_Smoother::reset()
|
|
|
|
{
|
|
|
|
initializing_ = true;
|
|
|
|
init_counter_ = 0;
|
|
|
|
init_buffer_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Exponential_Smoother::set_min_value(float value)
|
|
|
|
{
|
|
|
|
min_value_ = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double Exponential_Smoother::smooth(double raw)
|
|
|
|
{
|
2019-04-27 11:34:21 +00:00
|
|
|
auto raw_f = static_cast<float>(raw);
|
|
|
|
auto smooth_d = static_cast<double>((this)->smooth(raw_f));
|
2019-04-25 06:50:21 +00:00
|
|
|
return smooth_d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float Exponential_Smoother::smooth(float raw)
|
|
|
|
{
|
|
|
|
float smoothed_value;
|
|
|
|
if (initializing_ == true)
|
|
|
|
{
|
|
|
|
init_counter_++;
|
|
|
|
smoothed_value = raw;
|
|
|
|
init_buffer_.push_back(smoothed_value);
|
|
|
|
if (init_counter_ == samples_for_initialization_)
|
|
|
|
{
|
2019-04-27 16:39:20 +00:00
|
|
|
old_value_ = std::accumulate(std::begin(init_buffer_), std::end(init_buffer_), 0.0F) / static_cast<float>(init_buffer_.size());
|
2019-04-25 06:50:21 +00:00
|
|
|
if (old_value_ < (min_value_ + offset_))
|
|
|
|
{
|
|
|
|
// flush buffer and start again
|
|
|
|
init_counter_ = 0;
|
|
|
|
init_buffer_.clear();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
initializing_ = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
smoothed_value = alpha_ * raw + one_minus_alpha_ * old_value_;
|
|
|
|
old_value_ = smoothed_value;
|
|
|
|
}
|
|
|
|
return smoothed_value;
|
|
|
|
}
|