Created
April 17, 2018 22:15
-
-
Save rhee-airilab/0bd802562a51859d75b17ea935c9f046 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import numpy as np | |
from numpy.lib.stride_tricks import as_strided | |
def make_sequence(y, frame_length, hop_length=1): | |
'''Slice a time series into overlapping frames. | |
code derived from: | |
https://librosa.github.io/librosa/_modules/librosa/util/utils.html | |
This implementation uses low-level stride manipulation to avoid | |
redundant copies of the time series data. | |
code test: | |
# code derived from https://librosa.github.io/librosa/_modules/librosa/util/utils.html | |
from numpy.lib.stride_tricks import as_strided | |
frame_length = 360 | |
hop_length = 1 | |
n_frames = 1 + int((len(y) - frame_length) / hop_length) | |
n_vars = y.shape[1] | |
yy = as_strided(y, shape=(n_frames,frame_length,n_vars), | |
strides=(y.itemsize*n_vars,y.itemsize*n_vars,y.itemsize*hop_length)) | |
print(yy[0,1,0]) | |
print(yy[1,0,0]) | |
assert np.all(yy[1:,:-1,:] == yy[:-1,1:,:]) | |
''' | |
assert isinstance(y, np.ndarray),type(y) | |
assert len(y) >= frame_length,len(y) | |
assert hop_length >= 1,hop_length | |
assert y.flags['C_CONTIGUOUS'],y.flags | |
# Compute the number of frames that will fit. The end may get truncated. | |
n_frames = 1 + int((len(y) - frame_length) / hop_length) | |
n_vars = y.shape[1] | |
# 1st and 2nd stride is n_vars sample | |
# 3rd stride is `hop_length` samples | |
y_frames = as_strided(y, shape=(n_frames,frame_length,n_vars), | |
strides=(y.itemsize*n_vars,y.itemsize*n_vars,y.itemsize*hop_length)) | |
return y_frames |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment