disptrans 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: disptrans
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Dispersion-compensated algorithm for electromagnetic waveguides
|
|
5
|
+
Author-email: John Garrett <garrettj403@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/garrettj403/DispersionTransform/
|
|
8
|
+
Project-URL: Changelog, https://github.com/garrettj403/DispersionTransform/blob/main/CHANGES.md
|
|
9
|
+
Project-URL: Issue Tracker, https://github.com/garrettj403/DispersionTransform/issues
|
|
10
|
+
Keywords: dispersion,waveguide,signal-processing,dsp
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Natural Language :: English
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Physics
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE.md
|
|
23
|
+
Requires-Dist: numpy
|
|
24
|
+
Requires-Dist: numba
|
|
25
|
+
Provides-Extra: testing
|
|
26
|
+
Requires-Dist: pytest; extra == "testing"
|
|
27
|
+
Requires-Dist: scipy; extra == "testing"
|
|
28
|
+
Requires-Dist: scikit-rf; extra == "testing"
|
|
29
|
+
Provides-Extra: examples
|
|
30
|
+
Requires-Dist: matplotlib; extra == "examples"
|
|
31
|
+
Requires-Dist: scipy; extra == "examples"
|
|
32
|
+
Requires-Dist: scikit-rf; extra == "examples"
|
|
33
|
+
Requires-Dist: SciencePlots; extra == "examples"
|
|
34
|
+
Dynamic: license-file
|
|
35
|
+
|
|
36
|
+
Dispersion-Compensated Algorithm
|
|
37
|
+
================================
|
|
38
|
+
|
|
39
|
+
[](https://badge.fury.io/py/disptrans)
|
|
40
|
+
[](https://github.com/garrettj403/DispersionTransform/actions/workflows/ci.yml)
|
|
41
|
+
[](https://github.com/garrettj403/DispersionTransform/actions/workflows/linter.yml)
|
|
42
|
+
[](https://github.com/garrettj403/DispersionTransform/blob/main/LICENSE.md)
|
|
43
|
+
|
|
44
|
+
*Dispersion-Compensated Algorithm for the Analysis of Electromagnetic Waveguides*
|
|
45
|
+
|
|
46
|
+
This package allows you to map dispersive waveguide data from the frequency-domain to distance-domain, and vice versa. The benefit of this approach, compared to a standard Fourier transform, is that this algorithm compensates for dispersion. Normally, dispersion causes signals in the time-domain to broaden as they propagate, making it difficult to isolate or suppress adjacent signals. In the distance-domain, the signals remain sharp, even over long distances, allowing you to easily identify, isolate or suppress specific signals.
|
|
47
|
+
|
|
48
|
+
For more information, see:
|
|
49
|
+
|
|
50
|
+
- J. D. Garrett and C. E. Tong, ["A Dispersion-Compensated Algorithm for the Analysis of Electromagnetic Waveguides",](https://ieeexplore.ieee.org/document/9447194) *IEEE Signal Processing Letters*, vol. 28, pp. 1175-1179, Jun. 2021, doi: [10.1109/LSP.2021.3086695](https://doi.org/10.1109/LSP.2021.3086695).
|
|
51
|
+
|
|
52
|
+
Getting Started
|
|
53
|
+
---------------
|
|
54
|
+
|
|
55
|
+
You can install the DispersionTransform package using ``pip``:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
# to install the latest release (from PyPI)
|
|
59
|
+
pip install disptrans
|
|
60
|
+
|
|
61
|
+
# to install the latest commit (from GitHub)
|
|
62
|
+
git clone https://github.com/garrettj403/DispersionTransform.git
|
|
63
|
+
cd DispersionTransform
|
|
64
|
+
pip install -e .
|
|
65
|
+
|
|
66
|
+
# to install dependencies for examples
|
|
67
|
+
pip install -e ".[examples]"
|
|
68
|
+
|
|
69
|
+
# to install dependencies for testing
|
|
70
|
+
pip install -e ".[testing]"
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The package requires Python 3.10 or newer.
|
|
74
|
+
|
|
75
|
+
Usage
|
|
76
|
+
-----
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
import disptrans
|
|
80
|
+
|
|
81
|
+
# f: frequency [Hz], s21f: frequency-domain response, beta: phase constant [rad/m]
|
|
82
|
+
# (beta must be real and strictly monotonic, i.e., all frequencies above cutoff)
|
|
83
|
+
|
|
84
|
+
# Frequency-domain -> distance-domain
|
|
85
|
+
# (x [m] is optional; by default, it covers one unambiguous range starting at 0)
|
|
86
|
+
x = disptrans.default_distance(beta)
|
|
87
|
+
s21x = disptrans.freq2distance(f, s21f, beta, x)
|
|
88
|
+
|
|
89
|
+
# Isolate a single reflection centered at 1 m (+/- 0.05 m)
|
|
90
|
+
s21x_gated = disptrans.gate(x, s21x, center=1.0, half_width=0.05, window="hamming")
|
|
91
|
+
|
|
92
|
+
# Distance-domain -> frequency-domain (x must be uniformly spaced)
|
|
93
|
+
s21f_gated = disptrans.distance2freq(x, s21x_gated, beta, f)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
The phase constant can be calculated using, e.g., [scikit-rf](https://scikit-rf.readthedocs.io/) (``RectangularWaveguide(...).beta``). See the [examples](https://github.com/garrettj403/DispersionTransform/tree/main/examples) for complete scripts.
|
|
97
|
+
|
|
98
|
+
Example: Simple Waveguide Section
|
|
99
|
+
---------------------------------
|
|
100
|
+
|
|
101
|
+
Transmission through a 10" long gold-plated WR-2.8 waveguide:
|
|
102
|
+
|
|
103
|
+
<p align="center">
|
|
104
|
+
<img src="https://raw.githubusercontent.com/garrettj403/DispersionTransform/main/examples/results/simple-waveguide-frequency.jpg" width="400">
|
|
105
|
+
</p>
|
|
106
|
+
|
|
107
|
+
Below, the time-domain response (calculated by an IFFT) is compared to the distance-domain response. Notice how much sharper the distance-domain response is.
|
|
108
|
+
|
|
109
|
+
<p align="center">
|
|
110
|
+
<img src="https://raw.githubusercontent.com/garrettj403/DispersionTransform/main/examples/results/simple-waveguide-time-vs-distance.jpg" width="600">
|
|
111
|
+
</p>
|
|
112
|
+
|
|
113
|
+
Example: Waveguide Cavity Resonator
|
|
114
|
+
-----------------------------------
|
|
115
|
+
|
|
116
|
+
This is a quick example showing the power of the dispersion-compensated algorithm. See [the included notebook](https://github.com/garrettj403/DispersionTransform/blob/main/examples/waveguide-cavity.ipynb) for more information.
|
|
117
|
+
|
|
118
|
+
For this example, we will start with the frequency-domain response of a simple waveguide cavity resonator, as shown below. This is a 1 inch long WR-2.8 cavity. Whenever the length of the cavity is an integer number of the guided wavelength divided by two, there is a peak in transmission.
|
|
119
|
+
|
|
120
|
+
<p align="center">
|
|
121
|
+
<img src="https://raw.githubusercontent.com/garrettj403/DispersionTransform/main/examples/results/cavity-freq-domain.jpg" width="600">
|
|
122
|
+
</p>
|
|
123
|
+
|
|
124
|
+
In the distance-domain, we can see a series of reflections corresponding to different signal paths within the resonator. The first peak is the signal passing straight through the resonator (distance = 1 inch), the second peak is the signal that undergoes one internal back-and-forth reflection (distance = 3 inch), etc.
|
|
125
|
+
|
|
126
|
+
<p align="center">
|
|
127
|
+
<img src="https://raw.githubusercontent.com/garrettj403/DispersionTransform/main/examples/results/cavity-distance-domain.jpg" width="600">
|
|
128
|
+
</p>
|
|
129
|
+
|
|
130
|
+
In the distance-domain, we can easily isolate the first peak and then return to the frequency-domain. The isolated reflection provides a very close match to theory.
|
|
131
|
+
|
|
132
|
+
<p align="center">
|
|
133
|
+
<img src="https://raw.githubusercontent.com/garrettj403/DispersionTransform/main/examples/results/cavity-peak1.jpg" width="600">
|
|
134
|
+
</p>
|
|
135
|
+
|
|
136
|
+
Likewise, we can easily isolate the 6th peak and return to the frequency-domain. This is impossible in the time-domain because there is too much broadening and overlap between adjacent reflections.
|
|
137
|
+
|
|
138
|
+
<p align="center">
|
|
139
|
+
<img src="https://raw.githubusercontent.com/garrettj403/DispersionTransform/main/examples/results/cavity-peak6.jpg" width="600">
|
|
140
|
+
</p>
|
|
141
|
+
|
|
142
|
+
Note: This example is similar to the example presented by [Garrett & Tong 2021](https://ieeexplore.ieee.org/document/9447194), but it is slightly different (e.g., different dimensions, different iris parameters, etc.). Please see this paper for more information.
|
|
143
|
+
|
|
144
|
+
Citing This Repo
|
|
145
|
+
----------------
|
|
146
|
+
|
|
147
|
+
If you use this code, please cite the following paper:
|
|
148
|
+
|
|
149
|
+
@article{Garrett2021,
|
|
150
|
+
author = {John D. Garrett and
|
|
151
|
+
Cheuk-yu Edward Tong},
|
|
152
|
+
title = {{A Dispersion-Compensated Algorithm for the Analysis of Electromagnetic Waveguides}},
|
|
153
|
+
volume = {28},
|
|
154
|
+
pages = {1175--1179},
|
|
155
|
+
month = jun,
|
|
156
|
+
year = {2021},
|
|
157
|
+
journal = {IEEE Signal Processing Letters},
|
|
158
|
+
doi = {10.1109/LSP.2021.3086695},
|
|
159
|
+
url = {https://ieeexplore.ieee.org/document/9447194}
|
|
160
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
disptrans.py,sha256=JFw6FGbA1IaGDtAxZcSWfBfnSf9xUXiMBnnw_AJ39O8,8907
|
|
2
|
+
disptrans-0.1.0.dist-info/licenses/LICENSE.md,sha256=Zrw7YCXcTm9aIlaLok9qPwDbt8qJmkqYerdNrxLhNzs,1068
|
|
3
|
+
disptrans-0.1.0.dist-info/METADATA,sha256=rvJhpje5mCAihmym1wobvGJykosM5T780vbKM9-0rNc,8024
|
|
4
|
+
disptrans-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
5
|
+
disptrans-0.1.0.dist-info/top_level.txt,sha256=_SbzwMZVKyRxjPYznzsLQDcvT5EU-PIqsz42cSuC9cU,10
|
|
6
|
+
disptrans-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 John Garrett
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
disptrans
|
disptrans.py
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
"""Dispersion-compensated algorithm for mapping dispersive waveguide data from
|
|
2
|
+
the frequency-domain to distance-domain, and vice versa.
|
|
3
|
+
|
|
4
|
+
Reference:
|
|
5
|
+
|
|
6
|
+
J. D. Garrett and C. E. Tong, "A Dispersion-Compensated Algorithm for the
|
|
7
|
+
Analysis of Electromagnetic Waveguides," IEEE Signal Processing Letters,
|
|
8
|
+
vol. 28, pp. 1175-1179, 2021, doi: 10.1109/LSP.2021.3086695.
|
|
9
|
+
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import numba as nb
|
|
13
|
+
import numpy as np
|
|
14
|
+
|
|
15
|
+
__version__ = "0.1.0"
|
|
16
|
+
|
|
17
|
+
__all__ = ["freq2distance", "distance2freq", "default_distance", "gate"]
|
|
18
|
+
|
|
19
|
+
_WINDOWS = {
|
|
20
|
+
"boxcar": np.ones,
|
|
21
|
+
"rect": np.ones,
|
|
22
|
+
"hamming": np.hamming,
|
|
23
|
+
"hann": np.hanning,
|
|
24
|
+
"hanning": np.hanning,
|
|
25
|
+
"blackman": np.blackman,
|
|
26
|
+
"bartlett": np.bartlett,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
# Public API ------------------------------------------------------------------
|
|
31
|
+
|
|
32
|
+
def freq2distance(f, fresp, beta, x=None):
|
|
33
|
+
"""Transform a frequency-domain signal to the distance-domain.
|
|
34
|
+
|
|
35
|
+
Compensates for dispersion.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
f (np.ndarray): frequency for frequency-domain signal, in units [Hz]
|
|
39
|
+
fresp (np.ndarray): frequency-domain signal
|
|
40
|
+
beta (np.ndarray): phase constant, in units [rad/m]. Must be real and
|
|
41
|
+
strictly monotonic (i.e., all points above cutoff).
|
|
42
|
+
x (np.ndarray, optional): distance array for output data, in units
|
|
43
|
+
[m]. Defaults to ``default_distance(beta)``.
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
np.ndarray: distance-domain response
|
|
47
|
+
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
_, beta, fresp = _check_freq_inputs(f, beta, fresp=fresp)
|
|
51
|
+
|
|
52
|
+
if x is None:
|
|
53
|
+
x = default_distance(beta)
|
|
54
|
+
x = _as_float_array(x, "x")
|
|
55
|
+
if x.size == 0:
|
|
56
|
+
raise ValueError("x must not be empty.")
|
|
57
|
+
|
|
58
|
+
return _freq2distance(fresp, beta, x)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def distance2freq(x, xresp, beta, f):
|
|
62
|
+
"""Transform a distance-domain signal to the frequency-domain.
|
|
63
|
+
|
|
64
|
+
Compensates for dispersion.
|
|
65
|
+
|
|
66
|
+
The output frequency grid is defined by ``beta`` (one output point per
|
|
67
|
+
value of ``beta``); ``f`` must be the matching frequency array.
|
|
68
|
+
|
|
69
|
+
Args:
|
|
70
|
+
x (np.ndarray): distance array for input data, in units [m]. Must be
|
|
71
|
+
uniformly spaced, with at least 2 points.
|
|
72
|
+
xresp (np.ndarray): distance-domain response
|
|
73
|
+
beta (np.ndarray): phase constant, in units [rad/m]. Must be real and
|
|
74
|
+
strictly monotonic (i.e., all points above cutoff).
|
|
75
|
+
f (np.ndarray): frequency for frequency-domain response, in units [Hz]
|
|
76
|
+
|
|
77
|
+
Returns:
|
|
78
|
+
np.ndarray: frequency-domain response
|
|
79
|
+
|
|
80
|
+
"""
|
|
81
|
+
|
|
82
|
+
_, beta, _ = _check_freq_inputs(f, beta)
|
|
83
|
+
|
|
84
|
+
x = _as_float_array(x, "x")
|
|
85
|
+
xresp = _as_complex_array(xresp, "xresp")
|
|
86
|
+
if x.size < 2:
|
|
87
|
+
raise ValueError("x must have at least 2 points.")
|
|
88
|
+
if xresp.shape != x.shape:
|
|
89
|
+
raise ValueError("xresp and x must have the same length "
|
|
90
|
+
"(got {} and {}).".format(xresp.size, x.size))
|
|
91
|
+
xstep = _uniform_step(x)
|
|
92
|
+
|
|
93
|
+
# Phase constant step at each frequency point, d(beta)/d(index). This is
|
|
94
|
+
# the Jacobian that converts the sum over frequency points into an
|
|
95
|
+
# integral over beta.
|
|
96
|
+
dbeta = np.gradient(beta)
|
|
97
|
+
|
|
98
|
+
fresp = _distance2freq(xresp, beta, x)
|
|
99
|
+
fresp *= beta.size * xstep * dbeta / (2 * np.pi)
|
|
100
|
+
|
|
101
|
+
return fresp
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def default_distance(beta, npts=None):
|
|
105
|
+
"""Default distance array for the distance-domain response.
|
|
106
|
+
|
|
107
|
+
Analogous to the time array of an IFFT: the step size is set by the
|
|
108
|
+
total span of ``beta`` and the array covers one unambiguous range,
|
|
109
|
+
``2 * pi / mean(d_beta)``, starting at zero.
|
|
110
|
+
|
|
111
|
+
Args:
|
|
112
|
+
beta (np.ndarray): phase constant, in units [rad/m]
|
|
113
|
+
npts (int, optional): number of points. Defaults to ``len(beta)``.
|
|
114
|
+
|
|
115
|
+
Returns:
|
|
116
|
+
np.ndarray: distance array, in units [m]
|
|
117
|
+
|
|
118
|
+
"""
|
|
119
|
+
|
|
120
|
+
beta = _check_beta(beta)
|
|
121
|
+
if npts is None:
|
|
122
|
+
npts = beta.size
|
|
123
|
+
npts = int(npts)
|
|
124
|
+
if npts < 1:
|
|
125
|
+
raise ValueError("npts must be at least 1.")
|
|
126
|
+
mean_dbeta = np.abs(beta[-1] - beta[0]) / (beta.size - 1)
|
|
127
|
+
return np.linspace(0, 2 * np.pi / mean_dbeta, npts, endpoint=False)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def gate(x, xresp, center, half_width, window="hamming"):
|
|
131
|
+
"""Isolate part of a distance-domain response using a window.
|
|
132
|
+
|
|
133
|
+
Points outside of ``[center - half_width, center + half_width]`` are set
|
|
134
|
+
to zero and the window is applied to the points inside.
|
|
135
|
+
|
|
136
|
+
Args:
|
|
137
|
+
x (np.ndarray): distance array, in units [m]. Must be sorted.
|
|
138
|
+
xresp (np.ndarray): distance-domain response
|
|
139
|
+
center (float): center of the gate, in units [m]
|
|
140
|
+
half_width (float): half-width of the gate, in units [m]
|
|
141
|
+
window (str or callable): window name ("hamming", "hann", "blackman",
|
|
142
|
+
"bartlett" or "boxcar") or a function that takes the number of
|
|
143
|
+
points and returns the window, e.g., ``scipy.signal.windows.tukey``
|
|
144
|
+
wrapped in a lambda. Defaults to "hamming".
|
|
145
|
+
|
|
146
|
+
Returns:
|
|
147
|
+
np.ndarray: gated distance-domain response
|
|
148
|
+
|
|
149
|
+
"""
|
|
150
|
+
|
|
151
|
+
x = _as_float_array(x, "x")
|
|
152
|
+
xresp = _as_complex_array(xresp, "xresp")
|
|
153
|
+
if xresp.shape != x.shape:
|
|
154
|
+
raise ValueError("xresp and x must have the same length.")
|
|
155
|
+
if np.any(np.diff(x) <= 0):
|
|
156
|
+
raise ValueError("x must be strictly increasing.")
|
|
157
|
+
if half_width <= 0:
|
|
158
|
+
raise ValueError("half_width must be positive.")
|
|
159
|
+
|
|
160
|
+
if callable(window):
|
|
161
|
+
window_fn = window
|
|
162
|
+
else:
|
|
163
|
+
try:
|
|
164
|
+
window_fn = _WINDOWS[window.lower()]
|
|
165
|
+
except (KeyError, AttributeError):
|
|
166
|
+
raise ValueError("Unknown window: {!r}. Choose from: {}.".format(
|
|
167
|
+
window, ", ".join(sorted(_WINDOWS)))) from None
|
|
168
|
+
|
|
169
|
+
mask = (x >= center - half_width) & (x <= center + half_width)
|
|
170
|
+
npts = int(mask.sum())
|
|
171
|
+
if npts == 0:
|
|
172
|
+
raise ValueError("Gate does not contain any points of x.")
|
|
173
|
+
|
|
174
|
+
win = np.zeros(x.size)
|
|
175
|
+
win[mask] = np.asarray(window_fn(npts), dtype=float)
|
|
176
|
+
|
|
177
|
+
return xresp * win
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
# Input validation ------------------------------------------------------------
|
|
181
|
+
|
|
182
|
+
def _as_float_array(value, name):
|
|
183
|
+
arr = np.asarray(value)
|
|
184
|
+
if arr.ndim != 1:
|
|
185
|
+
raise ValueError("{} must be a 1D array.".format(name))
|
|
186
|
+
if np.iscomplexobj(arr):
|
|
187
|
+
raise ValueError("{} must be real.".format(name))
|
|
188
|
+
arr = arr.astype(np.float64)
|
|
189
|
+
if not np.all(np.isfinite(arr)):
|
|
190
|
+
raise ValueError("{} must be finite.".format(name))
|
|
191
|
+
return np.ascontiguousarray(arr)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _as_complex_array(value, name):
|
|
195
|
+
arr = np.asarray(value)
|
|
196
|
+
if arr.ndim != 1:
|
|
197
|
+
raise ValueError("{} must be a 1D array.".format(name))
|
|
198
|
+
arr = arr.astype(np.complex128)
|
|
199
|
+
if not np.all(np.isfinite(arr)):
|
|
200
|
+
raise ValueError("{} must be finite.".format(name))
|
|
201
|
+
return np.ascontiguousarray(arr)
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def _check_beta(beta):
|
|
205
|
+
beta = np.asarray(beta)
|
|
206
|
+
if np.iscomplexobj(beta):
|
|
207
|
+
if np.any(beta.imag != 0):
|
|
208
|
+
raise ValueError("beta must be real (the phase constant, not the "
|
|
209
|
+
"complex propagation constant).")
|
|
210
|
+
beta = beta.real
|
|
211
|
+
beta = _as_float_array(beta, "beta")
|
|
212
|
+
if beta.size < 2:
|
|
213
|
+
raise ValueError("beta must have at least 2 points.")
|
|
214
|
+
dbeta = np.diff(beta)
|
|
215
|
+
if not (np.all(dbeta > 0) or np.all(dbeta < 0)):
|
|
216
|
+
raise ValueError("beta must be strictly monotonic. Make sure all "
|
|
217
|
+
"frequency points are above the cutoff frequency.")
|
|
218
|
+
return beta
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def _check_freq_inputs(f, beta, fresp=None):
|
|
222
|
+
f = _as_float_array(f, "f")
|
|
223
|
+
beta = _check_beta(beta)
|
|
224
|
+
if f.size != beta.size:
|
|
225
|
+
raise ValueError("f and beta must have the same length "
|
|
226
|
+
"(got {} and {}).".format(f.size, beta.size))
|
|
227
|
+
if fresp is not None:
|
|
228
|
+
fresp = _as_complex_array(fresp, "fresp")
|
|
229
|
+
if fresp.size != f.size:
|
|
230
|
+
raise ValueError("fresp and f must have the same length "
|
|
231
|
+
"(got {} and {}).".format(fresp.size, f.size))
|
|
232
|
+
return f, beta, fresp
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def _uniform_step(x):
|
|
236
|
+
steps = np.diff(x)
|
|
237
|
+
step = steps.mean()
|
|
238
|
+
if step <= 0 or not np.allclose(steps, step, rtol=1e-6, atol=0):
|
|
239
|
+
raise ValueError("x must be uniformly spaced and increasing.")
|
|
240
|
+
return step
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
# Numba kernels ---------------------------------------------------------------
|
|
244
|
+
|
|
245
|
+
@nb.njit(parallel=True, cache=True)
|
|
246
|
+
def _freq2distance(fresp, beta, x):
|
|
247
|
+
"""Kernel for freq2distance (inputs must already be validated)."""
|
|
248
|
+
fpts = beta.size
|
|
249
|
+
xresp = np.empty(x.size, dtype=np.complex128)
|
|
250
|
+
for i in nb.prange(x.size):
|
|
251
|
+
acc = 0j
|
|
252
|
+
for k in range(fpts):
|
|
253
|
+
acc += fresp[k] * np.exp(1j * beta[k] * x[i])
|
|
254
|
+
xresp[i] = acc / fpts
|
|
255
|
+
return xresp
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
@nb.njit(parallel=True, cache=True)
|
|
259
|
+
def _distance2freq(xresp, beta, x):
|
|
260
|
+
"""Kernel for distance2freq, without normalization."""
|
|
261
|
+
fresp = np.empty(beta.size, dtype=np.complex128)
|
|
262
|
+
for k in nb.prange(beta.size):
|
|
263
|
+
acc = 0j
|
|
264
|
+
for i in range(x.size):
|
|
265
|
+
acc += xresp[i] * np.exp(-1j * beta[k] * x[i])
|
|
266
|
+
fresp[k] = acc
|
|
267
|
+
return fresp
|