master
Per A Brodtkorb 8 years ago
parent 5e6cfbe5ee
commit d10324085a

@ -1,7 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import absolute_import, division from __future__ import absolute_import, division
import numpy as np import numpy as np
import scipy.signal
# import scipy.sparse.linalg # @UnusedImport # import scipy.sparse.linalg # @UnusedImport
import scipy.sparse as sparse import scipy.sparse as sparse
from numpy import ones, zeros, prod, sin, diff, pi, inf, vstack, linspace from numpy import ones, zeros, prod, sin, diff, pi, inf, vstack, linspace
@ -106,12 +105,15 @@ def _get_turnpoint(xvals):
turnpoint = 0 turnpoint = 0
last = len(xvals) last = len(xvals)
if xvals[0] < xvals[1]: # x is increasing? if xvals[0] < xvals[1]: # x is increasing?
compare = lambda a, b: a < b def compare(a, b):
return a < b
else: # no, x is decreasing else: # no, x is decreasing
compare = lambda a, b: a > b def compare(a, b):
return a > b
for i in range(1, last): # yes for i in range(1, last): # yes
if compare(xvals[i], xvals[i - 1]): # search where x starts to fall or rise # search where x starts to fall or rise
if compare(xvals[i], xvals[i - 1]):
turnpoint = i turnpoint = i
break break
@ -245,36 +247,44 @@ def sgolay2d(z, window_size, order, derivative=None):
Z = np.zeros((new_shape)) Z = np.zeros((new_shape))
# top band # top band
band = z[0, :] band = z[0, :]
Z[:half_size, half_size:-half_size] = band - np.abs(np.flipud(z[1:half_size + 1, :]) - band) Z[:half_size, half_size:-half_size] = band - \
np.abs(np.flipud(z[1:half_size + 1, :]) - band)
# bottom band # bottom band
band = z[-1, :] band = z[-1, :]
Z[-half_size:, half_size:-half_size] = band + np.abs(np.flipud(z[-half_size - 1:-1, :]) - band) Z[-half_size:, half_size:-half_size] = band + \
np.abs(np.flipud(z[-half_size - 1:-1, :]) - band)
# left band # left band
band = np.tile(z[:, 0].reshape(-1, 1), [1, half_size]) band = np.tile(z[:, 0].reshape(-1, 1), [1, half_size])
Z[half_size:-half_size, :half_size] = band - np.abs(np.fliplr(z[:, 1:half_size + 1]) - band) Z[half_size:-half_size, :half_size] = band - \
np.abs(np.fliplr(z[:, 1:half_size + 1]) - band)
# right band # right band
band = np.tile(z[:, -1].reshape(-1, 1), [1, half_size]) band = np.tile(z[:, -1].reshape(-1, 1), [1, half_size])
Z[half_size:-half_size, -half_size:] = band + np.abs(np.fliplr(z[:, -half_size - 1:-1]) - band) Z[half_size:-half_size, -half_size:] = band + \
np.abs(np.fliplr(z[:, -half_size - 1:-1]) - band)
# central band # central band
Z[half_size:-half_size, half_size:-half_size] = z Z[half_size:-half_size, half_size:-half_size] = z
# top left corner # top left corner
band = z[0, 0] band = z[0, 0]
Z[:half_size, :half_size] = band - \ Z[:half_size, :half_size] = band - \
np.abs(np.flipud(np.fliplr(z[1:half_size + 1, 1:half_size + 1])) - band) np.abs(
np.flipud(np.fliplr(z[1:half_size + 1, 1:half_size + 1])) - band)
# bottom right corner # bottom right corner
band = z[-1, -1] band = z[-1, -1]
Z[-half_size:, -half_size:] = band + \ Z[-half_size:, -half_size:] = band + \
np.abs(np.flipud(np.fliplr(z[-half_size - 1:-1, -half_size - 1:-1])) - band) np.abs(
np.flipud(np.fliplr(z[-half_size - 1:-1, -half_size - 1:-1])) - band)
# top right corner # top right corner
band = Z[half_size, -half_size:] band = Z[half_size, -half_size:]
Z[:half_size, -half_size:] = band - \ Z[:half_size, -half_size:] = band - \
np.abs(np.flipud(Z[half_size + 1:2 * half_size + 1, -half_size:]) - band) np.abs(
np.flipud(Z[half_size + 1:2 * half_size + 1, -half_size:]) - band)
# bottom left corner # bottom left corner
band = Z[-half_size:, half_size].reshape(-1, 1) band = Z[-half_size:, half_size].reshape(-1, 1)
Z[-half_size:, :half_size] = band - \ Z[-half_size:, :half_size] = band - \
np.abs(np.fliplr(Z[-half_size:, half_size + 1:2 * half_size + 1]) - band) np.abs(
np.fliplr(Z[-half_size:, half_size + 1:2 * half_size + 1]) - band)
# solve system and convolve # solve system and convolve
sgn = {None: 1}.get(derivative, -1) sgn = {None: 1}.get(derivative, -1)
@ -597,7 +607,8 @@ class SmoothSpline(PPform):
di = di.T di = di.T
ci = ci.T ci = ci.T
ai = ai.T ai = ai.T
coefs = vstack([val.ravel() for val in [di, ci, bi, ai] if val.size>0]) coefs = vstack([val.ravel()
for val in [di, ci, bi, ai] if val.size > 0])
return coefs return coefs
def _compute_coefs(self, xx, yy, p=None, var=1): def _compute_coefs(self, xx, yy, p=None, var=1):
@ -978,6 +989,7 @@ class StinemanInterp(object):
-------- --------
slopes, Pchip slopes, Pchip
''' '''
def __init__(self, x, y, yp=None, method='parabola', monotone=False): def __init__(self, x, y, yp=None, method='parabola', monotone=False):
if yp is None: if yp is None:
yp = slopes(x, y, method, monotone=monotone) yp = slopes(x, y, method, monotone=monotone)
@ -1214,7 +1226,6 @@ def test_smoothing_spline():
plt.plot(x, y, x1, y1, '.', x1, dy1, 'ro', x1, y01, 'r-') plt.plot(x, y, x1, y1, '.', x1, dy1, 'ro', x1, y01, 'r-')
plt.show('hold') plt.show('hold')
pass
# tck = interpolate.splrep(x, y, s=len(x)) # tck = interpolate.splrep(x, y, s=len(x))

Loading…
Cancel
Save