Skip to content

Instantly share code, notes, and snippets.

@daguiam
Created November 2, 2017 15:59
Show Gist options
  • Save daguiam/1046aed15358d7f7550ec23964001342 to your computer and use it in GitHub Desktop.
Save daguiam/1046aed15358d7f7550ec23964001342 to your computer and use it in GitHub Desktop.
""" Calculate mean std of clipped array"""
import numpy as np
from scipy import interpolate
def calc_func_clipped(X, Y, func=np.mean, npoints=None, verbose=False):
""" Calculates func of Y values, where Y is clipped to the longest continuous
values of X to prevent aliasing
"""
# Size of each data vector
npoints = npoints or Y.shape[1]
# Finding consecutive value measurements
xmax = np.inf
xmin = -np.inf
for x in X:
if x.max() < xmax:
xmax = x.max()
if x.min() > xmin:
xmin = x.min()
if verbose:
print 'Clipped to:', xmin,xmax
# Clipping data
newx = np.linspace(xmin, xmax, npoints)
newy = []
for x,y in zip(X,Y):
valid_idx = (x>xmin)*(x<xmax)
f = interpolate.interp1d(x,y)
yi = f(newx)
newy.append(yi)
if verbose:
print newy
# Calculating function
newy = np.array(newy)
newy = func(newy, axis=0)
return newx, newy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment