1. ホーム
  2. python learning

軌跡平滑化法-滑り平均平滑化法(移動平均法)

2022-02-13 07:23:26

参考記事 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-axis data
# raw data
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 points 2 times MA smoothing
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)
# Plot scatter plot Define scatter size, color, transparency
plt.scatter(x, out, s=75, color="blue", alpha=1)
plt.scatter(x, y1, s=75, color="red", alpha=1)
# Set the x y axis range
plt.xlim(0, 30, 30)
plt.ylim(0, 30, 30)
plt.show()