我有一个重复的信号,它会随着一个进程的每个周期而变化,这个周期大约每秒钟重复一次,尽管在某些参数范围内,每个周期的持续时间和内容会有所不同。每秒钟我的信号数据有一千个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包含信号数据,包括将被替换为抛物线数据的损坏段,该数据段需要由该函数计算。在
所以,据我所知,你有3个点,你想拟合一条抛物线。在
通常,使用numpy.polyfit是最简单的方法,但是如果你真的担心速度,而且你正好拟合了三个点,那么使用最小二乘拟合就没有意义了。在
相反,我们有一个均匀确定的系统(将抛物线拟合到3x,y点),我们可以用简单的线性代数得到精确的解。在
所以,总而言之,你可以这样做(大部分都是绘制数据):
{1美元^
希望这有点帮助。。。在
相关问题 更多 >
编程相关推荐