大佬教程收集整理的这篇文章主要介绍了用由少数元素构成的向量平滑曲线?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有 4 条曲线由这些向量表示:
x = [300,700,1000,1500]
y1 = [-1.0055394199673442,-0.11221578805214968,-1.502661406039569,1.0216939169819494]
y2 = [-1.0200777228890747,-0.6951505674297687,-2.832988761335546,1.0253075071285915]
y3 = [2.0502387421569463,-1.3363305947335058,0.2893545237634795,0.8692051683379767]
y4 = [1.8676528391899183,-1.7554177636905024,0.2364994810496486,0.9811885784744991]
当我绘制它们时,我得到了这样的东西:
如您所见,这些向量的少量值导致这些曲线呈锯齿状,我希望它们更平滑。我在 Python 和 Matlab 中尝试了不同的方法。例如,在 python 中,我使用了 numpy 方法,如:
xp = np.linspace(300,1500,100,endpoint=TruE)
z1 = np.polyfit(x,y1,3)
p1 = np.poly1d(z1)
z2 = np.polyfit(x,y2,3)
p2 = np.poly1d(z2)
z3 = np.polyfit(x,y3,3)
p3 = np.poly1d(z3)
z4 = np.polyfit(x,y4,3)
p4 = np.poly1d(z4)
我得到了这个:
但是这样,对于 x 向量之间的值,存在我不确定的最小值和最大值点。 此外,我在 Matlab 中尝试了具有不同平滑值的“平滑”函数,但它们也更改了与原始向量不同的向量内的值,即它们对于我想解释的概念毫无意义。 我只想平滑一点情节的锯齿形部分,使曲线更美观..你能帮我吗? Python 或 Matlab 对我来说都是一样的,我对一切都持开放态度! :)
好吧,所以我在这里提议的是作弊和发明数据,但至少它使曲线看起来更像你(或你的主管)想要的。
x = [300,700,1000,1500] # your original x
x2 = [300,500,850,1250,1500] # add points in between
# interpolate your data for the new points in x2
p1 = np.interp(x2,x,y1)
p2 = np.interp(x2,y2)
p3 = np.interp(x2,y3)
p4 = np.interp(x2,y4)
# cubic splinE interpolation on xp,so it looks smooth
p1 = scipy.interpolate.CubicSpline(x2,p1)
p2 = scipy.interpolate.CubicSpline(x2,p2)
p3 = scipy.interpolate.CubicSpline(x2,p3)
p4 = scipy.interpolate.CubicSpline(x2,p4)
这是它的样子:
如果您对它的外观不满意,您可以在 x2
中尝试不同的值。
编辑:
以下是生成绘图的完整代码:
import numpy as np
from scipy.interpolate import CubicSpline
import matplotlib.pyplot as plt
x = [300,1500] # your orginial x
x2 = [300,1500] # add points in between
xp = np.linspace(300,1500,100,endpoint=TruE) # your x-axis for smooth curve plot
# your orginal data
y1 = [-1.0055394199673442,-0.11221578805214968,-1.502661406039569,1.0216939169819494]
y2 = [-1.0200777228890747,-0.6951505674297687,-2.832988761335546,1.0253075071285915]
y3 = [2.0502387421569463,-1.3363305947335058,0.2893545237634795,0.8692051683379767]
y4 = [1.8676528391899183,-1.7554177636905024,0.2364994810496486,0.9811885784744991]
for yi in [y1,y2,y3,y4]:
# Piecewise linear interpolation of data y over the points x2
y_interpolated_over_x2 = np.interp(x2,yi)
# Make a cubic spline from the manipulated data
y_cubic_spline = CubicSpline(x2,y_interpolated_over_x2)
# The smooth curve is the cubic spline evaluated at points xp
y_smooth = y_cubic_spline(xp)
plt.plot(xp,y_smooth) # plot the smooth curve
plt.scatter(x,yi) # plot the original data points
plt.show()
以上是大佬教程为你收集整理的用由少数元素构成的向量平滑曲线?全部内容,希望文章能够帮你解决用由少数元素构成的向量平滑曲线?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。