2013-05-01 11 views
5

में इन्फ्लिक्शन पॉइंट ढूंढ रहा है मेरे पास कुछ आयामी डेटा है और इसे एक स्पलीन के साथ फिट किया गया है। फिर मैं इसमें इन्फ्लिक्शन पॉइंट्स (सैडल पॉइंट्स को अनदेखा करना) ढूंढना चाहता हूं। अब मैं splev द्वारा उत्पन्न कई मूल्यों पर scipy.signal.argrelmin (और argrelmax) का उपयोग कर अपने पहले व्युत्पन्न की अतिरेक खोज रहा हूं।स्पिनलाइन 1 डी डेटा

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() 

लेकिन यह बहुत गलत लगता है। मुझे लगता है कि इतने सारे मूल्य पैदा किए बिना मैं जो खोज रहा हूं उसे ढूंढने की संभावना है। स्पूट की तरह कुछ लेकिन संभवतः दूसरे व्युत्पन्न पर लागू होता है?

+0

सकारात्मक

यहाँ यह करने के लिए कोड है। ;-) –

उत्तर

4

derivative of a B-spline is also a B-spline। इसलिए आप पहले अपने डेटा में एक स्पलीन फिट कर सकते हैं, फिर व्युत्पन्न स्पलीन के गुणांक बनाने के लिए व्युत्पन्न सूत्र का उपयोग कर सकते हैं, और अंत में व्युत्पन्न स्पलीन की जड़ें प्राप्त करने के लिए स्पलीन रूट खोज का उपयोग कर सकते हैं। ये मूल वक्र की अधिकतम/न्यूनतम सीमा हैं। 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

+1

वाह, बढ़िया, धन्यवाद। मुझे नहीं पता था, यह splines को अलग करने के लिए इतना आसान "था। चूंकि जड़ें() केवल ऑर्डर 3 के लिए काम करती हैं, मुझे लगता है कि अगर मुझे एक्स्ट्रेमा और इन्फ्लिक्शन पॉइंट्स मिलना है तो मुझे अलग-अलग स्प्लिंस का उपयोग करना होगा, लेकिन फिलहाल मेरे लिए यह कोई समस्या नहीं है। अगर किसी को आपकी समस्या के साथ हल करने में कोई दिलचस्पी है: https://ideone.com/qKja7X और http://s21.postimg.org/mbc96qcxj/out.png और https://ideone.com/ m757q9 –

संबंधित मुद्दे