避免随机数重复的方法 - python

时间:2014-01-23 00:49:30

标签: python arrays random noise

我在以下代码中使用python中的随机数例程来创建噪​​声信号。

res = 10
# Add noise to each X bin accross the signal

X = np.arange(-600,600,res)

for i in range(10000):
    noise = [random.uniform(-2,2) for i in xrange(len(X))]

# custom module to save output of X and noise to .fits file 
wp.save_fits('test10000', X, noise)

plt.plot(V, I)

plt.show()

在这个例子中,我生成了10,000个'noise.fits'文件,然后我希望将它们共同加在一起,以显示堆叠噪声均方根(rms)的预期1 / sqrt(N)依赖性)作为共同添加的对象数量的函数。

我的问题是rms遵循这种依赖性直到~1000个对象,此时它向上偏离,表明随机数生成器。

是否有例程或方法来构建代码以避免或最小化这种重复? (理想情况下,数字为最大值和最小值之间的浮点数>&1;< -1)?

以下是共同添加代码的输出以及粘贴在底部的代码以供参考。

 The dependance of rms of the co-added signal to as a function of number objects stacked. The green line is the expected 1/sqrt(N) dependence.(Ignore the title and units, they are a left over from another project.)

如果我使用模块random.random(),结果会更糟。 Dependance of rms co-added noise on N for random.random(). Again green line is expected 1/sqrt(N)

这是我的代码,它将噪声信号文件加在一起,对对象的数量进行平均。

import os
import numpy as np
from astropy.io import fits
import matplotlib.pyplot as plt
import glob

rms_arr =[]
#vel_w_arr = []
filelist = glob.glob('/Users/thbrown/Documents/HI_stacking/mockcat/testing/test10000/M*.fits')
filelist.sort()

for i in (filelist[:]):

    print(i)

    #open an existing FITS file
    hdulist = fits.open(str(i))

    # assuming the first extension is the table we assign data to record array
    tbdata = hdulist[1].data

    #index = np.arange(len(filelist))

    # Access the signal column
    noise = tbdata.field(1)

    # access the vel column
    X = tbdata.field(0)

    if i == filelist[0]:
        stack = np.zeros(len(noise))
        tot_rms = 0 

    #print len(stack)
    # sum signal in loop
    stack = (stack + noise)

    rms = np.std(stack)

    rms_arr = np.append(rms_arr, rms)

numgal = np.arange(1, np.size(filelist)+1)

avg_rms = rms_arr / numgal

0 个答案:

没有答案