Loading...
Searching...
No Matches
pseudo_moving_average.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2018 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_CUMULATIVE_MOVING_AVERAGE_INCLUDED
#define ETL_CUMULATIVE_MOVING_AVERAGE_INCLUDED
#include "platform.h"
#include "iterator.h"
#include "type_traits.h"
namespace etl
{
namespace private_pseudo_moving_average
{
//***************************************************
//***************************************************
template <typename TPseudo_Moving_Average>
class add_insert_iterator : public etl::iterator<ETL_OR_STD::output_iterator_tag, typename TPseudo_Moving_Average::value_type, void, void, void>
{
public:
//***********************************
explicit add_insert_iterator(TPseudo_Moving_Average& pma) ETL_NOEXCEPT
: p_pma(&pma)
{
}
//***********************************
add_insert_iterator& operator*() ETL_NOEXCEPT
{
return *this;
}
//***********************************
add_insert_iterator& operator++() ETL_NOEXCEPT
{
return *this;
}
//***********************************
add_insert_iterator& operator++(int) ETL_NOEXCEPT
{
return *this;
}
//***********************************
add_insert_iterator& operator=(typename TPseudo_Moving_Average::value_type value)
{
p_pma->add(value);
return *this;
}
private:
TPseudo_Moving_Average* p_pma;
};
} // namespace private_pseudo_moving_average
//***************************************************************************
//***************************************************************************
template <typename T, const size_t SAMPLE_SIZE, const size_t SCALING = 1U, const bool IsIntegral = etl::is_integral<T>::value,
const bool IsFloat = etl::is_floating_point<T>::value>
class pseudo_moving_average;
//***************************************************************************
//***************************************************************************
template <typename T, const size_t SAMPLE_SIZE_, const size_t SCALING_>
class pseudo_moving_average<T, SAMPLE_SIZE_, SCALING_, true, false>
{
private:
typedef pseudo_moving_average<T, SAMPLE_SIZE_, SCALING_, true, false> this_t;
typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>::type scale_t;
typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>::type sample_t;
static ETL_CONSTANT sample_t SAMPLES = static_cast<sample_t>(SAMPLE_SIZE_);
static ETL_CONSTANT scale_t SCALE = static_cast<scale_t>(SCALING_);
public:
typedef T value_type;
typedef private_pseudo_moving_average::add_insert_iterator<this_t> add_insert_iterator;
static ETL_CONSTANT size_t SAMPLE_SIZE = SAMPLE_SIZE_;
static ETL_CONSTANT size_t SCALING = SCALING_;
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR pseudo_moving_average(const T initial_value)
: average(initial_value * SCALE)
{
}
//*************************************************************************
//*************************************************************************
void clear(const T initial_value)
{
average = (initial_value * SCALE);
}
//*************************************************************************
//*************************************************************************
void add(T new_value)
{
average *= SAMPLES;
average += SCALE * new_value;
average /= SAMPLES + sample_t(1);
}
//*************************************************************************
//*************************************************************************
T value() const
{
return average;
}
//*************************************************************************
//*************************************************************************
add_insert_iterator input()
{
return add_insert_iterator(*this);
}
private:
T average;
};
template <typename T, const size_t SAMPLE_SIZE_, const size_t SCALING_>
ETL_CONSTANT size_t pseudo_moving_average<T, SAMPLE_SIZE_, SCALING_, true, false>::SAMPLE_SIZE;
template <typename T, const size_t SAMPLE_SIZE_, const size_t SCALING_>
ETL_CONSTANT size_t pseudo_moving_average<T, SAMPLE_SIZE_, SCALING_, true, false>::SCALING;
//***************************************************************************
//***************************************************************************
template <typename T, const size_t SCALING_>
class pseudo_moving_average<T, 0, SCALING_, true, false>
{
typedef pseudo_moving_average<T, 0, SCALING_, true, false> this_t;
typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>::type scale_t;
typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>::type sample_t;
static ETL_CONSTANT scale_t SCALE = static_cast<scale_t>(SCALING_);
public:
typedef T value_type;
typedef private_pseudo_moving_average::add_insert_iterator<this_t> add_insert_iterator;
static ETL_CONSTANT size_t SCALING = SCALING_;
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR pseudo_moving_average(const T initial_value, const size_t sample_size)
: average(initial_value * SCALE)
, samples(sample_t(sample_size))
{
}
//*************************************************************************
//*************************************************************************
void clear(const T initial_value)
{
average = (initial_value * SCALE);
}
//*************************************************************************
//*************************************************************************
void set_sample_size(const size_t sample_size)
{
samples = sample_t(sample_size);
}
//*************************************************************************
//*************************************************************************
void add(T new_value)
{
average *= samples;
average += SCALE * new_value;
average /= samples + sample_t(1);
}
//*************************************************************************
//*************************************************************************
T value() const
{
return average;
}
//*************************************************************************
//*************************************************************************
add_insert_iterator input()
{
return add_insert_iterator(*this);
}
private:
T average;
sample_t samples;
};
template <typename T, const size_t SCALING_>
ETL_CONSTANT size_t pseudo_moving_average<T, 0, SCALING_, true, false>::SCALING;
//***************************************************************************
//***************************************************************************
template <typename T, const size_t SAMPLE_SIZE_>
class pseudo_moving_average<T, SAMPLE_SIZE_, 1U, false, true>
{
typedef pseudo_moving_average<T, SAMPLE_SIZE_, 1U, false, true> this_t;
public:
typedef T value_type;
typedef private_pseudo_moving_average::add_insert_iterator<this_t> add_insert_iterator;
static ETL_CONSTANT size_t SAMPLE_SIZE = SAMPLE_SIZE_;
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR pseudo_moving_average(const T initial_value)
: reciprocal_samples_plus_1(T(1.0) / T(SAMPLE_SIZE_ + 1U))
, average(initial_value)
{
}
//*************************************************************************
//*************************************************************************
void clear(const T initial_value)
{
average = initial_value;
}
//*************************************************************************
//*************************************************************************
void add(const T new_value)
{
average += (new_value - average) * reciprocal_samples_plus_1;
}
//*************************************************************************
//*************************************************************************
T value() const
{
return average;
}
//*************************************************************************
//*************************************************************************
add_insert_iterator input()
{
return add_insert_iterator(*this);
}
private:
const T reciprocal_samples_plus_1;
T average;
};
template <typename T, const size_t SAMPLE_SIZE_>
ETL_CONSTANT size_t pseudo_moving_average<T, SAMPLE_SIZE_, 1U, false, true>::SAMPLE_SIZE;
//***************************************************************************
//***************************************************************************
template <typename T>
class pseudo_moving_average<T, 0U, 1U, false, true>
{
typedef pseudo_moving_average<T, 0U, 1U, false, true> this_t;
public:
typedef T value_type;
typedef private_pseudo_moving_average::add_insert_iterator<this_t> add_insert_iterator;
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR pseudo_moving_average(const T initial_value, const size_t sample_size)
: reciprocal_samples_plus_1(T(1.0) / T(sample_size + 1U))
, average(initial_value)
{
}
//*************************************************************************
//*************************************************************************
void clear(const T initial_value)
{
average = initial_value;
}
//*************************************************************************
//*************************************************************************
void set_sample_size(const size_t sample_size)
{
reciprocal_samples_plus_1 = T(1.0) / (T(sample_size) + T(1));
}
//*************************************************************************
//*************************************************************************
void add(const T new_value)
{
average += (new_value - average) * reciprocal_samples_plus_1;
}
//*************************************************************************
//*************************************************************************
T value() const
{
return average;
}
//*************************************************************************
//*************************************************************************
add_insert_iterator input()
{
return add_insert_iterator(*this);
}
private:
T reciprocal_samples_plus_1;
T average;
};
} // namespace etl
#endif
etl::private_pseudo_moving_average::add_insert_iterator
Definition pseudo_moving_average.h:48
etl::pseudo_moving_average< T, 0, SCALING_, true, false >::pseudo_moving_average
ETL_CONSTEXPR pseudo_moving_average(const T initial_value, const size_t sample_size)
Definition pseudo_moving_average.h:211
etl::pseudo_moving_average< T, 0, SCALING_, true, false >::clear
void clear(const T initial_value)
Definition pseudo_moving_average.h:221
etl::pseudo_moving_average< T, 0, SCALING_, true, false >::add
void add(T new_value)
Definition pseudo_moving_average.h:239
etl::pseudo_moving_average< T, 0, SCALING_, true, false >::value
T value() const
Definition pseudo_moving_average.h:250
etl::pseudo_moving_average< T, 0, SCALING_, true, false >::set_sample_size
void set_sample_size(const size_t sample_size)
Definition pseudo_moving_average.h:230
etl::pseudo_moving_average< T, 0, SCALING_, true, false >::input
add_insert_iterator input()
Definition pseudo_moving_average.h:259
etl::pseudo_moving_average< T, 0, SCALING_, true, false >::SCALING
static ETL_CONSTANT size_t SCALING
The sample scaling factor.
Definition pseudo_moving_average.h:205
etl::pseudo_moving_average< T, 0U, 1U, false, true >::input
add_insert_iterator input()
Definition pseudo_moving_average.h:412
etl::pseudo_moving_average< T, 0U, 1U, false, true >::set_sample_size
void set_sample_size(const size_t sample_size)
Definition pseudo_moving_average.h:385
etl::pseudo_moving_average< T, 0U, 1U, false, true >::value
T value() const
Definition pseudo_moving_average.h:403
etl::pseudo_moving_average< T, 0U, 1U, false, true >::pseudo_moving_average
ETL_CONSTEXPR pseudo_moving_average(const T initial_value, const size_t sample_size)
Definition pseudo_moving_average.h:366
etl::pseudo_moving_average< T, 0U, 1U, false, true >::add
void add(const T new_value)
Definition pseudo_moving_average.h:394
etl::pseudo_moving_average< T, 0U, 1U, false, true >::clear
void clear(const T initial_value)
Definition pseudo_moving_average.h:376
etl::pseudo_moving_average< T, SAMPLE_SIZE_, 1U, false, true >::clear
void clear(const T initial_value)
Definition pseudo_moving_average.h:305
etl::pseudo_moving_average< T, SAMPLE_SIZE_, 1U, false, true >::pseudo_moving_average
ETL_CONSTEXPR pseudo_moving_average(const T initial_value)
Definition pseudo_moving_average.h:295
etl::pseudo_moving_average< T, SAMPLE_SIZE_, 1U, false, true >::value
T value() const
Definition pseudo_moving_average.h:323
etl::pseudo_moving_average< T, SAMPLE_SIZE_, 1U, false, true >::input
add_insert_iterator input()
Definition pseudo_moving_average.h:332
etl::pseudo_moving_average< T, SAMPLE_SIZE_, 1U, false, true >::add
void add(const T new_value)
Definition pseudo_moving_average.h:314
etl::pseudo_moving_average< T, SAMPLE_SIZE_, SCALING_, true, false >::add
void add(T new_value)
Definition pseudo_moving_average.h:148
etl::pseudo_moving_average< T, SAMPLE_SIZE_, SCALING_, true, false >::pseudo_moving_average
ETL_CONSTEXPR pseudo_moving_average(const T initial_value)
Definition pseudo_moving_average.h:130
etl::pseudo_moving_average< T, SAMPLE_SIZE_, SCALING_, true, false >::value
T value() const
Definition pseudo_moving_average.h:159
etl::pseudo_moving_average< T, SAMPLE_SIZE_, SCALING_, true, false >::SAMPLE_SIZE
static ETL_CONSTANT size_t SAMPLE_SIZE
The number of samples averaged over.
Definition pseudo_moving_average.h:123
etl::pseudo_moving_average< T, SAMPLE_SIZE_, SCALING_, true, false >::clear
void clear(const T initial_value)
Definition pseudo_moving_average.h:139
etl::pseudo_moving_average< T, SAMPLE_SIZE_, SCALING_, true, false >::input
add_insert_iterator input()
Definition pseudo_moving_average.h:168
etl::pseudo_moving_average< T, SAMPLE_SIZE_, SCALING_, true, false >::SCALING
static ETL_CONSTANT size_t SCALING
The sample scaling factor.
Definition pseudo_moving_average.h:124
etl::pseudo_moving_average
Definition pseudo_moving_average.h:96
iterator.h
etl
Definition absolute.h:40
platform.h
etl::iterator
iterator
Definition iterator.h:482
etl::type
type_traits.h