简单平滑数据的功能

时间:2018-06-05 15:26:10

标签: python python-3.x function filtering

我不确定如何编写此函数并需要有关如何编写它的帮助。我有一维数据和一个列表。在绘制数据时,它看起来像这样: enter image description here

该列表包含发生步骤更改的数组的索引。在这种情况下,列表包含一个项目,其值为34.我希望该函数通过用直线替换它来平滑数组中的数据,从array [0]到array [34](该项中的项目)然后,将数组[35]中的剩余数据点平滑到数组[n]。

这是一个简单的数据示例,但由于可能会有多个步骤更改,因此可能会变得更加复杂。这是一个伪代码,但同样,我不知道如何写它。

def funcx (x, list_):
    for each set of points between list items
    replace with linear line
return(updated array) 

这是数组

x = [515.294128,  513.52941899999996,  517.05883800000004,  514.70593299999996,  518.23535200000003,  516.470642,  520.52630599999998,  520.0,  523.15783699999997,  523.15783699999997,  526.31573500000002,  525.26312299999995,  532.94116199999996,  543.68420400000002,  538.823486,  533.52941899999996,  540.52630599999998,  541.05267300000003,  541.05267300000003,  543.15789800000005,  545.26318400000002,  544.21057099999996,  546.84210199999995,  547.89477499999998,  548.94738800000005,  550.35717799999998,  550.714294,  551.42858899999999,  553.92858899999999,  556.785706,  548.94738800000005,  555.35717799999998,  571.90478499999995,  578.09521500000005,  97.647041000000002,  100.0,  101.333336,  101.333336,  101.333336,  103.333336,  108.0,  108.0,  110.0,  110.0,  112.85714,  113.571426,  117.14286,  116.428574,  120.0,  120.0,  120.5,  121.25,  121.25,  123.125,  126.25,  127.5,  129.375,  132.30767800000001,  133.84612999999999,  135.38459800000001,  134.61537200000001,  136.92306500000001,  135.38459800000001,  137.69229100000001,  142.5,  143.125,  146.25,  146.25,  148.125,  149.375,  149.99998500000001,  150.833313,  153.333313,  154.166641,  154.166641,  154.166641,  154.166641,  161.0,  161.0,  169.5,  169.5,  177.777725,  178.88883999999999,  180.625,  181.25,  183.75,  183.75,  185.0,  186.25,  186.25,  188.75,  188.125,  186.875,  188.125,  190.666687,  191.333359,  195.333359,  197.333359,  198.666687,  198.00001499999999,  198.00001499999999,  199.333359,  199.333359,  200.833313,  204.99998500000001,  209.166641,  211.42854299999999,  212.85711699999999,  214.28568999999999,  217.14283800000001,  215.71426400000001,  218.57141100000001,  217.85711699999999,  219.28568999999999,  220.66665599999999,  221.99998500000001,  220.66665599999999,  219.99998500000001,  223.99998500000001,  225.99998500000001,  227.333313,  228.66665599999999,  231.333313,  232.66665599999999,  233.333313,  235.333313,  233.99998500000001,  234.66665599999999,  236.66665599999999,  239.333313,  241.666687,  241.666687,  244.166687,  245.83334400000001,  247.50001499999999,  249.166687,  251.42854299999999,  251.42854299999999,  254.99996899999999,  254.28568999999999,  258.57141100000001,  259.285706,  259.285706,  261.875,  259.285706,  261.875,  264.375,  266.875]

1 个答案:

答案 0 :(得分:1)

您可以使用np.polyfit订单1,即线性函数,np.polyval执行此任务:

import numpy as np
#linear fit function for an array
def smoothline(arr):
    n = np.arange(len(arr))
    #retrieve the polynomial coefficients of a least-square fit
    fit = np.polyfit(n, arr, 1)
    #return the array with fitted data points
    return np.polyval(fit, n)

raw_arr = np.asarray( [515.294128, 513.52941899999996, 517.05883800000004, 514.70593299999996, 518.23535200000003, 516.470642, 520.52630599999998, 520.0, 523.15783699999997, 523.15783699999997, 526.31573500000002, 525.26312299999995, 532.94116199999996, 543.68420400000002, 538.823486, 533.52941899999996, 540.52630599999998, 541.05267300000003, 541.05267300000003, 543.15789800000005, 545.26318400000002, 544.21057099999996, 546.84210199999995, 547.89477499999998, 548.94738800000005, 550.35717799999998, 550.714294, 551.42858899999999, 553.92858899999999, 556.785706, 548.94738800000005, 555.35717799999998, 571.90478499999995, 578.09521500000005, 97.647041000000002, 100.0, 101.333336, 101.333336, 101.333336, 103.333336, 108.0, 108.0, 110.0, 110.0, 112.85714, 113.571426, 117.14286, 116.428574, 120.0, 120.0, 120.5, 121.25, 121.25, 123.125, 126.25, 127.5, 129.375, 132.30767800000001, 133.84612999999999, 135.38459800000001, 134.61537200000001, 136.92306500000001, 135.38459800000001, 137.69229100000001, 142.5, 143.125, 146.25, 146.25, 148.125, 149.375, 149.99998500000001, 150.833313, 153.333313, 154.166641, 154.166641, 154.166641, 154.166641, 161.0, 161.0, 169.5, 169.5, 177.777725, 178.88883999999999, 180.625, 181.25, 183.75, 183.75, 185.0, 186.25, 186.25, 188.75, 188.125, 186.875, 188.125, 190.666687, 191.333359, 195.333359, 197.333359, 198.666687, 198.00001499999999, 198.00001499999999, 199.333359, 199.333359, 200.833313, 204.99998500000001, 209.166641, 211.42854299999999, 212.85711699999999, 214.28568999999999, 217.14283800000001, 215.71426400000001, 218.57141100000001, 217.85711699999999, 219.28568999999999, 220.66665599999999, 221.99998500000001, 220.66665599999999, 219.99998500000001, 223.99998500000001, 225.99998500000001, 227.333313, 228.66665599999999, 231.333313, 232.66665599999999, 233.333313, 235.333313, 233.99998500000001, 234.66665599999999, 236.66665599999999, 239.333313, 241.666687, 241.666687, 244.166687, 245.83334400000001, 247.50001499999999, 249.166687, 251.42854299999999, 251.42854299999999, 254.99996899999999, 254.28568999999999, 258.57141100000001, 259.285706, 259.285706, 261.875, 259.285706, 261.875, 264.375, 266.875])
n = len(raw_arr)
smooth_arr = raw_arr.copy()
#predefined breakpoints, for demonstration I added another "break" point
breaks = [34, 87]
#create list of segments from break list and length of the raw data array
segments = [(i, j) for i, j in zip([0] + breaks, breaks + [n])]
#cycle through each array segment
for start, stop in segments:
    #fit each array segment
    smooth_arr[start:stop] = smoothline(raw_arr[start:stop])

#plot raw and fitted array    
plt.plot(np.arange(n), raw_arr, label = "raw")
plt.plot(np.arange(n), smooth_arr, label = "smooth")
plt.legend(loc = "best")
plt.show()

输出:

enter image description here