Search for inflection points in a spline

I have some one-dimensional data and put it with a spline. Then I want to find the inflection points (ignoring the saddle points). Now I am looking for extremes of my first output using scipy.signal.argrelmin (and argrelmax) for the set of values โ€‹โ€‹generated by splev.

import scipy.interpolate
import scipy.optimize
import scipy.signal
import numpy as np
import matplotlib.pyplot as plt
import operator

y = [-1, 5, 6, 4, 2, 5, 8, 5, 1]
x = np.arange(0, len(y))
tck = scipy.interpolate.splrep(x, y, s=0)

print 'roots', scipy.interpolate.sproot(tck)
# output:
# [0.11381478]

xnew = np.arange(0, len(y), 0.01)
ynew = scipy.interpolate.splev(xnew, tck, der=0)

ynew_deriv = scipy.interpolate.splev(xnew, tck, der=1)

min_idxs = scipy.signal.argrelmin(ynew_deriv)
max_idxs = scipy.signal.argrelmax(ynew_deriv)
mins = zip(xnew[min_idxs].tolist(), ynew_deriv[min_idxs].tolist())
maxs = zip(xnew[max_idxs].tolist(), ynew_deriv[max_idxs].tolist())
inflection_points = sorted(mins + maxs, key=operator.itemgetter(0))

print 'inflection_points', inflection_points
# output:
# [(3.13, -2.9822449358974357),
#  (5.03,  4.3817785256410255)
#  (7.13, -4.867132628205128)]

plt.legend(['data','Cubic Spline', '1st deriv'])
plt.plot(x, y, 'o',
        xnew, ynew, '-',
        xnew, ynew_deriv, '-')
plt.show()

But that seems terribly wrong. I think there is an opportunity to find what I am looking for without generating so many values. Something like sproot, but applicable to the second output, maybe?

+5
source share
1 answer

B- B-. , , , -, . / .

: https://gist.github.com/pv/5504366

:

t, c, k = scipys_spline_representation
# Compute the denominator in the differentiation formula.
dt = t[k+1:-1] - t[1:-k-1]
# Compute the new coefficients
d = (c[1:-1-k] - c[:-2-k]) * k / dt
# Adjust knots
t2 = t[1:-1]
# Pad coefficient array to same size as knots (FITPACK convention)
d = np.r_[d, [0]*k]
# Done, a new spline
new_spline_repr = t2, d, k-1

Finding inflection points of a curve via derivative splines

+4

All Articles