先看实例
python
import numpy as np
signal = [3,2,1,3,8,10]
grad = np.gradient(signal)
print(grad)
输出结果是
[-1. -1. 0.5 3.5 3.5 2. ]
这个结果是怎么来的呢?
np.gradient 计算信号的数值梯度,也就是信号值的变化率。它使用中心差分法来计算中间点的梯度,并使用前向差分法和后向差分法来计算边界点的梯度。
一、边界点:
g r a d [ 0 ] = s i g n a l [ 1 ] − s i g n a l [ 0 ] = 2 − 3 = − 1 grad[0] = signal[1] - signal[0] = 2 - 3 = -1 grad[0]=signal[1]−signal[0]=2−3=−1
g r a d [ 5 ] = s i g n a l [ 5 ] − s i g n a l [ 4 ] = 10 − 8 = 2 grad[5] = signal[5] - signal[4] = 10-8 = 2 grad[5]=signal[5]−signal[4]=10−8=2
二、中间点:
g r a d [ i ] = s i g n a l [ i + 1 ] − s i g n a l [ i − 1 ] 2 grad[i] =\frac{signal[i+1]-signal[i-1]}{2} grad[i]=2signal[i+1]−signal[i−1],由此可知
g r a d [ 1 ] = s i g n a l [ 2 ] − s i g n a l [ 0 ] 2 = − 1 grad[1] =\frac{signal[2]-signal[0]}{2} = -1 grad[1]=2signal[2]−signal[0]=−1
其他略。