填补角度列表(数字)之间的空白

filling the gaps between list of angles (numbers)

我先用简单的例子来解释,然后再深入 如果我有一个数字列表包括

t_original = [180,174,168,166,162,94,70,80,128,131,160,180]

如果我们绘制此图表,使其从 180 下降到 70,然后再次上升到 180

但如果我们突然将第四个值 (166) 更改为 450,那么列表将是

t = [180,174,168,700,162,94,70,80,128,131,160,180]

图中哪个没有意义

我想将第四个值 (700) 视为错误值 我想用相对值替换它,即使不是原始值而是相对于前两个元素 (168,174) 如果另一个错误的值再次出现,我想对整个列表做同样的事情

我们可以称之为[填补数字列表之间的空白]

所以我尝试做同样的想法,但为了更大的例子

我试过的方法

我将与输出共享我的代码,过滤意味着应用填充间隙函数

我的代码

def preprocFN(*U):
prePlst=[] # after preprocessing list
#preprocessing Fc =| 2*LF1 prev by 1 - LF2 prev by 2 |
c0 = -2     #(previous) by 2
c1 =-1      #(previous)
c2 =0       #(current)
c3 = 1      #(next)
preP = U[0] # original list
if c2 == 0:
    prePlst.append(preP[0])
    prePlst.append(preP[1])
    c1+=2
    c2+=2
    c0+=2
oldlen = len(preP)
while oldlen > c2:
    Equ = abs(2*preP[c1] - preP[c0]) #fn of preprocessing #removed abs()
    formatted_float = "{:.2f}".format(Equ) #with .2 number only
    equu = float(formatted_float)          #from string float to float
    prePlst.insert(c2,equu)      # insert the preprocessed value to the List
    c1+=1
    c2+=1
    c0+=1

return prePlst

我的输入:https://textuploader.com/t1py9

输出将是:https://textuploader.com/t1pyk

并且打印大于 180 的值时(错误值)

result_list = [item for item in list if item > 180]

人类的任何一个关节都可以通过180度的角度是没有意义的

the output was [183.6, 213.85, 221.62, 192.05, 203.39, 197.22, 188.45, 182.48, 180.41, 200.09, 200.67, 198.14, 199.44, 198.45, 200.55, 193.25, 204.19, 204.35, 200.59, 211.4, 180.51 , 183.4, 217.91, 218.94, 213.79, 205.62, 221.35, 182.39, 180.62, 183.06, 180.78, 231.09, 227.33, 224.49, 237.02, 212.53, 207.0, 212.92, 182.28, 254.02, 232.49, 224.78, 193.92, 216.0, 184.82, 214.68 , 182.04, 181.07, 234.68, 233.63, 182.84, 193.94, 226.8, 223.69, 222.77, 180.67, 184.72, 180.39, 183.99, 186.44, 233.35, 228.02, 195.31, 183.97, 185.26, 182.13, 207.09, 213.21, 238.41, 229.38, 181.57 , 211.19, 180.05, 181.47, 199.69, 213.59, 191.99, 194.65, 190.75, 199.93, 221.43, 181.51, 181.42, 180.22]

所以建议的方法中的填补空白 fn 并不能完成它的工作 对以不同方式应用相同概念有何建议?

额外信息可能有帮助

过滤图由填充间隙函数和应用归一化函数组成 我不认为问题出在归一化函数上,因为我认为填充间隙函数的输出不正确,也许我错了,但无论如何我提供了归一化步骤,以便您了解最终过滤图的制作方式

fn :

我的代码:

def outLiersFN(*U):
outliers=[] # after preprocessing list
#preprocessing Fc =| 2*LF1 prev by 1 - LF2 prev by 2 |
c0 = -2     #(previous) by 2 #from original
c1 =-1      #(previous)      #from original
c2 =0       #(current)       #from original
c3 = 1      #(next)          #from original
preP = U[0] # original list
if c2 == 0:
    outliers.append(preP[0])
    c1+=1
    c2+=1
    c0+=1
    c3+=1
oldlen = len(preP)
M_RangeOfMotion = 90
while oldlen > c2 :
    if c3 == oldlen:
        outliers.insert(c2, preP[c2]) #preP[c2] >> last element in old list
        break
    if (preP[c2] > M_RangeOfMotion and preP[c2] < (preP[c1] + preP[c3])/2) or (preP[c2] < M_RangeOfMotion and preP[c2] > (preP[c1] + preP[c3])/2): #Check Paper 3.3.1
        Equ = (preP[c1] + preP[c3])/2 #fn of preprocessing # From third index # ==== inserting current frame
        formatted_float = "{:.2f}".format(Equ) #with .2 number only
        equu = float(formatted_float)          #from string float to float
        outliers.insert(c2,equu)      # insert the preprocessed value to the List
        c1+=1
        c2+=1
        c0+=1
        c3+=1
    else :
        Equ = preP[c2] # fn of preprocessing #put same element (do nothing)
        formatted_float = "{:.2f}".format(Equ)  # with .2 number only
        equu = float(formatted_float)  # from string float to float
        outliers.insert(c2, equu)  # insert the preprocessed value to the List
        c1 += 1
        c2 += 1
        c0 += 1
        c3 += 1
return outliers

我建议采用以下算法:

  • 数据点 t[i] 如果偏离 t[i-2], t[i-1], t[i], t[i+1], t[i+2] 的平均值超过这 5 个元素的标准偏差,则被视为异常值。
  • 异常值被周围两个元素的平均值所取代。
import matplotlib.pyplot as plt
from statistics import mean, stdev

t = [180,174,168,700,162,94,70,80,128,131,160,180]

def smooth(t):
    new_t = []
    for i, x in enumerate(t):
        neighbourhood = t[max(i-2,0): i+3]
        m = mean(neighbourhood)
        s = stdev(neighbourhood, xbar=m)
        if abs(x - m) > s:
            x = ( t[i - 1 + (i==0)*2] + t[i + 1 - (i+1==len(t))*2] ) / 2
        new_t.append(x)
    return new_t

new_t = smooth(t)

plt.plot(t)
plt.plot(new_t)
plt.show()