本文在python 傅里叶曲线拟合的基础上对拟合函数做了修正
def fourier(x, *args):
t = np.arange(0, np.pi, np.pi/len(args)*2)
w = 2 * np.pi / 200
m = int(len(args) / 2)
arr = np.zeros((m, 2))
arr[:, 0] = np.array(args[:m:1])
arr[:, 1] = np.array(args[-1:-m-1:-1])
ret = np.sum(np.cos(t.reshape(-1, 1) * w * x).T * arr[:, 0].reshape(1, -1)
+ np.sin(t.reshape(-1, 1) * w * x).T * arr[:, 1].reshape(1, -1),
axis=1)
# ret = 0
# for deg in range(0, int(len(args) / 2) + 1):
# ret += args[deg] * np.cos(deg * w * x) + args[len(args) - deg - 1] * np.sin(deg * w * x)
return ret
将for循环的形式修改为矩阵计算,性能有较大提升.