本文介绍了如何在python(或numpy/scipy)中生成复杂的高斯白噪声信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些关于DSP(数字信号处理)的工作,并且需要生成离散的复杂高斯白噪声信号.我知道我可以使用numpy.random.normal(0, 1, n)生成离散序列,但这是在实数字段中.用Matlab进行仿真很容易,但是我徘徊在如何用python代替matlab代码?

I am doing some works about DSP(digital signal process), and there need to generate a discrete complex white gaussian noise signal. I know I can use numpy.random.normal(0, 1, n) to generate the discrete sequence, but it is in real number field. It is easy to simulate with Matlab, but I wander how to replace matlab code with python?

推荐答案

这是一种实现方法.这样会生成一个形状为(n,2)的标准正态变量数组,然后使用.view()方法将该数组视为形状为(n,)的复数值数组.

Here's one way you can do it. This generates an array of standard normal variates of shape (n, 2), and then uses the .view() method to view the array as an array of complex values with shape (n,).

In [26]: n = 10

In [27]: z = np.random.randn(n, 2).view(np.complex128)

In [28]: z
Out[28]:
array([[ 0.90179497-0.14081956j],
       [-2.17633115+0.88782764j],
       [ 0.94807348+0.27575325j],
       [-1.25452512+0.64883484j],
       [-0.58886548+0.15419947j],
       [ 0.58296574+1.45711421j],
       [ 0.803825  +0.6197812j ],
       [ 0.09225137+0.38012939j],
       [ 0.5017482 -0.39747648j],
       [-1.00186317+1.02918796j]])

如果您想使用np.random.normal(size=(n, 2)),可以将其替换为np.random.normal(size=(n, 2)).

You can replace np.random.randn(n, 2) with np.random.normal(size=(n, 2)) if you prefer to use that function.

根据复杂正态分布上的维基百科文章,实部和虚部的方差复杂的 standard 正常随机变量的部分应为1/2(因此,复杂样本的方差为1).这次我将使用np.random.normal,但是您也可以适当地缩放np.random.rand.

According to the wikipedia article on the complex normal distribution, the variance of the real and imaginary parts of a complex standard normal random variable should be 1/2 (so the variance of the complex samples is 1). I'll use np.random.normal this time, but you could also scale np.random.rand appropriately.

创建一个大样本,以便我们可以验证方差接近1:

Create a large sample so we can verify that the variance is close to 1:

In [19]: n = 100000

In [20]: z = np.random.normal(loc=0, scale=np.sqrt(2)/2, size=(n, 2)).view(np.complex128)

In [21]: z[:10]
Out[21]:
array([[ 0.31439115+1.39059186j],
       [ 0.18306617+1.19364778j],
       [ 0.20281354+0.31695626j],
       [ 0.27230747+1.18380383j],
       [-0.71353935-0.11587812j],
       [-0.2371236 +0.91542372j],
       [ 0.04254323+1.50538309j],
       [ 0.23024067+0.96947144j],
       [ 0.6954942 +0.20933687j],
       [-0.66853093+2.00389192j]])

按预期,方差接近1:

In [22]: np.var(z)
Out[22]: 0.9998204444495904

或者,您可以使用 np.random.multivariate_normal ,并将0.5*np.eye(2)用作协方差矩阵:

Alternatively, you can use np.random.multivariate_normal, and use 0.5*np.eye(2) for the covariance matrix:

In [31]: z = np.random.multivariate_normal(np.zeros(2), 0.5*np.eye(2), size=n).view(np.complex128)

In [32]: z[:10]
Out[32]:
array([[-0.25012362+0.80450233j],
       [-0.85853563+0.05350865j],
       [ 0.36715694-0.10483562j],
       [ 1.0740756 +0.081779j  ],
       [-1.04655701+0.15211247j],
       [ 0.18248473+0.49350875j],
       [ 0.6152102 +0.08037717j],
       [ 0.12423999+0.56175553j],
       [-1.05282963-0.60113989j],
       [-0.01340098+0.80751573j]])

In [33]: np.var(z)
Out[33]: 1.0001327524747319

这篇关于如何在python(或numpy/scipy)中生成复杂的高斯白噪声信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!