forked from fransschreuder/DeEmbed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deembed.cpp
391 lines (330 loc) · 13.1 KB
/
deembed.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
* DeEmbed
* Copyright (C) Frans Schreuder 2016 <info@schreuderelectronics.com>
*
software is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* software is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "deembed.h"
#include "ui_mainwindow.h"
#include "chart.h"
#include "vna-math.h"
#include "cal-dialog.h"
#include "touchstone-file.h"
#include "calstdsdialog.h"
#include "version.h"
#include <QFileInfo>
#include <QStandardPaths>
#include <QFileDialog>
#include <QSettings>
#include <QMessageBox>
#include <QDesktopServices>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
maxFileNr(4)
{
ui->setupUi(this);
this->resize(800,600);
ui->chart->SetPlotType(SMITH_PLOT);
QAction* recentFileAction = 0;
for(int i = 0; i < maxFileNr; ++i){
recentFileAction = new QAction(this);
recentFileAction->setVisible(false);
QObject::connect(recentFileAction, SIGNAL(triggered()),
this, SLOT(openRecent()));
recentFileActionList.append(recentFileAction);
}
recentFilesMenu = new QMenu(tr("Open Recent"));
saveSparsMenu = new QMenu(tr("Export TouchStone file"));
ui->menuFile->insertMenu(ui->actionExport_De_embedded_Data, recentFilesMenu);
QToolButton* openRecentButton=new QToolButton(this);
openRecentButton->setPopupMode(QToolButton::InstantPopup);
openRecentButton->setIcon(QIcon(QString(":icons/document-open.svg")));
openRecentButton->setMenu(recentFilesMenu);
QWidgetAction* toolButtonAction = new QWidgetAction(this);
toolButtonAction->setDefaultWidget(openRecentButton);
ui->mainToolBar->addAction(toolButtonAction);
ui->actionExport_De_embedded_Data->setMenu(saveSparsMenu);
QToolButton* saveTouchStoneButton=new QToolButton(this);
saveTouchStoneButton->setPopupMode(QToolButton::InstantPopup);
saveTouchStoneButton->setIcon(QIcon(QString(":icons/document-save.svg")));
saveTouchStoneButton->setMenu(saveSparsMenu);
QWidgetAction* saveButtonAction = new QWidgetAction(this);
saveButtonAction->setDefaultWidget(saveTouchStoneButton);
ui->mainToolBar->addAction(saveButtonAction);
ui->menuBar->insertMenu(ui->menuBar->actions()[2],ui->chart->popupMenu);
//ui->mainToolBar->actions()[1]->setMenu(recentFilesMenu);
for(int i = 0; i < maxFileNr; ++i)
recentFilesMenu->addAction(recentFileActionList.at(i));
updateRecentActionList();
calStdsDialog = new CalStdsDialog(this, &m_calStd);
connect(calStdsDialog, SIGNAL(calStdChanged()), this, SLOT(OnCalStdChanged()));
calDialog = new CalDialog(this);
calDialog->on_buttonBox_accepted();
m_cal = calDialog->getCal();
m_calibrator.SetCalStd(&m_calStd);
connect(&m_calibrator, SIGNAL(CalibrationWarning(QString)), this, SLOT(OnCalibrationWarning(QString)));
connect(ui->chart, SIGNAL(dataChanged()), this, SLOT(OnChartDataChanged()));
}
MainWindow::~MainWindow()
{
delete ui;
delete calDialog;
delete calStdsDialog;
}
void MainWindow::on_actionCalibration_files_triggered()
{
if(calDialog->exec())
{
m_cal = calDialog->getCal();
OnCalStdChanged();
}
}
void MainWindow::on_actionClear_triggered()
{
ui->chart->ClearSpars();
}
void MainWindow::on_actionOpen_Touchstone_File_triggered()
{
QSettings settings("Schreuder Electronics", "deembed");
QString defaultPath = settings.value("DefaultPath", QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)).toString();
QString path = QFileDialog::getOpenFileName(this, tr("Load Touchstone file"), defaultPath, tr("TouchStone files (*.s1p *.s2p *.s3p *.s4p)"));
settings.setValue("DefaultPath", QFileInfo(path).dir().absolutePath());
loadFile(path);
}
void MainWindow::openRecent()
{
QAction* action = qobject_cast<QAction*>(sender());
QString path=action->data().toString();
loadFile(path);
}
void MainWindow::loadFile(const QString& path)
{
QFileInfo info(path);
QString filename = info.fileName();
filename.remove("."+info.suffix());
if(!path.isEmpty())
{
TouchstoneFile file;
spar_t spar;
if(path.endsWith(".s4p",Qt::CaseInsensitive))
spar = file.Load4P(path.toUtf8().data());
else if(path.endsWith(".s3p",Qt::CaseInsensitive))
spar = file.Load3P(path.toUtf8().data());
else //S2P loads 1 and 2 port files.
spar = file.Load2P(path.toUtf8().data());
for(int i=0; i<(int)ui->chart->GetSpars().size(); i++)
{
int size = ui->chart->GetSpars()[i].S.size();
if(size<(int)spar.S.size()) //Chart has less data, extend to spar.size
{
spar_t ResizedSpar = ui->chart->GetSpars()[i];
ResizedSpar.S.resize(spar.S.size());
for(int j=0; j<(int)spar.S.size(); j++)
{
ResizedSpar.S[j].resize(spar.S.size());
for(int k=0; k<(int)spar.S.size(); k++)
{
ResizedSpar.S[j][k].resize(ResizedSpar.S[0][0].size(), complex_t(0,0)); //give all new sparameters the same NOP
}
}
ui->chart->PutSpar(ResizedSpar, i, ui->chart->GetSparNames()[i]); //replace with resized s-parameter
}
else if (size > (int)spar.S.size())
{
spar.S.resize(size);
for(int j=0; j<size; j++)
{
spar.S[j].resize(size);
for(int k=0; k<(int)spar.S.size(); k++)
{
spar.S[j][k].resize(spar.S[0][0].size(), complex_t(0,0)); //give all new sparameters the same NOP
}
}
}
if(spar.f.size()!=ui->chart->GetSpars()[i].f.size()||spar.f[0]!=ui->chart->GetSpars()[i].f[0]||spar.f.back()!=ui->chart->GetSpars()[i].f.back())
{
double fStart, fStop;
int nop;
if(ui->chart->GetSpars()[i].f[0]<spar.f[0])
fStart = ui->chart->GetSpars()[i].f[0];
else
fStart = spar.f[0];
if(ui->chart->GetSpars()[i].f.back()>spar.f.back())
fStop = ui->chart->GetSpars()[i].f.back();
else
fStop = spar.f.back();
if(ui->chart->GetSpars()[i].f.size()>spar.f.size())
nop = ui->chart->GetSpars()[i].f.size();
else
nop = spar.f.size();
ui->chart->PutSpar(ui->chart->TrimSpar(ui->chart->GetSpars()[i], fStart, fStop, nop), i, ui->chart->GetSparNames()[i]);
spar = ui->chart->TrimSpar(spar, fStart, fStop, nop);
}
}
ui->chart->PutSpar(spar,-1, filename);
if(m_cal.f[0]!=spar.f[0]||m_cal.f.back()!=spar.f.back()||m_cal.f.size()!=spar.f.size())
{
m_calibrator.TrimCal(m_cal, spar.f[0],spar.f.back(), spar.f.size(), &m_cal);
ui->statusBar->showMessage("Warning: Calibration data interpolated", 60000);
}
spar_t calSpar = m_calibrator.Cal(spar, m_cal);
ui->chart->PutSpar(calSpar, -1, filename+"(cal)");
ui->chart->EnableSpar(0,0);
adjustForCurrentFile(path);
}
}
void MainWindow::on_actionExport_De_embedded_Data_triggered()
{
QAction* action = qobject_cast<QAction*>(sender());
QString name=action->text();
int indexToSave=-1;
for(int i=0; i<saveSparsMenu->actions().size(); i++)
{
if(action==saveSparsMenu->actions()[i])
indexToSave = i;
}
if(indexToSave>=0)
{
spar_t spar = ui->chart->GetSpars()[indexToSave];
QString ext, filter;
switch(spar.S.size())
{
case 1:
ext = ".s1p";
filter = tr("S1P files (*.s1p)");
break;
case 2:
ext = ".s2p";
filter = tr("S2P files (*.s2p)");
break;
case 3:
ext = ".s3p";
filter = tr("S3P files (*.s3p)");
break;
case 4:
ext = ".s4p";
filter = tr("S4P files (*.s4p)");
break;
}
QSettings settings("Schreuder Electronics", "deembed");
QString defaultPath = settings.value("DefaultPath", QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)).toString();
QString path = QFileDialog::getSaveFileName(this, tr("Save Touchstone file: ")+name, defaultPath, filter);
settings.setValue("DefaultPath", QFileInfo(path).dir().absolutePath());
TouchstoneFile file;
if(!path.endsWith(ext, Qt::CaseInsensitive))
{
path=path+ext;
}
file.Save(spar, path.toUtf8().data());
}
}
void MainWindow::on_actionExit_triggered()
{
QApplication::exit(0);
}
void MainWindow::on_actionCalibration_standards_triggered()
{
if(calStdsDialog->exec())
{
OnCalStdChanged();
}
}
void MainWindow::on_actionHelp_triggered()
{
//QDesktopServices::openUrl ( QUrl("http://github.com/fransschreuder/deembed/wiki") );
QDesktopServices::openUrl ( QUrl(QCoreApplication::applicationDirPath()+"/../share/DeEmbed/doc/DeEmbed.pdf"));
}
void MainWindow::on_actionAbout_DeEmbed_triggered()
{
QString s;
QMessageBox::about(this, tr("About DeEmbed"), tr("<H1>DeEmbed v")+s.sprintf("%1.1f",DEEMBED_VERSION)+tr("</H1>DeEmbed is a tool to take raw S-Parameter from a touchstone file, and apply SOLTI calibration to it.\
<br/><br/>Developers:<ul><li>Frans Schreuder: <a href=\"mailto:info@schreuderelectronics.com\">info@schreuderelectronics.com</a></li></ul><br/>Website and code:\
<a href=\"https://github.com/fransschreuder/deembed/wiki\">https://github.com/fransschreuder/deembed/wiki</a><br/><br/>License: <a href=\"https://www.gnu.org/licenses/gpl-3.0.en.html\">GNU GPL 3.0</a>"));
}
void MainWindow::adjustForCurrentFile(const QString &filePath){
currentFilePath = filePath;
setWindowFilePath(currentFilePath);
QSettings settings("Schreuder Electronics", "deembed");
QStringList recentFilePaths =
settings.value("recentFiles").toStringList();
recentFilePaths.removeAll(filePath);
recentFilePaths.prepend(filePath);
while (recentFilePaths.size() > maxFileNr)
recentFilePaths.removeLast();
settings.setValue("recentFiles", recentFilePaths);
// see note
updateRecentActionList();
}
void MainWindow::updateRecentActionList(){
QSettings settings("Schreuder Electronics", "deembed");
QStringList recentFilePaths =
settings.value("recentFiles").toStringList();
int itEnd = 0;
if(recentFilePaths.size() <= maxFileNr)
itEnd = recentFilePaths.size();
else
itEnd = maxFileNr;
for (int i = 0; i < itEnd; ++i) {
QString strippedName = QFileInfo(recentFilePaths.at(i)).fileName();
recentFileActionList.at(i)->setText(strippedName);
recentFileActionList.at(i)->setData(recentFilePaths.at(i));
recentFileActionList.at(i)->setVisible(true);
}
for (int i = itEnd; i < maxFileNr; ++i)
recentFileActionList.at(i)->setVisible(false);
}
void MainWindow::OnCalStdChanged()
{
vector<QString> names = ui->chart->GetSparNames();
vector<spar_t> spars = ui->chart->GetSpars();
vector<int> indexesUncal, indexesCal;
for(int i=0; i<(int)names.size(); i++)
{
if(!names[i].contains("(cal)"))
{
for(int j=i; j<(int)names.size(); j++)
{
if(names[i]+"(cal)"==names[j])
{
indexesUncal.push_back(i);
indexesCal.push_back(j);
break;
}
}
}
}
if(spars.size()>0)
if(m_cal.f.size()!=spars[0].f.size()||m_cal.f[0]!=spars[0].f[0]||m_cal.f.back()!=spars[0].f.back()) //frequency range does not match, trim calibration set.
m_calibrator.TrimCal(m_cal, spars[0].f[0], spars[0].f.back(), spars[0].f.size(), &m_cal);
for(int i=0; i<(int)indexesUncal.size(); i++)
{
spar_t calibrated = m_calibrator.Cal(spars[indexesUncal[i]], m_cal);
ui->chart->PutSpar(calibrated, indexesCal[i], names[indexesCal[i]]);
}
}
void MainWindow::OnChartDataChanged()
{
vector<QString> names = ui->chart->GetSparNames();
saveSparsMenu->clear();
for(int i=0; i<(int)names.size(); i++)
{
QAction* action = saveSparsMenu->addAction(names[i]);
connect(action, SIGNAL(triggered(bool)), this, SLOT(on_actionExport_De_embedded_Data_triggered()));
}
}
void MainWindow::OnCalibrationWarning(QString warning)
{
ui->statusBar->showMessage(warning, 60000);
}