Filters

Filters are a common preprocessing method for reducing noise in signal processing.

mean_filter() and median_filter() can be applied to individual sequences.

See also

Consider using IndependentFunctionTransformer to apply these filters to multiple sequences.

API reference

Methods

mean_filter(x, *[, k])

Applies a mean filter of size k independently to each feature of the sequence, retaining the original input shape by using appropriate padding.

median_filter(x, *[, k])

Applies a median filter of size k independently to each feature of the sequence, retaining the original input shape by using appropriate padding.


sequentia.preprocessing.transforms.mean_filter(x, *, k=5)

Applies a mean filter of size k independently to each feature of the sequence, retaining the original input shape by using appropriate padding.

This is implemented as a 1D convolution with a kernel of size k and values 1 / k.

Parameters:
  • x (ndarray[Any, dtype[float64]]) – Observation sequence.

  • k (int) – Width of the filter.

Returns:

The filtered array.

Return type:

numpy.ndarray

Examples

Applying a mean_filter() to a single sequence and multiple sequences (independently via IndependentFunctionTransformer) from the spoken digits dataset.

from sequentia.preprocessing import IndependentFunctionTransformer, mean_filter
from sequentia.datasets import load_digits

# Fetch MFCCs of spoken digits
data = load_digits()

# Apply the mean filter to the first sequence
x, _ = data[0]
xt = mean_filter(x, k=7)

# Create an independent mean filter transform
transform = IndependentFunctionTransformer(mean_filter, kw_args={"k": 7})

# Apply the transform to all sequences
Xt = transform.transform(data.X, lengths=data.lengths)
sequentia.preprocessing.transforms.median_filter(x, *, k=5)

Applies a median filter of size k independently to each feature of the sequence, retaining the original input shape by using appropriate padding.

Parameters:
  • x (ndarray[Any, dtype[float64]]) – Observation sequence.

  • k (int) – Width of the filter.

Returns:

The filtered array.

Return type:

numpy.ndarray

Examples

Applying a median_filter() to a single sequence and multiple sequences (independently via IndependentFunctionTransformer) from the spoken digits dataset.

from sequentia.preprocessing import IndependentFunctionTransformer, median_filter
from sequentia.datasets import load_digits

# Fetch MFCCs of spoken digits
data = load_digits()

# Apply the median filter to the first sequence
x, _ = data[0]
xt = median_filter(x, k=7)

# Create an independent median filter transform
transform = IndependentFunctionTransformer(median_filter, kw_args={"k": 7})

# Apply the transform to all sequences
Xt = transform.transform(data.X, lengths=data.lengths)