scipy 绘制B样条

import numpy as np
import pylab as pl
from scipy import interpolate
import random
x_end = 2*np.pi

interpolate_cnt = 10000


def spline_interpolate(x, y):
    x_new = np.linspace(0, x_end, interpolate_cnt)
    tck = interpolate.splrep(x, y)  # 这个必须有,splrep()的结果作为splev()的第二个参数
    y_spline = interpolate.splev(x_new, tck, 0)
    return x_new, y_spline


def spline_der(x, y, der):
    tck = interpolate.splrep(x, y)
    x_new = np.linspace(0, x_end, interpolate_cnt)
    # d = interpolate.splev(x_new, tck, 0)
    d = interpolate.splev(x_new, tck, der)
    return d

##############################################################################


samples = [0, 0.25, 0, -.5] * 5
# samples = [np.sin(x/10) for x in range(0, 100, 1)]
x = np.linspace(0, x_end, len(samples))
y = np.array(samples)
little = .5
pl.plot([0, x_end], [little, little], '--')  # 水平直线
pl.plot([0, x_end], [-little, -little], '--')

##############################################################################

x_new, y_spline = spline_interpolate(x, y)
der2 = spline_der(x_new, y_spline, 2)
der3 = spline_der(x_new, y_spline, 3)


pl.plot(x, y, '*', markersize=20,  label='pts')
pl.plot(x_new, y_spline, 'y', linewidth=3, label='B_spline interpolation')
# pl.plot(x_new, der2, 'b', linewidth=3, label='D2')
# pl.plot(x_new, der3, 'r', linewidth=3, label='D3')
pl.legend()
pl.show()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值