Optionally interpolate markers

This commit is contained in:
Jan Käberich 2022-01-07 12:37:47 +01:00
parent 367451dc04
commit 2f7449ed21
8 changed files with 210 additions and 42 deletions

View File

@ -85,8 +85,8 @@ void Marker::assignTrace(Trace *t)
// Marker was just created and this is the first assignment to a trace. // Marker was just created and this is the first assignment to a trace.
// Use display format on graph from preferences // Use display format on graph from preferences
auto p = Preferences::getInstance(); auto p = Preferences::getInstance();
if(p.Graphs.markerBehavior.showDataOnGraphs) { if(p.Marker.defaultBehavior.showDataOnGraphs) {
if(p.Graphs.markerBehavior.showAllData) { if(p.Marker.defaultBehavior.showAllData) {
for(auto f : applicableFormats()) { for(auto f : applicableFormats()) {
formatGraph.insert(f); formatGraph.insert(f);
} }
@ -598,7 +598,7 @@ void Marker::traceDataChanged()
newdata = numeric_limits<complex<double>>::quiet_NaN(); newdata = numeric_limits<complex<double>>::quiet_NaN();
} else { } else {
// some data of the parent trace changed, check if marker data also changed // some data of the parent trace changed, check if marker data also changed
newdata = parentTrace->sample(parentTrace->index(position)).y; newdata = parentTrace->interpolatedSample(position).y;
} }
} }
if (newdata != data) { if (newdata != data) {
@ -839,8 +839,10 @@ void Marker::constrainPosition()
} else if(position < parentTrace->minX()) { } else if(position < parentTrace->minX()) {
position = parentTrace->minX(); position = parentTrace->minX();
} }
// set position to closest trace index if(!Preferences::getInstance().Marker.interpolatePoints) {
position = parentTrace->sample(parentTrace->index(position)).x; // marker interpolation disabled, set position to closest trace index
position = parentTrace->sample(parentTrace->index(position)).x;
}
} }
traceDataChanged(); traceDataChanged();
} }

View File

@ -5,6 +5,7 @@
#include "ui_smithchartdialog.h" #include "ui_smithchartdialog.h"
#include "unit.h" #include "unit.h"
#include "QFileDialog" #include "QFileDialog"
#include "Util/util.h"
#include <QPainter> #include <QPainter>
#include <array> #include <array>
@ -141,6 +142,7 @@ double TraceSmithChart::nearestTracePoint(Trace *t, QPoint pixel, double *distan
{ {
double closestDistance = numeric_limits<double>::max(); double closestDistance = numeric_limits<double>::max();
double closestXpos = 0; double closestXpos = 0;
unsigned int closestIndex = 0;
auto samples = t->size(); auto samples = t->size();
for(unsigned int i=0;i<samples;i++) { for(unsigned int i=0;i<samples;i++) {
auto data = t->sample(i); auto data = t->sample(i);
@ -154,6 +156,28 @@ double TraceSmithChart::nearestTracePoint(Trace *t, QPoint pixel, double *distan
if(distance < closestDistance) { if(distance < closestDistance) {
closestDistance = distance; closestDistance = distance;
closestXpos = t->sample(i).x; closestXpos = t->sample(i).x;
closestIndex = i;
}
}
closestDistance = sqrt(closestDistance);
if(closestIndex > 0) {
auto l1 = dataToPixel(t->sample(closestIndex-1));
auto l2 = dataToPixel(t->sample(closestIndex));
double ratio;
auto distance = Util::distanceToLine(pixel, l1, l2, nullptr, &ratio);
if(distance < closestDistance) {
closestDistance = distance;
closestXpos = t->sample(closestIndex-1).x + (t->sample(closestIndex).x - t->sample(closestIndex-1).x) * ratio;
}
}
if(closestIndex < t->size() - 1) {
auto l1 = dataToPixel(t->sample(closestIndex));
auto l2 = dataToPixel(t->sample(closestIndex+1));
double ratio;
auto distance = Util::distanceToLine(pixel, l1, l2, nullptr, &ratio);
if(distance < closestDistance) {
closestDistance = distance;
closestXpos = t->sample(closestIndex).x + (t->sample(closestIndex+1).x - t->sample(closestIndex).x) * ratio;
} }
} }
if(distance) { if(distance) {

View File

@ -491,7 +491,17 @@ void TraceXYPlot::draw(QPainter &p)
continue; continue;
} }
auto t = m->getTrace(); auto t = m->getTrace();
QPointF markerPoint = traceToCoordinate(t, t->index(xPosition), YAxis[i].type); auto index = t->index(xPosition);
QPointF markerPoint;
if(xPosition < t->sample(index).x && index > 0) {
// marker is not located exactly at this point, interpolate display location
QPointF l0 = traceToCoordinate(t, index - 1, YAxis[i].type);
QPointF l1 = traceToCoordinate(t, index, YAxis[i].type);
auto t0 = (xPosition - t->sample(index - 1).x) / (t->sample(index).x - t->sample(index - 1).x);
markerPoint = l0 + (l1 - l0) * t0;
} else {
markerPoint = traceToCoordinate(t, t->index(xPosition), YAxis[i].type);
}
auto point = plotValueToPixel(markerPoint, i); auto point = plotValueToPixel(markerPoint, i);
if(!plotRect.contains(point)) { if(!plotRect.contains(point)) {
// out of screen // out of screen
@ -1096,6 +1106,7 @@ double TraceXYPlot::nearestTracePoint(Trace *t, QPoint pixel, double *distance)
} }
double closestDistance = numeric_limits<double>::max(); double closestDistance = numeric_limits<double>::max();
double closestXpos = 0; double closestXpos = 0;
unsigned int closestIndex = 0;
auto samples = t->size(); auto samples = t->size();
for(unsigned int i=0;i<samples;i++) { for(unsigned int i=0;i<samples;i++) {
auto point = traceToCoordinate(t, i, YAxis[0].type); auto point = traceToCoordinate(t, i, YAxis[0].type);
@ -1108,6 +1119,28 @@ double TraceXYPlot::nearestTracePoint(Trace *t, QPoint pixel, double *distance)
if(distance < closestDistance) { if(distance < closestDistance) {
closestDistance = distance; closestDistance = distance;
closestXpos = point.x(); closestXpos = point.x();
closestIndex = i;
}
}
closestDistance = sqrt(closestDistance);
if(closestIndex > 0) {
auto l1 = plotValueToPixel(traceToCoordinate(t, closestIndex - 1, YAxis[0].type), 0);
auto l2 = plotValueToPixel(traceToCoordinate(t, closestIndex, YAxis[0].type), 0);
double ratio;
auto distance = Util::distanceToLine(pixel, l1, l2, nullptr, &ratio);
if(distance < closestDistance) {
closestDistance = distance;
closestXpos = t->sample(closestIndex-1).x + (t->sample(closestIndex).x - t->sample(closestIndex-1).x) * ratio;
}
}
if(closestIndex < t->size() - 1) {
auto l1 = plotValueToPixel(traceToCoordinate(t, closestIndex, YAxis[0].type), 0);
auto l2 = plotValueToPixel(traceToCoordinate(t, closestIndex + 1, YAxis[0].type), 0);
double ratio;
auto distance = Util::distanceToLine(pixel, l1, l2, nullptr, &ratio);
if(distance < closestDistance) {
closestDistance = distance;
closestXpos = t->sample(closestIndex).x + (t->sample(closestIndex+1).x - t->sample(closestIndex).x) * ratio;
} }
} }
if(XAxis.type == XAxisType::Distance) { if(XAxis.type == XAxisType::Distance) {

View File

@ -1,5 +1,7 @@
#include "util.h" #include "util.h"
#include <QVector2D>
void Util::unwrapPhase(std::vector<double> &phase) void Util::unwrapPhase(std::vector<double> &phase)
{ {
for (unsigned int i = 1; i < phase.size(); i++) { for (unsigned int i = 1; i < phase.size(); i++) {
@ -24,3 +26,31 @@ void Util::linearRegression(const std::vector<double> &input, double &B_0, doubl
B_1 = ss_xy / ss_xx; B_1 = ss_xy / ss_xx;
B_0 = y_mean - B_1 * x_mean; B_0 = y_mean - B_1 * x_mean;
} }
double Util::distanceToLine(QPointF point, QPointF l1, QPointF l2, QPointF *closestLinePoint, double *pointRatio)
{
auto M = l2 - l1;
auto t0 = QPointF::dotProduct(M, point - l1) / QPointF::dotProduct(M, M);
QPointF closestPoint;
QVector2D orthVect;
if (t0 <= 0) {
orthVect = QVector2D(point - l1);
closestPoint = l1;
t0 = 0;
} else if(t0 >= 1) {
orthVect = QVector2D(point - l2);
closestPoint = l2;
t0 = 1;
} else {
auto intersect = l1 + t0 * M;
orthVect = QVector2D(point - intersect);
closestPoint = intersect;
}
if(closestLinePoint) {
*closestLinePoint = closestPoint;
}
if(pointRatio) {
*pointRatio = t0;
}
return orthVect.length();
}

View File

@ -7,6 +7,7 @@
#include <vector> #include <vector>
#include <QColor> #include <QColor>
#include <QPoint>
namespace Util { namespace Util {
template<typename T> T Scale(T value, T from_low, T from_high, T to_low, T to_high, bool log_from = false, bool log_to = false) { template<typename T> T Scale(T value, T from_low, T from_high, T to_low, T to_high, bool log_from = false, bool log_to = false) {
@ -69,6 +70,8 @@ namespace Util {
// input values are Y coordinates, assumes evenly spaced linear X values from 0 to input.size() - 1 // input values are Y coordinates, assumes evenly spaced linear X values from 0 to input.size() - 1
void linearRegression(const std::vector<double> &input, double &B_0, double &B_1); void linearRegression(const std::vector<double> &input, double &B_0, double &B_1);
double distanceToLine(QPointF point, QPointF l1, QPointF l2, QPointF *closestLinePoint = nullptr, double *pointRatio = nullptr);
} }
#endif // UTILH_H #endif // UTILH_H

View File

@ -80,8 +80,8 @@ PreferencesDialog::PreferencesDialog(Preferences *pref, QWidget *parent) :
ui->SCPIServerEnabled->setEnabled(false); ui->SCPIServerEnabled->setEnabled(false);
} }
connect(ui->GraphsShowMarkerData, &QCheckBox::toggled, [=](bool enabled) { connect(ui->MarkerShowMarkerData, &QCheckBox::toggled, [=](bool enabled) {
ui->GraphsShowAllMarkerData->setEnabled(enabled); ui->MarkerShowAllMarkerData->setEnabled(enabled);
}); });
// Page selection // Page selection
@ -128,6 +128,7 @@ PreferencesDialog::PreferencesDialog(Preferences *pref, QWidget *parent) :
p->Startup.SA.window = ui->StartupSAWindow->currentIndex(); p->Startup.SA.window = ui->StartupSAWindow->currentIndex();
p->Startup.SA.detector = ui->StartupSADetector->currentIndex(); p->Startup.SA.detector = ui->StartupSADetector->currentIndex();
p->Startup.SA.signalID = ui->StartupSASignalID->isChecked(); p->Startup.SA.signalID = ui->StartupSASignalID->isChecked();
p->Acquisition.alwaysExciteBothPorts = ui->AcquisitionAlwaysExciteBoth->isChecked(); p->Acquisition.alwaysExciteBothPorts = ui->AcquisitionAlwaysExciteBoth->isChecked();
p->Acquisition.suppressPeaks = ui->AcquisitionSuppressPeaks->isChecked(); p->Acquisition.suppressPeaks = ui->AcquisitionSuppressPeaks->isChecked();
p->Acquisition.adjustPowerLevel = ui->AcquisitionAdjustPowerLevel->isChecked(); p->Acquisition.adjustPowerLevel = ui->AcquisitionAdjustPowerLevel->isChecked();
@ -135,6 +136,7 @@ PreferencesDialog::PreferencesDialog(Preferences *pref, QWidget *parent) :
p->Acquisition.useDFTinSAmode = ui->AcquisitionUseDFT->isChecked(); p->Acquisition.useDFTinSAmode = ui->AcquisitionUseDFT->isChecked();
p->Acquisition.RBWLimitForDFT = ui->AcquisitionDFTlimitRBW->value(); p->Acquisition.RBWLimitForDFT = ui->AcquisitionDFTlimitRBW->value();
p->Acquisition.useMedianAveraging = ui->AcquisitionAveragingMode->currentIndex() == 1; p->Acquisition.useMedianAveraging = ui->AcquisitionAveragingMode->currentIndex() == 1;
p->Graphs.showUnits = ui->GraphsShowUnit->isChecked(); p->Graphs.showUnits = ui->GraphsShowUnit->isChecked();
p->Graphs.Color.background = ui->GraphsColorBackground->getColor(); p->Graphs.Color.background = ui->GraphsColorBackground->getColor();
p->Graphs.Color.axis = ui->GraphsColorAxis->getColor(); p->Graphs.Color.axis = ui->GraphsColorAxis->getColor();
@ -142,9 +144,12 @@ PreferencesDialog::PreferencesDialog(Preferences *pref, QWidget *parent) :
p->Graphs.Color.Ticks.Background.background = ui->GraphsColorTicksBackground->getColor(); p->Graphs.Color.Ticks.Background.background = ui->GraphsColorTicksBackground->getColor();
p->Graphs.Color.Ticks.divisions = ui->GraphsColorTicksDivisions->getColor(); p->Graphs.Color.Ticks.divisions = ui->GraphsColorTicksDivisions->getColor();
p->Graphs.domainChangeBehavior = (GraphDomainChangeBehavior) ui->GraphsDomainChangeBehavior->currentIndex(); p->Graphs.domainChangeBehavior = (GraphDomainChangeBehavior) ui->GraphsDomainChangeBehavior->currentIndex();
p->Graphs.markerBehavior.showDataOnGraphs = ui->GraphsShowMarkerData->isChecked();
p->Graphs.markerBehavior.showAllData = ui->GraphsShowAllMarkerData->isChecked();
p->Graphs.lineWidth = ui->GraphsLineWidth->value(); p->Graphs.lineWidth = ui->GraphsLineWidth->value();
p->Marker.defaultBehavior.showDataOnGraphs = ui->MarkerShowMarkerData->isChecked();
p->Marker.defaultBehavior.showAllData = ui->MarkerShowAllMarkerData->isChecked();
p->Marker.interpolatePoints = ui->MarkerInterpolate->currentIndex() == 1;
p->SCPIServer.enabled = ui->SCPIServerEnabled->isChecked(); p->SCPIServer.enabled = ui->SCPIServerEnabled->isChecked();
p->SCPIServer.port = ui->SCPIServerPort->value(); p->SCPIServer.port = ui->SCPIServerPort->value();
accept(); accept();
@ -211,10 +216,12 @@ void PreferencesDialog::setInitialGUIState()
ui->GraphsColorTicksBackgroundEnabled->setChecked(p->Graphs.Color.Ticks.Background.enabled); ui->GraphsColorTicksBackgroundEnabled->setChecked(p->Graphs.Color.Ticks.Background.enabled);
ui->GraphsColorTicksBackground->setColor(p->Graphs.Color.Ticks.Background.background); ui->GraphsColorTicksBackground->setColor(p->Graphs.Color.Ticks.Background.background);
ui->GraphsDomainChangeBehavior->setCurrentIndex((int) p->Graphs.domainChangeBehavior); ui->GraphsDomainChangeBehavior->setCurrentIndex((int) p->Graphs.domainChangeBehavior);
ui->GraphsShowMarkerData->setChecked(p->Graphs.markerBehavior.showDataOnGraphs);
ui->GraphsShowAllMarkerData->setChecked(p->Graphs.markerBehavior.showAllData);
ui->GraphsLineWidth->setValue(p->Graphs.lineWidth); ui->GraphsLineWidth->setValue(p->Graphs.lineWidth);
ui->MarkerShowMarkerData->setChecked(p->Marker.defaultBehavior.showDataOnGraphs);
ui->MarkerShowAllMarkerData->setChecked(p->Marker.defaultBehavior.showAllData);
ui->MarkerInterpolate->setCurrentIndex(p->Marker.interpolatePoints ? 1 : 0);
ui->SCPIServerEnabled->setChecked(p->SCPIServer.enabled); ui->SCPIServerEnabled->setChecked(p->SCPIServer.enabled);
ui->SCPIServerPort->setValue(p->SCPIServer.port); ui->SCPIServerPort->setValue(p->SCPIServer.port);

View File

@ -83,12 +83,16 @@ public:
} Ticks; } Ticks;
} Color; } Color;
GraphDomainChangeBehavior domainChangeBehavior; GraphDomainChangeBehavior domainChangeBehavior;
double lineWidth;
} Graphs;
struct {
struct { struct {
bool showDataOnGraphs; bool showDataOnGraphs;
bool showAllData; bool showAllData;
} markerBehavior; } defaultBehavior;
double lineWidth; bool interpolatePoints;
} Graphs; } Marker;
struct { struct {
bool enabled; bool enabled;
int port; int port;
@ -104,7 +108,7 @@ private:
QString name; QString name;
QVariant def; QVariant def;
}; };
const std::array<SettingDescription, 41> descr = {{ const std::array<SettingDescription, 42> descr = {{
{&Startup.ConnectToFirstDevice, "Startup.ConnectToFirstDevice", true}, {&Startup.ConnectToFirstDevice, "Startup.ConnectToFirstDevice", true},
{&Startup.RememberSweepSettings, "Startup.RememberSweepSettings", false}, {&Startup.RememberSweepSettings, "Startup.RememberSweepSettings", false},
{&Startup.DefaultSweep.type, "Startup.DefaultSweep.type", "Frequency"}, {&Startup.DefaultSweep.type, "Startup.DefaultSweep.type", "Frequency"},
@ -141,9 +145,10 @@ private:
{&Graphs.Color.Ticks.Background.background, "Graphs.Color.Ticks.Background.background", QColor(20, 20, 20)}, {&Graphs.Color.Ticks.Background.background, "Graphs.Color.Ticks.Background.background", QColor(20, 20, 20)},
{&Graphs.Color.Ticks.divisions, "Graphs.Color.Ticks.divisions", QColor(Qt::gray)}, {&Graphs.Color.Ticks.divisions, "Graphs.Color.Ticks.divisions", QColor(Qt::gray)},
{&Graphs.domainChangeBehavior, "Graphs.domainChangeBehavior", GraphDomainChangeBehavior::AdjustGraphs}, {&Graphs.domainChangeBehavior, "Graphs.domainChangeBehavior", GraphDomainChangeBehavior::AdjustGraphs},
{&Graphs.markerBehavior.showDataOnGraphs, "Graphs.markerBehavior.ShowDataOnGraphs", true},
{&Graphs.markerBehavior.showAllData, "Graphs.markerBehavior.ShowAllData", false},
{&Graphs.lineWidth, "Graphs.lineWidth", 1.0}, {&Graphs.lineWidth, "Graphs.lineWidth", 1.0},
{&Marker.defaultBehavior.showDataOnGraphs, "Marker.defaultBehavior.ShowDataOnGraphs", true},
{&Marker.defaultBehavior.showAllData, "Marker.defaultBehavior.ShowAllData", false},
{&Marker.interpolatePoints, "Marker.interpolatePoints", false},
{&SCPIServer.enabled, "SCPIServer.enabled", true}, {&SCPIServer.enabled, "SCPIServer.enabled", true},
{&SCPIServer.port, "SCPIServer.port", 19542}, {&SCPIServer.port, "SCPIServer.port", 19542},
}}; }};

View File

@ -56,6 +56,11 @@
<string>Graphs</string> <string>Graphs</string>
</property> </property>
</item> </item>
<item>
<property name="text">
<string>Marker</string>
</property>
</item>
<item> <item>
<property name="text"> <property name="text">
<string>SCPI Server</string> <string>SCPI Server</string>
@ -78,7 +83,7 @@
</size> </size>
</property> </property>
<property name="currentIndex"> <property name="currentIndex">
<number>2</number> <number>3</number>
</property> </property>
<widget class="QWidget" name="Startup"> <widget class="QWidget" name="Startup">
<layout class="QHBoxLayout" name="horizontalLayout_4"> <layout class="QHBoxLayout" name="horizontalLayout_4">
@ -831,29 +836,6 @@
</layout> </layout>
</widget> </widget>
</item> </item>
<item>
<widget class="QGroupBox" name="groupBox_9">
<property name="title">
<string>Marker Default Behavior</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_9">
<item>
<widget class="QCheckBox" name="GraphsShowMarkerData">
<property name="text">
<string>Show data on graphs</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="GraphsShowAllMarkerData">
<property name="text">
<string>Show data in all available formats</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item> <item>
<spacer name="verticalSpacer_3"> <spacer name="verticalSpacer_3">
<property name="orientation"> <property name="orientation">
@ -884,6 +866,88 @@
</item> </item>
</layout> </layout>
</widget> </widget>
<widget class="QWidget" name="Marker">
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<layout class="QVBoxLayout" name="verticalLayout_9">
<item>
<widget class="QGroupBox" name="groupBox_9">
<property name="title">
<string>Default Behavior</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_12">
<item>
<widget class="QCheckBox" name="MarkerShowMarkerData">
<property name="text">
<string>Show data on graphs</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="MarkerShowAllMarkerData">
<property name="text">
<string>Show data in all available formats</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<layout class="QFormLayout" name="formLayout_9">
<item row="0" column="0">
<widget class="QLabel" name="label_28">
<property name="text">
<string>Positioning:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="MarkerInterpolate">
<item>
<property name="text">
<string>Snap to measurement points</string>
</property>
</item>
<item>
<property name="text">
<string>Interpolate between points</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer_5">
<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>
</item>
<item>
<spacer name="horizontalSpacer_5">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>368</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QWidget" name="SCPIServer"> <widget class="QWidget" name="SCPIServer">
<layout class="QHBoxLayout" name="horizontalLayout_10"> <layout class="QHBoxLayout" name="horizontalLayout_10">
<item> <item>