test_sdk/math/gtl_math_ampl.cpp

64 lines
1.8 KiB
C++
Raw Permalink Normal View History

#include "gtl_math_ampl.h"
namespace gtl
{
namespace math
{
ampl::ampl(gtl::analog_data *data)
: analog_value(data)
, _sum(0)
, _dc(0)
{
_name = "amplitude";
}
void ampl::before_copying_data(std::vector<qreal>::iterator begin, std::vector<qreal>::iterator end)
{
_sum -= std::accumulate(begin, end, 0.0);
}
void ampl::after_copying_data(std::vector<qreal>::iterator begin, std::vector<qreal>::iterator end)
{
_sum += std::accumulate(begin, end, 0.0);
_dc = _sum/_data.size();
auto [min, max] = std::minmax_element(begin, end);
auto ampl_iter = abs(*max - _dc) > abs(*min - _dc) ? max : min;
std::set<int>::iterator iter_remove_begin = std::lower_bound(_ampl_indexes.begin(), _ampl_indexes.end(), std::distance(_data.begin(), begin));
std::set<int>::iterator iter_remove_end = std::lower_bound(_ampl_indexes.begin(), _ampl_indexes.end(), std::distance(_data.begin(), end));
_ampl_indexes.erase(iter_remove_begin, iter_remove_end);
_ampl_indexes.insert(std::distance(_data.begin(), ampl_iter));
}
void ampl::data_changed()
{
analog_value::data_changed();
if(_ampl_indexes.empty())
{
_value = 0;
return;
}
qreal ampl = abs(_data[*_ampl_indexes.begin()] - _dc);
for(int it: _ampl_indexes)
{
qreal ampl_current = abs(_data[it] - _dc);
if(ampl_current > ampl)
ampl = ampl_current;
}
_value = ampl;
emit value_changed();
}
}
}