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
signal (
ndarray
) – input signal in shape(samples,)
or(channels, samples)
sampling_rate (
int
) – sampling rate in Hzwin_dur (
Union
[float
,int
,str
,Timedelta
]) – window duration, if value is as a float or integer it is treated as seconds. Seeaudinterface.utils.to_timedelta()
for further optionshop_dur (
Union
[float
,int
,str
,Timedelta
]) – hop duration, if value is as a float or integer it is treated as seconds. Seeaudinterface.utils.to_timedelta()
for further options
- Return type
- Returns
view of signal with shape
(channels, samples, frames)
- Raises
ValueError – if
win_dur
orhop_dur
is smaller than1/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.]])