26 #include "../components/capacitor.h"
27 #include "../components/inductor.h"
28 #include "../components/ground.h"
34 #include <qlineedit.h>
35 #include <qcombobox.h>
36 #include <qcheckbox.h>
37 #include <qvalidator.h>
38 #include <qpushbutton.h>
39 #include <qmessagebox.h>
40 #include <qapplication.h>
41 #include <qclipboard.h>
42 #include <qhgroupbox.h>
43 #include <qvgroupbox.h>
47 : QDialog(parent, 0, TRUE, Qt::WDestructiveClose)
49 setCaption(tr(
"Create Matching Circuit"));
50 DoubleVal =
new QDoubleValidator(
this);
52 all =
new QVBoxLayout(
this, 3,3);
54 TwoCheck =
new QCheckBox(tr(
"calculate two-port matching"),
this);
60 QHGroupBox *ImpBox =
new QHGroupBox(tr(
"Reference Impedance"),
this);
61 all->addWidget(ImpBox);
62 Port1Label =
new QLabel(tr(
"Port 1"), ImpBox);
63 Ref1Edit =
new QLineEdit(
"50", ImpBox);
65 Ohm1Label =
new QLabel(tr(
"ohms"), ImpBox);
66 connect(
Ref1Edit, SIGNAL(textChanged(
const QString&)),
69 Port2Label =
new QLabel(tr(
"Port 2"), ImpBox);
70 Ref2Edit =
new QLineEdit(
"50", ImpBox);
72 Ohm2Label =
new QLabel(tr(
"ohms"), ImpBox);
76 QVGroupBox *SParBox =
new QVGroupBox(tr(
"S Parameter"),
this);
77 all->addWidget(SParBox);
79 QHBox *h1 =
new QHBox(SParBox);
81 FormatLabel =
new QLabel(tr(
"Input format"), h1);
82 FormatCombo =
new QComboBox(h1);
83 FormatCombo->insertItem(tr(
"real/imag"));
84 FormatCombo->insertItem(tr(
"mag/deg"));
85 connect(FormatCombo, SIGNAL(activated(
int)), SLOT(
slotChangeMode(
int)));
86 QWidget *place1 =
new QWidget(h1);
87 h1->setStretchFactor(place1, 5);
89 QHBox *h3 =
new QHBox(SParBox);
91 QVBox *VBox1 =
new QVBox(h3);
92 S11Label =
new QLabel(tr(
"S11"), VBox1);
93 S21Label =
new QLabel(tr(
"S21"), VBox1);
94 QVBox *VBox2 =
new QVBox(h3);
99 QVBox *VBox3 =
new QVBox(h3);
100 S11sLabel =
new QLabel(
"+j", VBox3);
101 S21sLabel =
new QLabel(
"+j", VBox3);
102 QVBox *VBox4 =
new QVBox(h3);
107 QVBox *VBox5 =
new QVBox(h3);
108 S11uLabel =
new QLabel(
" ", VBox5);
109 S21uLabel =
new QLabel(
" ", VBox5);
110 QWidget *place4 =
new QWidget(h3);
111 h3->setStretchFactor(place4, 5);
112 QVBox *VBox6 =
new QVBox(h3);
113 S12Label =
new QLabel(tr(
"S12"), VBox6);
114 S22Label =
new QLabel(tr(
"S22"), VBox6);
115 QVBox *VBox7 =
new QVBox(h3);
120 QVBox *VBox8 =
new QVBox(h3);
121 S12sLabel =
new QLabel(
"+j", VBox8);
122 S22sLabel =
new QLabel(
"+j", VBox8);
123 QVBox *VBox9 =
new QVBox(h3);
128 QVBox *VBox0 =
new QVBox(h3);
129 S12uLabel =
new QLabel(
" ", VBox0);
130 S22uLabel =
new QLabel(
" ", VBox0);
132 connect(
S21magEdit, SIGNAL(textChanged(
const QString&)),
134 connect(
S21degEdit, SIGNAL(textChanged(
const QString&)),
136 connect(
S11magEdit, SIGNAL(textChanged(
const QString&)),
138 connect(
S11degEdit, SIGNAL(textChanged(
const QString&)),
142 QHBox *h2 =
new QHBox(SParBox);
144 FrequencyLabel =
new QLabel(tr(
"Frequency:"), h2);
147 UnitCombo =
new QComboBox(h2);
148 UnitCombo->insertItem(
"Hz");
149 UnitCombo->insertItem(
"kHz");
150 UnitCombo->insertItem(
"MHz");
151 UnitCombo->insertItem(
"GHz");
152 QWidget *place2 =
new QWidget(h2);
153 h2->setStretchFactor(place2, 5);
156 QHBox *h0 =
new QHBox(
this);
159 QWidget *place3 =
new QWidget(h0);
160 h0->setStretchFactor(place3, 5);
161 connect(
new QPushButton(tr(
"Create"),h0), SIGNAL(clicked()),
163 connect(
new QPushButton(tr(
"Cancel"),h0), SIGNAL(clicked()),
181 int Expo = int(
log10(Freq_) / 3.0);
182 if(Expo < 0) Expo = 0;
183 else if(Expo > 3) Expo = 3;
184 UnitCombo->setCurrentItem(Expo);
185 Freq_ /=
pow(10.0,
double(3*Expo));
194 S11Label->setText(tr(
"S11"));
195 S21Label->setText(tr(
"S21"));
200 S12Label->setEnabled(
true);
201 S22Label->setEnabled(
true);
202 S12sLabel->setEnabled(
true);
203 S22sLabel->setEnabled(
true);
206 S12uLabel->setEnabled(
true);
207 S22uLabel->setEnabled(
true);
208 Port2Label->setEnabled(
true);
210 Ohm2Label->setEnabled(
true);
213 S11Label->setText(tr(
"Reflexion Coefficient"));
214 S21Label->setText(tr(
"Impedance (ohms)"));
219 S12Label->setEnabled(
false);
220 S22Label->setEnabled(
false);
221 S12sLabel->setEnabled(
false);
222 S22sLabel->setEnabled(
false);
225 S12uLabel->setEnabled(
false);
226 S22uLabel->setEnabled(
false);
227 Port2Label->setEnabled(
false);
229 Ohm2Label->setEnabled(
false);
238 S11sLabel->setText(
"/");
239 S12sLabel->setText(
"/");
240 S21sLabel->setText(
"/");
241 S22sLabel->setText(
"/");
242 S11uLabel->setText(
"°");
243 S12uLabel->setText(
"°");
244 S21uLabel->setText(
"°");
245 S22uLabel->setText(
"°");
272 S11sLabel->setText(
"+j");
273 S12sLabel->setText(
"+j");
274 S21sLabel->setText(
"+j");
275 S22sLabel->setText(
"+j");
276 S11uLabel->setText(
" ");
277 S12uLabel->setText(
" ");
278 S21uLabel->setText(
" ");
279 S22uLabel->setText(
" ");
349 double Z1 =
Ref1Edit->text().toDouble();
350 double Z2 =
Ref2Edit->text().toDouble();
352 pow(10.0, 3.0*
double(UnitCombo->currentItem()));
354 double S11real =
S11magEdit->text().toDouble();
355 double S11imag =
S11degEdit->text().toDouble();
356 double S12real =
S12magEdit->text().toDouble();
357 double S12imag =
S12degEdit->text().toDouble();
358 double S21real =
S21magEdit->text().toDouble();
359 double S21imag =
S21degEdit->text().toDouble();
360 double S22real =
S22magEdit->text().toDouble();
361 double S22imag =
S22degEdit->text().toDouble();
362 if(FormatCombo->currentItem()) {
363 p2c(S11real, S11imag);
364 p2c(S12real, S12imag);
365 p2c(S21real, S21imag);
366 p2c(S22real, S22imag);
371 double DetReal = S11real*S22real - S11imag*S22imag
372 - S12real*S21real + S12imag*S21imag;
373 double DetImag = S11real*S22imag + S11imag*S22real
374 - S12real*S21imag - S12imag*S21real;
377 DetReal, DetImag, Z1, Z2, Freq))
394 Real =
sqrt(Real*Real + Imag*Imag);
403 Real = Real_ *
cos(Imag *
M_PI/180.0);
404 Imag = Real_ *
sin(Imag *
M_PI/180.0);
411 double tmp = Z0 / ((1.0-
Real)*(1.0-Real) + Imag*Imag);
412 Real = (1.0 - Real*Real - Imag*Imag) * tmp;
420 double tmp = (Real+
Z0)*(Real+Z0) + Imag*Imag;
421 Real = (Real*Real + Imag*Imag - Z0*
Z0) / tmp;
422 Imag *= 2.0 * Z0 / tmp;
428 double Z0,
double Freq)
430 double Zreal = r_real, Zimag = r_imag;
431 r2z(Zreal, Zimag, Z0);
434 if (Zreal < -1
e-13) {
435 QMessageBox::critical(0, tr(
"Error"),
436 tr(
"Real part of impedance must be greater zero,\nbut is %1 !").
arg(Zreal));
446 double X1,
X2, Omega = 2.0 *
M_PI * Freq;
452 X1 =
sqrt(Zreal * (Z0 - Zreal));
453 if (Zimag < 0.0) X1 *= -1.0;
457 X2 = (Zimag +
X1) / (Zreal*Zreal + (Zimag + X1)*(Zimag +
X1));
462 X1 = Zreal + Zimag*Zimag / Zreal -
Z0;
466 if (Zimag > 0.0) X1 *= -1.0;
469 X2 = Zimag / (Zreal*Zreal + Zimag*Zimag) + X1 / (Z0*Z0 + X1*X1);
475 Str +=
':' +
num2str(-1.0 / Omega / X1) +
'F';
477 Str +=
':' +
num2str(X1 / Omega) +
'H';
481 Str +=
':' +
num2str(-1.0 / Omega / X2) +
'H';
483 Str +=
':' +
num2str(X2 / Omega) +
'F';
490 double Z0,
double Freq)
493 "<Qucs Schematic " PACKAGE_VERSION
">\n"
498 if(Str.isEmpty())
return false;
500 if(Str.section(
':', 0,0) ==
"sp") {
504 if(Str.section(
':', 1,1).right(1) ==
"F")
505 Schematic +=
"<C C1";
507 Schematic +=
"<L L1";
508 Schematic +=
" 1 100 10 -26 10 0 0 \"" + Str.section(
':', 1,1) +
"\" 1>\n";
510 if(Str.section(
':', 2,2).right(1) ==
"F")
511 Schematic +=
"<C C2";
513 Schematic +=
"<L L2";
514 Schematic +=
" 1 30 80 17 -26 0 1 \"" + Str.section(
':', 2,2) +
"\" 1>\n";
517 "<GND * 1 30 110 0 0 0 0>\n"
520 "<10 10 30 10 \"\" 0 0 0 \"\">\n"
521 "<30 10 70 10 \"\" 0 0 0 \"\">\n"
522 "<30 10 30 50 \"\" 0 0 0 \"\">\n";
528 if(Str.section(
':', 1,1).right(1) ==
"F")
529 Schematic +=
"<C C1";
531 Schematic +=
"<L L1";
532 Schematic +=
" 1 50 10 -26 10 0 0 \"" + Str.section(
':', 1,1) +
"\" 1>\n";
534 if(Str.section(
':', 2,2).right(1) ==
"F")
535 Schematic +=
"<C C2";
537 Schematic +=
"<L L2";
538 Schematic +=
" 1 130 70 17 -26 0 1 \"" + Str.section(
':', 2,2) +
"\" 1>\n";
541 "<GND * 1 130 100 0 0 0 0>\n"
544 "<130 10 130 40 \"\" 0 0 0 \"\">\n"
545 "<130 10 150 10 \"\" 0 0 0 \"\">\n"
546 "<80 10 130 10 \"\" 0 0 0 \"\">\n";
549 Schematic += QString(
554 " <Text -20 -10 12 #000000 0 \"%1 Ohm\">\n"
555 " <Text 120 -10 12 #000000 0 \"device\">\n"
556 "</Paintings>\n").arg(Z0);
558 QApplication::clipboard()->setText(Schematic, QClipboard::Clipboard);
565 double S22real,
double S22imag,
double DetReal,
double DetImag,
566 double Z0,
double Freq)
568 double B = 1.0 + S11real*S11real + S11imag*S11imag
569 - S22real*S22real - S22imag*S22imag
570 - DetReal*DetReal - DetImag*DetImag;
571 double Creal = S11real - S22real*DetReal - S22imag*DetImag;
572 double Cimag = S22real*DetImag - S11imag - S22imag*DetReal;
573 double Cmag = 2.0 * (Creal*Creal + Cimag*Cimag);
577 double Rreal = B*B - 2.0*Cmag;
580 Rimag = Cimag * B - Creal *
sqrt(-Rreal);
581 Rreal = Creal * B + Cimag *
sqrt(-Rreal);
584 Rreal = B -
sqrt(Rreal);
585 Rimag = Cimag * Rreal;
594 double S22real,
double S22imag,
double DetReal,
double DetImag,
595 double Z1,
double Z2,
double Freq)
597 QString Input =
calcBiMatch(S11real, S11imag, S22real, S22imag,
598 DetReal, DetImag, Z1, Freq);
599 if(Input.isEmpty())
return false;
601 QString Output =
calcBiMatch(S22real, S22imag, S11real, S11imag,
602 DetReal, DetImag, Z2, Freq);
603 if(Output.isEmpty())
return false;
606 "<Qucs Schematic " PACKAGE_VERSION
">\n"
612 if(Input.section(
':', 0,0) ==
"sp") {
615 if(Input.section(
':', 1,1).right(1) ==
"F")
616 Schematic +=
"<C C1";
618 Schematic +=
"<L L1";
619 Schematic +=
" 1 -50 10 -26 10 0 0 \"" + Input.section(
':', 1,1) +
"\" 1>\n";
621 if(Input.section(
':', 2,2).right(1) ==
"F")
622 Schematic +=
"<C C2";
624 Schematic +=
"<L L2";
625 Schematic +=
" 1 -120 80 17 -26 0 1 \"" + Input.section(
':', 2,2) +
"\" 1>\n";
627 Schematic +=
"<GND * 1 -120 110 0 0 0 0>\n";
632 if(Input.section(
':', 1,1).right(1) ==
"F")
633 Schematic +=
"<C C1";
635 Schematic +=
"<L L1";
636 Schematic +=
" 1 -140 10 -26 10 0 0 \"" + Input.section(
':', 1,1) +
"\" 1>\n";
638 if(Input.section(
':', 2,2).right(1) ==
"F")
639 Schematic +=
"<C C2";
641 Schematic +=
"<L L2";
642 Schematic +=
" 1 -60 70 17 -26 0 1 \"" + Input.section(
':', 2,2) +
"\" 1>\n";
644 Schematic +=
"<GND * 1 -60 100 0 0 0 0>\n";
650 if(Output.section(
':', 0,0) ==
"sp") {
653 if(Output.section(
':', 1,1).right(1) ==
"F")
654 Schematic +=
"<C C1";
656 Schematic +=
"<L L1";
657 Schematic +=
" 1 50 10 -26 10 0 0 \"" + Output.section(
':', 1,1) +
"\" 1>\n";
659 if(Output.section(
':', 2,2).right(1) ==
"F")
660 Schematic +=
"<C C2";
662 Schematic +=
"<L L2";
663 Schematic +=
" 1 120 80 17 -26 0 1 \"" + Output.section(
':', 2,2) +
"\" 1>\n";
665 Schematic +=
"<GND * 1 120 110 0 0 0 0>\n";
671 if(Output.section(
':', 1,1).right(1) ==
"F")
672 Schematic +=
"<C C1";
674 Schematic +=
"<L L1";
675 Schematic +=
" 1 140 10 -26 10 0 0 \"" + Output.section(
':', 1,1) +
"\" 1>\n";
677 if(Output.section(
':', 2,2).right(1) ==
"F")
678 Schematic +=
"<C C2";
680 Schematic +=
"<L L2";
681 Schematic +=
" 1 60 70 17 -26 0 1 \"" + Output.section(
':', 2,2) +
"\" 1>\n";
683 Schematic +=
"<GND * 1 60 100 0 0 0 0>\n";
685 Schematic +=
"</Components>\n"
691 if(Input.section(
':', 0,0) ==
"sp")
693 "<-140 10 -120 10 \"\" 0 0 0 \"\">\n"
694 "<-120 10 -80 10 \"\" 0 0 0 \"\">\n"
695 "<-120 10 -120 50 \"\" 0 0 0 \"\">\n";
698 "<-60 10 -60 40 \"\" 0 0 0 \"\">\n"
699 "<-60 10 -40 10 \"\" 0 0 0 \"\">\n"
700 "<-110 10 -60 10 \"\" 0 0 0 \"\">\n";
705 if(Output.section(
':', 0,0) ==
"sp")
707 "<140 10 120 10 \"\" 0 0 0 \"\">\n"
708 "<120 10 80 10 \"\" 0 0 0 \"\">\n"
709 "<120 10 120 50 \"\" 0 0 0 \"\">\n";
712 "<60 10 60 40 \"\" 0 0 0 \"\">\n"
713 "<60 10 40 10 \"\" 0 0 0 \"\">\n"
714 "<110 10 60 10 \"\" 0 0 0 \"\">\n";
721 " <Text -200 -10 12 #000000 0 \"Port 1\">\n"
722 " <Text -20 -10 12 #000000 0 \"device\">\n"
723 " <Text 160 -10 12 #000000 0 \"Port 2\">\n"
726 QApplication::clipboard()->setText(Schematic, QClipboard::Clipboard);