moving_variance.cpp
Go to the documentation of this file.
1 #include "moving_variance.h"
2 #include "ui_moving_variance.h"
3 #include <QCheckBox>
4 
6  : ui(new Ui::MovingVarianceFilter)
7  , _widget(new QWidget())
8  , _buffer(1)
9  , _ring_view(_buffer.begin(), _buffer.end())
10 {
11  ui->setupUi(_widget);
12 
13  connect(ui->spinBoxSamples, qOverload<int>(&QSpinBox::valueChanged), this,
14  [=](int) { emit parametersChanged(); });
15 
16  connect(ui->checkBoxStdDev, &QCheckBox::toggled, this,
17  [=]() { emit parametersChanged(); });
18 }
19 
21 {
22  delete ui;
23  delete _widget;
24 }
25 
27 {
28  _buffer.clear();
30 }
31 
32 std::optional<PlotData::Point> MovingVarianceFilter::calculateNextPoint(size_t index)
33 {
34  size_t buffer_size =
35  std::min(size_t(ui->spinBoxSamples->value()), size_t(dataSource()->size()));
36  if (buffer_size != _buffer.size())
37  {
38  _buffer.resize(buffer_size);
40  }
41 
42  const auto& p = dataSource()->at(index);
44 
45  while (_ring_view.size() < buffer_size)
46  {
48  }
49 
50  double total = 0;
51  for (const auto& point : _ring_view)
52  {
53  total += point.y;
54  }
55  const double N = double(_ring_view.size());
56  const double avg = total / N;
57 
58  double total_sqr = 0;
59  for (const auto& point : _ring_view)
60  {
61  const auto v = point.y - avg;
62  total_sqr += v * v;
63  }
64 
65  if (ui->checkBoxStdDev->isChecked())
66  {
67  return PlotData::Point{ p.x, std::sqrt(total_sqr / N) };
68  }
69  return PlotData::Point{ p.x, total_sqr / N };
70 }
71 
73 {
74  return _widget;
75 }
76 
77 bool MovingVarianceFilter::xmlSaveState(QDomDocument& doc,
78  QDomElement& parent_element) const
79 {
80  QDomElement widget_el = doc.createElement("options");
81  if (widget_el.isNull())
82  {
83  return false;
84  }
85  widget_el.setAttribute("value", ui->spinBoxSamples->value());
86  widget_el.setAttribute("apply_sqrt",
87  ui->checkBoxStdDev->isChecked() ? "true" : "false");
88  parent_element.appendChild(widget_el);
89  return true;
90 }
91 
92 bool MovingVarianceFilter::xmlLoadState(const QDomElement& parent_element)
93 {
94  QDomElement widget_el = parent_element.firstChildElement("options");
95  ui->spinBoxSamples->setValue(widget_el.attribute("value").toInt());
96  bool checked = widget_el.attribute("apply_sqrt") == "true";
97  ui->checkBoxStdDev->setChecked(checked);
98  return true;
99 }
MovingVarianceFilter
Definition: moving_variance.h:17
MovingVarianceFilter::~MovingVarianceFilter
~MovingVarianceFilter() override
Definition: moving_variance.cpp:20
MovingVarianceFilter::optionsWidget
QWidget * optionsWidget() override
optionsWidget pointer to a persistent widget used to set the plugin options .
Definition: moving_variance.cpp:72
MovingVarianceFilter::ui
Ui::MovingVarianceFilter * ui
Definition: moving_variance.h:43
MovingVarianceFilter::reset
void reset() override
Definition: moving_variance.cpp:26
MovingVarianceFilter::xmlLoadState
bool xmlLoadState(const QDomElement &parent_element) override
Override this method to load the status of the plugin from XML.
Definition: moving_variance.cpp:92
nonstd::span_lite::size
span_constexpr std::size_t size(span< T, Extent > const &spn)
Definition: span.hpp:1554
Ui
Definition: cheatsheet_dialog.h:6
moving_variance.h
MovingVarianceFilter::MovingVarianceFilter
MovingVarianceFilter()
Definition: moving_variance.cpp:5
PJ::TimeseriesBase::Point
typename PlotDataBase< double, Value >::Point Point
Definition: timeseries.h:23
nonstd::ring_span_lite::ring_span::size
size_type size() const nsrs_noexcept
Definition: ring_span.hpp:500
backward::Color::reset
@ reset
Definition: backward.hpp:3678
MovingVarianceFilter::_ring_view
nonstd::ring_span_lite::ring_span< PlotData::Point > _ring_view
Definition: moving_variance.h:46
PJ::TransformFunction_SISO::dataSource
const PlotData * dataSource() const
Definition: transform_function.cpp:113
PJ::PlotDataBase::at
const Point & at(size_t index) const
Definition: plotdatabase.h:193
MovingVarianceFilter::calculateNextPoint
std::optional< PlotData::Point > calculateNextPoint(size_t index) override
Definition: moving_variance.cpp:32
nonstd::ring_span_lite::ring_span::push_back
void push_back(value_type const &value)
Definition: ring_span.hpp:639
MovingVarianceFilter::xmlSaveState
bool xmlSaveState(QDomDocument &doc, QDomElement &parent_element) const override
Override this method to save the status of the plugin to XML.
Definition: moving_variance.cpp:77
MovingVarianceFilter::_widget
QWidget * _widget
Definition: moving_variance.h:44
MovingVarianceFilter::_buffer
std::vector< PlotData::Point > _buffer
Definition: moving_variance.h:45
nonstd::ring_span_lite::ring_span
Definition: ring_span.hpp:432


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45