added edit dialog for median filter and optimized for speed

This commit is contained in:
Jan Käberich 2020-11-28 13:57:22 +01:00
parent 163b23f28d
commit 8e661aecd6
12 changed files with 342 additions and 77 deletions

View File

@ -127,6 +127,7 @@ FORMS += \
Device/manualcontroldialog.ui \ Device/manualcontroldialog.ui \
Generator/signalgenwidget.ui \ Generator/signalgenwidget.ui \
Tools/impedancematchdialog.ui \ Tools/impedancematchdialog.ui \
Traces/Math/medianfilterdialog.ui \
Traces/Math/tracematheditdialog.ui \ Traces/Math/tracematheditdialog.ui \
Traces/markerwidget.ui \ Traces/markerwidget.ui \
Traces/smithchartdialog.ui \ Traces/smithchartdialog.ui \

View File

@ -651,15 +651,15 @@ std::vector<Trace *> Calibration::getMeasurementTraces()
t->setReflection(prefix == "S11" || prefix == "S22"); t->setReflection(prefix == "S11" || prefix == "S22");
for(auto p : m.second.datapoints) { for(auto p : m.second.datapoints) {
Trace::Data d; Trace::Data d;
d.frequency = p.frequency; d.x = p.frequency;
if(prefix == "S11") { if(prefix == "S11") {
d.S = complex<double>(p.real_S11, p.imag_S11); d.y = complex<double>(p.real_S11, p.imag_S11);
} else if(prefix == "S12") { } else if(prefix == "S12") {
d.S = complex<double>(p.real_S12, p.imag_S12); d.y = complex<double>(p.real_S12, p.imag_S12);
} else if(prefix == "S21") { } else if(prefix == "S21") {
d.S = complex<double>(p.real_S21, p.imag_S21); d.y = complex<double>(p.real_S21, p.imag_S21);
} else { } else {
d.S = complex<double>(p.real_S22, p.imag_S22); d.y = complex<double>(p.real_S22, p.imag_S22);
} }
t->addData(d); t->addData(d);
} }

View File

@ -1,8 +1,15 @@
#include "medianfilter.h" #include "medianfilter.h"
#include "ui_medianfilterdialog.h"
#include <QMessageBox>
using namespace Math; using namespace Math;
using namespace std; using namespace std;
namespace Ui {
class MedianFilterDialog;
}
MedianFilter::MedianFilter() MedianFilter::MedianFilter()
{ {
kernelSize = 3; kernelSize = 3;
@ -17,49 +24,112 @@ TraceMath::DataType MedianFilter::outputType(TraceMath::DataType inputType)
QString MedianFilter::description() QString MedianFilter::description()
{ {
return "Median filter"; return "Median filter, size "+QString::number(kernelSize)+", sorting: " + orderToString(order);
}
void MedianFilter::edit()
{
auto d = new QDialog();
auto ui = new Ui::MedianFilterDialog();
ui->setupUi(d);
ui->kernelSize->setValue(kernelSize);
ui->sortingMethod->setCurrentIndex((int) order);
connect(ui->kernelSize, qOverload<int>(&QSpinBox::valueChanged), [=](int newval) {
if((newval & 0x01) == 0) {
QMessageBox::information(d, "Median filter", "Only odd values are allowed for the kernel size");
newval++;
}
ui->kernelSize->setValue(newval);
kernelSize = newval;
});
connect(ui->sortingMethod, qOverload<int>(&QComboBox::currentIndexChanged), [=](int index) {
order = (Order) index;
});
d->show();
} }
void MedianFilter::inputSamplesChanged(unsigned int begin, unsigned int end) { void MedianFilter::inputSamplesChanged(unsigned int begin, unsigned int end) {
if(data.size() != input->rData().size()) { if(data.size() != input->rData().size()) {
data.resize(input->rData().size()); data.resize(input->rData().size());
} }
int start = (int) begin - (kernelSize-1)/2; if(data.size() > 0) {
unsigned int stop = (int) end + (kernelSize-1)/2; auto kernelOffset = (kernelSize-1)/2;
int start = (int) begin - kernelOffset;
unsigned int stop = (int) end + kernelOffset;
if(start < 0) { if(start < 0) {
start = 0; start = 0;
} }
if(stop >= input->rData().size()) { if(stop >= input->rData().size()) {
stop = input->rData().size(); stop = input->rData().size();
} }
for(unsigned int i=start;i<stop;i++) {
updateSample(i);
}
emit outputSamplesChanged(start, stop);
}
void MedianFilter::updateSample(int index) auto comp = [=](const complex<double>&a, const complex<double>&b){
{ switch(order) {
vector<complex<double>> values; case Order::AbsoluteValue: return abs(a) < abs(b);
for(int i=index - (kernelSize-1)/2;i<=index+(kernelSize-1)/2;i++) { case Order::Phase: return arg(a) < arg(b);
case Order::Real: return real(a) < real(b);
case Order::Imag: return imag(a) < imag(b);
}
};
vector<complex<double>> kernel(kernelSize);
for(unsigned int out=start;out<stop;out++) {
if(out == (unsigned int) start) {
// this is the first sample to update, fill initial kernel
for(unsigned int in=0;in<kernelSize;in++) {
unsigned int inputSample; unsigned int inputSample;
if(i<0) { if(kernelOffset > in + out) {
inputSample = 0; inputSample = 0;
} else if(i>=(int) input->rData().size()) { } else if(in + kernelOffset + out >= input->rData().size()) {
inputSample = input->rData().size() - 1; inputSample = input->rData().size() - 1;
} else { } else {
inputSample = i; inputSample = in + out - kernelOffset;
} }
values.push_back(input->rData().at(inputSample).y); auto sample = input->rData().at(inputSample).y;
if(out == (unsigned int) start) {
// this is the first sample to update, fill initial kernel
kernel[in] = sample;
} }
sort(values.begin(), values.end(), [=](const complex<double>&a, const complex<double>&b){
switch(order) {
case Order::AbsoluteValue: return abs(a) > abs(b);
case Order::Phase: return arg(a) > arg(b);
case Order::Real: return real(a) > real(b);
case Order::Imag: return imag(a) > imag(b);
} }
}); // sort initial kernel
data.at(index).y = values[(kernelSize-1)/2]; sort(kernel.begin(), kernel.end(), comp);
data.at(index).x = input->rData().at(index).x; } else {
// kernel already filled and sorted from last output sample. Only remove the one input sample that
// is no longer needed for this output and add the one additional input sample
int toRemove = out - kernelOffset - 1;
unsigned int toAdd = out + kernelOffset;
if(toRemove < 0) {
toRemove = 0;
}
if(toAdd >= input->rData().size()) {
toAdd = input->rData().size() - 1;
}
auto sampleToRemove = input->rData().at(toRemove).y;
auto remove_iterator = lower_bound(kernel.begin(), kernel.end(), sampleToRemove, comp);
kernel.erase(remove_iterator);
auto sampleToAdd = input->rData().at(toAdd).y;
// insert sample at correct position in vector
kernel.insert(upper_bound(kernel.begin(), kernel.end(), sampleToAdd, comp), sampleToAdd);
}
data.at(out).y = kernel[kernelOffset];
data.at(out).x = input->rData().at(out).x;
}
emit outputSamplesChanged(start, stop);
success();
} else {
warning("No input data");
}
}
QString MedianFilter::orderToString(MedianFilter::Order o)
{
switch(o) {
case Order::AbsoluteValue: return "Absolute";
case Order::Phase: return "Phase";
case Order::Real: return "Real";
case Order::Imag: return "Imag";
}
} }

View File

@ -13,19 +13,21 @@ public:
virtual DataType outputType(DataType inputType) override; virtual DataType outputType(DataType inputType) override;
virtual QString description() override; virtual QString description() override;
virtual void edit() override;
public slots: public slots:
// a single value of the input data has changed, index determines which sample has changed // a single value of the input data has changed, index determines which sample has changed
virtual void inputSamplesChanged(unsigned int begin, unsigned int end) override; virtual void inputSamplesChanged(unsigned int begin, unsigned int end) override;
private: private:
void updateSample(int index); unsigned int kernelSize;
int kernelSize;
enum class Order { enum class Order {
AbsoluteValue, AbsoluteValue = 0,
Phase, Phase = 1,
Real, Real = 2,
Imag, Imag = 3,
} order; } order;
static QString orderToString(Order o);
}; };
} }

View File

@ -1,19 +1,106 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"> <ui version="4.0">
<class>Dialog</class> <class>MedianFilterDialog</class>
<widget class="QDialog" name="Dialog" > <widget class="QDialog" name="MedianFilterDialog">
<property name="windowModality">
<enum>Qt::ApplicationModal</enum>
</property>
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>400</width> <width>269</width>
<height>300</height> <height>108</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>Dialog</string> <string>Median Filter</string>
</property>
<property name="modal">
<bool>false</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Kernel size:</string>
</property> </property>
</widget> </widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="kernelSize">
<property name="toolTip">
<string>Number of input samples that the filter uses for each output sample</string>
</property>
<property name="minimum">
<number>3</number>
</property>
<property name="singleStep">
<number>2</number>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Sorting method:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="sortingMethod">
<item>
<property name="text">
<string>Absolute value</string>
</property>
</item>
<item>
<property name="text">
<string>Phase</string>
</property>
</item>
<item>
<property name="text">
<string>Real part</string>
</property>
</item>
<item>
<property name="text">
<string>Imaginary part</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/> <resources/>
<connections/> <connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>MedianFilterDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>134</x>
<y>86</y>
</hint>
<hint type="destinationlabel">
<x>134</x>
<y>53</y>
</hint>
</hints>
</connection>
</connections>
</ui> </ui>

View File

@ -4,6 +4,7 @@ TraceMath::TraceMath()
{ {
input = nullptr; input = nullptr;
dataType = DataType::Invalid; dataType = DataType::Invalid;
error("Invalid input");
} }
TraceMath::Data TraceMath::getSample(unsigned int index) TraceMath::Data TraceMath::getSample(unsigned int index)
@ -51,8 +52,43 @@ void TraceMath::inputTypeChanged(TraceMath::DataType type)
data.clear(); data.clear();
inputSamplesChanged(0, input->data.size()); inputSamplesChanged(0, input->data.size());
emit outputTypeChanged(dataType); emit outputTypeChanged(dataType);
if(dataType == DataType::Invalid) {
error("Invalid input data");
} }
} }
}
void TraceMath::warning(QString warn)
{
statusString = warn;
status = Status::Warning;
emit statusChanged();
}
void TraceMath::error(QString err)
{
statusString = err;
status = Status::Error;
emit statusChanged();
}
void TraceMath::success()
{
if(status != Status::Ok) {
status = Status::Ok;
emit statusChanged();
}
}
QString TraceMath::getStatusDescription() const
{
return statusString;
}
TraceMath::Status TraceMath::getStatus() const
{
return status;
}
TraceMath::DataType TraceMath::getDataType() const TraceMath::DataType TraceMath::getDataType() const
{ {

View File

@ -1,4 +1,4 @@
#ifndef TRACEMATH_H #ifndef TRACEMATH_H
#define TRACEMATH_H #define TRACEMATH_H
#include <QObject> #include <QObject>
@ -22,6 +22,12 @@ public:
Invalid, Invalid,
}; };
enum class Status {
Ok,
Warning,
Error,
};
Data getSample(unsigned int index); Data getSample(unsigned int index);
unsigned int numSamples(); unsigned int numSamples();
@ -35,6 +41,8 @@ public:
DataType getDataType() const; DataType getDataType() const;
std::vector<Data>& rData() { return data;}; std::vector<Data>& rData() { return data;};
Status getStatus() const;
QString getStatusDescription() const;
public slots: public slots:
// some values of the input data have changed, begin/end determine which sample(s) has changed // some values of the input data have changed, begin/end determine which sample(s) has changed
@ -49,9 +57,19 @@ signals:
void outputTypeChanged(DataType type); void outputTypeChanged(DataType type);
protected: protected:
// call one of these functions in the derived classes after output data has been updated
void warning(QString warn);
void error(QString err);
void success();
std::vector<Data> data; std::vector<Data> data;
TraceMath *input; TraceMath *input;
DataType dataType; DataType dataType;
private:
Status status;
QString statusString;
signals:
void statusChanged();
}; };
#endif // TRACEMATH_H #endif // TRACEMATH_H

View File

@ -1,5 +1,6 @@
#include "tracematheditdialog.h" #include "tracematheditdialog.h"
#include "ui_tracematheditdialog.h" #include "ui_tracematheditdialog.h"
#include <QHeaderView>
#include "medianfilter.h" #include "medianfilter.h"
@ -11,6 +12,11 @@ TraceMathEditDialog::TraceMathEditDialog(Trace &t, QWidget *parent) :
ui->setupUi(this); ui->setupUi(this);
ui->view->setModel(model); ui->view->setModel(model);
QHeaderView *headerView = ui->view->horizontalHeader();
headerView->setSectionResizeMode(MathModel::ColIndexDescription, QHeaderView::Stretch);
headerView->setSectionResizeMode(MathModel::ColIndexStatus, QHeaderView::ResizeToContents);
headerView->setSectionResizeMode(MathModel::ColIndexDomain, QHeaderView::ResizeToContents);
connect(ui->view->selectionModel(), &QItemSelectionModel::currentRowChanged, [=](const QModelIndex &current, const QModelIndex &previous){ connect(ui->view->selectionModel(), &QItemSelectionModel::currentRowChanged, [=](const QModelIndex &current, const QModelIndex &previous){
if(!current.isValid()) { if(!current.isValid()) {
ui->bDelete->setEnabled(false); ui->bDelete->setEnabled(false);
@ -23,6 +29,13 @@ TraceMathEditDialog::TraceMathEditDialog(Trace &t, QWidget *parent) :
} }
}); });
connect(ui->view, &QTableView::doubleClicked, [&](const QModelIndex &index) {
if(index.isValid()) {
auto math = t.getMathOperations().at(index.row()).math;
math->edit();
}
});
connect(ui->bAdd, &QPushButton::clicked, [=](){ connect(ui->bAdd, &QPushButton::clicked, [=](){
model->addOperation(new Math::MedianFilter); model->addOperation(new Math::MedianFilter);
}); });
@ -72,18 +85,41 @@ QVariant MathModel::data(const QModelIndex &index, int role) const
} }
auto math = t.getMathOperations().at(index.row()); auto math = t.getMathOperations().at(index.row());
switch(index.column()) { switch(index.column()) {
// case ColIndexEnabled: case ColIndexStatus:
// if(role == Qt::CheckStateRole) { if(role == Qt::DecorationRole) {
// return math.enabled ? Qt::Checked : Qt::Unchecked; switch(math.math->getStatus()) {
// } case TraceMath::Status::Ok:
// break; return QApplication::style()->standardIcon(QStyle::SP_DialogApplyButton);
case TraceMath::Status::Warning:
return QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning);
case TraceMath::Status::Error:
return QApplication::style()->standardIcon(QStyle::SP_MessageBoxCritical);
}
} else if(role == Qt::ToolTipRole) {
if(math.math->getStatus() != TraceMath::Status::Ok) {
return math.math->getStatusDescription();
}
}
break;
case ColIndexDescription: case ColIndexDescription:
if(role == Qt::DisplayRole) { if(role == Qt::DisplayRole) {
return math.math->description(); return math.math->description();
} else if(role == Qt::CheckStateRole){
return math.enabled ? Qt::Checked : Qt::Unchecked;
} }
// else if(role == Qt::CheckStateRole){
// return math.enabled ? Qt::Checked : Qt::Unchecked;
// }
break; break;
case ColIndexDomain:
if(role == Qt::DisplayRole) {
switch(math.math->getDataType()) {
case TraceMath::DataType::Time:
return "Time";
case TraceMath::DataType::Frequency:
return "Frequency";
case TraceMath::DataType::Invalid:
return "Invalid";
}
}
} }
return QVariant(); return QVariant();
} }
@ -92,8 +128,9 @@ QVariant MathModel::headerData(int section, Qt::Orientation orientation, int rol
{ {
if(orientation == Qt::Horizontal && role == Qt::DisplayRole) { if(orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch(section) { switch(section) {
// case ColIndexEnabled: return "Enabled"; break; case ColIndexStatus: return "Status"; break;
case ColIndexDescription: return "Description"; break; case ColIndexDescription: return "Description"; break;
case ColIndexDomain: return "Output domain"; break;
default: break; default: break;
} }
} }
@ -107,11 +144,11 @@ Qt::ItemFlags MathModel::flags(const QModelIndex &index) const
// the first entry is always the trace itself and not enabled // the first entry is always the trace itself and not enabled
flags |= Qt::ItemIsEnabled | Qt::ItemIsSelectable; flags |= Qt::ItemIsEnabled | Qt::ItemIsSelectable;
} }
switch(index.column()) { // switch(index.column()) {
case ColIndexDescription: flags |= Qt::ItemIsUserCheckable; break; // case ColIndexDescription: flags |= Qt::ItemIsUserCheckable; break;
default: // default:
break; // break;
} // }
return (Qt::ItemFlags) flags; return (Qt::ItemFlags) flags;
} }
@ -120,6 +157,8 @@ void MathModel::addOperation(TraceMath *math)
beginInsertRows(QModelIndex(), t.getMathOperations().size(), t.getMathOperations().size()); beginInsertRows(QModelIndex(), t.getMathOperations().size(), t.getMathOperations().size());
t.addMathOperation(math); t.addMathOperation(math);
endInsertRows(); endInsertRows();
// open the editor for the newly added operation
math->edit();
} }
void MathModel::deleteRow(unsigned int row) void MathModel::deleteRow(unsigned int row)

View File

@ -16,8 +16,9 @@ public:
MathModel(Trace &t, QObject *parent = 0); MathModel(Trace &t, QObject *parent = 0);
enum { enum {
// ColIndexEnabled = 0, ColIndexStatus = 0,
ColIndexDescription = 0, ColIndexDescription = 1,
ColIndexDomain = 2,
ColIndexLast, ColIndexLast,
}; };

View File

@ -2,19 +2,22 @@
<ui version="4.0"> <ui version="4.0">
<class>TraceMathEditDialog</class> <class>TraceMathEditDialog</class>
<widget class="QDialog" name="TraceMathEditDialog"> <widget class="QDialog" name="TraceMathEditDialog">
<property name="windowModality">
<enum>Qt::ApplicationModal</enum>
</property>
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>411</width> <width>903</width>
<height>302</height> <height>350</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>Math functions</string> <string>Math functions</string>
</property> </property>
<property name="modal"> <property name="modal">
<bool>true</bool> <bool>false</bool>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
<item> <item>
@ -46,7 +49,7 @@
<number>70</number> <number>70</number>
</attribute> </attribute>
<attribute name="horizontalHeaderStretchLastSection"> <attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool> <bool>false</bool>
</attribute> </attribute>
<attribute name="verticalHeaderVisible"> <attribute name="verticalHeaderVisible">
<bool>false</bool> <bool>false</bool>
@ -110,7 +113,8 @@
<string/> <string/>
</property> </property>
<property name="icon"> <property name="icon">
<iconset theme="go-up"/> <iconset theme="go-up">
<normaloff>.</normaloff>.</iconset>
</property> </property>
</widget> </widget>
</item> </item>
@ -126,7 +130,8 @@
<string/> <string/>
</property> </property>
<property name="icon"> <property name="icon">
<iconset theme="go-down"/> <iconset theme="go-down">
<normaloff>.</normaloff>.</iconset>
</property> </property>
</widget> </widget>
</item> </item>

View File

@ -22,6 +22,7 @@ Trace::Trace(QString name, QColor color, LiveParameter live)
updateLastMath(mathOps.rbegin()); updateLastMath(mathOps.rbegin());
self.enabled = false; self.enabled = false;
dataType = DataType::Frequency;
} }
Trace::~Trace() Trace::~Trace()
@ -35,6 +36,7 @@ void Trace::clear() {
} }
data.clear(); data.clear();
settings.valid = false; settings.valid = false;
warning("No data");
emit cleared(this); emit cleared(this);
emit outputSamplesChanged(0, 0); emit outputSamplesChanged(0, 0);
} }
@ -71,6 +73,7 @@ void Trace::addData(const Trace::Data& d) {
// insert at this position // insert at this position
data.insert(lower, d); data.insert(lower, d);
} }
success();
emit outputSamplesChanged(lower - data.begin(), lower - data.begin() + 1); emit outputSamplesChanged(lower - data.begin(), lower - data.begin() + 1);
if(lower == data.begin()) { if(lower == data.begin()) {
// received the first point, which means the last sweep just finished // received the first point, which means the last sweep just finished

View File

@ -16,6 +16,9 @@
<property name="windowTitle"> <property name="windowTitle">
<string>Edit Trace</string> <string>Edit Trace</string>
</property> </property>
<property name="modal">
<bool>false</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item> <item>
<layout class="QFormLayout" name="formLayout"> <layout class="QFormLayout" name="formLayout">