本文在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循環的形式修改為矩陣計算,性能有較大提升.