Debian Codegraph @

skrf/taper.py

raw symbols SCIP
1 """
2 taper (:mod:`skrf.taper`)
3 =========================
4
5 Taper Objects
6
7 Tapered transformers, or tapers in short, are used to match
8 one impedance to another (from Z1 to Z2) [#]_.
9
10 By tapering a transmission line, a very broadband impedance match (low VSWR)
11 can be realized over a wide bandwidth, the longer the taper, the wider the frequency band.
12
13 References
14 ----------
15 .. [#] https://www.microwaves101.com/encyclopedias/tapered-transformers
16
17 .. autosummary::
18 :toctree: generated/
19
20 Taper1D
21 Linear
22 Exponential
23 SmoothStep
24 Klopfenstein
25
26 """
27 from __future__ import annotations
28
29 from collections.abc import Callable
30 from numbers import Number
31
32 from numpy import exp, linspace, log
33
34 from .network import cascade_list
35
36
37 class Taper1D:
38 """
39 Generic 1D Taper Object
40 """
41 def __init__(self, med, start: Number, stop: Number, n_sections: int, f: Callable,
42 length: Number, length_unit: str = 'm', param: str = 'z0', f_is_normed: bool = True,
43 med_kw: dict = None, f_kw: dict = None):
44 """
45 Generic 1D Taper Constructor.
46
47 Parameters
48 ----------
49 med : :class:`~skrf.media.media.Media`
50 A :class:`~skrf.media.media.Media` object or a `@classmethod` `__init__`,
51 used to generate the transmission line.
52 See `med_kw` for arguments.
53 Examples:
54
55 * `skrf.media.RectangularWaveguide` # a class
56 * `skrf.media.RectangularWaveguide.from_z0` # an init
57
58 start : number
59 starting value for `param`
60 stop : number
61 stop value for `param`
62 n_sections : int
63 number of sections in taper
64 f : Callable
65 function defining the taper transition. must take either
66 no arguments or take `(x,length, start, stop)`.
67 see `f_is_normed` arguments
68 length : number
69 physical length of the taper (in `length_unit`)
70 length_unit : str
71 unit of length variable. see `skrf.to_meters`
72 param : str
73 name of the parameter of `med` that varies along the taper
74 f_is_normed: bool
75 is `f` scalable and normalized. ie can f just be scaled
76 to fit different start/stop conditions? if so then f is
77 called with no arguments, and must have domain and raings
78 of [0,1], and [0,1]
79 f_kw : dict
80 passed to `f()` when called
81 med_kw : dict
82 passed to `med.__init__` when an instance is created
83
84 Note
85 ----
86 The default behaviour should is to taper based on impedance.
87 To do this we inspect the `med` class for a `from_z0`
88 init method, and if it exists, we assign it to `med` attribute,
89 in `__init__`.
90 Admittedly having `med` be a class or a method is abuse,
91 it makes for a intuitive operation.
92
93 Examples
94 --------
95 Create a linear taper from 100 to 1000mil
96
97 >>> from skrf import Frequency, RectangularWaveguide, Taper1D, mil, inch
98 >>> taper = Taper1D(med=RectangularWaveguide,
99 param='a',
100 start=100*mil,
101 stop=1000*mil,
102 length=1*inch,
103 n_sections=20,
104 f=lambda x: x,
105 f_is_normed=True,
106 med_kw={'frequency':Frequency(75,110,101,'ghz')})
107 """
108 if f_kw is None:
109 f_kw = {}
110 if med_kw is None:
111 med_kw = {}
112 self.med = med
113 self.param = param
114 self.start = start
115 self.stop = stop
116 self.f = f
117 self.f_is_normed = f_is_normed
118 self.length = length
119 self.length_unit = length_unit
120 self.n_sections = n_sections
121 self.med_kw = med_kw
122 self.f_kw = f_kw
123
124 # the default behaviour should be to taper based on impedance.
125 # to do this we inspect the media class for a `from_z0`
126 # init method, and if it exists, we assign it to `med` attribute
127 # admittedly having `med` be a class or a method is abuse,
128 # it makes for a intuitive operation
129 if param == 'z0':
130 if hasattr(self.med, 'from_z0'):
131 self.med = self.med.from_z0
132
133 def __str__(self) -> str:
134 return 'Taper: {classname}: {param} from {start}-{stop}'
135
136
137 @property
138 def section_length(self) -> Number:
139 """
140 Section length.
141
142 Returns
143 -------
144 l : number
145 """
146 return 1.0*self.length/self.n_sections
147
148 @property
149 def value_vector(self):
150 if self.f_is_normed:
151 x = linspace(0,1,self.n_sections)
152 y = self.f(x, **self.f_kw)*(self.stop-self.start) + self.start
153 else:
154 x = linspace(0,self.length,self.n_sections)
155 y = self.f(x,self.length, self.start, self.stop, **self.f_kw)
156 return y
157
158 def media_at(self, val: Number):
159 """
160 Create a media instance for the taper with parameter value `val`.
161
162 Parameters
163 ----------
164 val : number
165 parameter value
166
167 Returns
168 -------
169 media : :class:`~skrf.media.media.Media`
170 media instance for the taper for the given parameter value
171 """
172 med_kw = self.med_kw.copy()
173 med_kw.update({self.param: val})
174 return self.med(**med_kw)
175
176 def section_at(self, val: Number):
177 """
178 Create a single section of the taper with parameter value `val`.
179
180 Parameters
181 ----------
182 val : number
183 parameter value
184
185 Returns
186 -------
187 media : :class:`~skrf.network.Network`
188 Network instance for the section of the taper
189 for the given parameter value
190 """
191 return self.media_at(val).line(self.section_length,
192 unit=self.length_unit)
193
194 @property
195 def medias(self) -> list:
196 """
197 List of medias.
198
199 Returns
200 -------
201 medias : list of :class:`~skrf.media.media.Media`
202 """
203 return [self.media_at(k) for k in self.value_vector]
204
205 @property
206 def sections(self) -> list:
207 """
208 List of sections.
209
210 Returns
211 -------
212 sections : list of :class:`~skrf.network.Network`
213 """
214 return [self.section_at(k) for k in self.value_vector]
215
216 @property
217 def network(self):
218 """
219 Resulting Network
220
221 Returns
222 -------
223 ntwk : :class:`~skrf.network.Network`
224 """
225 return cascade_list(self.sections)
226
227
228
229 class Linear(Taper1D):
230 """
231 A linear Taper.
232
233 Defined by :math:`f(x)=x`
234
235 """
236 def __init__(self, **kw):
237 opts = dict(f=lambda x:x, f_is_normed=True)
238 kw.update(opts)
239 super().__init__(**kw)
240
241
242 class Exponential(Taper1D):
243 r"""
244 An Exponential Taper.
245
246 Defined by :math:`f(x) = f_0 \exp\left[ \frac{x}{x_1} \ln\left( \frac{f_1}{f_0} \right) \right]`
247
248 where:
249
250 * :math:`f_0`: start param value
251 * :math:`f_1`: stop param value
252 * :math:`x`: independent variable (position along taper)
253 * :math:`x_1`: length of taper
254
255 """
256 def __init__(self, **kw):
257 """
258 Exponential Taper Constructor
259 """
260
261 def f(x, length, start, stop):
262 return start*exp(x/length*(log(stop/start)))
263
264 opts = dict(f=f, f_is_normed=False)
265 kw.update(opts)
266 super().__init__(**kw)
267
268
269 class SmoothStep(Taper1D):
270 """
271 A smoothstep Taper.
272
273 There is no analytical basis for this in the EE world that I know
274 of. it is just a reasonable smooth curve, that is easy to implement.
275
276 :math:`f(x) = (3 x^2 - 2x^3)`
277
278 References
279 ----------
280 https://en.wikipedia.org/wiki/Smoothstep
281
282 """
283 def __init__(self, **kw):
284 """
285 Smoothstep Taper Constructor.
286 """
287
288 def f(x):
289 return 3 * x ** 2 - 2 * x ** 3
290 opts = dict(f=f, f_is_normed=True)
291 kw.update(opts)
292 super().__init__(**kw)
293
294
295 class Klopfenstein(Taper1D):
296 """
297 Klopfenstein Taper.
298
299 This impedance taper was first described by R. W. Klopfenstein [#microwaves101]_ in a paper titled
300 "A Transmission Line Taper of Improved Design", published in 1956 [#Klopfenstein]_ .
301 A correction to Klopfenstein's math was published by in May 1973 by Darko Kajfez and Jame Prewit [#Kajfez]_ .
302
303 References
304 ----------
305 .. [#microwaves101] https://www.microwaves101.com/encyclopedias/klopfenstein-taper
306 .. [#Klopfenstein] R. W. Klopfenstein Proc. IRE. vol. 44 pp. 31-35 Jan. 1956.
307 doi: 10.1109/JRPROC.1956.274847
308 https://ieeexplore.ieee.org/document/4051841
309 .. [#Kajfez] D. Kajfez and J. O. Prewitt, "Correction to "A Transmission Line Taper of Improved Design" (Letters),"
310 in IEEE Transactions on Microwave Theory and Techniques, vol. 21, no. 5, pp. 364-364, May 1973,
311 doi: 10.1109/TMTT.1973.1128003.
312 https://ieeexplore.ieee.org/document/1128003
313 """
314 def __init__(self, **kw):
315 raise NotImplementedError('Not Yet Implemented')