使用numpy在重复信号的一部分内绘制抛物线

2024-09-30 20:17:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个重复的信号,它会随着一个进程的每个周期而变化,这个周期大约每秒钟重复一次,尽管在某些参数范围内,每个周期的持续时间和内容会有所不同。每秒钟我的信号数据有一千个x,y坐标。每个周期内的一个小的但很重要的数据段被破坏了,我想用一条向上的抛物线替换每个损坏的数据段。在

对于每个需要用抛物线代替的数据段,我有三个点的x,y坐标。顶点/最小值就是其中一个点。另外两个点是朝上的U形抛物线的左上和右上。换句话说,左上角是该函数域中x值最小的x,y坐标对,而右上角是该函数域中x值最高的x,y坐标对。左上和右上的y坐标彼此相等,是数据段中最高的两个y值。在

如何编写代码来绘制这个向上抛物线中剩余的数据点?请记住,对于每分钟的数据,此函数需要调用60或70次,并且每次调用此函数时,抛物线的形状/公式都需要更改,以便解释每个生成的抛物线中这三对x、y坐标之间的不同关系。在

def ReplaceCorruptedDataWithParabola(Xarray, Yarray, LeftTopX, LeftTopY
                                     , LeftTopIndex, MinX, MinY, MinIndex
                                     , RightTopX, RightTopY, RightTopIndex):  

    # Step One: Derive the formula for the upward-facing parabola using 
    # the following data from the three points:
        LeftTopX,LeftTopY,LeftTopIndex  
        MinX,MinY,MinIndex  
        RightTopX,RightTopY,RightTopIndex 

    # Step Two: Use the formula derived in step one to plot the parabola in
    # the places where the corrupted data used to reside:
    for n in Xarray[LeftTopX:RightTopX]:
        Yarray[n]=[_**The formula goes here**_]

    return Yarray 

注意:Xarray和Yarray都是单列向量,每个索引处的数据将两个数组作为x,y坐标集链接起来。它们都是numpy阵列。Xarray包含时间信息并且没有变化,但是Yarray包含信号数据,包括将被替换为抛物线数据的损坏段,该数据段需要由该函数计算。在


Tags: the数据函数in信号xarrayformula抛物线
1条回答
网友
1楼 · 发布于 2024-09-30 20:17:36

所以,据我所知,你有3个点,你想拟合一条抛物线。在

通常,使用numpy.polyfit是最简单的方法,但是如果你真的担心速度,而且你正好拟合了三个点,那么使用最小二乘拟合就没有意义了。在

相反,我们有一个均匀确定的系统(将抛物线拟合到3x,y点),我们可以用简单的线性代数得到精确的解。在

所以,总而言之,你可以这样做(大部分都是绘制数据):

import numpy as np                                                                              
import matplotlib.pyplot as plt                                                                 

def main():
    # Generate some random data
    x = np.linspace(0, 10, 100)
    y = np.cumsum(np.random.random(100) - 0.5)

    # Just selecting these arbitrarly 
    left_idx, right_idx = 20, 50      
    # Using the mininum y-value within the arbitrary range
    min_idx = np.argmin(y[left_idx:right_idx]) + left_idx 

    # Replace the data within the range with a fitted parabola
    new_y = replace_data(x, y, left_idx, right_idx, min_idx)  

    # Plot the data
    fig = plt.figure()
    indicies = [left_idx, min_idx, right_idx]

    ax1 = fig.add_subplot(2, 1, 1)
    ax1.axvspan(x[left_idx], x[right_idx], facecolor='red', alpha=0.5)
    ax1.plot(x, y)                                                    
    ax1.plot(x[indicies], y[indicies], 'ro')                          

    ax2 = fig.add_subplot(2, 1, 2)
    ax2.axvspan(x[left_idx], x[right_idx], facecolor='red', alpha=0.5)
    ax2.plot(x,new_y)                                                 
    ax2.plot(x[indicies], y[indicies], 'ro')

    plt.show()

def fit_parabola(x, y):
    """Fits the equation "y = ax^2 + bx + c" given exactly 3 points as two
    lists or arrays of x & y coordinates"""
    A = np.zeros((3,3), dtype=np.float)
    A[:,0] = x**2
    A[:,1] = x
    A[:,2] = 1
    a, b, c = np.linalg.solve(A, y)
    return a, b, c

def replace_data(x, y, left_idx, right_idx, min_idx):
    """Replace the section of "y" between the indicies "left_idx" and
    "right_idx" with a parabola fitted to the three x,y points represented
    by "left_idx", "min_idx", and "right_idx"."""
    x_fit = x[[left_idx, min_idx, right_idx]]
    y_fit = y[[left_idx, min_idx, right_idx]]
    a, b, c = fit_parabola(x_fit, y_fit)

    new_x = x[left_idx:right_idx]
    new_y = a * new_x**2 + b * new_x + c

    y = y.copy() # Remove this if you want to modify y in-place
    y[left_idx:right_idx] = new_y
    return y

if __name__ == '__main__':
    main()

{1美元^

希望这有点帮助。。。在

相关问题 更多 >