reza-filter 0.2.5__cp311-cp311-win_amd64.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.
reza/__init__.py
ADDED
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
"""
|
|
4
|
+
Reza Filter (minimal, SciPy-like user-facing API)
|
|
5
|
+
------------------------------------------------
|
|
6
|
+
Goal: users should only need:
|
|
7
|
+
|
|
8
|
+
import reza
|
|
9
|
+
|
|
10
|
+
# SciPy-like (preferred)
|
|
11
|
+
y = reza.filter(x, fs=200, Wn=5, btype="low") # low-pass 5 Hz
|
|
12
|
+
y = reza.filter(x, fs=200, Wn=10, btype="high") # high-pass 10 Hz
|
|
13
|
+
y = reza.filter(x, fs=200, Wn=(5, 10), btype="band") # band-pass 5-10 Hz
|
|
14
|
+
|
|
15
|
+
# Convenience wrappers
|
|
16
|
+
y = reza.low(x, fs=200, fc=5)
|
|
17
|
+
y = reza.high(x, fs=200, fc=10)
|
|
18
|
+
y = reza.band(x, fs=200, f1=5, f2=10)
|
|
19
|
+
|
|
20
|
+
Notes
|
|
21
|
+
-----
|
|
22
|
+
- Reza Filter is applied in the FFT domain as a zero-phase magnitude shaping curve.
|
|
23
|
+
- All shaping parameters are internal. The decay exponent d is auto-selected and cached.
|
|
24
|
+
- For frequency response, use reza.freqz(...). Unlike IIR filters, Reza's response
|
|
25
|
+
depends on the effective FFT length; we choose an internal default automatically
|
|
26
|
+
so users do not need to pass n.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
import math
|
|
30
|
+
from functools import lru_cache
|
|
31
|
+
import numpy as np
|
|
32
|
+
|
|
33
|
+
# Package version (must match installed distribution metadata)
|
|
34
|
+
try:
|
|
35
|
+
from importlib.metadata import version as _pkg_version # py3.8+
|
|
36
|
+
__version__ = _pkg_version("reza-filter")
|
|
37
|
+
except Exception:
|
|
38
|
+
__version__ = "0.0.0"
|
|
39
|
+
|
|
40
|
+
from . import _fallback
|
|
41
|
+
|
|
42
|
+
try:
|
|
43
|
+
from . import _reza_cpp as _cpp # compiled extension
|
|
44
|
+
_HAS_CPP = True
|
|
45
|
+
except Exception:
|
|
46
|
+
_cpp = None
|
|
47
|
+
_HAS_CPP = False
|
|
48
|
+
|
|
49
|
+
__all__ = [
|
|
50
|
+
# Primary (SciPy-like)
|
|
51
|
+
"filter",
|
|
52
|
+
"freqz",
|
|
53
|
+
|
|
54
|
+
# Convenience wrappers
|
|
55
|
+
"low",
|
|
56
|
+
"high",
|
|
57
|
+
"band",
|
|
58
|
+
|
|
59
|
+
# Backward-compatible aliases
|
|
60
|
+
"lowpass",
|
|
61
|
+
"highpass",
|
|
62
|
+
"bandpass",
|
|
63
|
+
"lp",
|
|
64
|
+
"hp",
|
|
65
|
+
"bp",
|
|
66
|
+
|
|
67
|
+
# Utilities
|
|
68
|
+
"has_cpp",
|
|
69
|
+
"__version__",
|
|
70
|
+
]
|
|
71
|
+
|
|
72
|
+
# ---------------------------------------------------------------------
|
|
73
|
+
# Internal defaults (NOT part of the public API)
|
|
74
|
+
# ---------------------------------------------------------------------
|
|
75
|
+
_C_DEFAULT = 0.9
|
|
76
|
+
_OFFSET_DEFAULT = 1.0
|
|
77
|
+
|
|
78
|
+
# Dynamic-decay search parameters (kept internal)
|
|
79
|
+
_D_INIT = 10.0
|
|
80
|
+
_D_INC = 5.0
|
|
81
|
+
_D_THRESHOLD = 1e-4
|
|
82
|
+
_D_MAX_ITER = 200
|
|
83
|
+
_D_MAX = 1e6
|
|
84
|
+
|
|
85
|
+
# Freq-response internal defaults (kept internal)
|
|
86
|
+
_FREQZ_MIN_N = 4096
|
|
87
|
+
_FREQZ_MAX_N = 262144
|
|
88
|
+
_FREQZ_MIN_DF_HZ = 0.02 # do not chase absurdly fine grids by default
|
|
89
|
+
_FREQZ_FMIN_FRAC = 1.0 / 100 # aim for ~100 points up to the smallest cutoff
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def has_cpp() -> bool:
|
|
93
|
+
return _HAS_CPP
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _move_axis_to_last(x: np.ndarray, axis: int) -> np.ndarray:
|
|
97
|
+
return np.moveaxis(x, axis, -1) if axis != -1 else x
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _move_axis_back(x: np.ndarray, axis: int) -> np.ndarray:
|
|
101
|
+
return np.moveaxis(x, -1, axis) if axis != -1 else x
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _apply_gain_rfft(X: np.ndarray, gain: np.ndarray) -> np.ndarray:
|
|
105
|
+
if _HAS_CPP:
|
|
106
|
+
return _cpp.apply_gain_rfft(X, gain)
|
|
107
|
+
return _fallback.apply_gain_rfft(X, gain)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
@lru_cache(maxsize=256)
|
|
111
|
+
def _auto_d_lowpass(fs: float, n: int, fc: float) -> float:
|
|
112
|
+
if _HAS_CPP:
|
|
113
|
+
return float(
|
|
114
|
+
_cpp.auto_d_lowpass(
|
|
115
|
+
float(fs), int(n), float(fc),
|
|
116
|
+
float(_C_DEFAULT), float(_OFFSET_DEFAULT),
|
|
117
|
+
float(_D_INIT), float(_D_INC), float(_D_THRESHOLD),
|
|
118
|
+
int(_D_MAX_ITER), float(_D_MAX),
|
|
119
|
+
)
|
|
120
|
+
)
|
|
121
|
+
return float(
|
|
122
|
+
_fallback._auto_d_lowpass(
|
|
123
|
+
float(fs), int(n), float(fc),
|
|
124
|
+
c=float(_C_DEFAULT), offset=float(_OFFSET_DEFAULT),
|
|
125
|
+
initial_d=float(_D_INIT), d_increment=float(_D_INC),
|
|
126
|
+
threshold=float(_D_THRESHOLD), max_iter=int(_D_MAX_ITER), max_d=float(_D_MAX),
|
|
127
|
+
)
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
@lru_cache(maxsize=256)
|
|
132
|
+
def _auto_d_highpass(fs: float, n: int, fc: float) -> float:
|
|
133
|
+
if _HAS_CPP:
|
|
134
|
+
return float(
|
|
135
|
+
_cpp.auto_d_highpass(
|
|
136
|
+
float(fs), int(n), float(fc),
|
|
137
|
+
float(_C_DEFAULT), float(_OFFSET_DEFAULT),
|
|
138
|
+
float(_D_INIT), float(_D_INC), float(_D_THRESHOLD),
|
|
139
|
+
int(_D_MAX_ITER), float(_D_MAX),
|
|
140
|
+
)
|
|
141
|
+
)
|
|
142
|
+
return float(
|
|
143
|
+
_fallback._auto_d_highpass(
|
|
144
|
+
float(fs), int(n), float(fc),
|
|
145
|
+
c=float(_C_DEFAULT), offset=float(_OFFSET_DEFAULT),
|
|
146
|
+
initial_d=float(_D_INIT), d_increment=float(_D_INC),
|
|
147
|
+
threshold=float(_D_THRESHOLD), max_iter=int(_D_MAX_ITER), max_d=float(_D_MAX),
|
|
148
|
+
)
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
@lru_cache(maxsize=256)
|
|
153
|
+
def _auto_d_bandpass(fs: float, n: int, f1: float, f2: float) -> float:
|
|
154
|
+
if _HAS_CPP:
|
|
155
|
+
return float(
|
|
156
|
+
_cpp.auto_d_bandpass(
|
|
157
|
+
float(fs), int(n), float(f1), float(f2),
|
|
158
|
+
float(_C_DEFAULT), float(_OFFSET_DEFAULT),
|
|
159
|
+
float(_D_INIT), float(_D_INC), float(_D_THRESHOLD),
|
|
160
|
+
int(_D_MAX_ITER), float(_D_MAX),
|
|
161
|
+
)
|
|
162
|
+
)
|
|
163
|
+
return float(
|
|
164
|
+
_fallback._auto_d_bandpass(
|
|
165
|
+
float(fs), int(n), float(f1), float(f2),
|
|
166
|
+
c=float(_C_DEFAULT), offset=float(_OFFSET_DEFAULT),
|
|
167
|
+
initial_d=float(_D_INIT), d_increment=float(_D_INC),
|
|
168
|
+
threshold=float(_D_THRESHOLD), max_iter=int(_D_MAX_ITER), max_d=float(_D_MAX),
|
|
169
|
+
)
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
@lru_cache(maxsize=256)
|
|
174
|
+
def _gain_lowpass(fs: float, n: int, fc: float) -> np.ndarray:
|
|
175
|
+
d = _auto_d_lowpass(fs, n, fc)
|
|
176
|
+
if _HAS_CPP:
|
|
177
|
+
g = _cpp.gain_lowpass(float(fs), int(n), float(fc),
|
|
178
|
+
float(_C_DEFAULT), float(_OFFSET_DEFAULT), float(d))
|
|
179
|
+
else:
|
|
180
|
+
g = _fallback.calculate_gain_lowpass(float(fs), int(n), float(fc),
|
|
181
|
+
c=float(_C_DEFAULT), offset=float(_OFFSET_DEFAULT), d=float(d))
|
|
182
|
+
g = np.ascontiguousarray(np.asarray(g, dtype=np.float64))
|
|
183
|
+
g.setflags(write=False)
|
|
184
|
+
return g
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
@lru_cache(maxsize=256)
|
|
188
|
+
def _gain_highpass(fs: float, n: int, fc: float) -> np.ndarray:
|
|
189
|
+
d = _auto_d_highpass(fs, n, fc)
|
|
190
|
+
if _HAS_CPP:
|
|
191
|
+
g = _cpp.gain_highpass(float(fs), int(n), float(fc),
|
|
192
|
+
float(_C_DEFAULT), float(_OFFSET_DEFAULT), float(d))
|
|
193
|
+
else:
|
|
194
|
+
g = _fallback.calculate_gain_highpass(float(fs), int(n), float(fc),
|
|
195
|
+
c=float(_C_DEFAULT), offset=float(_OFFSET_DEFAULT), d=float(d))
|
|
196
|
+
g = np.ascontiguousarray(np.asarray(g, dtype=np.float64))
|
|
197
|
+
g.setflags(write=False)
|
|
198
|
+
return g
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
@lru_cache(maxsize=256)
|
|
202
|
+
def _gain_bandpass(fs: float, n: int, f1: float, f2: float) -> np.ndarray:
|
|
203
|
+
d = _auto_d_bandpass(fs, n, f1, f2)
|
|
204
|
+
if _HAS_CPP:
|
|
205
|
+
g = _cpp.gain_bandpass(float(fs), int(n), float(f1), float(f2),
|
|
206
|
+
float(_C_DEFAULT), float(_OFFSET_DEFAULT), float(d))
|
|
207
|
+
else:
|
|
208
|
+
g = _fallback.calculate_gain_bandpass(float(fs), int(n), float(f1), float(f2),
|
|
209
|
+
c=float(_C_DEFAULT), offset=float(_OFFSET_DEFAULT), d=float(d))
|
|
210
|
+
g = np.ascontiguousarray(np.asarray(g, dtype=np.float64))
|
|
211
|
+
g.setflags(write=False)
|
|
212
|
+
return g
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
# ---------------------------------------------------------------------
|
|
216
|
+
# Core filtering (kept stable; used by wrappers)
|
|
217
|
+
# ---------------------------------------------------------------------
|
|
218
|
+
def lowpass(data, fs: float, fc: float, axis: int = -1):
|
|
219
|
+
x = np.asarray(data, dtype=float)
|
|
220
|
+
x_m = _move_axis_to_last(x, axis)
|
|
221
|
+
n = int(x_m.shape[-1])
|
|
222
|
+
|
|
223
|
+
gain = _gain_lowpass(float(fs), n, float(fc))
|
|
224
|
+
X = np.ascontiguousarray(np.fft.rfft(x_m, axis=-1).astype(np.complex128, copy=False))
|
|
225
|
+
Y = _apply_gain_rfft(X, gain)
|
|
226
|
+
y = np.fft.irfft(Y, n=n, axis=-1)
|
|
227
|
+
return _move_axis_back(y, axis)
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def highpass(data, fs: float, fc: float, axis: int = -1):
|
|
231
|
+
x = np.asarray(data, dtype=float)
|
|
232
|
+
x_m = _move_axis_to_last(x, axis)
|
|
233
|
+
n = int(x_m.shape[-1])
|
|
234
|
+
|
|
235
|
+
gain = _gain_highpass(float(fs), n, float(fc))
|
|
236
|
+
X = np.ascontiguousarray(np.fft.rfft(x_m, axis=-1).astype(np.complex128, copy=False))
|
|
237
|
+
Y = _apply_gain_rfft(X, gain)
|
|
238
|
+
y = np.fft.irfft(Y, n=n, axis=-1)
|
|
239
|
+
return _move_axis_back(y, axis)
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def bandpass(data, fs: float, f1: float, f2: float, axis: int = -1):
|
|
243
|
+
if float(f2) <= float(f1):
|
|
244
|
+
raise ValueError("bandpass requires f2 > f1")
|
|
245
|
+
|
|
246
|
+
x = np.asarray(data, dtype=float)
|
|
247
|
+
x_m = _move_axis_to_last(x, axis)
|
|
248
|
+
n = int(x_m.shape[-1])
|
|
249
|
+
|
|
250
|
+
gain = _gain_bandpass(float(fs), n, float(f1), float(f2))
|
|
251
|
+
X = np.ascontiguousarray(np.fft.rfft(x_m, axis=-1).astype(np.complex128, copy=False))
|
|
252
|
+
Y = _apply_gain_rfft(X, gain)
|
|
253
|
+
y = np.fft.irfft(Y, n=n, axis=-1)
|
|
254
|
+
return _move_axis_back(y, axis=axis)
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
# ---------------------------------------------------------------------
|
|
258
|
+
# SciPy-like user API (preferred)
|
|
259
|
+
# ---------------------------------------------------------------------
|
|
260
|
+
def _normalize_btype(btype: str | None) -> str:
|
|
261
|
+
if btype is None:
|
|
262
|
+
return "low"
|
|
263
|
+
b = str(btype).strip().lower()
|
|
264
|
+
if b in ("lp", "low", "lowpass", "low-pass"):
|
|
265
|
+
return "low"
|
|
266
|
+
if b in ("hp", "high", "highpass", "high-pass"):
|
|
267
|
+
return "high"
|
|
268
|
+
if b in ("bp", "band", "bandpass", "band-pass"):
|
|
269
|
+
return "band"
|
|
270
|
+
raise ValueError("btype must be one of: 'low', 'high', 'band' (or lowpass/highpass/bandpass aliases)")
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def filter(data, fs: float, Wn=None, btype: str = "low", axis: int = -1, *,
|
|
274
|
+
lowcut=None, highcut=None):
|
|
275
|
+
"""
|
|
276
|
+
Filter data with a SciPy-like signature.
|
|
277
|
+
|
|
278
|
+
Preferred:
|
|
279
|
+
reza.filter(x, fs, Wn, btype="low|high|band")
|
|
280
|
+
|
|
281
|
+
Backward compatibility:
|
|
282
|
+
reza.filter(x, fs, lowcut=..., highcut=...)
|
|
283
|
+
"""
|
|
284
|
+
# Legacy path (lowcut/highcut)
|
|
285
|
+
if Wn is None:
|
|
286
|
+
if lowcut is None and highcut is None:
|
|
287
|
+
raise ValueError("Provide Wn=... (preferred) or at least one of lowcut/highcut (legacy).")
|
|
288
|
+
if lowcut is not None and highcut is not None:
|
|
289
|
+
return bandpass(data, fs, lowcut, highcut, axis=axis)
|
|
290
|
+
if highcut is not None:
|
|
291
|
+
return lowpass(data, fs, highcut, axis=axis)
|
|
292
|
+
return highpass(data, fs, lowcut, axis=axis)
|
|
293
|
+
|
|
294
|
+
bt = _normalize_btype(btype)
|
|
295
|
+
|
|
296
|
+
if bt in ("low", "high") and isinstance(Wn, (tuple, list, np.ndarray)):
|
|
297
|
+
raise ValueError("For btype='low' or 'high', Wn must be a scalar cutoff (Hz).")
|
|
298
|
+
if bt == "band" and not isinstance(Wn, (tuple, list, np.ndarray)):
|
|
299
|
+
raise ValueError("For btype='band', Wn must be a (low, high) tuple in Hz.")
|
|
300
|
+
|
|
301
|
+
if bt == "low":
|
|
302
|
+
return lowpass(data, fs, float(Wn), axis=axis)
|
|
303
|
+
if bt == "high":
|
|
304
|
+
return highpass(data, fs, float(Wn), axis=axis)
|
|
305
|
+
|
|
306
|
+
# band
|
|
307
|
+
f1, f2 = float(Wn[0]), float(Wn[1])
|
|
308
|
+
return bandpass(data, fs, f1, f2, axis=axis)
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
# Convenience wrappers
|
|
312
|
+
def low(data, fs: float, fc: float, axis: int = -1):
|
|
313
|
+
return lowpass(data, fs, fc, axis=axis)
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def high(data, fs: float, fc: float, axis: int = -1):
|
|
317
|
+
return highpass(data, fs, fc, axis=axis)
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def band(data, fs: float, f1: float, f2: float, axis: int = -1):
|
|
321
|
+
return bandpass(data, fs, f1, f2, axis=axis)
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
# Backward-compatible short aliases
|
|
325
|
+
def lp(data, fs: float, fc: float, axis: int = -1):
|
|
326
|
+
return lowpass(data, fs, fc, axis=axis)
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def hp(data, fs: float, fc: float, axis: int = -1):
|
|
330
|
+
return highpass(data, fs, fc, axis=axis)
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
def bp(data, fs: float, f1: float, f2: float, axis: int = -1):
|
|
334
|
+
return bandpass(data, fs, f1, f2, axis=axis)
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
# Optional capitalized aliases (do not advertise; harmless compatibility)
|
|
338
|
+
Low = low
|
|
339
|
+
High = high
|
|
340
|
+
Band = band
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
# ---------------------------------------------------------------------
|
|
344
|
+
# Frequency response (SciPy-like; no user-supplied n)
|
|
345
|
+
# ---------------------------------------------------------------------
|
|
346
|
+
def _next_pow2(n: int) -> int:
|
|
347
|
+
if n <= 1:
|
|
348
|
+
return 1
|
|
349
|
+
return 1 << int(math.ceil(math.log2(n)))
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def _default_n_for_freqz(fs: float, Wn, btype: str) -> int:
|
|
353
|
+
fs = float(fs)
|
|
354
|
+
|
|
355
|
+
# Determine smallest relevant cutoff (Hz)
|
|
356
|
+
if btype == "band":
|
|
357
|
+
fmin = min(float(Wn[0]), float(Wn[1]))
|
|
358
|
+
else:
|
|
359
|
+
fmin = float(Wn)
|
|
360
|
+
|
|
361
|
+
fmin = max(fmin, 1e-6)
|
|
362
|
+
|
|
363
|
+
# Target frequency resolution
|
|
364
|
+
target_df = max(_FREQZ_MIN_DF_HZ, _FREQZ_FMIN_FRAC * fmin)
|
|
365
|
+
|
|
366
|
+
n = int(math.ceil(fs / target_df))
|
|
367
|
+
n = _next_pow2(max(_FREQZ_MIN_N, n))
|
|
368
|
+
n = int(min(_FREQZ_MAX_N, n))
|
|
369
|
+
return n
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
def freqz(*args, fs: float, worN: int = 2048, Wn=None, btype: str = "low",
|
|
373
|
+
fc: float = None, f1: float = None, f2: float = None):
|
|
374
|
+
"""
|
|
375
|
+
SciPy-like frequency response for Reza filter.
|
|
376
|
+
|
|
377
|
+
Preferred:
|
|
378
|
+
w_hz, H = reza.freqz(fs=200, Wn=5, btype="low", worN=2048)
|
|
379
|
+
|
|
380
|
+
Backward compatibility:
|
|
381
|
+
reza.freqz("lp", fs=..., fc=...)
|
|
382
|
+
reza.freqz("hp", fs=..., fc=...)
|
|
383
|
+
reza.freqz("bp", fs=..., f1=..., f2=...)
|
|
384
|
+
"""
|
|
385
|
+
# Accept legacy positional "kind"
|
|
386
|
+
if len(args) >= 1 and isinstance(args[0], str):
|
|
387
|
+
btype = args[0]
|
|
388
|
+
|
|
389
|
+
bt = _normalize_btype(btype)
|
|
390
|
+
|
|
391
|
+
# Normalize cutoffs: prefer Wn, but accept legacy fc/f1/f2
|
|
392
|
+
if Wn is None:
|
|
393
|
+
if bt in ("low", "high"):
|
|
394
|
+
if fc is None:
|
|
395
|
+
raise ValueError("freqz requires Wn=... (preferred) or fc=... (legacy) for low/high.")
|
|
396
|
+
Wn = float(fc)
|
|
397
|
+
else:
|
|
398
|
+
if f1 is None or f2 is None:
|
|
399
|
+
raise ValueError("freqz requires Wn=(f1,f2) (preferred) or f1=... and f2=... (legacy) for band.")
|
|
400
|
+
Wn = (float(f1), float(f2))
|
|
401
|
+
|
|
402
|
+
fs = float(fs)
|
|
403
|
+
worN = int(worN)
|
|
404
|
+
if worN < 16:
|
|
405
|
+
worN = 16
|
|
406
|
+
|
|
407
|
+
n = _default_n_for_freqz(fs, Wn, bt)
|
|
408
|
+
|
|
409
|
+
imp = np.zeros(n, dtype=float)
|
|
410
|
+
imp[0] = 1.0
|
|
411
|
+
|
|
412
|
+
if bt == "low":
|
|
413
|
+
h = low(imp, fs=fs, fc=float(Wn))
|
|
414
|
+
elif bt == "high":
|
|
415
|
+
h = high(imp, fs=fs, fc=float(Wn))
|
|
416
|
+
else:
|
|
417
|
+
h = band(imp, fs=fs, f1=float(Wn[0]), f2=float(Wn[1]))
|
|
418
|
+
|
|
419
|
+
H_full = np.fft.rfft(h)
|
|
420
|
+
f_full = np.fft.rfftfreq(n, d=1.0 / fs)
|
|
421
|
+
|
|
422
|
+
w = np.linspace(0.0, fs / 2.0, worN, endpoint=True)
|
|
423
|
+
Hr = np.interp(w, f_full, H_full.real)
|
|
424
|
+
Hi = np.interp(w, f_full, H_full.imag)
|
|
425
|
+
return w, Hr + 1j * Hi
|
reza/_fallback.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import numpy as np
|
|
3
|
+
|
|
4
|
+
def _gain_lowpass(freqs, fc, c, offset, d):
|
|
5
|
+
return np.where(freqs <= fc, 1.0, np.exp(-c * ((freqs - fc) + offset) ** d))
|
|
6
|
+
|
|
7
|
+
def _gain_highpass(freqs, fc, c, offset, d):
|
|
8
|
+
return np.where(freqs >= fc, 1.0, np.exp(-c * ((fc - freqs) + offset) ** d))
|
|
9
|
+
|
|
10
|
+
def _gain_bandpass(freqs, fc_low, fc_high, c, offset, d):
|
|
11
|
+
return _gain_highpass(freqs, fc_low, c, offset, d) * _gain_lowpass(freqs, fc_high, c, offset, d)
|
|
12
|
+
|
|
13
|
+
def _auto_d(mode, freqs, *, fc=None, fc_low=None, fc_high=None,
|
|
14
|
+
c=0.9, offset=1.0,
|
|
15
|
+
initial_d=10.0, d_increment=5.0, threshold=1e-4, max_iter=200, max_d=1e6):
|
|
16
|
+
d = float(initial_d)
|
|
17
|
+
last_sharp = 0.0
|
|
18
|
+
for _ in range(int(max_iter)):
|
|
19
|
+
if d > max_d:
|
|
20
|
+
break
|
|
21
|
+
|
|
22
|
+
if mode == "lowpass":
|
|
23
|
+
g = _gain_lowpass(freqs, fc, c, offset, d)
|
|
24
|
+
idx = int(np.searchsorted(freqs, fc))
|
|
25
|
+
idx = max(1, min(idx, len(g) - 2))
|
|
26
|
+
sharp = abs(g[idx] - g[idx + 1])
|
|
27
|
+
elif mode == "highpass":
|
|
28
|
+
g = _gain_highpass(freqs, fc, c, offset, d)
|
|
29
|
+
idx = int(np.searchsorted(freqs, fc))
|
|
30
|
+
idx = max(1, min(idx, len(g) - 2))
|
|
31
|
+
sharp = abs(g[idx] - g[idx - 1])
|
|
32
|
+
else:
|
|
33
|
+
g = _gain_bandpass(freqs, fc_low, fc_high, c, offset, d)
|
|
34
|
+
i1 = int(np.searchsorted(freqs, fc_low))
|
|
35
|
+
i2 = int(np.searchsorted(freqs, fc_high))
|
|
36
|
+
i1 = max(1, min(i1, len(g) - 2))
|
|
37
|
+
i2 = max(1, min(i2, len(g) - 2))
|
|
38
|
+
sharp = (abs(g[i1] - g[i1 - 1]) + abs(g[i2] - g[i2 + 1])) / 2.0
|
|
39
|
+
|
|
40
|
+
if abs(sharp - last_sharp) > threshold:
|
|
41
|
+
last_sharp = sharp
|
|
42
|
+
d += d_increment
|
|
43
|
+
else:
|
|
44
|
+
break
|
|
45
|
+
|
|
46
|
+
return float(d)
|
|
47
|
+
|
|
48
|
+
def lowpass(data, fs, fc, *, axis=-1, c=0.9, offset=1.0, d=None,
|
|
49
|
+
initial_d=10.0, d_increment=5.0, threshold=1e-4, max_iter=200, max_d=1e6):
|
|
50
|
+
x = np.asarray(data, dtype=float)
|
|
51
|
+
x_m = np.moveaxis(x, axis, -1)
|
|
52
|
+
n = x_m.shape[-1]
|
|
53
|
+
freqs = np.fft.rfftfreq(n, d=1.0 / fs)
|
|
54
|
+
|
|
55
|
+
if d is None:
|
|
56
|
+
d = _auto_d("lowpass", freqs, fc=fc, c=c, offset=offset,
|
|
57
|
+
initial_d=initial_d, d_increment=d_increment, threshold=threshold, max_iter=max_iter, max_d=max_d)
|
|
58
|
+
gain = _gain_lowpass(freqs, fc, c, offset, d)
|
|
59
|
+
Y = np.fft.rfft(x_m, axis=-1) * gain
|
|
60
|
+
y = np.fft.irfft(Y, n=n, axis=-1)
|
|
61
|
+
return np.moveaxis(y, -1, axis)
|
|
62
|
+
|
|
63
|
+
def highpass(data, fs, fc, *, axis=-1, c=0.9, offset=1.0, d=None,
|
|
64
|
+
initial_d=10.0, d_increment=5.0, threshold=1e-4, max_iter=200, max_d=1e6):
|
|
65
|
+
x = np.asarray(data, dtype=float)
|
|
66
|
+
x_m = np.moveaxis(x, axis, -1)
|
|
67
|
+
n = x_m.shape[-1]
|
|
68
|
+
freqs = np.fft.rfftfreq(n, d=1.0 / fs)
|
|
69
|
+
|
|
70
|
+
if d is None:
|
|
71
|
+
d = _auto_d("highpass", freqs, fc=fc, c=c, offset=offset,
|
|
72
|
+
initial_d=initial_d, d_increment=d_increment, threshold=threshold, max_iter=max_iter, max_d=max_d)
|
|
73
|
+
gain = _gain_highpass(freqs, fc, c, offset, d)
|
|
74
|
+
Y = np.fft.rfft(x_m, axis=-1) * gain
|
|
75
|
+
y = np.fft.irfft(Y, n=n, axis=-1)
|
|
76
|
+
return np.moveaxis(y, -1, axis)
|
|
77
|
+
|
|
78
|
+
def bandpass(data, fs, fc_low, fc_high, *, axis=-1, c=0.9, offset=1.0, d=None,
|
|
79
|
+
initial_d=10.0, d_increment=5.0, threshold=1e-4, max_iter=200, max_d=1e6):
|
|
80
|
+
x = np.asarray(data, dtype=float)
|
|
81
|
+
x_m = np.moveaxis(x, axis, -1)
|
|
82
|
+
n = x_m.shape[-1]
|
|
83
|
+
freqs = np.fft.rfftfreq(n, d=1.0 / fs)
|
|
84
|
+
|
|
85
|
+
if d is None:
|
|
86
|
+
d = _auto_d("bandpass", freqs, fc_low=fc_low, fc_high=fc_high, c=c, offset=offset,
|
|
87
|
+
initial_d=initial_d, d_increment=d_increment, threshold=threshold, max_iter=max_iter, max_d=max_d)
|
|
88
|
+
gain = _gain_bandpass(freqs, fc_low, fc_high, c, offset, d)
|
|
89
|
+
Y = np.fft.rfft(x_m, axis=-1) * gain
|
|
90
|
+
y = np.fft.irfft(Y, n=n, axis=-1)
|
|
91
|
+
return np.moveaxis(y, -1, axis)
|
|
Binary file
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: reza-filter
|
|
3
|
+
Version: 0.2.5
|
|
4
|
+
Summary: Reza exponential-window frequency-domain filter (NumPy FFT + C++/pybind11 acceleration).
|
|
5
|
+
Keywords: signal-processing,filter,EEG,IMU,gait,biomechanics
|
|
6
|
+
Author: Reza Pousti
|
|
7
|
+
License: MIT
|
|
8
|
+
Project-URL: Homepage, https://github.com/Rezapousti/Reza-Filter
|
|
9
|
+
Project-URL: Source, https://github.com/Rezapousti/Reza-Filter
|
|
10
|
+
Project-URL: Issues, https://github.com/Rezapousti/Reza-Filter/issues
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Requires-Dist: numpy<2.0,>=1.21
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# Reza Filter (C++-accelerated) — Python package
|
|
16
|
+
|
|
17
|
+
**Goal:** users do:
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
import reza
|
|
21
|
+
y = reza.bandpass(x, fs=100.0, fc_low=0.5, fc_high=5.0)
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## What is accelerated in C++
|
|
25
|
+
- Gain template generation (low/high/band)
|
|
26
|
+
- Auto-`d` selection via edge-sharpness convergence
|
|
27
|
+
- rFFT-domain complex multiply (X * gain)
|
|
28
|
+
|
|
29
|
+
FFT/iFFT uses NumPy.
|
|
30
|
+
|
|
31
|
+
## Install
|
|
32
|
+
```bash
|
|
33
|
+
pip install reza-filter
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Local dev install
|
|
37
|
+
```bash
|
|
38
|
+
python -m pip install -U pip
|
|
39
|
+
python -m pip install -e .
|
|
40
|
+
python -c "import reza; print('has_cpp=', reza.has_cpp()); print(reza.__version__)"
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Build wheels
|
|
44
|
+
Use GitHub Actions + cibuildwheel: see `.github/workflows/wheels.yml`.
|
|
45
|
+
|
|
46
|
+
## Publish to PyPI
|
|
47
|
+
```bash
|
|
48
|
+
python -m pip install -U build twine
|
|
49
|
+
python -m build
|
|
50
|
+
python -m twine upload dist/*
|
|
51
|
+
```
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
reza/__init__.py,sha256=VOnZpQHYKmbZnjCL1PP7gefNQmMqX6EvGbuY-0UnOzU,14135
|
|
2
|
+
reza/_fallback.py,sha256=6tc4eMEqYayM8be_hghNCaMpXgAuuVFpBsHFpReZNEk,3875
|
|
3
|
+
reza/_reza_cpp.cp311-win_amd64.pyd,sha256=KdVrJGtAPnkJCjMrgZ5QJogPMCJKsqORwnl5PBiBf28,164864
|
|
4
|
+
reza_filter-0.2.5.dist-info/METADATA,sha256=lch3QV93-2o-y295o5HEUe837m--1FNUmrZ51lxEyuM,1311
|
|
5
|
+
reza_filter-0.2.5.dist-info/WHEEL,sha256=oXhHG6ewLm-FNdEna2zwgy-K0KEl4claZ1ztR4VTx0I,106
|
|
6
|
+
reza_filter-0.2.5.dist-info/licenses/LICENSE,sha256=i771wlL-pW0u4zJuU3vAUcK4Dcq_yjKhpyS5N2xKffc,1089
|
|
7
|
+
reza_filter-0.2.5.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Reza Pousti
|
|
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.
|