segment
- ketos.audio.utils.misc.segment(x, win_len, step_len, num_segs=None, offset_len=0, pad_mode='reflect', mem_warning=True)[source]
Divide an array into segments of equal length along its first axis (0), each segment being shifted by a fixed amount with respetive to the previous segment.
If offset_len is negative the input array will be padded with its own inverted reflection on the left.
If the combined length of the segments exceeds the length of the input array (minus any positive offset), the array will be padded with its own inverted reflection on the right.
- Args:
- x: numpy.array
The data to be segmented
- win_len: int
Window length in no. of samples
- step_len: float
Step size in no. of samples
- num_segs: int
Number of segments. Optional.
- offset_len: int
Position of the first frame in no. of samples. Defaults to 0, if not specified.
- pad_mode: str
Padding mode. Select between ‘reflect’ (default) and ‘zero’.
- mem_warning: bool
Print warning if the size of the array exceeds 10% of the available memory.
- Returns:
- segs: numpy.array
Segmented data, has shape (num_segs, win_len, x.shape[1:])
- Example:
>>> from ketos.audio.utils.misc import segment >>> x = np.arange(10) >>> print(x) [0 1 2 3 4 5 6 7 8 9] >>> y = segment(x, win_len=4, step_len=2, num_segs=3, offset_len=0) >>> print(y) [[0 1 2 3] [2 3 4 5] [4 5 6 7]] >>> y = segment(x, win_len=4, step_len=2, num_segs=3, offset_len=-3) >>> print(y) [[3 2 1 0] [1 0 1 2] [1 2 3 4]]