ezmsg-sigproc 1.5.0__py3-none-any.whl → 1.6.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 +3 -2
- ezmsg/sigproc/affinetransform.py +9 -8
- ezmsg/sigproc/aggregate.py +7 -7
- ezmsg/sigproc/bandpower.py +2 -2
- ezmsg/sigproc/butterworthfilter.py +88 -90
- ezmsg/sigproc/cheby.py +119 -0
- ezmsg/sigproc/decimate.py +11 -15
- ezmsg/sigproc/downsample.py +2 -2
- ezmsg/sigproc/ewmfilter.py +10 -5
- ezmsg/sigproc/filter.py +82 -115
- ezmsg/sigproc/filterbank.py +6 -5
- ezmsg/sigproc/math/abs.py +2 -1
- ezmsg/sigproc/math/clip.py +2 -1
- ezmsg/sigproc/math/difference.py +2 -1
- ezmsg/sigproc/math/invert.py +2 -1
- ezmsg/sigproc/math/log.py +2 -1
- ezmsg/sigproc/math/scale.py +2 -1
- ezmsg/sigproc/messages.py +1 -2
- ezmsg/sigproc/sampler.py +10 -14
- ezmsg/sigproc/scaler.py +153 -35
- ezmsg/sigproc/signalinjector.py +8 -7
- ezmsg/sigproc/slicer.py +6 -6
- ezmsg/sigproc/spectrogram.py +6 -6
- ezmsg/sigproc/spectrum.py +11 -11
- ezmsg/sigproc/synth.py +24 -23
- ezmsg/sigproc/wavelets.py +39 -15
- ezmsg/sigproc/window.py +12 -12
- {ezmsg_sigproc-1.5.0.dist-info → ezmsg_sigproc-1.6.0.dist-info}/METADATA +2 -2
- ezmsg_sigproc-1.6.0.dist-info/RECORD +36 -0
- ezmsg_sigproc-1.5.0.dist-info/RECORD +0 -35
- {ezmsg_sigproc-1.5.0.dist-info → ezmsg_sigproc-1.6.0.dist-info}/WHEEL +0 -0
- {ezmsg_sigproc-1.5.0.dist-info → ezmsg_sigproc-1.6.0.dist-info}/licenses/LICENSE.txt +0 -0
ezmsg/sigproc/__version__.py
CHANGED
ezmsg/sigproc/activation.py
CHANGED
|
@@ -3,7 +3,8 @@ import typing
|
|
|
3
3
|
import numpy as np
|
|
4
4
|
import scipy.special
|
|
5
5
|
import ezmsg.core as ez
|
|
6
|
-
from ezmsg.util.messages.axisarray import AxisArray
|
|
6
|
+
from ezmsg.util.messages.axisarray import AxisArray
|
|
7
|
+
from ezmsg.util.messages.util import replace
|
|
7
8
|
from ezmsg.util.generator import consumer
|
|
8
9
|
|
|
9
10
|
from .spectral import OptionsEnum
|
|
@@ -40,7 +41,7 @@ ACTIVATIONS = {
|
|
|
40
41
|
|
|
41
42
|
@consumer
|
|
42
43
|
def activation(
|
|
43
|
-
function:
|
|
44
|
+
function: str | ActivationFunction,
|
|
44
45
|
) -> typing.Generator[AxisArray, AxisArray, None]:
|
|
45
46
|
"""
|
|
46
47
|
Transform the data with a simple activation function.
|
ezmsg/sigproc/affinetransform.py
CHANGED
|
@@ -5,7 +5,8 @@ import typing
|
|
|
5
5
|
import numpy as np
|
|
6
6
|
import numpy.typing as npt
|
|
7
7
|
import ezmsg.core as ez
|
|
8
|
-
from ezmsg.util.messages.axisarray import AxisArray, AxisBase
|
|
8
|
+
from ezmsg.util.messages.axisarray import AxisArray, AxisBase
|
|
9
|
+
from ezmsg.util.messages.util import replace
|
|
9
10
|
from ezmsg.util.generator import consumer
|
|
10
11
|
|
|
11
12
|
from .base import GenAxisArray
|
|
@@ -13,8 +14,8 @@ from .base import GenAxisArray
|
|
|
13
14
|
|
|
14
15
|
@consumer
|
|
15
16
|
def affine_transform(
|
|
16
|
-
weights:
|
|
17
|
-
axis:
|
|
17
|
+
weights: np.ndarray | str | Path,
|
|
18
|
+
axis: str | None = None,
|
|
18
19
|
right_multiply: bool = True,
|
|
19
20
|
) -> typing.Generator[AxisArray, AxisArray, None]:
|
|
20
21
|
"""
|
|
@@ -46,7 +47,7 @@ def affine_transform(
|
|
|
46
47
|
|
|
47
48
|
# State variables
|
|
48
49
|
# New axis with transformed labels, if required
|
|
49
|
-
new_axis:
|
|
50
|
+
new_axis: AxisBase | None = None
|
|
50
51
|
|
|
51
52
|
# Reset if any of these change.
|
|
52
53
|
check_input = {"key": None}
|
|
@@ -132,8 +133,8 @@ class AffineTransformSettings(ez.Settings):
|
|
|
132
133
|
See :obj:`affine_transform` for argument details.
|
|
133
134
|
"""
|
|
134
135
|
|
|
135
|
-
weights:
|
|
136
|
-
axis:
|
|
136
|
+
weights: np.ndarray | str | Path
|
|
137
|
+
axis: str | None = None
|
|
137
138
|
right_multiply: bool = True
|
|
138
139
|
|
|
139
140
|
|
|
@@ -156,7 +157,7 @@ def zeros_for_noop(data: npt.NDArray, **ignore_kwargs) -> npt.NDArray:
|
|
|
156
157
|
|
|
157
158
|
@consumer
|
|
158
159
|
def common_rereference(
|
|
159
|
-
mode: str = "mean", axis:
|
|
160
|
+
mode: str = "mean", axis: str | None = None, include_current: bool = True
|
|
160
161
|
) -> typing.Generator[AxisArray, AxisArray, None]:
|
|
161
162
|
"""
|
|
162
163
|
Perform common average referencing (CAR) on streaming data.
|
|
@@ -213,7 +214,7 @@ class CommonRereferenceSettings(ez.Settings):
|
|
|
213
214
|
"""
|
|
214
215
|
|
|
215
216
|
mode: str = "mean"
|
|
216
|
-
axis:
|
|
217
|
+
axis: str | None = None
|
|
217
218
|
include_current: bool = True
|
|
218
219
|
|
|
219
220
|
|
ezmsg/sigproc/aggregate.py
CHANGED
|
@@ -56,8 +56,8 @@ AGGREGATORS = {
|
|
|
56
56
|
|
|
57
57
|
@consumer
|
|
58
58
|
def ranged_aggregate(
|
|
59
|
-
axis:
|
|
60
|
-
bands:
|
|
59
|
+
axis: str | None = None,
|
|
60
|
+
bands: list[tuple[float, float]] | None = None,
|
|
61
61
|
operation: AggregationFunction = AggregationFunction.MEAN,
|
|
62
62
|
):
|
|
63
63
|
"""
|
|
@@ -75,9 +75,9 @@ def ranged_aggregate(
|
|
|
75
75
|
msg_out = AxisArray(np.array([]), dims=[""])
|
|
76
76
|
|
|
77
77
|
# State variables
|
|
78
|
-
slices:
|
|
79
|
-
out_axis:
|
|
80
|
-
ax_vec:
|
|
78
|
+
slices: list[tuple[typing.Any, ...]] | None = None
|
|
79
|
+
out_axis: AxisBase | None = None
|
|
80
|
+
ax_vec: npt.NDArray | None = None
|
|
81
81
|
|
|
82
82
|
# Reset if any of these changes. Key not checked because continuity between chunks not required.
|
|
83
83
|
check_inputs = {"gain": None, "offset": None, "len": None, "key": None}
|
|
@@ -163,8 +163,8 @@ class RangedAggregateSettings(ez.Settings):
|
|
|
163
163
|
See :obj:`ranged_aggregate` for details.
|
|
164
164
|
"""
|
|
165
165
|
|
|
166
|
-
axis:
|
|
167
|
-
bands:
|
|
166
|
+
axis: str | None = None
|
|
167
|
+
bands: list[tuple[float, float]] | None = None
|
|
168
168
|
operation: AggregationFunction = AggregationFunction.MEAN
|
|
169
169
|
|
|
170
170
|
|
ezmsg/sigproc/bandpower.py
CHANGED
|
@@ -14,7 +14,7 @@ from .base import GenAxisArray
|
|
|
14
14
|
@consumer
|
|
15
15
|
def bandpower(
|
|
16
16
|
spectrogram_settings: SpectrogramSettings,
|
|
17
|
-
bands:
|
|
17
|
+
bands: list[tuple[float, float]] | None = [
|
|
18
18
|
(17, 30),
|
|
19
19
|
(70, 170),
|
|
20
20
|
],
|
|
@@ -58,7 +58,7 @@ class BandPowerSettings(ez.Settings):
|
|
|
58
58
|
spectrogram_settings: SpectrogramSettings = field(
|
|
59
59
|
default_factory=SpectrogramSettings
|
|
60
60
|
)
|
|
61
|
-
bands:
|
|
61
|
+
bands: list[tuple[float, float]] | None = field(
|
|
62
62
|
default_factory=lambda: [(17, 30), (70, 170)]
|
|
63
63
|
)
|
|
64
64
|
|
|
@@ -1,15 +1,19 @@
|
|
|
1
|
+
import functools
|
|
1
2
|
import typing
|
|
2
3
|
|
|
3
|
-
import ezmsg.core as ez
|
|
4
4
|
import scipy.signal
|
|
5
|
-
import numpy as np
|
|
6
5
|
from ezmsg.util.messages.axisarray import AxisArray
|
|
7
|
-
from
|
|
6
|
+
from scipy.signal import normalize
|
|
8
7
|
|
|
9
|
-
from .filter import
|
|
8
|
+
from .filter import (
|
|
9
|
+
FilterBaseSettings,
|
|
10
|
+
FilterCoefsMultiType,
|
|
11
|
+
FilterBase,
|
|
12
|
+
filter_gen_by_design,
|
|
13
|
+
)
|
|
10
14
|
|
|
11
15
|
|
|
12
|
-
class ButterworthFilterSettings(
|
|
16
|
+
class ButterworthFilterSettings(FilterBaseSettings):
|
|
13
17
|
"""Settings for :obj:`ButterworthFilter`."""
|
|
14
18
|
|
|
15
19
|
order: int = 0
|
|
@@ -17,25 +21,28 @@ class ButterworthFilterSettings(FilterSettingsBase):
|
|
|
17
21
|
Filter order
|
|
18
22
|
"""
|
|
19
23
|
|
|
20
|
-
cuton:
|
|
24
|
+
cuton: float | None = None
|
|
21
25
|
"""
|
|
22
26
|
Cuton frequency (Hz). If `cutoff` is not specified then this is the highpass corner. Otherwise,
|
|
23
27
|
if this is lower than `cutoff` then this is the beginning of the bandpass
|
|
24
28
|
or if this is greater than `cutoff` then this is the end of the bandstop.
|
|
25
29
|
"""
|
|
26
30
|
|
|
27
|
-
cutoff:
|
|
31
|
+
cutoff: float | None = None
|
|
28
32
|
"""
|
|
29
33
|
Cutoff frequency (Hz). If `cuton` is not specified then this is the lowpass corner. Otherwise,
|
|
30
34
|
if this is greater than `cuton` then this is the end of the bandpass,
|
|
31
35
|
or if this is less than `cuton` then this is the beginning of the bandstop.
|
|
32
36
|
"""
|
|
33
37
|
|
|
38
|
+
wn_hz: bool = True
|
|
39
|
+
"""
|
|
40
|
+
Set False if provided Wn are normalized from 0 to 1, where 1 is the Nyquist frequency
|
|
41
|
+
"""
|
|
42
|
+
|
|
34
43
|
def filter_specs(
|
|
35
44
|
self,
|
|
36
|
-
) ->
|
|
37
|
-
typing.Tuple[str, typing.Union[float, typing.Tuple[float, float]]]
|
|
38
|
-
]:
|
|
45
|
+
) -> tuple[str, float | tuple[float, float]] | None:
|
|
39
46
|
"""
|
|
40
47
|
Determine the filter type given the corner frequencies.
|
|
41
48
|
|
|
@@ -58,15 +65,74 @@ class ButterworthFilterSettings(FilterSettingsBase):
|
|
|
58
65
|
return "bandstop", (self.cutoff, self.cuton)
|
|
59
66
|
|
|
60
67
|
|
|
61
|
-
|
|
68
|
+
def butter_design_fun(
|
|
69
|
+
fs: float,
|
|
70
|
+
order: int = 0,
|
|
71
|
+
cuton: float | None = None,
|
|
72
|
+
cutoff: float | None = None,
|
|
73
|
+
coef_type: str = "ba",
|
|
74
|
+
wn_hz: bool = True,
|
|
75
|
+
) -> FilterCoefsMultiType | None:
|
|
76
|
+
"""
|
|
77
|
+
See :obj:`ButterworthFilterSettings.filter_specs` for an explanation of specifying different
|
|
78
|
+
filter types (lowpass, highpass, bandpass, bandstop) from the parameters.
|
|
79
|
+
You are likely to want to use this function with :obj:`filter_by_design`, which only passes `fs` to the design
|
|
80
|
+
function (this), meaning that you should wrap this function with a lambda or prepare with functools.partial.
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
fs: The sampling frequency of the data in Hz.
|
|
84
|
+
order: Filter order.
|
|
85
|
+
cuton: Corner frequency of the filter in Hz.
|
|
86
|
+
cutoff: Corner frequency of the filter in Hz.
|
|
87
|
+
coef_type: "ba", "sos", or "zpk"
|
|
88
|
+
wn_hz: Set False if provided Wn are normalized from 0 to 1, where 1 is the Nyquist frequency
|
|
89
|
+
|
|
90
|
+
Returns:
|
|
91
|
+
The filter coefficients as a tuple of (b, a) for coef_type "ba", or as a single ndarray for "sos",
|
|
92
|
+
or (z, p, k) for "zpk".
|
|
93
|
+
|
|
94
|
+
"""
|
|
95
|
+
coefs = None
|
|
96
|
+
if order > 0:
|
|
97
|
+
btype, cutoffs = ButterworthFilterSettings(
|
|
98
|
+
order=order, cuton=cuton, cutoff=cutoff
|
|
99
|
+
).filter_specs()
|
|
100
|
+
coefs = scipy.signal.butter(
|
|
101
|
+
order,
|
|
102
|
+
Wn=cutoffs,
|
|
103
|
+
btype=btype,
|
|
104
|
+
fs=fs if wn_hz else None,
|
|
105
|
+
output=coef_type,
|
|
106
|
+
)
|
|
107
|
+
if coefs is not None and coef_type == "ba":
|
|
108
|
+
coefs = normalize(*coefs)
|
|
109
|
+
return coefs
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
class ButterworthFilter(FilterBase):
|
|
113
|
+
SETTINGS = ButterworthFilterSettings
|
|
114
|
+
|
|
115
|
+
def design_filter(
|
|
116
|
+
self,
|
|
117
|
+
) -> typing.Callable[[float], FilterCoefsMultiType | None]:
|
|
118
|
+
return functools.partial(
|
|
119
|
+
butter_design_fun,
|
|
120
|
+
order=self.SETTINGS.order,
|
|
121
|
+
cuton=self.SETTINGS.cuton,
|
|
122
|
+
cutoff=self.SETTINGS.cutoff,
|
|
123
|
+
coef_type=self.SETTINGS.coef_type,
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
|
|
62
127
|
def butter(
|
|
63
|
-
axis:
|
|
128
|
+
axis: str | None,
|
|
64
129
|
order: int = 0,
|
|
65
|
-
cuton:
|
|
66
|
-
cutoff:
|
|
130
|
+
cuton: float | None = None,
|
|
131
|
+
cutoff: float | None = None,
|
|
67
132
|
coef_type: str = "ba",
|
|
68
133
|
) -> typing.Generator[AxisArray, AxisArray, None]:
|
|
69
134
|
"""
|
|
135
|
+
Convenience generator wrapping filter_gen_by_design for Butterworth filters.
|
|
70
136
|
Apply Butterworth filter to streaming data. Uses :obj:`scipy.signal.butter` to design the filter.
|
|
71
137
|
See :obj:`ButterworthFilterSettings.filter_specs` for an explanation of specifying different
|
|
72
138
|
filter types (lowpass, highpass, bandpass, bandstop) from the parameters.
|
|
@@ -84,79 +150,11 @@ def butter(
|
|
|
84
150
|
and yields an :obj:`AxisArray` with filtered data.
|
|
85
151
|
|
|
86
152
|
"""
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
)
|
|
94
|
-
|
|
95
|
-
# State variables
|
|
96
|
-
# Initialize filtergen as passthrough until we can calculate coefs.
|
|
97
|
-
filter_gen = filtergen(axis, None, coef_type)
|
|
98
|
-
|
|
99
|
-
# Reset if these change.
|
|
100
|
-
check_input = {"gain": None}
|
|
101
|
-
# Key not checked because filter_gen will handle resetting if .key changes.
|
|
102
|
-
|
|
103
|
-
while True:
|
|
104
|
-
msg_in: AxisArray = yield msg_out
|
|
105
|
-
axis = axis or msg_in.dims[0]
|
|
106
|
-
|
|
107
|
-
b_reset = msg_in.axes[axis].gain != check_input["gain"]
|
|
108
|
-
b_reset = b_reset and order > 0 # Not passthrough
|
|
109
|
-
if b_reset:
|
|
110
|
-
check_input["gain"] = msg_in.axes[axis].gain
|
|
111
|
-
coefs = scipy.signal.butter(
|
|
112
|
-
order,
|
|
113
|
-
Wn=cutoffs,
|
|
114
|
-
btype=btype,
|
|
115
|
-
fs=1 / msg_in.axes[axis].gain,
|
|
116
|
-
output=coef_type,
|
|
117
|
-
)
|
|
118
|
-
filter_gen = filtergen(axis, coefs, coef_type)
|
|
119
|
-
|
|
120
|
-
msg_out = filter_gen.send(msg_in)
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
class ButterworthFilterState(FilterState):
|
|
124
|
-
design: ButterworthFilterSettings
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
class ButterworthFilter(Filter):
|
|
128
|
-
""":obj:`Unit` for :obj:`butterworth`"""
|
|
129
|
-
|
|
130
|
-
SETTINGS = ButterworthFilterSettings
|
|
131
|
-
STATE = ButterworthFilterState
|
|
132
|
-
|
|
133
|
-
INPUT_FILTER = ez.InputStream(ButterworthFilterSettings)
|
|
134
|
-
|
|
135
|
-
async def initialize(self) -> None:
|
|
136
|
-
self.STATE.design = self.SETTINGS
|
|
137
|
-
self.STATE.filt_designed = True
|
|
138
|
-
await super().initialize()
|
|
139
|
-
|
|
140
|
-
def design_filter(self) -> typing.Optional[typing.Tuple[np.ndarray, np.ndarray]]:
|
|
141
|
-
specs = self.STATE.design.filter_specs()
|
|
142
|
-
if self.STATE.design.order > 0 and specs is not None:
|
|
143
|
-
btype, cut = specs
|
|
144
|
-
return scipy.signal.butter(
|
|
145
|
-
self.STATE.design.order,
|
|
146
|
-
Wn=cut,
|
|
147
|
-
btype=btype,
|
|
148
|
-
fs=self.STATE.fs,
|
|
149
|
-
output="ba",
|
|
150
|
-
)
|
|
151
|
-
else:
|
|
152
|
-
return None
|
|
153
|
-
|
|
154
|
-
@ez.subscriber(INPUT_FILTER)
|
|
155
|
-
async def redesign(self, message: ButterworthFilterSettings) -> None:
|
|
156
|
-
if type(message) is not ButterworthFilterSettings:
|
|
157
|
-
return
|
|
158
|
-
|
|
159
|
-
if self.STATE.design.order != message.order:
|
|
160
|
-
self.STATE.zi = None
|
|
161
|
-
self.STATE.design = message
|
|
162
|
-
self.update_filter()
|
|
153
|
+
design_fun = functools.partial(
|
|
154
|
+
butter_design_fun,
|
|
155
|
+
order=order,
|
|
156
|
+
cuton=cuton,
|
|
157
|
+
cutoff=cutoff,
|
|
158
|
+
coef_type=coef_type,
|
|
159
|
+
)
|
|
160
|
+
return filter_gen_by_design(axis, coef_type, design_fun)
|
ezmsg/sigproc/cheby.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import functools
|
|
2
|
+
import typing
|
|
3
|
+
|
|
4
|
+
import scipy.signal
|
|
5
|
+
from scipy.signal import normalize
|
|
6
|
+
|
|
7
|
+
from .filter import (
|
|
8
|
+
FilterBaseSettings,
|
|
9
|
+
FilterCoefsMultiType,
|
|
10
|
+
FilterBase,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ChebyshevFilterSettings(FilterBaseSettings):
|
|
15
|
+
"""Settings for :obj:`ButterworthFilter`."""
|
|
16
|
+
|
|
17
|
+
order: int = 0
|
|
18
|
+
"""
|
|
19
|
+
Filter order
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
ripple_tol: float | None = None
|
|
23
|
+
"""
|
|
24
|
+
The maximum ripple allowed below unity gain in the passband. Specified in decibels, as a positive number.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
Wn: float | tuple[float, float] | None = None
|
|
28
|
+
"""
|
|
29
|
+
A scalar or length-2 sequence giving the critical frequencies.
|
|
30
|
+
For Type I filters, this is the point in the transition band at which the gain first drops below -rp.
|
|
31
|
+
For digital filters, Wn are in the same units as fs unless wn_hz is False.
|
|
32
|
+
For analog filters, Wn is an angular frequency (e.g., rad/s).
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
btype: str = "lowpass"
|
|
36
|
+
"""
|
|
37
|
+
{‘lowpass’, ‘highpass’, ‘bandpass’, ‘bandstop’}
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
analog: bool = False
|
|
41
|
+
"""
|
|
42
|
+
When True, return an analog filter, otherwise a digital filter is returned.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
cheby_type: str = "cheby1"
|
|
46
|
+
"""
|
|
47
|
+
Which type of Chebyshev filter to design. Either "cheby1" or "cheby2".
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
wn_hz: bool = True
|
|
51
|
+
"""
|
|
52
|
+
Set False if provided Wn are normalized from 0 to 1, where 1 is the Nyquist frequency
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def cheby_design_fun(
|
|
57
|
+
fs: float,
|
|
58
|
+
order: int = 0,
|
|
59
|
+
ripple_tol: float | None = None,
|
|
60
|
+
Wn: float | tuple[float, float] | None = None,
|
|
61
|
+
btype: str = "lowpass",
|
|
62
|
+
analog: bool = False,
|
|
63
|
+
coef_type: str = "ba",
|
|
64
|
+
cheby_type: str = "cheby1",
|
|
65
|
+
wn_hz: bool = True,
|
|
66
|
+
) -> FilterCoefsMultiType:
|
|
67
|
+
"""
|
|
68
|
+
Chebyshev type I and type II digital and analog filter design.
|
|
69
|
+
Design an `order`th-order digital or analog Chebyshev type I or type II filter and return the filter coefficients.
|
|
70
|
+
See :obj:`ChebyFilterSettings` for argument description.
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
The filter coefficients as a tuple of (b, a) for coef_type "ba", or as a single ndarray for "sos",
|
|
74
|
+
or (z, p, k) for "zpk".
|
|
75
|
+
"""
|
|
76
|
+
coefs = None
|
|
77
|
+
if order > 0:
|
|
78
|
+
if cheby_type == "cheby1":
|
|
79
|
+
coefs = scipy.signal.cheby1(
|
|
80
|
+
order,
|
|
81
|
+
ripple_tol,
|
|
82
|
+
Wn,
|
|
83
|
+
btype=btype,
|
|
84
|
+
analog=analog,
|
|
85
|
+
output=coef_type,
|
|
86
|
+
fs=fs if wn_hz else None,
|
|
87
|
+
)
|
|
88
|
+
elif cheby_type == "cheby2":
|
|
89
|
+
coefs = scipy.signal.cheby2(
|
|
90
|
+
order,
|
|
91
|
+
ripple_tol,
|
|
92
|
+
Wn,
|
|
93
|
+
btype=btype,
|
|
94
|
+
analog=analog,
|
|
95
|
+
output=coef_type,
|
|
96
|
+
fs=fs,
|
|
97
|
+
)
|
|
98
|
+
if coefs is not None and coef_type == "ba":
|
|
99
|
+
coefs = normalize(*coefs)
|
|
100
|
+
return coefs
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class ChebyshevFilter(FilterBase):
|
|
104
|
+
SETTINGS = ChebyshevFilterSettings
|
|
105
|
+
|
|
106
|
+
def design_filter(
|
|
107
|
+
self,
|
|
108
|
+
) -> typing.Callable[[float], FilterCoefsMultiType | None]:
|
|
109
|
+
return functools.partial(
|
|
110
|
+
cheby_design_fun,
|
|
111
|
+
order=self.SETTINGS.order,
|
|
112
|
+
ripple_tol=self.SETTINGS.ripple_tol,
|
|
113
|
+
Wn=self.SETTINGS.Wn,
|
|
114
|
+
btype=self.SETTINGS.btype,
|
|
115
|
+
analog=self.SETTINGS.analog,
|
|
116
|
+
coef_type=self.SETTINGS.coef_type,
|
|
117
|
+
cheby_type=self.SETTINGS.cheby_type,
|
|
118
|
+
wn_hz=self.SETTINGS.wn_hz,
|
|
119
|
+
)
|
ezmsg/sigproc/decimate.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import scipy.signal
|
|
2
1
|
import ezmsg.core as ez
|
|
3
2
|
from ezmsg.util.messages.axisarray import AxisArray
|
|
4
3
|
|
|
4
|
+
from .cheby import ChebyshevFilter, ChebyshevFilterSettings
|
|
5
5
|
from .downsample import Downsample, DownsampleSettings
|
|
6
|
-
from .filter import Filter, FilterCoefficients, FilterSettings
|
|
7
6
|
|
|
8
7
|
|
|
9
8
|
class Decimate(ez.Collection):
|
|
@@ -17,24 +16,21 @@ class Decimate(ez.Collection):
|
|
|
17
16
|
INPUT_SIGNAL = ez.InputStream(AxisArray)
|
|
18
17
|
OUTPUT_SIGNAL = ez.OutputStream(AxisArray)
|
|
19
18
|
|
|
20
|
-
FILTER =
|
|
19
|
+
FILTER = ChebyshevFilter()
|
|
21
20
|
DOWNSAMPLE = Downsample()
|
|
22
21
|
|
|
23
22
|
def configure(self) -> None:
|
|
23
|
+
cheby_settings = ChebyshevFilterSettings(
|
|
24
|
+
order=8 if self.SETTINGS.factor > 1 else 0,
|
|
25
|
+
ripple_tol=0.05,
|
|
26
|
+
Wn=0.8 / self.SETTINGS.factor if self.SETTINGS.factor > 1 else None,
|
|
27
|
+
btype="lowpass",
|
|
28
|
+
axis=self.SETTINGS.axis,
|
|
29
|
+
wn_hz=False,
|
|
30
|
+
)
|
|
31
|
+
self.FILTER.apply_settings(cheby_settings)
|
|
24
32
|
self.DOWNSAMPLE.apply_settings(self.SETTINGS)
|
|
25
33
|
|
|
26
|
-
if self.SETTINGS.factor < 1:
|
|
27
|
-
raise ValueError("Decimation factor must be >= 1 (no decimation")
|
|
28
|
-
elif self.SETTINGS.factor == 1:
|
|
29
|
-
filt = FilterCoefficients()
|
|
30
|
-
else:
|
|
31
|
-
# See scipy.signal.decimate for IIR Filter Condition
|
|
32
|
-
b, a = scipy.signal.cheby1(8, 0.05, 0.8 / self.SETTINGS.factor)
|
|
33
|
-
system = scipy.signal.dlti(b, a)
|
|
34
|
-
filt = FilterCoefficients(b=system.num, a=system.den) # type: ignore
|
|
35
|
-
|
|
36
|
-
self.FILTER.apply_settings(FilterSettings(filt=filt))
|
|
37
|
-
|
|
38
34
|
def network(self) -> ez.NetworkDefinition:
|
|
39
35
|
return (
|
|
40
36
|
(self.INPUT_SIGNAL, self.FILTER.INPUT_SIGNAL),
|
ezmsg/sigproc/downsample.py
CHANGED
|
@@ -14,7 +14,7 @@ from .base import GenAxisArray
|
|
|
14
14
|
|
|
15
15
|
@consumer
|
|
16
16
|
def downsample(
|
|
17
|
-
axis:
|
|
17
|
+
axis: str | None = None, factor: int = 1
|
|
18
18
|
) -> typing.Generator[AxisArray, AxisArray, None]:
|
|
19
19
|
"""
|
|
20
20
|
Construct a generator that yields a downsampled version of the data .send() to it.
|
|
@@ -96,7 +96,7 @@ class DownsampleSettings(ez.Settings):
|
|
|
96
96
|
See :obj:`downsample` documentation for a description of the parameters.
|
|
97
97
|
"""
|
|
98
98
|
|
|
99
|
-
axis:
|
|
99
|
+
axis: str | None = None
|
|
100
100
|
factor: int = 1
|
|
101
101
|
|
|
102
102
|
|
ezmsg/sigproc/ewmfilter.py
CHANGED
|
@@ -2,14 +2,15 @@ import asyncio
|
|
|
2
2
|
import typing
|
|
3
3
|
|
|
4
4
|
import ezmsg.core as ez
|
|
5
|
-
from ezmsg.util.messages.axisarray import AxisArray
|
|
5
|
+
from ezmsg.util.messages.axisarray import AxisArray
|
|
6
|
+
from ezmsg.util.messages.util import replace
|
|
6
7
|
import numpy as np
|
|
7
8
|
|
|
8
9
|
from .window import Window, WindowSettings
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
class EWMSettings(ez.Settings):
|
|
12
|
-
axis:
|
|
13
|
+
axis: str | None = None
|
|
13
14
|
"""Name of the axis to accumulate."""
|
|
14
15
|
|
|
15
16
|
zero_offset: bool = True
|
|
@@ -23,7 +24,8 @@ class EWMState(ez.State):
|
|
|
23
24
|
|
|
24
25
|
class EWM(ez.Unit):
|
|
25
26
|
"""
|
|
26
|
-
Exponentially Weighted Moving Average Standardization
|
|
27
|
+
Exponentially Weighted Moving Average Standardization.
|
|
28
|
+
This is deprecated. Please use :obj:`ezmsg.sigproc.scaler.AdaptiveStandardScaler` instead.
|
|
27
29
|
|
|
28
30
|
References https://stackoverflow.com/a/42926270
|
|
29
31
|
"""
|
|
@@ -36,6 +38,9 @@ class EWM(ez.Unit):
|
|
|
36
38
|
OUTPUT_SIGNAL = ez.OutputStream(AxisArray)
|
|
37
39
|
|
|
38
40
|
async def initialize(self) -> None:
|
|
41
|
+
ez.logger.warning(
|
|
42
|
+
"EWM/EWMFilter is deprecated and will be removed in a future version. Use AdaptiveStandardScaler instead."
|
|
43
|
+
)
|
|
39
44
|
self.STATE.signal_queue = asyncio.Queue()
|
|
40
45
|
self.STATE.buffer_queue = asyncio.Queue()
|
|
41
46
|
|
|
@@ -99,7 +104,7 @@ class EWMFilterSettings(ez.Settings):
|
|
|
99
104
|
history_dur: float
|
|
100
105
|
"""Previous data to accumulate for standardization."""
|
|
101
106
|
|
|
102
|
-
axis:
|
|
107
|
+
axis: str | None = None
|
|
103
108
|
"""Name of the axis to accumulate."""
|
|
104
109
|
|
|
105
110
|
zero_offset: bool = True
|
|
@@ -112,7 +117,7 @@ class EWMFilter(ez.Collection):
|
|
|
112
117
|
leads to :obj:`Window` which then feeds into :obj:`EWM` 's INPUT_BUFFER
|
|
113
118
|
and another branch that feeds directly into :obj:`EWM` 's INPUT_SIGNAL.
|
|
114
119
|
|
|
115
|
-
|
|
120
|
+
This is deprecated. Please use :obj:`ezmsg.sigproc.scaler.AdaptiveStandardScaler` instead.
|
|
116
121
|
"""
|
|
117
122
|
|
|
118
123
|
SETTINGS = EWMFilterSettings
|