From 2d13fdfe5ee9bef149fe26f378c33a0ba56fb901 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4berich?= Date: Wed, 12 May 2021 22:55:05 +0200 Subject: [PATCH] Improvements to time domain gate handling --- Software/PC_Application/Traces/Math/tdr.cpp | 12 +++++ Software/PC_Application/Traces/Math/tdr.h | 1 + .../PC_Application/Traces/Math/timegate.cpp | 51 +++++++++++++++---- .../PC_Application/Traces/traceeditdialog.cpp | 35 +++++++++---- 4 files changed, 81 insertions(+), 18 deletions(-) diff --git a/Software/PC_Application/Traces/Math/tdr.cpp b/Software/PC_Application/Traces/Math/tdr.cpp index bc9288d..9c05983 100644 --- a/Software/PC_Application/Traces/Math/tdr.cpp +++ b/Software/PC_Application/Traces/Math/tdr.cpp @@ -160,6 +160,18 @@ void TDR::fromJSON(nlohmann::json j) } } +void TDR::setMode(Mode m) +{ + if(mode == m) { + // already set to correct mode + return; + } + mode = m; + if(input) { + inputSamplesChanged(0, input->rData().size()); + } +} + void TDR::inputSamplesChanged(unsigned int begin, unsigned int end) { Q_UNUSED(end); diff --git a/Software/PC_Application/Traces/Math/tdr.h b/Software/PC_Application/Traces/Math/tdr.h index 4d223fb..c1b2ca9 100644 --- a/Software/PC_Application/Traces/Math/tdr.h +++ b/Software/PC_Application/Traces/Math/tdr.h @@ -25,6 +25,7 @@ public: Lowpass, Bandpass, }; + void setMode(Mode m); Mode getMode() const; const WindowFunction& getWindow() const; diff --git a/Software/PC_Application/Traces/Math/timegate.cpp b/Software/PC_Application/Traces/Math/timegate.cpp index 8fd4974..ff36b34 100644 --- a/Software/PC_Application/Traces/Math/timegate.cpp +++ b/Software/PC_Application/Traces/Math/timegate.cpp @@ -135,6 +135,10 @@ void Math::TimeGate::fromJSON(nlohmann::json j) void Math::TimeGate::setStart(double start) { + if(input && input->rData().size() > 0 && start < input->rData().front().x) { + start = input->rData().back().x; + } + double stop = center + span / 2; if(start < stop) { span = stop - start; @@ -148,6 +152,10 @@ void Math::TimeGate::setStart(double start) void Math::TimeGate::setStop(double stop) { + if(input && input->rData().size() > 0 && stop > input->rData().back().x) { + stop = input->rData().back().x; + } + double start = center - span / 2; if(stop > start) { span = stop - start; @@ -198,7 +206,11 @@ void Math::TimeGate::inputSamplesChanged(unsigned int begin, unsigned int end) data[i].y *= filter[i]; } emit outputSamplesChanged(begin, end); - success(); + if(input->rData().size() > 0) { + success(); + } else { + warning("No input data"); + } } void Math::TimeGate::updateFilter() @@ -214,12 +226,16 @@ void Math::TimeGate::updateFilter() } auto maxX = input->rData().back().x; auto minX = input->rData().front().x; - if(center - span / 2 < minX) { - span = (center - minX) * 2; - } - if(center + span / 2 > maxX) { - span = (maxX - center) * 2; - } + +// auto c1 = center - span / 2; +// auto c2 = center + span / 2; + +// if(c1 < minX) { +// c1 = minX; +// } +// if(c2 > maxX) { +// c2 = maxX; +// } auto wc1 = Util::Scale(center - span / 2, minX, maxX, 0, 1); auto wc2 = Util::Scale(center + span / 2, minX, maxX, 0, 1); @@ -265,6 +281,10 @@ Math::TimeGateGraph::TimeGateGraph(QWidget *parent) QPoint Math::TimeGateGraph::plotValueToPixel(double x, double y) { + if(!gate->getInput() || !gate->getInput()->rData().size()) { + return QPoint(0, 0); + } + auto input = gate->getInput()->rData(); auto minX = input.front().x; auto maxX = input.back().x; @@ -282,6 +302,10 @@ QPoint Math::TimeGateGraph::plotValueToPixel(double x, double y) QPointF Math::TimeGateGraph::pixelToPlotValue(QPoint p) { + if(!gate->getInput() || !gate->getInput()->rData().size()) { + return QPointF(0.0, 0.0); + } + auto input = gate->getInput()->rData(); auto minX = input.front().x; auto maxX = input.back().x; @@ -367,10 +391,19 @@ void Math::TimeGateGraph::mousePressEvent(QMouseEvent *event) grabbedStart = false; auto startX = plotValueToPixel(gate->getStart(), 0).x(); auto stopX = plotValueToPixel(gate->getStop(), 0).x(); - if(abs(event->pos().x() - startX) < catchDistance) { + auto distStart = abs(event->pos().x() - startX); + auto distStop = abs(event->pos().x() - stopX); + if(distStart < distStop && distStart < catchDistance) { grabbedStart = true; - } else if(abs(event->pos().x() - stopX) < catchDistance) { + } else if(distStop < distStart && distStop < catchDistance) { grabbedStop = true; + } else if(distStop == distStart && distStop < catchDistance) { + // could happen if start/stop are close to each with respect to input range + if(event->pos().x() > stopX) { + grabbedStop = true; + } else { + grabbedStart = true; + } } } diff --git a/Software/PC_Application/Traces/traceeditdialog.cpp b/Software/PC_Application/Traces/traceeditdialog.cpp index 8010880..eacb0d2 100644 --- a/Software/PC_Application/Traces/traceeditdialog.cpp +++ b/Software/PC_Application/Traces/traceeditdialog.cpp @@ -3,6 +3,8 @@ #include #include #include "ui_newtracemathdialog.h" +#include "Math/tdr.h" + namespace Ui { class NewTraceMathDialog; } @@ -145,18 +147,33 @@ TraceEditDialog::TraceEditDialog(Trace &t, QWidget *parent) : auto newMath = TraceMath::createMath(type); model->addOperations(newMath); if(newMath.size() == 1) { - // any normal math operation added, edit now - newMath[0]->edit(); + // any normal math operation added, edit now + newMath[0]->edit(); } else { - // composite operation added, check which one and edit the correct suboperation - switch(type) { - case TraceMath::Type::TimeDomainGating: - // TDR/DFT can be left at default, edit the actual gate - newMath[1]->edit(); + // composite operation added, check which one and edit the correct suboperation + switch(type) { + case TraceMath::Type::TimeDomainGating: + // Automatically select bandpass/lowpass TDR, depending on selected span + if(newMath[0]->getInput()->rData().size() > 0) { + // Automatically select bandpass/lowpass TDR, depending on selected span + auto tdr = (Math::TDR*) newMath[0]; + auto fstart = tdr->getInput()->rData().front().x; + auto fstop = tdr->getInput()->rData().back().x; + + if(fstart < fstop / 100.0) { + tdr->setMode(Math::TDR::Mode::Lowpass); + } else { + // lowpass mode would result in very few points in the time domain, switch to bandpass mode + tdr->setMode(Math::TDR::Mode::Bandpass); + } + } + + // TDR/DFT can be left at default, edit the actual gate + newMath[1]->edit(); break; - default: + default: break; - } + } } }); ui->list->setCurrentRow(0);