Improvements to time domain gate handling

This commit is contained in:
Jan Käberich 2021-05-12 22:55:05 +02:00
parent 3d4d3c9468
commit 2d13fdfe5e
4 changed files with 81 additions and 18 deletions

View File

@ -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);

View File

@ -25,6 +25,7 @@ public:
Lowpass,
Bandpass,
};
void setMode(Mode m);
Mode getMode() const;
const WindowFunction& getWindow() const;

View File

@ -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<double>(center - span / 2, minX, maxX, 0, 1);
auto wc2 = Util::Scale<double>(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;
}
}
}

View File

@ -3,6 +3,8 @@
#include <QColorDialog>
#include <QFileDialog>
#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);