sliding_window()

audinterface.utils.sliding_window(signal, sampling_rate, win_dur, hop_dur)[source]

Reshape signal by applying a sliding window.

Windows that do not match the specified duration at the end of the signals will be dropped.

Parameters
Return type

ndarray

Returns

view of signal with shape (channels, samples, frames)

Raises

ValueError – if win_dur or hop_dur is smaller than 1/sampling_rate

Examples

>>> signal = np.array(
...     [
...         [0, 1, 2, 3, 4, 5],
...         [0, 10, 20, 30, 40, 50],
...     ],
... )
>>> signal
array([[ 0,  1,  2,  3,  4,  5],
       [ 0, 10, 20, 30, 40, 50]])
>>> frames = sliding_window(
...     signal,
...     sampling_rate=1,
...     win_dur=3,
...     hop_dur=2,
... )
>>> # First frame
>>> frames[..., 0]
array([[ 0,  1,  2],
       [ 0, 10, 20]])
>>> # Last frame
>>> frames[..., -1]
array([[ 2,  3,  4],
       [20, 30, 40]])
>>> # Mean per frame
>>> frames.mean(axis=1)
array([[ 1.,  3.],
       [10., 30.]])