Python-多段折線Lut擬合工具

用途:給定折斷點坐標信息,插值出折線圖上對應點的y值
例如:

輸入: cut_dots=np.array([[0,0.5],[8,0.65],[18,0.996],[63,0.996]]) #need min/max
擬合結果圖
輸出: 0.5 0.519 0.538 0.556 0.575 0.594 0.612 0.631 0.65 0.685 0.719 0.754 0.788 0.823 0.858 0.892 0.927 0.961 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996

完整代碼

import numpy as np
import matplotlib.pyplot as plt

def write2txt(txt_path,write_array,type):
    with open(txt_path,'w') as f:
        for i in range(write_array.size):
            if type=='float':
                print(np.around(write_array[i],3),end=' ',file=f)
                print(np.around(write_array[i],3),end=' ')
            elif type=='int':
                print(write_array[i].astype(int),end=' ',file=f)
                print(write_array[i].astype(int),end=' ')

def line_mode(cut_dots):
    cut_dots_T = cut_dots.T
    x = cut_dots_T[0]
    y = cut_dots_T[1]
    table_x = np.linspace(x[0],x[-1],x[-1]-x[0]+1)

    for i in range(x.size-1):
        if i==0:
            table_y = np.linspace(y[0],y[1],x[1]-x[0]+1)
        else:
            table_y = np.concatenate((table_y, np.linspace(y[i],y[i+1],x[i+1]-x[i]+1)[1:]))
    plt.plot(table_x, table_y)
    plt.show()
    return(table_x,table_y)

if __name__ == '__main__':
    # 1. 定義折點,須包括第一個和最后一個點(務必確保最小和最大的x值),定義輸出值類型
    cut_dots=np.array([[0,0.5],[8,0.65],[18,0.996],[63,0.996]]) #need min/max
    type = 'float'#'int'
    # 2. 繪制多段折線圖,并返回擬合的y值
    table_x,table_y = line_mode(cut_dots)
    # 3. 輸出&輸出為txt,指定輸出文件名
    txt = r'table_y.txt'

    write2txt(txt,table_y,type)
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容