Skip to content

Fixed Sliding Window

A fixed sliding window is an abstraction that can be used to compute features across a signal using a sliding window. The window size and stride are fixed upon instantiation by the user. This operation can be represented as:

\[ f_{i} = \text{F}(x_{i - w}, \dots, x_{i - 1}, x_{i}, \dots, x_{i + w}), \quad \forall i \in \{w, \dots, N - w\} \]

where \(F\) represents a feature extractor function, \(f_{i}\) represents the \(i\)th feature, \(x_{i}\) represents the \(i\)th element of the input signal, \(w\) represents the window size, and \(N\) represents the number of elements in the signal.

Here's a visual illustration of the above:

FixedSlidingWindow

Overflow can occur when the window extends beyond the bounds of the signal. This can be handled in a number of ways. The default behavior is to pad the signal with zeros. However, we provide several other options for handling overflow.

We provide some examples below on how to combine the fixed sliding window abstraction with feature extractors to compute features across a signal.

Bases: object

Represents a 1D sliding window over a time series signal.

__init__(window_size, step_size, overflow='pad', padding=0)

Initialize a new 1D sliding window.

Parameters:

Name Type Description Default
window_size Union[int, int_]

The size of the window.

required
step_size Union[int, int_]

The step size of the window.

required
overflow str

The method to take when there is an overflow of the window compared to the signal. Default is pad. Valid methods:

restrict - Reduces the window to the end of the signal (results in performance degradation due to broken cache lines).

pad - Pad signal with value passed to argument padding.

stop - Skips computation of feature over final window that contains overflow.

'pad'
padding Union[int, float, int_, float_]

The value to pad the signal with in the case of an overflow. Default is 0.

0

__repr__()

Get the string representation of the sliding window.

Returns:

Type Description
str

The string representation of the sliding window.

__str__()

Get the string representation of the sliding window.

Returns:

Type Description
str

The string representation of the sliding window.

get_overflow()

Get the overflow method.

Returns:

Type Description
str

The overflow method.

get_overflow_methods()

Get the overflow methods supported.

Returns:

Type Description
List[str]

A list of supported overflow methods.

get_padding()

Get the padding value.

Returns:

Type Description
Union[int, float, int_, float_]

The padding value.

get_step_size()

Get the step size.

Returns:

Type Description
Union[int, int_]

The step size.

get_window_size()

Get the window size.

Returns:

Type Description
Union[int, int_]

The window size.

set_overflow(overflow)

Set the overflow method.

Parameters:

Name Type Description Default
overflow str

The overflow method to set.

required

Raises:

Type Description
ValueError

If the overflow method is not supported.

set_padding(padding)

Set the padding value.

Parameters:

Name Type Description Default
padding Union[int, float, int_, float_]

The padding value to set.

required

set_step_size(step_size)

Set the step size.

Parameters:

Name Type Description Default
step_size Union[int, int_]

The step size.

required

Raises:

Type Description
TypeError

If the step size is not an integer.

set_window_size(window_size)

Set the window size.

Parameters:

Name Type Description Default
window_size Union[int, int_]

The window size.

required

Raises:

Type Description
TypeError

If the window size is not an integer.

use(transform)

Use a transform function to transform each window.

Parameters:

Name Type Description Default
transform Callable[[ndarray], Union[float_, int_]]

The transformation to apply to the signal.

required

Returns:

Type Description
Callable[[ndarray], ndarray]

A function that applies the transformation to the signal using the sliding window.

Raises:

Type Description
TypeError

If the transform is not callable.

Examples

Problem Setup

Consider the following example with signal \(x\), window \(W\) and feature extractor \(F\):

\(t = \left[0, \dots, 10\right]\)

\(x = \sin(2t) + \cos(3t) + \sin(5t) + \cos(7t) + \exp(-t / 5)\)

\(W_{size} = 50\)

\(W_{stride} = 25\)

\(F = \text{mean}\)

import numpy as np
import autonfeat as aft

# Setup the signal
t = np.linspace(0, 10, 1000)
signal = np.sin(2 * t) + np.cos(3 * t) + np.sin(5 * t) + np.cos(7 * t) + np.exp(-t / 5)

Setup Sliding Window and Feature Extractor

# Setup the sliding window
window_size = 50
step_size = 25
sliding_window = aft.SlidingWindow(window_size=window_size, step_size=step_size)

# Setup the feature extractor
feature_extractor = aft.MeanTransform()

# Get the featurizer object
featurizer = sliding_window.use(feature_extractor)

Extract Features

# Extract features
features = featurizer(signal)

print(features)

We can view the following operation below:

FixedSlidingWindow

If you enjoy using AutonFeat, please consider starring the repository ⭐️.