-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some documentation rework and resampling notebook example with CM
Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>
- Loading branch information
Showing
4 changed files
with
264 additions
and
15 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,65 @@ | ||
from scipy import signal | ||
from typing import Callable | ||
from typing import Callable, Union, Collection | ||
from speasy.products import SpeasyVariable | ||
import numpy as np | ||
|
||
|
||
def apply_sos_filter(sos: np.ndarray, filter_function: Callable, var: SpeasyVariable) -> SpeasyVariable: | ||
res = np.empty_like(var) | ||
def _apply_filter(filter_function: Callable, sos: np.ndarray, var: SpeasyVariable) -> SpeasyVariable: | ||
res = SpeasyVariable.reserve_like(var) | ||
res.values[:] = filter_function(sos, var.values, axis=0) | ||
return res | ||
|
||
|
||
def sosfiltfilt(sos: np.ndarray, var: SpeasyVariable) -> SpeasyVariable: | ||
"""Apply an IIR filter to the data using :func:`scipy.signal.sosfiltfilt`. | ||
def apply_sos_filter(sos: np.ndarray, filter_function: Callable, | ||
var: Union[SpeasyVariable, Collection[SpeasyVariable]]) -> Union[ | ||
SpeasyVariable, Collection[SpeasyVariable]]: | ||
"""Apply an IIR filter to the variable(s) using the given filter function. This function just applies the filter to the | ||
values of the variable without any resampling, it assumes that the variable has a regular time axis. | ||
Parameters | ||
---------- | ||
sos: np.ndarray | ||
Second-order sections representation of the filter, as returned by :func:`scipy.signal.iirfilter` with `output='sos'` for example. | ||
filter_function: Callable | ||
The filter function to use (e.g. :func:`scipy.signal.sosfiltfilt`) | ||
var: SpeasyVariable or Collection[SpeasyVariable] | ||
The variable(s) to filter | ||
Returns | ||
------- | ||
SpeasyVariable or Collection[SpeasyVariable] | ||
The filtered variable(s) | ||
Notes | ||
----- | ||
It only supports 1D variables. | ||
""" | ||
|
||
if isinstance(var, SpeasyVariable): | ||
return _apply_filter(filter_function, sos, var) | ||
else: | ||
return [_apply_filter(filter_function, sos, v) for v in var] | ||
|
||
|
||
def sosfiltfilt(sos: np.ndarray, var: Union[SpeasyVariable, Collection[SpeasyVariable]]) -> Union[ | ||
SpeasyVariable, Collection[SpeasyVariable]]: | ||
"""Apply an IIR filter to the variable(s) using :func:`scipy.signal.sosfiltfilt`. This function just applies the filter to | ||
the values of the variable without any resampling, it assumes that the variable has a regular time axis. | ||
Parameters | ||
---------- | ||
sos: np.ndarray | ||
Second-order sections representation of the filter | ||
var: SpeasyVariable | ||
The variable to filter | ||
var: SpeasyVariable or Collection[SpeasyVariable] | ||
The variable(s) to filter | ||
Returns | ||
------- | ||
SpeasyVariable | ||
The filtered variable | ||
SpeasyVariable or Collection[SpeasyVariable] | ||
The filtered variable(s) | ||
Notes | ||
----- | ||
It only supports 1D variables. | ||
""" | ||
return apply_sos_filter(sos, signal.sosfiltfilt, var) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters