在我目前的项目中,我对计算大型模型网格的空间相关噪声感兴趣。噪声在短距离上应该是强相关的,而在远距离上应该是不相关的。我目前的方法使用带有协方差矩阵的多元高斯,指定所有单元格之间的相关性。
不幸的是,这种方法对于大型网格来说非常慢。您对如何更有效地生成空间相关噪声有什么建议吗?(不一定是高斯)
import scipy.stats
import numpy as np
import scipy.spatial.distance
import matplotlib.pyplot as plt
# Create a 50-by-50 grid; My actual grid will be a LOT larger
X,Y = np.meshgrid(np.arange(50),np.arange(50))
# Create a vector of cells
XY = np.column_stack((np.ndarray.flatten(X),np.ndarray.flatten(Y)))
# Calculate a matrix of distances between the cells
dist = scipy.spatial.distance.pdist(XY)
dist = scipy.spatial.distance.squareform(dist)
# Convert the distance matrix into a covariance matrix
correlation_scale = 50
cov = np.exp(-dist**2/(2*correlation_scale)) # This will do as a covariance matrix
# Sample some noise !slow!
noise = scipy.stats.multivariate_normal.rvs(
mean = np.zeros(50**2),
cov = cov)
# Plot the result
plt.contourf(X,Y,noise.reshape((50,50)))