27 #include "../components/capacitor.h"
28 #include "../components/inductor.h"
29 #include "../components/ground.h"
32 #include <QHBoxLayout>
33 #include <QVBoxLayout>
38 #include <QPushButton>
39 #include <QMessageBox>
40 #include <QApplication>
48 setWindowTitle(tr(
"Create Matching Circuit"));
51 all =
new QVBoxLayout(
this);
53 TwoCheck =
new QCheckBox(tr(
"calculate two-port matching"));
59 QGroupBox *ImpBox =
new QGroupBox(tr(
"Reference Impedance"));
60 all->addWidget(ImpBox);
61 QHBoxLayout *ImpLayout =
new QHBoxLayout();
66 connect(
Ref1Edit, SIGNAL(textChanged(
const QString&)),
75 ImpLayout->addSpacing(50);
79 ImpBox->setLayout(ImpLayout);
82 QGroupBox *SParBox =
new QGroupBox(tr(
"S Parameter"));
83 all->addWidget(SParBox);
84 QVBoxLayout *SParLayout =
new QVBoxLayout();
85 SParBox->setLayout(SParLayout);
87 QHBoxLayout *h1 =
new QHBoxLayout();
97 SParLayout->addLayout(h1);
99 QHBoxLayout *h3 =
new QHBoxLayout();
101 QVBoxLayout *VBox1 =
new QVBoxLayout();
102 h3->addLayout(VBox1);
107 QVBoxLayout *VBox2 =
new QVBoxLayout();
108 h3->addLayout(VBox2);
115 QVBoxLayout *VBox3 =
new QVBoxLayout();
116 h3->addLayout(VBox3);
121 QVBoxLayout *VBox4 =
new QVBoxLayout();
122 h3->addLayout(VBox4);
129 QVBoxLayout *VBox5 =
new QVBoxLayout();
130 h3->addLayout(VBox5);
136 QVBoxLayout *VBox6 =
new QVBoxLayout();
137 h3->addLayout(VBox6);
142 QVBoxLayout *VBox7 =
new QVBoxLayout();
143 h3->addLayout(VBox7);
150 QVBoxLayout *VBox8 =
new QVBoxLayout();
151 h3->addLayout(VBox8);
156 QVBoxLayout *VBox9 =
new QVBoxLayout();
157 h3->addLayout(VBox9);
164 QVBoxLayout *VBox0 =
new QVBoxLayout();
165 h3->addLayout(VBox0);
170 SParLayout->addLayout(h3);
172 connect(
S21magEdit, SIGNAL(textChanged(
const QString&)),
174 connect(
S21degEdit, SIGNAL(textChanged(
const QString&)),
176 connect(
S11magEdit, SIGNAL(textChanged(
const QString&)),
178 connect(
S11degEdit, SIGNAL(textChanged(
const QString&)),
182 QHBoxLayout *h2 =
new QHBoxLayout();
196 SParLayout->addLayout(h2);
199 QHBoxLayout *h0 =
new QHBoxLayout();
203 QPushButton *buttCreate =
new QPushButton(tr(
"Create"));
204 QPushButton *buttCancel =
new QPushButton(tr(
"Cancel"));
205 h0->addWidget(buttCreate);
206 h0->addWidget(buttCancel);
208 connect(buttCancel, SIGNAL(clicked()), SLOT(reject()));
224 int Expo = int(log10(Freq_) / 3.0);
225 if(Expo < 0) Expo = 0;
226 else if(Expo > 3) Expo = 3;
228 Freq_ /= pow(10.0,
double(3*Expo));
256 S11Label->setText(tr(
"Reflexion Coefficient"));
257 S21Label->setText(tr(
"Impedance (ohms)"));
285 S11uLabel->setText(QString::fromUtf8(
"°"));
286 S12uLabel->setText(QString::fromUtf8(
"°"));
287 S21uLabel->setText(QString::fromUtf8(
"°"));
288 S22uLabel->setText(QString::fromUtf8(
"°"));
357 double Z0 =
Ref1Edit->text().toDouble();
376 double Z0 =
Ref1Edit->text().toDouble();
392 double Z1 =
Ref1Edit->text().toDouble();
393 double Z2 =
Ref2Edit->text().toDouble();
395 pow(10.0, 3.0*
double(
UnitCombo->currentItem()));
397 double S11real =
S11magEdit->text().toDouble();
398 double S11imag =
S11degEdit->text().toDouble();
399 double S12real =
S12magEdit->text().toDouble();
400 double S12imag =
S12degEdit->text().toDouble();
401 double S21real =
S21magEdit->text().toDouble();
402 double S21imag =
S21degEdit->text().toDouble();
403 double S22real =
S22magEdit->text().toDouble();
404 double S22imag =
S22degEdit->text().toDouble();
406 p2c(S11real, S11imag);
407 p2c(S12real, S12imag);
408 p2c(S21real, S21imag);
409 p2c(S22real, S22imag);
414 double DetReal = S11real*S22real - S11imag*S22imag
415 - S12real*S21real + S12imag*S21imag;
416 double DetImag = S11real*S22imag + S11imag*S22real
417 - S12real*S21imag - S12imag*S21real;
420 DetReal, DetImag, Z1, Z2, Freq))
437 Real = sqrt(Real*Real + Imag*Imag);
438 Imag = 180.0/
M_PI * atan2(Imag, Real_);
446 Real = Real_ * cos(Imag *
M_PI/180.0);
447 Imag = Real_ * sin(Imag *
M_PI/180.0);
454 double tmp = Z0 / ((1.0-Real)*(1.0-Real) + Imag*Imag);
455 Real = (1.0 - Real*Real - Imag*Imag) * tmp;
463 double tmp = (Real+Z0)*(Real+Z0) + Imag*Imag;
464 Real = (Real*Real + Imag*Imag - Z0*Z0) / tmp;
465 Imag *= 2.0 * Z0 / tmp;
471 double Z0,
double Freq)
473 double Zreal = r_real, Zimag = r_imag;
474 r2z(Zreal, Zimag, Z0);
477 if (Zreal < -1e-13) {
478 QMessageBox::critical(0, tr(
"Error"),
479 tr(
"Real part of impedance must be greater zero,\nbut is %1 !").arg(Zreal));
489 double X1, X2, Omega = 2.0 *
M_PI * Freq;
495 X1 = sqrt(Zreal * (Z0 - Zreal));
496 if (Zimag < 0.0) X1 *= -1.0;
500 X2 = (Zimag + X1) / (Zreal*Zreal + (Zimag + X1)*(Zimag + X1));
505 X1 = Zreal + Zimag*Zimag / Zreal - Z0;
509 if (Zimag > 0.0) X1 *= -1.0;
512 X2 = Zimag / (Zreal*Zreal + Zimag*Zimag) + X1 / (Z0*Z0 + X1*X1);
518 Str +=
':' +
num2str(-1.0 / Omega / X1) +
'F';
520 Str +=
':' +
num2str(X1 / Omega) +
'H';
524 Str +=
':' +
num2str(-1.0 / Omega / X2) +
'H';
526 Str +=
':' +
num2str(X2 / Omega) +
'F';
533 double Z0,
double Freq)
536 "<Qucs Schematic " PACKAGE_VERSION
">\n"
541 if(Str.isEmpty())
return false;
543 if(Str.section(
':', 0,0) ==
"sp") {
547 if(Str.section(
':', 1,1).right(1) ==
"F")
548 Schematic +=
"<C C1";
550 Schematic +=
"<L L1";
551 Schematic +=
" 1 100 10 -26 10 0 0 \"" + Str.section(
':', 1,1) +
"\" 1>\n";
553 if(Str.section(
':', 2,2).right(1) ==
"F")
554 Schematic +=
"<C C2";
556 Schematic +=
"<L L2";
557 Schematic +=
" 1 30 80 17 -26 0 1 \"" + Str.section(
':', 2,2) +
"\" 1>\n";
560 "<GND * 1 30 110 0 0 0 0>\n"
563 "<10 10 30 10 \"\" 0 0 0 \"\">\n"
564 "<30 10 70 10 \"\" 0 0 0 \"\">\n"
565 "<30 10 30 50 \"\" 0 0 0 \"\">\n";
571 if(Str.section(
':', 1,1).right(1) ==
"F")
572 Schematic +=
"<C C1";
574 Schematic +=
"<L L1";
575 Schematic +=
" 1 50 10 -26 10 0 0 \"" + Str.section(
':', 1,1) +
"\" 1>\n";
577 if(Str.section(
':', 2,2).right(1) ==
"F")
578 Schematic +=
"<C C2";
580 Schematic +=
"<L L2";
581 Schematic +=
" 1 130 70 17 -26 0 1 \"" + Str.section(
':', 2,2) +
"\" 1>\n";
584 "<GND * 1 130 100 0 0 0 0>\n"
587 "<130 10 130 40 \"\" 0 0 0 \"\">\n"
588 "<130 10 150 10 \"\" 0 0 0 \"\">\n"
589 "<80 10 130 10 \"\" 0 0 0 \"\">\n";
592 Schematic += QString(
597 " <Text -20 -10 12 #000000 0 \"%1 Ohm\">\n"
598 " <Text 120 -10 12 #000000 0 \"device\">\n"
599 "</Paintings>\n").arg(Z0);
601 QApplication::clipboard()->setText(Schematic, QClipboard::Clipboard);
608 double S22real,
double S22imag,
double DetReal,
double DetImag,
609 double Z0,
double Freq)
611 double B = 1.0 + S11real*S11real + S11imag*S11imag
612 - S22real*S22real - S22imag*S22imag
613 - DetReal*DetReal - DetImag*DetImag;
614 double Creal = S11real - S22real*DetReal - S22imag*DetImag;
615 double Cimag = S22real*DetImag - S11imag - S22imag*DetReal;
616 double Cmag = 2.0 * (Creal*Creal + Cimag*Cimag);
620 double Rreal = B*B - 2.0*Cmag;
623 Rimag = Cimag * B - Creal * sqrt(-Rreal);
624 Rreal = Creal * B + Cimag * sqrt(-Rreal);
627 Rreal = B - sqrt(Rreal);
628 Rimag = Cimag * Rreal;
637 double S22real,
double S22imag,
double DetReal,
double DetImag,
638 double Z1,
double Z2,
double Freq)
640 QString Input =
calcBiMatch(S11real, S11imag, S22real, S22imag,
641 DetReal, DetImag, Z1, Freq);
642 if(Input.isEmpty())
return false;
644 QString Output =
calcBiMatch(S22real, S22imag, S11real, S11imag,
645 DetReal, DetImag, Z2, Freq);
646 if(Output.isEmpty())
return false;
649 "<Qucs Schematic " PACKAGE_VERSION
">\n"
655 if(Input.section(
':', 0,0) ==
"sp") {
658 if(Input.section(
':', 1,1).right(1) ==
"F")
659 Schematic +=
"<C C1";
661 Schematic +=
"<L L1";
662 Schematic +=
" 1 -50 10 -26 10 0 0 \"" + Input.section(
':', 1,1) +
"\" 1>\n";
664 if(Input.section(
':', 2,2).right(1) ==
"F")
665 Schematic +=
"<C C2";
667 Schematic +=
"<L L2";
668 Schematic +=
" 1 -120 80 17 -26 0 1 \"" + Input.section(
':', 2,2) +
"\" 1>\n";
670 Schematic +=
"<GND * 1 -120 110 0 0 0 0>\n";
675 if(Input.section(
':', 1,1).right(1) ==
"F")
676 Schematic +=
"<C C1";
678 Schematic +=
"<L L1";
679 Schematic +=
" 1 -140 10 -26 10 0 0 \"" + Input.section(
':', 1,1) +
"\" 1>\n";
681 if(Input.section(
':', 2,2).right(1) ==
"F")
682 Schematic +=
"<C C2";
684 Schematic +=
"<L L2";
685 Schematic +=
" 1 -60 70 17 -26 0 1 \"" + Input.section(
':', 2,2) +
"\" 1>\n";
687 Schematic +=
"<GND * 1 -60 100 0 0 0 0>\n";
693 if(Output.section(
':', 0,0) ==
"sp") {
696 if(Output.section(
':', 1,1).right(1) ==
"F")
697 Schematic +=
"<C C1";
699 Schematic +=
"<L L1";
700 Schematic +=
" 1 50 10 -26 10 0 0 \"" + Output.section(
':', 1,1) +
"\" 1>\n";
702 if(Output.section(
':', 2,2).right(1) ==
"F")
703 Schematic +=
"<C C2";
705 Schematic +=
"<L L2";
706 Schematic +=
" 1 120 80 17 -26 0 1 \"" + Output.section(
':', 2,2) +
"\" 1>\n";
708 Schematic +=
"<GND * 1 120 110 0 0 0 0>\n";
714 if(Output.section(
':', 1,1).right(1) ==
"F")
715 Schematic +=
"<C C1";
717 Schematic +=
"<L L1";
718 Schematic +=
" 1 140 10 -26 10 0 0 \"" + Output.section(
':', 1,1) +
"\" 1>\n";
720 if(Output.section(
':', 2,2).right(1) ==
"F")
721 Schematic +=
"<C C2";
723 Schematic +=
"<L L2";
724 Schematic +=
" 1 60 70 17 -26 0 1 \"" + Output.section(
':', 2,2) +
"\" 1>\n";
726 Schematic +=
"<GND * 1 60 100 0 0 0 0>\n";
728 Schematic +=
"</Components>\n"
734 if(Input.section(
':', 0,0) ==
"sp")
736 "<-140 10 -120 10 \"\" 0 0 0 \"\">\n"
737 "<-120 10 -80 10 \"\" 0 0 0 \"\">\n"
738 "<-120 10 -120 50 \"\" 0 0 0 \"\">\n";
741 "<-60 10 -60 40 \"\" 0 0 0 \"\">\n"
742 "<-60 10 -40 10 \"\" 0 0 0 \"\">\n"
743 "<-110 10 -60 10 \"\" 0 0 0 \"\">\n";
748 if(Output.section(
':', 0,0) ==
"sp")
750 "<140 10 120 10 \"\" 0 0 0 \"\">\n"
751 "<120 10 80 10 \"\" 0 0 0 \"\">\n"
752 "<120 10 120 50 \"\" 0 0 0 \"\">\n";
755 "<60 10 60 40 \"\" 0 0 0 \"\">\n"
756 "<60 10 40 10 \"\" 0 0 0 \"\">\n"
757 "<110 10 60 10 \"\" 0 0 0 \"\">\n";
764 " <Text -200 -10 12 #000000 0 \"Port 1\">\n"
765 " <Text -20 -10 12 #000000 0 \"device\">\n"
766 " <Text 160 -10 12 #000000 0 \"Port 2\">\n"
769 QApplication::clipboard()->setText(Schematic, QClipboard::Clipboard);
Defines drawing elements for schematics.
static void p2c(double &, double &)
MatchDialog(QWidget *parent=0)
void slotSetTwoPort(bool)
static QString calcBiMatch(double, double, double, double, double, double, double, double)
static void z2r(double &, double &, double)
Definitions and declarations for the main application.
static bool calcMatchingCircuit(double, double, double, double)
QString num2str(double Num)
void slotReflexionChanged(const QString &)
static bool calc2PortMatch(double, double, double, double, double, double, double, double, double)
static void r2z(double &, double &, double)
QLineEdit * FrequencyEdit
void setFrequency(double)
void slotImpedanceChanged(const QString &)
QDoubleValidator * DoubleVal
static QString calcMatching(double, double, double, double)
static void c2p(double &, double &)