Skip to content

Instantly share code, notes, and snippets.

@eickenberg
Created July 11, 2016 12:19
Show Gist options
  • Save eickenberg/8e27c758511841022b801cef333ed284 to your computer and use it in GitHub Desktop.
Save eickenberg/8e27c758511841022b801cef333ed284 to your computer and use it in GitHub Desktop.
Code for periodizing a signal in Fourier (corresponds to subsampling in signal space)
# Code for periodizing a signal in Fourier (corresponds to subsampling in signal space)
def periodize_1d(array, axis, downsampling):
axis_shape = array.shape[axis]
before_shape = array.shape[:axis]
after_shape = array.shape[axis + 1:]
new_shape = before_shape + (downsampling, -1) + after_shape
return array.reshape(new_shape).mean(axis)
def periodize(array, axes, stride):
axes = tuple(np.atleast_1d(axes))
downsampling = tuple(np.atleast_1d(stride))
if len(downsampling) == 1:
downsampling = downsampling * len(axes)
for axis, ds in zip(axes, downsampling):
array = periodize_1d(array, axis, ds)
return array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment