KiCad PCB EDA Suite
Loading...
Searching...
No Matches
smith_math.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef SMITH_MATH_H
21#define SMITH_MATH_H
22
23#include <algorithm>
24#include <cmath>
25#include <limits>
26#include <math/util.h>
27#include <wx/gdicmn.h>
28#include <wx/string.h>
29
32{
33 wxPoint center;
34 double radius = 0.0; // unit-circle radius in pixels, already scaled by zoom
35 double zoom = 1.0; // 1.0 = fit to window
36 wxRealPoint pan; // gamma point shown at the window center
37 wxRect plotRect;
38
39 wxPoint ToScreen( double aRe, double aIm ) const
40 {
41 // clamp so huge or non-finite gammas cannot overflow the int math
42 constexpr double LIMIT = 1e7;
43
44 double x = ( aRe - pan.x ) * radius;
45 double y = ( aIm - pan.y ) * radius;
46
47 x = std::isfinite( x ) ? std::clamp( x, -LIMIT, LIMIT ) : 0.0;
48 y = std::isfinite( y ) ? std::clamp( y, -LIMIT, LIMIT ) : 0.0;
49
50 return wxPoint( center.x + KiROUND( x ), center.y - KiROUND( y ) );
51 }
52
53 wxRealPoint ToGamma( const wxPoint& aPt ) const
54 {
55 return wxRealPoint( pan.x + ( aPt.x - center.x ) / radius, pan.y - ( aPt.y - center.y ) / radius );
56 }
57};
58
59
60namespace SMITH_MATH
61{
62
65inline bool GammaToImpedance( double aRe, double aIm, double aZ0, double& aResistance, double& aReactance )
66{
67 double denom = ( 1.0 - aRe ) * ( 1.0 - aRe ) + aIm * aIm;
68
69 if( !std::isfinite( denom ) || denom < 1e-12 )
70 return false;
71
72 aResistance = ( 1.0 - aRe * aRe - aIm * aIm ) / denom * aZ0;
73 aReactance = 2.0 * aIm / denom * aZ0;
74
75 return true;
76}
77
78inline double VSWR( double aGammaMag )
79{
80 if( aGammaMag >= 1.0 )
81 return std::numeric_limits<double>::infinity();
82
83 return ( 1.0 + aGammaMag ) / ( 1.0 - aGammaMag );
84}
85
86inline double ReturnLoss( double aGammaMag )
87{
88 if( aGammaMag <= 0.0 )
89 return std::numeric_limits<double>::infinity();
90
91 return -20.0 * std::log10( aGammaMag );
92}
93
96inline double SeriesInductance( double aReactance, double aFreq )
97{
98 return aReactance / ( 2.0 * M_PI * aFreq );
99}
100
101inline double SeriesCapacitance( double aReactance, double aFreq )
102{
103 return 1.0 / ( 2.0 * M_PI * aFreq * -aReactance );
104}
105
107inline wxRealPoint ZoomAboutPoint( const SMITH_VIEW& aView, const wxPoint& aPos, double aNewZoom )
108{
109 wxRealPoint gamma = aView.ToGamma( aPos );
110 double newRadius = aView.radius / aView.zoom * aNewZoom;
111
112 return wxRealPoint( gamma.x - ( aPos.x - aView.center.x ) / newRadius,
113 gamma.y + ( aPos.y - aView.center.y ) / newRadius );
114}
115
117inline bool ParseSParamPorts( const wxString& aVectorName, long* aResponsePort, long* aDrivePort )
118{
119 if( !aVectorName.StartsWith( wxS( "S_" ) ) )
120 return false;
121
122 wxString rest = aVectorName.Mid( 2 );
123
124 return rest.BeforeFirst( '_' ).ToLong( aResponsePort ) && rest.AfterFirst( '_' ).ToLong( aDrivePort );
125}
126
127} // namespace SMITH_MATH
128
129#endif // SMITH_MATH_H
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
double SeriesCapacitance(double aReactance, double aFreq)
Pan that keeps the gamma point under aPos fixed when the view zooms to aNewZoom.
Definition smith_math.h:101
double SeriesInductance(double aReactance, double aFreq)
Definition smith_math.h:96
double VSWR(double aGammaMag)
Definition smith_math.h:78
wxRealPoint ZoomAboutPoint(const SMITH_VIEW &aView, const wxPoint &aPos, double aNewZoom)
S-parameter vectors are named S_<responsePort>_<drivePort>.
Definition smith_math.h:107
bool ParseSParamPorts(const wxString &aVectorName, long *aResponsePort, long *aDrivePort)
Definition smith_math.h:117
bool GammaToImpedance(double aRe, double aIm, double aZ0, double &aResistance, double &aReactance)
< Impedance of a reflection coefficient, z = z0 ( 1 + gamma ) / ( 1 - gamma ), false at the gamma = 1...
Definition smith_math.h:65
double ReturnLoss(double aGammaMag)
Series equivalent element of a reactance at one frequency, henries for aReactance > 0,...
Definition smith_math.h:86
< Smith chart placement plus pan/zoom, maps a gamma point to a screen pixel and back.
Definition smith_math.h:32
wxRealPoint pan
Definition smith_math.h:36
double zoom
Definition smith_math.h:35
wxRealPoint ToGamma(const wxPoint &aPt) const
Definition smith_math.h:53
wxPoint center
Definition smith_math.h:33
wxPoint ToScreen(double aRe, double aIm) const
Definition smith_math.h:39
double radius
Definition smith_math.h:34
wxRect plotRect
Definition smith_math.h:37
#define M_PI