-
-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8fee659
commit 730172e
Showing
5 changed files
with
235 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include "moving_variance.h" | ||
#include "ui_moving_variance.h" | ||
#include <QCheckBox> | ||
|
||
MovingVarianceFilter::MovingVarianceFilter() | ||
: ui(new Ui::MovingVarianceFilter) | ||
, _widget(new QWidget()) | ||
, _buffer(1) | ||
, _ring_view(_buffer.begin(), _buffer.end()) | ||
{ | ||
ui->setupUi(_widget); | ||
|
||
connect(ui->spinBoxSamples, qOverload<int>(&QSpinBox::valueChanged), this, | ||
[=](int) { emit parametersChanged(); }); | ||
|
||
connect(ui->checkBoxStdDev, &QCheckBox::toggled, this, | ||
[=]() { emit parametersChanged(); }); | ||
} | ||
|
||
MovingVarianceFilter::~MovingVarianceFilter() | ||
{ | ||
delete ui; | ||
delete _widget; | ||
} | ||
|
||
void MovingVarianceFilter::reset() | ||
{ | ||
_buffer.clear(); | ||
TransformFunction_SISO::reset(); | ||
} | ||
|
||
std::optional<PlotData::Point> MovingVarianceFilter::calculateNextPoint(size_t index) | ||
{ | ||
size_t buffer_size = | ||
std::min(size_t(ui->spinBoxSamples->value()), size_t(dataSource()->size())); | ||
if (buffer_size != _buffer.size()) | ||
{ | ||
_buffer.resize(buffer_size); | ||
_ring_view = nonstd::ring_span<PlotData::Point>(_buffer.begin(), _buffer.end()); | ||
} | ||
|
||
const auto& p = dataSource()->at(index); | ||
_ring_view.push_back(p); | ||
|
||
while (_ring_view.size() < buffer_size) | ||
{ | ||
_ring_view.push_back(p); | ||
} | ||
|
||
double total = 0; | ||
for (const auto& point: _ring_view) | ||
{ | ||
total += point.y; | ||
} | ||
const double N = double(_ring_view.size()); | ||
const double avg = total / N; | ||
|
||
double total_sqr = 0; | ||
for (const auto& point: _ring_view) | ||
{ | ||
const auto v = point.y - avg; | ||
total_sqr += v*v; | ||
} | ||
|
||
if(ui->checkBoxStdDev->isChecked()) | ||
{ | ||
return PlotData::Point{ p.x, std::sqrt(total_sqr / N)}; | ||
} | ||
return PlotData::Point{ p.x, total_sqr / N}; | ||
} | ||
|
||
QWidget* MovingVarianceFilter::optionsWidget() | ||
{ | ||
return _widget; | ||
} | ||
|
||
bool MovingVarianceFilter::xmlSaveState(QDomDocument& doc, | ||
QDomElement& parent_element) const | ||
{ | ||
QDomElement widget_el = doc.createElement("options"); | ||
widget_el.setAttribute("value", ui->spinBoxSamples->value()); | ||
widget_el.setAttribute("apply_sqrt", | ||
ui->checkBoxStdDev->isChecked() ? "true" : "false"); | ||
parent_element.appendChild(widget_el); | ||
return true; | ||
} | ||
|
||
bool MovingVarianceFilter::xmlLoadState(const QDomElement& parent_element) | ||
{ | ||
QDomElement widget_el = parent_element.firstChildElement("options"); | ||
ui->spinBoxSamples->setValue(widget_el.attribute("value").toInt()); | ||
bool checked = widget_el.attribute("apply_sqrt") == "true"; | ||
ui->checkBoxStdDev->setChecked(checked); | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#pragma once | ||
|
||
#include <QRadioButton> | ||
#include <QSpinBox> | ||
#include <QDoubleSpinBox> | ||
#include "PlotJuggler/transform_function.h" | ||
#include "ui_moving_variance.h" | ||
#include "PlotJuggler/ring_span.hpp" | ||
|
||
using namespace PJ; | ||
|
||
namespace Ui | ||
{ | ||
class MovingVarianceFilter; | ||
} | ||
|
||
class MovingVarianceFilter : public TransformFunction_SISO | ||
{ | ||
public: | ||
explicit MovingVarianceFilter(); | ||
|
||
~MovingVarianceFilter() override; | ||
|
||
void reset() override; | ||
|
||
const char* name() const override | ||
{ | ||
return "Moving Variance"; | ||
} | ||
|
||
QWidget* optionsWidget() override; | ||
|
||
bool xmlSaveState(QDomDocument& doc, QDomElement& parent_element) const override; | ||
|
||
bool xmlLoadState(const QDomElement& parent_element) override; | ||
|
||
private: | ||
Ui::MovingVarianceFilter* ui; | ||
QWidget* _widget; | ||
std::vector<PlotData::Point> _buffer; | ||
nonstd::ring_span_lite::ring_span<PlotData::Point> _ring_view; | ||
|
||
std::optional<PlotData::Point> calculateNextPoint(size_t index) override; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ui version="4.0"> | ||
<class>MovingVarianceFilter</class> | ||
<widget class="QWidget" name="MovingVarianceFilter"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>425</width> | ||
<height>300</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle"> | ||
<string>Form</string> | ||
</property> | ||
<layout class="QVBoxLayout" name="verticalLayout"> | ||
<item> | ||
<widget class="QLabel" name="label"> | ||
<property name="font"> | ||
<font> | ||
<bold>true</bold> | ||
</font> | ||
</property> | ||
<property name="text"> | ||
<string>Select the size of the window</string> | ||
</property> | ||
</widget> | ||
</item> | ||
<item> | ||
<layout class="QFormLayout" name="formLayout"> | ||
<item row="0" column="1"> | ||
<widget class="QSpinBox" name="spinBoxSamples"> | ||
<property name="maximumSize"> | ||
<size> | ||
<width>100</width> | ||
<height>16777215</height> | ||
</size> | ||
</property> | ||
<property name="minimum"> | ||
<number>1</number> | ||
</property> | ||
<property name="maximum"> | ||
<number>1000</number> | ||
</property> | ||
<property name="value"> | ||
<number>10</number> | ||
</property> | ||
</widget> | ||
</item> | ||
<item row="0" column="0"> | ||
<widget class="QLabel" name="label_2"> | ||
<property name="text"> | ||
<string>Window size:</string> | ||
</property> | ||
</widget> | ||
</item> | ||
</layout> | ||
</item> | ||
<item> | ||
<widget class="QCheckBox" name="checkBoxStdDev"> | ||
<property name="text"> | ||
<string>Apply square root</string> | ||
</property> | ||
</widget> | ||
</item> | ||
<item> | ||
<widget class="QLabel" name="label_3"> | ||
<property name="text"> | ||
<string>(i.e., convert to standard deviation)</string> | ||
</property> | ||
</widget> | ||
</item> | ||
<item> | ||
<spacer name="verticalSpacer"> | ||
<property name="orientation"> | ||
<enum>Qt::Vertical</enum> | ||
</property> | ||
<property name="sizeHint" stdset="0"> | ||
<size> | ||
<width>20</width> | ||
<height>40</height> | ||
</size> | ||
</property> | ||
</spacer> | ||
</item> | ||
</layout> | ||
</widget> | ||
<resources/> | ||
<connections/> | ||
</ui> |