- import numpy as np
- from scipy.special import gamma
- from scipy.linalg import toeplitz
- import matplotlib.pyplot as plt
-
- # 第一种实现方式
- def fgl_deriv(a, y, h=0.01):
- """
- Args:
- a: 微分阶数,整数
- y: 求微分的数列
- h: 步伐
- Returns:
- """
- n = len(y)
- J = np.arange(n)
- G1 = gamma(J + 1)
- G2 = gamma(a + 1 - J)
- s = (-1) ** J
-
- M = np.tril(np.ones((n, n)))
- R = np.array([y[i-j] for i in range(n) for j in range(n)]).reshape((n, n))
- T = (gamma(a + 1) / (h ** a)) * s / (G1 * G2)
- Y = np.sum(R * (M * T[:, None].T), axis=1)
-
- return Y
-
- # # 第二种实现方式
- # def fgl_deriv(a, y, h):
- # n = len(y)
- # J = np.arange(n)
- # G1 = gamma(J + 1)
- # G2 = gamma(a + 1 - J)
- # s = (-1) ** J
- #
- # M = np.tril(np.ones((n, n)))
- # R = toeplitz(y)
- # T = np.meshgrid((gamma(a + 1) / (h ** a)) * s / (G1 * G2))[0]
- # Y = np.reshape(np.sum(R * M * T, axis=1), y.shape)
- #
- # return Y
-
-
-
- data = np.random.random((100))
- data = np.sin(data)
-
- dataa = fgl_deriv(1, data)
- print(dataa)
- # plt.plot(range(len(data)),data, label="001",linewidth=1.5)
- plt.plot(range(len(data)),dataa, label="001",linewidth=1.5)
- plt.show()