轨迹平滑方法-滑动平均平滑(Moving average)

参考文章:https://blog.csdn.net/Dangkie/article/details/53311516

python实现代码:以下为7点2次MA算法

实验结果:

from matplotlib import pyplot as plt
import numpy as np

x = np.linspace(1, 30, 30)  # X轴数据
# 原始数据
a = [1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 13, 11, 12, 14, 18, 19, 21, 22, 22, 23, 24, 25, 26, 27, 28, 29]
y1 = np.array(a)


# 7点2次MA平滑
def MovingAverage(input):
    output = {}
    print(input)
    size = len(input)
    print(size)
    i = 0
    output[0] = (32.0 * input[0] + 15.0 * input[1] + 3.0 * input[2] - 4.0 * input[3] - 6.0 * input[4] - 3.0 * input[
        5] + 5.0 * input[6]) / 42.0
    output[1] = (5.0 * input[0] + 4.0 * input[1] + 3.0 * input[2] + 2.0 * input[3] +
                 input[4] - input[6]) / 14.0
    output[2] = (1.0 * input[0] + 3.0 * input[1] + 4.0 * input[2] + 4.0 * input[3] +
                 3.0 * input[4] + 1.0 * input[5] - 2.0 * input[6]) / 14.0

    for i in range(3, size - 3):
        output[i] = (-2.0 * (input[i - 3] + input[i + 3]) + 3.0 * (input[i - 2] + input[i + 2]) + 6.0 * (
                input[i - 1] + input[i + 1]) + 7.0 * input[i]) / 21.0

    output[size - 3] = (1.0 * input[size - 1] + 3.0 * input[size - 2] + 4.0 * input[size - 3] + 4.0 * input[
        size - 4] + 3.0 * input[size - 5] + 1.0 * input[size - 6] - 2.0 * input[size - 7]) / 14.0
    output[size - 2] = (5.0 * input[size - 1] + 4.0 * input[size - 2] + 3.0 * input[size - 3] + 2.0 * input[size - 4] +
                        input[size - 5] - input[size - 7]) / 14.0
    output[size - 1] = (32.0 * input[size - 1] + 15.0 * input[size - 2] + 3.0 * input[size - 3] - 4.0 * input[
        size - 4] - 6.0 * input[size - 5] - 3.0 * input[size - 6] + 5.0 * input[size - 7]) / 42.0
    return output


output = list(MovingAverage(a).values())
print(output)
out = np.array(output)
# 绘制散点图  定义散点大小,颜色,透明度
plt.scatter(x, out, s=75, color="blue", alpha=1)
plt.scatter(x, y1, s=75, color="red", alpha=1)
# 设置x y坐标轴范围
plt.xlim(0, 30, 30)
plt.ylim(0, 30, 30)
plt.show()
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页