Skip to content

Commit

Permalink
continuous updates for TDR/DFT with optional rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
jankae committed May 8, 2024
1 parent 5ae57bf commit 792a6a4
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 17 deletions.
18 changes: 12 additions & 6 deletions Software/PC_Application/LibreVNA-GUI/Traces/Math/dft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include "ui_dftexplanationwidget.h"
#include "appwindow.h"

#include <chrono>
#include <thread>

#include <QDebug>

using namespace std;
Expand Down Expand Up @@ -133,11 +136,6 @@ void Math::DFT::inputSamplesChanged(unsigned int begin, unsigned int end)
warning("Not enough input samples");
return;
}
// DFT is computationally expensive, only update at the end of sweep -> check if this is the first changed data in the next sweep
if(begin != 0) {
// not the end, do nothing
return;
}
// trigger calculation in thread
semphr.release();
success();
Expand All @@ -159,6 +157,8 @@ Math::DFTThread::DFTThread(Math::DFT &dft)
void Math::DFTThread::run()
{
qDebug() << "DFT thread starting";
using namespace std::chrono;
auto lastCalc = system_clock::now();
while(1) {
dft.semphr.acquire();
// clear possible additional semaphores
Expand All @@ -168,7 +168,13 @@ void Math::DFTThread::run()
qDebug() << "DFT thread exiting";
return;
}
qDebug() << "DFT thread calculating";
// limit update rate if configured in preferences
auto &p = Preferences::getInstance();
if(p.Acquisition.limitDFT) {
std::this_thread::sleep_until(lastCalc + duration<double>(1.0 / p.Acquisition.maxDFTrate));
lastCalc = system_clock::now();
}
// qDebug() << "DFT thread calculating";
double DC = dft.DCfreq;
TDR *tdr = nullptr;
if(dft.automaticDC) {
Expand Down
18 changes: 12 additions & 6 deletions Software/PC_Application/LibreVNA-GUI/Traces/Math/tdr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include "Util/util.h"
#include "appwindow.h"

#include <chrono>
#include <thread>

#include <QVBoxLayout>
#include <QLabel>
#include <QDebug>
Expand Down Expand Up @@ -201,11 +204,6 @@ void TDR::inputSamplesChanged(unsigned int begin, unsigned int end)
{
Q_UNUSED(end);
if(input->rData().size() >= 2) {
// TDR is computationally expensive, only update at the end of sweep -> check if this is the first changed data in the next sweep
if(begin != 0) {
// not the end, do nothing
return;
}
// trigger calculation in thread
semphr.release();
success();
Expand Down Expand Up @@ -244,6 +242,8 @@ TDRThread::TDRThread(TDR &tdr)
void TDRThread::run()
{
qDebug() << "TDR thread starting";
using namespace std::chrono;
auto lastCalc = system_clock::now();
while(1) {
tdr.semphr.acquire();
// clear possible additional semaphores
Expand All @@ -253,7 +253,13 @@ void TDRThread::run()
qDebug() << "TDR thread exiting";
return;
}
qDebug() << "TDR thread calculating";
// limit update rate if configured in preferences
auto &p = Preferences::getInstance();
if(p.Acquisition.limitDFT) {
std::this_thread::sleep_until(lastCalc + duration<double>(1.0 / p.Acquisition.maxDFTrate));
lastCalc = system_clock::now();
}
// qDebug() << "TDR thread calculating";
// perform calculation
vector<complex<double>> frequencyDomain;
auto stepSize = (tdr.input->rData().back().x - tdr.input->rData().front().x) / (tdr.input->rData().size() - 1);
Expand Down
7 changes: 7 additions & 0 deletions Software/PC_Application/LibreVNA-GUI/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ PreferencesDialog::PreferencesDialog(Preferences *pref, QWidget *parent) :
setInitialGUIState();
}
});
connect(ui->AcquisitionLimitTDRCheckbox, &QCheckBox::toggled, [=](bool enabled){
ui->AcquisitionDFTLimitValue->setEnabled(enabled);
});

setInitialGUIState();
}
Expand Down Expand Up @@ -257,6 +260,8 @@ void PreferencesDialog::setInitialGUIState()
ui->AcquisitionFullSpanStart->setValue(p->Acquisition.fullSpanStart);
ui->AcquisitionFullSpanStop->setValue(p->Acquisition.fullSpanStop);
ui->AcquisitionFullSpanCalibrated->setChecked(p->Acquisition.fullSpanCalibratedRange);
ui->AcquisitionLimitTDRCheckbox->setChecked(p->Acquisition.limitDFT);
ui->AcquisitionDFTLimitValue->setValue(p->Acquisition.maxDFTrate);

ui->GraphsDefaultTransmission->setCurrentText(p->Graphs.defaultGraphs.transmission);
ui->GraphsDefaultReflection->setCurrentText(p->Graphs.defaultGraphs.reflection);
Expand Down Expand Up @@ -354,6 +359,8 @@ void PreferencesDialog::updateFromGUI()
p->Acquisition.fullSpanStart = ui->AcquisitionFullSpanStart->value();
p->Acquisition.fullSpanStop = ui->AcquisitionFullSpanStop->value();
p->Acquisition.fullSpanCalibratedRange = ui->AcquisitionFullSpanCalibrated->isChecked();
p->Acquisition.limitDFT = ui->AcquisitionLimitTDRCheckbox->isChecked();
p->Acquisition.maxDFTrate = ui->AcquisitionDFTLimitValue->value();

p->Graphs.defaultGraphs.transmission = ui->GraphsDefaultTransmission->currentText();
p->Graphs.defaultGraphs.reflection = ui->GraphsDefaultReflection->currentText();
Expand Down
6 changes: 6 additions & 0 deletions Software/PC_Application/LibreVNA-GUI/preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ class Preferences : public Savable {
double fullSpanStart;
double fullSpanStop;
bool fullSpanCalibratedRange;

// Math settings
bool limitDFT;
double maxDFTrate;
} Acquisition;
struct {
bool showUnits;
Expand Down Expand Up @@ -208,6 +212,8 @@ class Preferences : public Savable {
{&Acquisition.fullSpanStart, "Acquisition.fullSpanStart", 0.0},
{&Acquisition.fullSpanStop, "Acquisition.fullSpanStop", 6000000000.0},
{&Acquisition.fullSpanCalibratedRange, "Acquisition.fullSpanCalibratedRange", false},
{&Acquisition.limitDFT, "Acquisition.limitDFT", true},
{&Acquisition.maxDFTrate, "Acquisition.maxDFTrate", 1.0},
{&Graphs.showUnits, "Graphs.showUnits", true},
{&Graphs.Color.background, "Graphs.Color.background", QColor(Qt::black)},
{&Graphs.Color.axis, "Graphs.Color.axis", QColor(Qt::white)},
Expand Down
59 changes: 54 additions & 5 deletions Software/PC_Application/LibreVNA-GUI/preferencesdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
</size>
</property>
<property name="currentIndex">
<number>2</number>
<number>1</number>
</property>
<widget class="QWidget" name="Startup">
<layout class="QHBoxLayout" name="horizontalLayout_4">
Expand Down Expand Up @@ -824,6 +824,55 @@
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_15">
<property name="title">
<string>Math operations</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="QCheckBox" name="AcquisitionLimitTDRCheckbox">
<property name="text">
<string>Limit TDR/DFT to </string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="AcquisitionDFTLimitValue">
<property name="enabled">
<bool>false</bool>
</property>
<property name="minimum">
<double>0.100000000000000</double>
</property>
<property name="maximum">
<double>100.000000000000000</double>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_30">
<property name="text">
<string>updates per second</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_8">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
Expand Down Expand Up @@ -1688,8 +1737,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>168</width>
<height>127</height>
<width>697</width>
<height>563</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_12">
Expand Down Expand Up @@ -1778,8 +1827,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>215</width>
<height>168</height>
<width>697</width>
<height>563</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_19">
Expand Down

0 comments on commit 792a6a4

Please sign in to comment.