from IPython.core.display import Image Image(url='http://labrosa.ee.columbia.edu/crucialpython/logo.png', width=600) # Get our pylab mise en place going import numpy as np import matplotlib.pyplot as plt # I'll make up a time series of sequential data # x = np.arange(8 * 16) # And print it print x frame_length = 8 x_framed = np.reshape(x, (-1, frame_length)) print x_framed hop_length = frame_length / 2 num_frames = 1 + (len(x) - frame_length) / hop_length x_framed_overlap = np.zeros( (num_frames, frame_length), dtype=int ) for t in range(num_frames): x_framed_overlap[t, :] = x[t*hop_length:t*hop_length + frame_length] print x_framed_overlap from numpy.lib import stride_tricks # Ok, it's a linear index, so it's both F- and C-contiguous # Next, let's see how big each item in x is. This is provided by the `itemsize` field print x.itemsize, x.dtype # Now, we're ready to set up our fake frmaed data # Each row advances by `hop_length` entries, times the number of bytes in each entry row_stride = x.itemsize * hop_length # Columns are still contiguous, and only advance by one entry col_stride = x.itemsize # as_strided will construct a new view of the data in x, using the shape and stride parameters we supply x_framed_strided = stride_tricks.as_strided(x, shape=(num_frames, frame_length), strides=(row_stride, col_stride)) print x_framed_strided # What if we want our frames vertical instead? Just swap the row and column strides x_framed_strided_column = stride_tricks.as_strided(x, shape=(num_frames, frame_length), strides=(col_stride, row_stride)) print x_framed_strided_column