NumericalFilter¶
yohou.preprocessing.NumericalFilter
¶
Bases: BaseActualTransformer
Apply digital IIR or FIR filters to time series data.
Applies standard digital filters (Butterworth, Chebyshev, Bessel, etc.) for lowpass, highpass, bandpass, or bandstop filtering. Useful for noise removal, drift correction, and signal preprocessing.
Parameters ¶
| Name | Type | Description | Default |
|---|---|---|---|
design
|
(butterworth, chebyshev1, chebyshev2, elliptic, bessel)
|
Filter design method: - "butterworth": Butterworth (maximally flat passband) - "chebyshev1": Chebyshev Type I (passband ripple) - "chebyshev2": Chebyshev Type II (stopband ripple) - "elliptic": Elliptic/Cauer (passband and stopband ripple) - "bessel": Bessel (linear phase) |
"butterworth"
|
mode
|
(lowpass, highpass, bandpass, bandstop)
|
Filter mode. For bandpass/bandstop, cutoff_frequency should be a 2-tuple. |
"lowpass"
|
order
|
int
|
Filter order. Higher order = sharper cutoff but more phase distortion. |
4
|
cutoff_frequency
|
float or tuple of float
|
Cutoff frequency as fraction of Nyquist (0 to 1). For bandpass/bandstop, provide (low_freq, high_freq). |
0.1
|
passband_ripple
|
float or None
|
Passband ripple in dB (for chebyshev1, elliptic). Defaults to 1.0 if required. |
None
|
stopband_attenuation
|
float or None
|
Stopband attenuation in dB (for chebyshev2, elliptic). Defaults to 40.0 if required. |
None
|
Attributes ¶
| Name | Type | Description |
|---|---|---|
b_ |
ndarray
|
Numerator coefficients of the filter. |
a_ |
ndarray
|
Denominator coefficients of the filter. |
zi_ |
dict of ndarray
|
Filter delay state per column. Updated after each transform call to enable streaming. |
Notes ¶
Statefulness: The filter maintains internal state (delay line values) between transform calls. This enables streaming/chunked processing without transients at chunk boundaries.
Use rewind() to clear the filter state and start fresh.
Examples ¶
>>> import polars as pl
>>> from datetime import datetime
>>> import numpy as np
>>> from yohou.preprocessing import NumericalFilter
>>> # Generate noisy signal
>>> times = pl.datetime_range(
... start=datetime(2020, 1, 1), end=datetime(2020, 1, 1, 0, 1), interval="1s", eager=True
... )
>>> t = np.arange(len(times))
>>> np.random.seed(42) # deterministic noise for a reproducible example
>>> signal = np.sin(2 * np.pi * 0.05 * t) + 0.5 * np.random.randn(len(t))
>>> X = pl.DataFrame({"time": times, "signal": signal.tolist()})
>>> # Apply lowpass filter (causal, stateful)
>>> transformer = NumericalFilter(design="butterworth", mode="lowpass", order=4, cutoff_frequency=0.2)
>>> transformer.fit(X)
NumericalFilter(...)
>>> X_filtered = transformer.transform(X)
>>> "time" in X_filtered.columns
True
>>> # Filter state preserved for subsequent chunks
>>> # Use transformer.rewind() to clear state
See Also ¶
NumericalIntegrator: Numerical integration.NumericalDifferentiator: Numerical differentiation.scipy.signal.butter: Butterworth filter design.
Source Code ¶
Source code in src/yohou/preprocessing/signal.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
Methods ¶
rewind(X)
¶
Rewind the filter state and observation horizon.
Clears the stored filter delay state and rewinds the observation window, so the next transform call starts fresh.
Parameters ¶
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Input time series to set new observation window. |
required |
Returns ¶
| Type | Description |
|---|---|
self
|
|
Source Code ¶
Source code in src/yohou/preprocessing/signal.py
observe_transform(X, **params)
¶
Transform new data and update state without clearing filter delays.
The base observe_transform calls observe(), which in turn calls
rewind(), clearing the filter delay state zi_ as a side effect and
reintroducing chunk-boundary transients. This override bypasses that chain
and preserves zi_ so streaming/chunked processing continues seamlessly.
Parameters ¶
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Input time series with a |
required |
**params
|
dict
|
Metadata to route to nested estimators. |
{}
|
Returns ¶
| Type | Description |
|---|---|
DataFrame
|
Transformed time series with a |
Source Code ¶
Source code in src/yohou/preprocessing/signal.py
get_feature_names_out(input_features=None)
¶
Get output feature names for transformation.
Parameters ¶
| Name | Type | Description | Default |
|---|---|---|---|
input_features
|
list of str or None
|
Column names of the input features. If |
None
|
Returns ¶
| Type | Description |
|---|---|
list of str
|
Output feature names after transformation. |
Source Code ¶
Source code in src/yohou/preprocessing/signal.py
Tutorials¶
The following example notebooks use this component:
-
How to Apply Signal Processing Filters
Apply NumericalFilter (Butterworth, Chebyshev, Bessel), NumericalDifferentiator, and NumericalIntegrator for signal smoothing and rate-of-change extraction.
-
How to Visualize Signal Processing
Butterworth low-pass filtering with frequency spectrum analysis and phase shift inspection on half-hourly electricity demand data using Plotly.