ezmsg-sigproc 1.8.2__py3-none-any.whl → 2.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.
- ezmsg/sigproc/__version__.py +2 -2
- ezmsg/sigproc/activation.py +36 -39
- ezmsg/sigproc/adaptive_lattice_notch.py +231 -0
- ezmsg/sigproc/affinetransform.py +169 -163
- ezmsg/sigproc/aggregate.py +133 -101
- ezmsg/sigproc/bandpower.py +64 -52
- ezmsg/sigproc/base.py +1242 -0
- ezmsg/sigproc/butterworthfilter.py +37 -33
- ezmsg/sigproc/cheby.py +29 -17
- ezmsg/sigproc/combfilter.py +163 -0
- ezmsg/sigproc/decimate.py +19 -10
- ezmsg/sigproc/detrend.py +29 -0
- ezmsg/sigproc/diff.py +81 -0
- ezmsg/sigproc/downsample.py +78 -84
- ezmsg/sigproc/ewma.py +197 -0
- ezmsg/sigproc/extract_axis.py +41 -0
- ezmsg/sigproc/filter.py +257 -141
- ezmsg/sigproc/filterbank.py +247 -199
- ezmsg/sigproc/math/abs.py +17 -22
- ezmsg/sigproc/math/clip.py +24 -24
- ezmsg/sigproc/math/difference.py +34 -30
- ezmsg/sigproc/math/invert.py +13 -25
- ezmsg/sigproc/math/log.py +28 -33
- ezmsg/sigproc/math/scale.py +18 -26
- ezmsg/sigproc/quantize.py +71 -0
- ezmsg/sigproc/resample.py +298 -0
- ezmsg/sigproc/sampler.py +241 -259
- ezmsg/sigproc/scaler.py +55 -218
- ezmsg/sigproc/signalinjector.py +52 -43
- ezmsg/sigproc/slicer.py +81 -89
- ezmsg/sigproc/spectrogram.py +77 -75
- ezmsg/sigproc/spectrum.py +203 -168
- ezmsg/sigproc/synth.py +546 -393
- ezmsg/sigproc/transpose.py +131 -0
- ezmsg/sigproc/util/asio.py +156 -0
- ezmsg/sigproc/util/message.py +31 -0
- ezmsg/sigproc/util/profile.py +55 -12
- ezmsg/sigproc/util/typeresolution.py +83 -0
- ezmsg/sigproc/wavelets.py +154 -153
- ezmsg/sigproc/window.py +269 -211
- {ezmsg_sigproc-1.8.2.dist-info → ezmsg_sigproc-2.1.0.dist-info}/METADATA +2 -1
- ezmsg_sigproc-2.1.0.dist-info/RECORD +51 -0
- ezmsg_sigproc-1.8.2.dist-info/RECORD +0 -39
- {ezmsg_sigproc-1.8.2.dist-info → ezmsg_sigproc-2.1.0.dist-info}/WHEEL +0 -0
- {ezmsg_sigproc-1.8.2.dist-info → ezmsg_sigproc-2.1.0.dist-info}/licenses/LICENSE.txt +0 -0
ezmsg/sigproc/spectrogram.py
CHANGED
|
@@ -1,95 +1,97 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
from typing import Generator
|
|
3
2
|
import ezmsg.core as ez
|
|
4
3
|
from ezmsg.util.messages.axisarray import AxisArray
|
|
5
|
-
from ezmsg.util.generator import consumer, compose
|
|
6
4
|
from ezmsg.util.messages.modify import modify_axis
|
|
7
5
|
|
|
8
|
-
from .window import
|
|
9
|
-
from .spectrum import
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
output: SpectralOutput = SpectralOutput.POSITIVE,
|
|
21
|
-
) -> typing.Generator[AxisArray | None, AxisArray, None]:
|
|
22
|
-
"""
|
|
23
|
-
Calculate a spectrogram on streaming data.
|
|
24
|
-
|
|
25
|
-
Chains :obj:`ezmsg.sigproc.window.windowing` to apply a moving window on the data,
|
|
26
|
-
:obj:`ezmsg.sigproc.spectrum.spectrum` to calculate spectra for each window,
|
|
27
|
-
and finally :obj:`ezmsg.util.messages.modify.modify_axis` to convert the win axis back to time axis.
|
|
28
|
-
|
|
29
|
-
Args:
|
|
30
|
-
window_dur: See :obj:`ezmsg.sigproc.window.windowing`
|
|
31
|
-
window_shift: See :obj:`ezmsg.sigproc.window.windowing`
|
|
32
|
-
window_anchor: See :obj:`ezmsg.sigproc.window.windowing`
|
|
33
|
-
window: See :obj:`ezmsg.sigproc.spectrum.spectrum`
|
|
34
|
-
transform: See :obj:`ezmsg.sigproc.spectrum.spectrum`
|
|
35
|
-
output: See :obj:`ezmsg.sigproc.spectrum.spectrum`
|
|
36
|
-
|
|
37
|
-
Returns:
|
|
38
|
-
A primed generator object that expects an :obj:`AxisArray` via `.send(axis_array)`
|
|
39
|
-
with continuous data in its .data payload, and yields an :obj:`AxisArray` of time-frequency power values.
|
|
40
|
-
"""
|
|
41
|
-
|
|
42
|
-
pipeline = compose(
|
|
43
|
-
windowing(
|
|
44
|
-
axis="time",
|
|
45
|
-
newaxis="win",
|
|
46
|
-
window_dur=window_dur,
|
|
47
|
-
window_shift=window_shift,
|
|
48
|
-
zero_pad_until="shift" if window_shift is not None else "input",
|
|
49
|
-
anchor=window_anchor,
|
|
50
|
-
),
|
|
51
|
-
spectrum(axis="time", window=window, transform=transform, output=output),
|
|
52
|
-
modify_axis(name_map={"win": "time"}),
|
|
53
|
-
)
|
|
54
|
-
|
|
55
|
-
# State variables
|
|
56
|
-
msg_out: AxisArray | None = None
|
|
57
|
-
|
|
58
|
-
while True:
|
|
59
|
-
msg_in: AxisArray = yield msg_out
|
|
60
|
-
msg_out = pipeline(msg_in)
|
|
6
|
+
from .window import Anchor, WindowTransformer
|
|
7
|
+
from .spectrum import (
|
|
8
|
+
WindowFunction,
|
|
9
|
+
SpectralTransform,
|
|
10
|
+
SpectralOutput,
|
|
11
|
+
SpectrumTransformer,
|
|
12
|
+
)
|
|
13
|
+
from .base import (
|
|
14
|
+
CompositeProcessor,
|
|
15
|
+
BaseStatefulProcessor,
|
|
16
|
+
BaseTransformerUnit,
|
|
17
|
+
)
|
|
61
18
|
|
|
62
19
|
|
|
63
20
|
class SpectrogramSettings(ez.Settings):
|
|
64
21
|
"""
|
|
65
|
-
Settings for :obj:`
|
|
66
|
-
See :obj:`spectrogram` for a description of the parameters.
|
|
22
|
+
Settings for :obj:`SpectrogramTransformer`.
|
|
67
23
|
"""
|
|
68
24
|
|
|
69
|
-
window_dur: float | None = None
|
|
25
|
+
window_dur: float | None = None
|
|
26
|
+
"""window duration in seconds."""
|
|
27
|
+
|
|
70
28
|
window_shift: float | None = None
|
|
71
29
|
""""window step in seconds. If None, window_shift == window_dur"""
|
|
30
|
+
|
|
72
31
|
window_anchor: str | Anchor = Anchor.BEGINNING
|
|
32
|
+
"""See :obj"`WindowTransformer`"""
|
|
73
33
|
|
|
74
|
-
# See SpectrumSettings for details of following settings:
|
|
75
34
|
window: WindowFunction = WindowFunction.HAMMING
|
|
76
|
-
|
|
77
|
-
output: SpectralOutput = SpectralOutput.POSITIVE
|
|
78
|
-
|
|
35
|
+
"""The :obj:`WindowFunction` to apply to the data slice prior to calculating the spectrum."""
|
|
79
36
|
|
|
80
|
-
|
|
81
|
-
"""
|
|
82
|
-
Unit for :obj:`spectrogram`.
|
|
83
|
-
"""
|
|
37
|
+
transform: SpectralTransform = SpectralTransform.REL_DB
|
|
38
|
+
"""The :obj:`SpectralTransform` to apply to the spectral magnitude."""
|
|
84
39
|
|
|
40
|
+
output: SpectralOutput = SpectralOutput.POSITIVE
|
|
41
|
+
"""The :obj:`SpectralOutput` format."""
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class SpectrogramTransformer(
|
|
45
|
+
CompositeProcessor[SpectrogramSettings, AxisArray, AxisArray]
|
|
46
|
+
):
|
|
47
|
+
@staticmethod
|
|
48
|
+
def _initialize_processors(
|
|
49
|
+
settings: SpectrogramSettings,
|
|
50
|
+
) -> dict[str, BaseStatefulProcessor | Generator[AxisArray, AxisArray, None]]:
|
|
51
|
+
return {
|
|
52
|
+
"windowing": WindowTransformer(
|
|
53
|
+
axis="time",
|
|
54
|
+
newaxis="win",
|
|
55
|
+
window_dur=settings.window_dur,
|
|
56
|
+
window_shift=settings.window_shift,
|
|
57
|
+
zero_pad_until="shift"
|
|
58
|
+
if settings.window_shift is not None
|
|
59
|
+
else "input",
|
|
60
|
+
anchor=settings.window_anchor,
|
|
61
|
+
),
|
|
62
|
+
"spectrum": SpectrumTransformer(
|
|
63
|
+
axis="time",
|
|
64
|
+
window=settings.window,
|
|
65
|
+
transform=settings.transform,
|
|
66
|
+
output=settings.output,
|
|
67
|
+
),
|
|
68
|
+
"modify_axis": modify_axis(name_map={"win": "time"}),
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class Spectrogram(
|
|
73
|
+
BaseTransformerUnit[
|
|
74
|
+
SpectrogramSettings, AxisArray, AxisArray, SpectrogramTransformer
|
|
75
|
+
]
|
|
76
|
+
):
|
|
85
77
|
SETTINGS = SpectrogramSettings
|
|
86
78
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
79
|
+
|
|
80
|
+
def spectrogram(
|
|
81
|
+
window_dur: float | None = None,
|
|
82
|
+
window_shift: float | None = None,
|
|
83
|
+
window_anchor: str | Anchor = Anchor.BEGINNING,
|
|
84
|
+
window: WindowFunction = WindowFunction.HAMMING,
|
|
85
|
+
transform: SpectralTransform = SpectralTransform.REL_DB,
|
|
86
|
+
output: SpectralOutput = SpectralOutput.POSITIVE,
|
|
87
|
+
) -> SpectrogramTransformer:
|
|
88
|
+
return SpectrogramTransformer(
|
|
89
|
+
SpectrogramSettings(
|
|
90
|
+
window_dur=window_dur,
|
|
91
|
+
window_shift=window_shift,
|
|
92
|
+
window_anchor=window_anchor,
|
|
93
|
+
window=window,
|
|
94
|
+
transform=transform,
|
|
95
|
+
output=output,
|
|
95
96
|
)
|
|
97
|
+
)
|
ezmsg/sigproc/spectrum.py
CHANGED
|
@@ -3,15 +3,19 @@ from functools import partial
|
|
|
3
3
|
import typing
|
|
4
4
|
|
|
5
5
|
import numpy as np
|
|
6
|
+
import numpy.typing as npt
|
|
6
7
|
import ezmsg.core as ez
|
|
7
8
|
from ezmsg.util.messages.axisarray import (
|
|
8
9
|
AxisArray,
|
|
9
10
|
slice_along_axis,
|
|
10
11
|
replace,
|
|
11
12
|
)
|
|
12
|
-
from ezmsg.util.generator import consumer
|
|
13
13
|
|
|
14
|
-
from .base import
|
|
14
|
+
from .base import (
|
|
15
|
+
BaseStatefulTransformer,
|
|
16
|
+
BaseTransformerUnit,
|
|
17
|
+
processor_state,
|
|
18
|
+
)
|
|
15
19
|
|
|
16
20
|
|
|
17
21
|
class OptionsEnum(enum.Enum):
|
|
@@ -66,198 +70,229 @@ class SpectralOutput(OptionsEnum):
|
|
|
66
70
|
NEGATIVE = "Negative Frequencies"
|
|
67
71
|
|
|
68
72
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
) -> typing.Generator[AxisArray, AxisArray, None]:
|
|
73
|
+
class SpectrumSettings(ez.Settings):
|
|
74
|
+
"""
|
|
75
|
+
Settings for :obj:`Spectrum.
|
|
76
|
+
See :obj:`spectrum` for a description of the parameters.
|
|
77
|
+
"""
|
|
78
|
+
|
|
79
|
+
axis: str | None = None
|
|
80
|
+
"""
|
|
81
|
+
The name of the axis on which to calculate the spectrum.
|
|
82
|
+
Note: The axis must have an .axes entry of type LinearAxis, not CoordinateAxis.
|
|
80
83
|
"""
|
|
81
|
-
Calculate a spectrum on a data slice.
|
|
82
84
|
|
|
83
|
-
|
|
84
|
-
axis: The name of the axis on which to calculate the spectrum.
|
|
85
|
-
Note: The axis must have an .axes entry of type LinearAxis, not CoordinateAxis.
|
|
86
|
-
out_axis: The name of the new axis. Defaults to "freq".
|
|
87
|
-
window: The :obj:`WindowFunction` to apply to the data slice prior to calculating the spectrum.
|
|
88
|
-
transform: The :obj:`SpectralTransform` to apply to the spectral magnitude.
|
|
89
|
-
output: The :obj:`SpectralOutput` format.
|
|
90
|
-
norm: Normalization mode. Default "forward" is best used when the inverse transform is not needed,
|
|
91
|
-
for example when the goal is to get spectral power. Use "backward" (equivalent to None) to not
|
|
92
|
-
scale the spectrum which is useful when the spectra will be manipulated and possibly inverse-transformed.
|
|
93
|
-
See numpy.fft.fft for details.
|
|
94
|
-
do_fftshift: Whether to apply fftshift to the output. Default is True. This value is ignored unless
|
|
95
|
-
output is SpectralOutput.FULL.
|
|
96
|
-
nfft: The number of points to use for the FFT. If None, the length of the input data is used.
|
|
85
|
+
# n: int | None = None # n parameter for fft
|
|
97
86
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
87
|
+
out_axis: str | None = "freq"
|
|
88
|
+
"""The name of the new axis. Defaults to "freq". If none; don't change dim name"""
|
|
89
|
+
|
|
90
|
+
window: WindowFunction = WindowFunction.HAMMING
|
|
91
|
+
"""The :obj:`WindowFunction` to apply to the data slice prior to calculating the spectrum."""
|
|
92
|
+
|
|
93
|
+
transform: SpectralTransform = SpectralTransform.REL_DB
|
|
94
|
+
"""The :obj:`SpectralTransform` to apply to the spectral magnitude."""
|
|
95
|
+
|
|
96
|
+
output: SpectralOutput = SpectralOutput.POSITIVE
|
|
97
|
+
"""The :obj:`SpectralOutput` format."""
|
|
98
|
+
|
|
99
|
+
norm: str | None = "forward"
|
|
100
|
+
"""
|
|
101
|
+
Normalization mode. Default "forward" is best used when the inverse transform is not needed,
|
|
102
|
+
for example when the goal is to get spectral power. Use "backward" (equivalent to None) to not
|
|
103
|
+
scale the spectrum which is useful when the spectra will be manipulated and possibly inverse-transformed.
|
|
104
|
+
See numpy.fft.fft for details.
|
|
105
|
+
"""
|
|
106
|
+
|
|
107
|
+
do_fftshift: bool = True
|
|
101
108
|
"""
|
|
102
|
-
|
|
109
|
+
Whether to apply fftshift to the output. Default is True.
|
|
110
|
+
This value is ignored unless output is SpectralOutput.FULL.
|
|
111
|
+
"""
|
|
112
|
+
|
|
113
|
+
nfft: int | None = None
|
|
114
|
+
"""
|
|
115
|
+
The number of points to use for the FFT. If None, the length of the input data is used.
|
|
116
|
+
"""
|
|
117
|
+
|
|
103
118
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
119
|
+
@processor_state
|
|
120
|
+
class SpectrumState:
|
|
121
|
+
f_sl: slice | None = None
|
|
122
|
+
# I would prefer `slice(None)` as f_sl default but this fails because it is mutable.
|
|
108
123
|
freq_axis: AxisArray.LinearAxis | None = None
|
|
109
124
|
fftfun: typing.Callable | None = None
|
|
110
125
|
f_transform: typing.Callable | None = None
|
|
111
126
|
new_dims: list[str] | None = None
|
|
127
|
+
window: npt.NDArray | None = None
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
class SpectrumTransformer(
|
|
131
|
+
BaseStatefulTransformer[SpectrumSettings, AxisArray, AxisArray, SpectrumState]
|
|
132
|
+
):
|
|
133
|
+
def _hash_message(self, message: AxisArray) -> int:
|
|
134
|
+
axis = self.settings.axis or message.dims[0]
|
|
135
|
+
ax_idx = message.get_axis_idx(axis)
|
|
136
|
+
ax_info = message.axes[axis]
|
|
137
|
+
targ_len = message.data.shape[ax_idx]
|
|
138
|
+
return hash(
|
|
139
|
+
(targ_len, message.data.ndim, message.data.dtype.kind, ax_idx, ax_info.gain)
|
|
140
|
+
)
|
|
112
141
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
#
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
# Pre-calculate windowing
|
|
148
|
-
window = WINDOWS[window](targ_len)
|
|
149
|
-
window = window.reshape(
|
|
150
|
-
[1] * ax_idx
|
|
151
|
-
+ [
|
|
152
|
-
len(window),
|
|
153
|
-
]
|
|
154
|
-
+ [1] * (msg_in.data.ndim - 1 - ax_idx)
|
|
155
|
-
)
|
|
156
|
-
if transform != SpectralTransform.RAW_COMPLEX and not (
|
|
157
|
-
transform == SpectralTransform.REAL
|
|
158
|
-
or transform == SpectralTransform.IMAG
|
|
159
|
-
):
|
|
160
|
-
scale = np.sum(window**2.0) * ax_info.gain
|
|
161
|
-
|
|
162
|
-
# Pre-calculate frequencies and select our fft function.
|
|
163
|
-
b_complex = msg_in.data.dtype.kind == "c"
|
|
164
|
-
if (not b_complex) and output == SpectralOutput.POSITIVE:
|
|
165
|
-
# If input is not complex and desired output is SpectralOutput.POSITIVE, we can save some computation
|
|
166
|
-
# by using rfft and rfftfreq.
|
|
167
|
-
fftfun = partial(np.fft.rfft, n=nfft, axis=ax_idx, norm=norm)
|
|
168
|
-
freqs = np.fft.rfftfreq(nfft, d=ax_info.gain * targ_len / nfft)
|
|
169
|
-
else:
|
|
170
|
-
fftfun = partial(np.fft.fft, n=nfft, axis=ax_idx, norm=norm)
|
|
171
|
-
freqs = np.fft.fftfreq(nfft, d=ax_info.gain * targ_len / nfft)
|
|
172
|
-
if output == SpectralOutput.POSITIVE:
|
|
173
|
-
f_sl = slice(None, nfft // 2 + 1 - (nfft % 2))
|
|
174
|
-
elif output == SpectralOutput.NEGATIVE:
|
|
175
|
-
freqs = np.fft.fftshift(freqs, axes=-1)
|
|
176
|
-
f_sl = slice(None, nfft // 2 + 1)
|
|
177
|
-
elif do_fftshift: # and FULL
|
|
178
|
-
freqs = np.fft.fftshift(freqs, axes=-1)
|
|
179
|
-
freqs = freqs[f_sl]
|
|
180
|
-
freqs = freqs.tolist() # To please type checking
|
|
181
|
-
freq_axis = AxisArray.LinearAxis(
|
|
182
|
-
unit="Hz", gain=freqs[1] - freqs[0], offset=freqs[0]
|
|
142
|
+
def _reset_state(self, message: AxisArray) -> None:
|
|
143
|
+
axis = self.settings.axis or message.dims[0]
|
|
144
|
+
ax_idx = message.get_axis_idx(axis)
|
|
145
|
+
ax_info = message.axes[axis]
|
|
146
|
+
targ_len = message.data.shape[ax_idx]
|
|
147
|
+
nfft = self.settings.nfft or targ_len
|
|
148
|
+
|
|
149
|
+
# Pre-calculate windowing
|
|
150
|
+
window = WINDOWS[self.settings.window](targ_len)
|
|
151
|
+
window = window.reshape(
|
|
152
|
+
[1] * ax_idx
|
|
153
|
+
+ [
|
|
154
|
+
len(window),
|
|
155
|
+
]
|
|
156
|
+
+ [1] * (message.data.ndim - 1 - ax_idx)
|
|
157
|
+
)
|
|
158
|
+
if self.settings.transform != SpectralTransform.RAW_COMPLEX and not (
|
|
159
|
+
self.settings.transform == SpectralTransform.REAL
|
|
160
|
+
or self.settings.transform == SpectralTransform.IMAG
|
|
161
|
+
):
|
|
162
|
+
scale = np.sum(window**2.0) * ax_info.gain
|
|
163
|
+
|
|
164
|
+
if self.settings.window != WindowFunction.NONE:
|
|
165
|
+
self.state.window = window
|
|
166
|
+
|
|
167
|
+
# Pre-calculate frequencies and select our fft function.
|
|
168
|
+
b_complex = message.data.dtype.kind == "c"
|
|
169
|
+
self.state.f_sl = slice(None)
|
|
170
|
+
if (not b_complex) and self.settings.output == SpectralOutput.POSITIVE:
|
|
171
|
+
# If input is not complex and desired output is SpectralOutput.POSITIVE, we can save some computation
|
|
172
|
+
# by using rfft and rfftfreq.
|
|
173
|
+
self.state.fftfun = partial(
|
|
174
|
+
np.fft.rfft, n=nfft, axis=ax_idx, norm=self.settings.norm
|
|
183
175
|
)
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
+ [
|
|
189
|
-
out_axis,
|
|
190
|
-
]
|
|
191
|
-
+ msg_in.dims[ax_idx + 1 :]
|
|
176
|
+
freqs = np.fft.rfftfreq(nfft, d=ax_info.gain * targ_len / nfft)
|
|
177
|
+
else:
|
|
178
|
+
self.state.fftfun = partial(
|
|
179
|
+
np.fft.fft, n=nfft, axis=ax_idx, norm=self.settings.norm
|
|
192
180
|
)
|
|
181
|
+
freqs = np.fft.fftfreq(nfft, d=ax_info.gain * targ_len / nfft)
|
|
182
|
+
if self.settings.output == SpectralOutput.POSITIVE:
|
|
183
|
+
self.state.f_sl = slice(None, nfft // 2 + 1 - (nfft % 2))
|
|
184
|
+
elif self.settings.output == SpectralOutput.NEGATIVE:
|
|
185
|
+
freqs = np.fft.fftshift(freqs, axes=-1)
|
|
186
|
+
self.state.f_sl = slice(None, nfft // 2 + 1)
|
|
187
|
+
elif (
|
|
188
|
+
self.settings.do_fftshift
|
|
189
|
+
and self.settings.output == SpectralOutput.FULL
|
|
190
|
+
):
|
|
191
|
+
freqs = np.fft.fftshift(freqs, axes=-1)
|
|
192
|
+
freqs = freqs[self.state.f_sl]
|
|
193
|
+
freqs = freqs.tolist() # To please type checking
|
|
194
|
+
self.state.freq_axis = AxisArray.LinearAxis(
|
|
195
|
+
unit="Hz", gain=freqs[1] - freqs[0], offset=freqs[0]
|
|
196
|
+
)
|
|
197
|
+
self.state.new_dims = (
|
|
198
|
+
message.dims[:ax_idx]
|
|
199
|
+
+ [
|
|
200
|
+
self.settings.out_axis or axis,
|
|
201
|
+
]
|
|
202
|
+
+ message.dims[ax_idx + 1 :]
|
|
203
|
+
)
|
|
193
204
|
|
|
194
|
-
|
|
195
|
-
|
|
205
|
+
def f_transform(x):
|
|
206
|
+
return x
|
|
196
207
|
|
|
197
|
-
|
|
198
|
-
|
|
208
|
+
if self.settings.transform != SpectralTransform.RAW_COMPLEX:
|
|
209
|
+
if self.settings.transform == SpectralTransform.REAL:
|
|
199
210
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
211
|
+
def f_transform(x):
|
|
212
|
+
return x.real
|
|
213
|
+
elif self.settings.transform == SpectralTransform.IMAG:
|
|
203
214
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
def f1(x):
|
|
209
|
-
return (np.abs(x) ** 2.0) / scale
|
|
210
|
-
|
|
211
|
-
if transform == SpectralTransform.REL_DB:
|
|
215
|
+
def f_transform(x):
|
|
216
|
+
return x.imag
|
|
217
|
+
else:
|
|
212
218
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
else:
|
|
216
|
-
f_transform = f1
|
|
219
|
+
def f1(x):
|
|
220
|
+
return (np.abs(x) ** 2.0) / scale
|
|
217
221
|
|
|
218
|
-
|
|
219
|
-
new_axes[out_axis] = freq_axis
|
|
222
|
+
if self.settings.transform == SpectralTransform.REL_DB:
|
|
220
223
|
|
|
221
|
-
|
|
222
|
-
|
|
224
|
+
def f_transform(x):
|
|
225
|
+
return 10 * np.log10(f1(x))
|
|
226
|
+
else:
|
|
227
|
+
f_transform = f1
|
|
228
|
+
self.state.f_transform = f_transform
|
|
229
|
+
|
|
230
|
+
def _process(self, message: AxisArray) -> AxisArray:
|
|
231
|
+
axis = self.settings.axis or message.dims[0]
|
|
232
|
+
ax_idx = message.get_axis_idx(axis)
|
|
233
|
+
targ_len = message.data.shape[ax_idx]
|
|
234
|
+
|
|
235
|
+
new_axes = {
|
|
236
|
+
k: v
|
|
237
|
+
for k, v in message.axes.items()
|
|
238
|
+
if k not in [self.settings.out_axis, axis]
|
|
239
|
+
}
|
|
240
|
+
new_axes[self.settings.out_axis or axis] = self.state.freq_axis
|
|
241
|
+
|
|
242
|
+
if self.state.window is not None:
|
|
243
|
+
win_dat = message.data * self.state.window
|
|
223
244
|
else:
|
|
224
|
-
win_dat =
|
|
225
|
-
spec = fftfun(
|
|
245
|
+
win_dat = message.data
|
|
246
|
+
spec = self.state.fftfun(
|
|
247
|
+
win_dat,
|
|
248
|
+
n=self.settings.nfft or targ_len,
|
|
249
|
+
axis=ax_idx,
|
|
250
|
+
norm=self.settings.norm,
|
|
251
|
+
)
|
|
226
252
|
# Note: norm="forward" equivalent to `/ nfft`
|
|
227
|
-
if
|
|
253
|
+
if (
|
|
254
|
+
self.settings.do_fftshift and self.settings.output == SpectralOutput.FULL
|
|
255
|
+
) or self.settings.output == SpectralOutput.NEGATIVE:
|
|
228
256
|
spec = np.fft.fftshift(spec, axes=ax_idx)
|
|
229
|
-
spec = f_transform(spec)
|
|
230
|
-
spec = slice_along_axis(spec, f_sl, ax_idx)
|
|
231
|
-
|
|
232
|
-
msg_out = replace(msg_in, data=spec, dims=new_dims, axes=new_axes)
|
|
257
|
+
spec = self.state.f_transform(spec)
|
|
258
|
+
spec = slice_along_axis(spec, self.state.f_sl, ax_idx)
|
|
233
259
|
|
|
260
|
+
msg_out = replace(message, data=spec, dims=self.state.new_dims, axes=new_axes)
|
|
261
|
+
return msg_out
|
|
234
262
|
|
|
235
|
-
class SpectrumSettings(ez.Settings):
|
|
236
|
-
"""
|
|
237
|
-
Settings for :obj:`Spectrum.
|
|
238
|
-
See :obj:`spectrum` for a description of the parameters.
|
|
239
|
-
"""
|
|
240
|
-
|
|
241
|
-
axis: str | None = None
|
|
242
|
-
# n: int | None = None # n parameter for fft
|
|
243
|
-
out_axis: str | None = "freq" # If none; don't change dim name
|
|
244
|
-
window: WindowFunction = WindowFunction.HAMMING
|
|
245
|
-
transform: SpectralTransform = SpectralTransform.REL_DB
|
|
246
|
-
output: SpectralOutput = SpectralOutput.POSITIVE
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
class Spectrum(GenAxisArray):
|
|
250
|
-
"""Unit for :obj:`spectrum`"""
|
|
251
263
|
|
|
264
|
+
class Spectrum(
|
|
265
|
+
BaseTransformerUnit[SpectrumSettings, AxisArray, AxisArray, SpectrumTransformer]
|
|
266
|
+
):
|
|
252
267
|
SETTINGS = SpectrumSettings
|
|
253
268
|
|
|
254
|
-
INPUT_SETTINGS = ez.InputStream(SpectrumSettings)
|
|
255
269
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
270
|
+
def spectrum(
|
|
271
|
+
axis: str | None = None,
|
|
272
|
+
out_axis: str | None = "freq",
|
|
273
|
+
window: WindowFunction = WindowFunction.HANNING,
|
|
274
|
+
transform: SpectralTransform = SpectralTransform.REL_DB,
|
|
275
|
+
output: SpectralOutput = SpectralOutput.POSITIVE,
|
|
276
|
+
norm: str | None = "forward",
|
|
277
|
+
do_fftshift: bool = True,
|
|
278
|
+
nfft: int | None = None,
|
|
279
|
+
) -> SpectrumTransformer:
|
|
280
|
+
"""
|
|
281
|
+
Calculate a spectrum on a data slice.
|
|
282
|
+
|
|
283
|
+
Returns:
|
|
284
|
+
A :obj:`SpectrumTransformer` object that expects an :obj:`AxisArray` via `.(axis_array)` (__call__)
|
|
285
|
+
containing continuous data and returns an :obj:`AxisArray` with data of spectral magnitudes or powers.
|
|
286
|
+
"""
|
|
287
|
+
return SpectrumTransformer(
|
|
288
|
+
SpectrumSettings(
|
|
289
|
+
axis=axis,
|
|
290
|
+
out_axis=out_axis,
|
|
291
|
+
window=window,
|
|
292
|
+
transform=transform,
|
|
293
|
+
output=output,
|
|
294
|
+
norm=norm,
|
|
295
|
+
do_fftshift=do_fftshift,
|
|
296
|
+
nfft=nfft,
|
|
263
297
|
)
|
|
298
|
+
)
|