Skip to content

Commit

Permalink
implement cable Z0 correction for port extension
Browse files Browse the repository at this point in the history
  • Loading branch information
nanovna committed Jan 23, 2020
1 parent aedc540 commit 86f9959
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 27 deletions.
80 changes: 67 additions & 13 deletions vna_qt/mainwindow.C
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,13 @@ QWidget *MainWindow::createTopFloat(QWidget *w) {
return widget2;
}

void MainWindow::setPortExtension(double t1_seconds, double t2_seconds) {
portExt1Seconds = t1_seconds;
portExt2Seconds = t2_seconds;
ui->t_ext1->setText(qs(ssprintf(32, "%.0f ps", round(t1_seconds*1e12))));
ui->t_ext2->setText(qs(ssprintf(32, "%.0f ps", round(t2_seconds*1e12))));
void MainWindow::updatePortExtension() {
portExt1Seconds = ui->slider_ext1->value()*1e-12;
portExt2Seconds = ui->slider_ext2->value()*1e-12;
portExtZ0 = ui->slider_extz->value()/100.;
ui->t_ext1->setText(qs(ssprintf(32, "%.0f ps", round(portExt1Seconds*1e12))));
ui->t_ext2->setText(qs(ssprintf(32, "%.0f ps", round(portExt2Seconds*1e12))));
ui->t_extz->setText(qs(ssprintf(32, "%.2f", portExtZ0)));
for(int i=0;i<vna->nPoints;i++)
updateViews(i);
}
Expand Down Expand Up @@ -705,6 +707,46 @@ void MainWindow::on_actionSweep_params_triggered() {
}
}

complex<double> calculatePortExt(complex<double> refl, complex<double> Tcable, double Z0, double Zcable) {
double Z0sq = Z0*Z0;
double Zcablesq = Zcable*Zcable;

/* derived using sympy:
from sympy import *
def toRefl(Z, Z0):
return (Z/Z0-1)/(Z/Z0+1)
def toZ(refl, Z0):
return (1+refl)/(1-refl)*Z0
def convertRefl(refl, Z0, newZ0):
Z = toZ(refl, Z0)
return toRefl(Z, newZ0)
refl, Z0, Zcable, Tcable = symbols('refl Z0 Zcable Tcable')
cableFarRefl = convertRefl(refl, Z0, Zcable)
cableNearRefl = cableFarRefl*Tcable
nearRefl = convertRefl(cableNearRefl, Zcable, Z0)
result = simplify(nearRefl)
result = collect(result, Tcable)
result = collect(result, refl)
print result
*/

//auto result = (Tcable*(-Z0sq*refl - Z0sq - 2*Z0*Zcable*refl - Zcablesq*refl + Zcablesq) + Z0sq - Zcablesq + refl*(Z0sq - 2*Z0*Zcable + Zcablesq))
// / (Tcable*(Z0sq*refl + Z0sq - 2*Z0*Zcable - Zcablesq*refl + Zcablesq) - Z0sq - 2*Z0*Zcable - Zcablesq + refl*(-Z0sq + Zcablesq));
auto term1 = Z0sq*refl + Z0sq;
auto term2 = -Zcablesq*refl + Zcablesq;
auto term3 = 2*Z0*Zcable;
auto result = (Tcable*(-term1 - term3*refl + term2) + Z0sq - Zcablesq + refl*(Z0sq - term3 + Zcablesq))
/ (Tcable*(term1 - term3 + term2) - Z0sq - Zcablesq - term3 + refl*(-Z0sq + Zcablesq));
return result;
}

void MainWindow::updateViews(int freqIndex) {
if(freqIndex >= (int)nv.values.size()) return;
if(curCal)
Expand All @@ -713,14 +755,17 @@ void MainWindow::updateViews(int freqIndex) {

// calculate port extension
double freqHz = vna->freqAt(freqIndex);
// S11
nv.values.at(freqIndex)(0, 0) *= polar(1., 4*M_PI*freqHz*portExt1Seconds);

auto& S11 = nv.values.at(freqIndex)(0, 0);
S11 = calculatePortExt(S11, polar(1., 4*M_PI*freqHz*portExt1Seconds), 50., portExtZ0);

auto& S22 = nv.values.at(freqIndex)(1, 1);
S22 = calculatePortExt(S22, polar(1., 4*M_PI*freqHz*portExt2Seconds), 50., portExtZ0);

// S21
nv.values.at(freqIndex)(1, 0) *= polar(1., 2*M_PI*freqHz*(portExt1Seconds+portExt2Seconds));
// S12
nv.values.at(freqIndex)(0, 1) *= polar(1., 2*M_PI*freqHz*(portExt1Seconds+portExt2Seconds));
// S22
nv.values.at(freqIndex)(1, 1) *= polar(1., 4*M_PI*freqHz*portExt2Seconds);

if(ui->actionDisable_chart_update->isChecked()) return;
nv.updateViews(freqIndex);
Expand Down Expand Up @@ -889,11 +934,11 @@ void MainWindow::on_actionPort_length_extension_toggled(bool arg1) {
}

void MainWindow::on_slider_ext1_valueChanged(int) {
setPortExtension(ui->slider_ext1->value()*1e-12, ui->slider_ext2->value()*1e-12);
updatePortExtension();
}

void MainWindow::on_slider_ext2_valueChanged(int) {
setPortExtension(ui->slider_ext1->value()*1e-12, ui->slider_ext2->value()*1e-12);
updatePortExtension();
}

void MainWindow::on_dock_ext_visibilityChanged(bool visible) {
Expand All @@ -904,20 +949,19 @@ void MainWindow::on_dock_ext_visibilityChanged(bool visible) {
void MainWindow::on_b_reset_ext_clicked() {
ui->slider_ext1->setValue(0);
ui->slider_ext2->setValue(0);
ui->slider_extz->setValue(5000);
}

void MainWindow::on_t_ext1_returnPressed() {
string txt = ui->t_ext1->text().toStdString();
double lengthPs = atof(txt.c_str());
ui->slider_ext1->setValue(int(lengthPs));
setPortExtension(lengthPs*1e-12, portExt2Seconds);
}

void MainWindow::on_t_ext2_returnPressed() {
string txt = ui->t_ext2->text().toStdString();
double lengthPs = atof(txt.c_str());
ui->slider_ext2->setValue(int(lengthPs));
setPortExtension(portExt1Seconds, lengthPs*1e-12);
}

void MainWindow::on_actionT_R_mode_toggled(bool checked) {
Expand Down Expand Up @@ -1009,3 +1053,13 @@ void MainWindow::on_actionFine_tune_triggered() {
dialog.modelsChanged(); // this calls the lambda above
}
}

void MainWindow::on_t_extz_returnPressed() {
string txt = ui->t_extz->text().toStdString();
double Z = atof(txt.c_str());
ui->slider_extz->setValue(int(round(Z*100)));
}

void MainWindow::on_slider_extz_valueChanged(int) {
updatePortExtension();
}
5 changes: 4 additions & 1 deletion vna_qt/mainwindow.H
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public:

// the current length extension adjustment
double portExt1Seconds = 0., portExt2Seconds = 0.;
double portExtZ0 = 50.;

vector<complex<double> > tmp_s11;
vector<VNACalibratedValue> tmp_sn1,tmp_sn2,tmp_sparams;
Expand Down Expand Up @@ -142,7 +143,7 @@ public:
bool maximizePane(QWidget* w);
QWidget* createTopFloat(QWidget* w);

void setPortExtension(double t1_seconds, double t2_seconds);
void updatePortExtension();

string serializeCalibration(const CalibrationInfo& cal);
CalibrationInfo deserializeCalibration(QTextStream &inp);
Expand Down Expand Up @@ -219,6 +220,8 @@ private slots:
void on_b_cal_help_clicked();
void on_actionExport_csv_triggered();
void on_actionFine_tune_triggered();
void on_t_extz_returnPressed();
void on_slider_extz_valueChanged(int value);
};

#endif // MAINWINDOW_H
77 changes: 64 additions & 13 deletions vna_qt/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -484,13 +484,7 @@ Measurements</string>
</attribute>
<widget class="QWidget" name="dockWidgetContents">
<layout class="QGridLayout" name="gridLayout_2">
<property name="leftMargin">
<number>5</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<item row="0" column="0" colspan="2">
<item row="0" column="0" colspan="3">
<widget class="QPushButton" name="b_reset_ext">
<property name="sizePolicy">
<sizepolicy hsizetype="Ignored" vsizetype="Fixed">
Expand All @@ -509,7 +503,7 @@ Measurements</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<item row="1" column="0" colspan="3">
<widget class="QLineEdit" name="t_ext1">
<property name="sizePolicy">
<sizepolicy hsizetype="Ignored" vsizetype="Fixed">
Expand All @@ -531,7 +525,7 @@ Measurements</string>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<item row="2" column="0" colspan="3">
<widget class="QLineEdit" name="t_ext2">
<property name="sizePolicy">
<sizepolicy hsizetype="Ignored" vsizetype="Fixed">
Expand All @@ -547,7 +541,23 @@ Measurements</string>
</property>
</widget>
</item>
<item row="3" column="0">
<item row="3" column="0" colspan="3">
<widget class="QLineEdit" name="t_extz">
<property name="sizePolicy">
<sizepolicy hsizetype="Ignored" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Cable Z0</string>
</property>
<property name="text">
<string>50</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>1</string>
Expand All @@ -557,7 +567,7 @@ Measurements</string>
</property>
</widget>
</item>
<item row="3" column="1">
<item row="4" column="1">
<widget class="QLabel" name="label_2">
<property name="text">
<string>2</string>
Expand All @@ -567,7 +577,17 @@ Measurements</string>
</property>
</widget>
</item>
<item row="4" column="0">
<item row="4" column="2">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Z</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QSlider" name="slider_ext1">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
Expand Down Expand Up @@ -595,7 +615,7 @@ Measurements</string>
</property>
</widget>
</item>
<item row="4" column="1">
<item row="5" column="1">
<widget class="QSlider" name="slider_ext2">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
Expand Down Expand Up @@ -623,6 +643,37 @@ Measurements</string>
</property>
</widget>
</item>
<item row="5" column="2">
<widget class="QSlider" name="slider_extz">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Cable Z0</string>
</property>
<property name="minimum">
<number>2000</number>
</property>
<property name="maximum">
<number>8000</number>
</property>
<property name="singleStep">
<number>5</number>
</property>
<property name="pageStep">
<number>100</number>
</property>
<property name="value">
<number>5000</number>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
Expand Down

0 comments on commit 86f9959

Please sign in to comment.