微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

产生空间相关噪声的更快方法?

如何解决产生空间相关噪声的更快方法?

在我当前的项目中,我对计算大型模型网格的空间相关噪声感兴趣。噪声应在短距离上强相关,而在长距离上不相关。我目前的方法是使用带有协方差矩阵的多元高斯函数来指定所有像元之间的相关性。

不幸的是,这种方法对于大型网格非常慢。您是否建议如何更有效地产生与空间相关的噪声? (不必是高斯的)

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)))

解决方法

更快的方法:

  • 生成空间不相关的噪声。
  • 与高斯滤波器内核进行模糊处理以使噪声在空间上相关。

由于过滤器内核很大,因此最好使用基于快速傅立叶变换的卷积方法。

import numpy as np
import scipy.signal
import matplotlib.pyplot as plt

# Compute filter kernel with radius correlation_scale (can probably be a bit smaller)
correlation_scale = 50
x = np.arange(-correlation_scale,correlation_scale)
y = np.arange(-correlation_scale,correlation_scale)
X,Y = np.meshgrid(x,y)
dist = np.sqrt(X*X + Y*Y)
filter_kernel = np.exp(-dist**2/(2*correlation_scale))

# Generate n-by-n grid of spatially correlated noise
n = 50
noise = np.random.randn(n,n)
noise = scipy.signal.fftconvolve(noise,filter_kernel,mode='same')
plt.contourf(np.arange(n),np.arange(n),noise)
plt.savefig("fast.png")

此方法的示例输出:

https://fastapi.tiangolo.com/async/#very-technical-details

问题的慢速方法的样本输出:

enter image description here

图像大小与运行时间:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。