Skip to content

Commit

Permalink
bugfix: @as_producer decorator used in iir filters does not support c…
Browse files Browse the repository at this point in the history
…oncurrency so it is replaced with producers made from partial generating functions; the test_iir passes
  • Loading branch information
mscaudill committed Jul 11, 2023
1 parent e3bd836 commit fcfc4ca
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
21 changes: 11 additions & 10 deletions src/openseize/core/numerical.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import scipy.signal as sps

from openseize import producer
from openseize.core.producer import Producer, as_producer, pad_producer
from openseize.core.producer import Producer, pad_producer
from openseize.core.queues import FIFOArray
from openseize.core.arraytools import (pad_along_axis, slice_along_axis,
multiply_along_axis, split_along_axis)
Expand Down Expand Up @@ -288,7 +288,6 @@ def _add_overlap(y, overlap, wlen, axis):
yield _oa_boundary(y, window, 'right', axis, mode)


@as_producer
def sosfilt(pro, sos, axis, zi=None):
"""Batch applies a forward second-order-section fmt filter to a
producer of numpy arrays.
Expand All @@ -309,7 +308,9 @@ def sosfilt(pro, sos, axis, zi=None):
sosfilt_zi in scipy's signal module. Default is None
which sets the initial nsections of filtered values to 0.
Returns: A producer of filtered values of len chunksize along axis.
Returns:
A generator of ndarrays of forward filtered values of shape chunksize
along the sample axis.
"""

# set initial conditions for filter (see scipy sosfilt; zi)
Expand All @@ -324,7 +325,6 @@ def sosfilt(pro, sos, axis, zi=None):
yield y


@as_producer
def sosfiltfilt(pro, sos, axis, **kwargs):
"""Batch applies a forward-backward second order section fmt filter to
a producer of numpy arrays.
Expand All @@ -338,8 +338,9 @@ def sosfiltfilt(pro, sos, axis, **kwargs):
axis: int
The axis along which to apply the filter in chunksize batches
Returns: a producer of forward-backward filtered values of len
chunksize along axis
Returns:
A generator of forward-backward filtered values of len chunksize
along axis.
Notes:
1. This iterative algorithm is not nearly as efficient as working on
Expand Down Expand Up @@ -400,7 +401,6 @@ def sosfiltfilt(pro, sos, axis, **kwargs):
yield np.flip(rfilt, axis=axis)


@as_producer
def lfilter(pro, coeffs, axis, zi=None):
"""Batch appliies a forward transfer function fmt (b,a) filter to
a producer of numpy arrays.
Expand All @@ -418,7 +418,8 @@ def lfilter(pro, coeffs, axis, zi=None):
(default), the initial values are zeros. Please see scipy
signal lfilter for more details.
Returns: A producer of filtered values of len chunksize along axis.
Returns:
A generator of filtered values of len chunksize along axis.
"""

b, a = coeffs
Expand All @@ -435,7 +436,6 @@ def lfilter(pro, coeffs, axis, zi=None):
yield y


@as_producer
def filtfilt(pro, coeffs, axis, **kwargs):
"""Batch applies a forward-backward filter in transfer func fmt (b,a)
to a producer of numpy arrays.
Expand All @@ -449,7 +449,8 @@ def filtfilt(pro, coeffs, axis, **kwargs):
axis: int
The axis along which to apply the filter in chunksize batches
Returns: A producer of filtered values of len chunksize along axis.
Returns:
A generator of filtered values of len chunksize along axis.
Notes:
1. This iterative algorithm is not nearly as efficient as working on
Expand Down
7 changes: 5 additions & 2 deletions src/openseize/filtering/bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,11 @@ def __call__(self,
else:
filtfunc = nm.lfilter

# zi is ignored if filtfunc is a forward/backward filtfilt
result = filtfunc(pro, self.coeffs, axis, zi=zi)
# zi is ignored if filtfunc is a forward/backward filter
genfunc = partial(filtfunc, pro, self.coeffs, axis, zi=zi)

# build producer from a generating function
result = producer(genfunc, chunksize, axis, shape=pro.shape)

# if data is an ndarray return an ndarray
if isinstance(data, np.ndarray):
Expand Down

0 comments on commit fcfc4ca

Please sign in to comment.