自定义高斯核
size为一个数表示一维高斯核,两个数的列表表示二维高斯核。
import numpy as np
def gaussian_filter(size, sigma):
if type(size) == int:
siz = int((size - 1) / 2)
x = np.array(range(-siz, siz + 1))
arg = -x ** 2 / (2 * sigma ** 2)
h = np.exp(arg)
sumh = np.sum(h)
h = h / sumh
elif type(size) == list:
siz0 = int((size[0] - 1) / 2)
siz1 = int((size[1] - 1) / 2)
x = np.array(range(-siz0, siz0 + 1))
y = np.array(range(-siz1, siz1 + 1))
[x, y] = np.meshgrid(x, y)
arg = -(x ** 2 + y ** 2) / (2 * sigma ** 2)
h = np.exp(arg)
sumh = np.sum(h)
h = h / sumh
return np.round(h, 4)
对数据进行滤波
f = gaussian_filter(5, 0,6)
data_fed = np.convolve(data, f, mode="same")