【问题标题】:Adding gaussian noise in fortran to an array of data将fortran中的高斯噪声添加到数据数组中
【发布时间】:2015-12-29 03:28:16
【问题描述】:

在向我的 Fortran 代码添加高斯噪声方面需要一些帮助。我必须基本上构造一个大小为 4 的数组。我将值输入到数组中,然后我必须添加噪声。我尝试在互联网上查找此内容,并找到了使用随机数数组然后将高斯噪声添加到数组的解决方案,但无法找到将噪声添加到预设数组的解决方案。

我已经给出了下面的部分代码

real, dimesnsion(1:4):: stoke

!create an array
stoke(1)=sum_iu
stoke(2)=diff_iu
stoke(3)=sum_iq
stoke(4)=diff_iq

do ij=1,4

   print*, 'stoke=' ,ij,'=',stoke(ij)

end do

! add gaussian noise to the data

do ij=1,xx-1,2

      temp= sd*sqrt(-2.0*log(stoke(ij)))*cos(2*pi*(stoke(ij+1)))+mean
      stoke(ij+1)=sd*sqrt(-2.0*log(stoke(ij))) * sin(2*pi*(stoke(ij+1)))+mean
      stoke(ij)=temp

print*,'the values are= ','stoke(ij)',stoke(ij),'stoke(ij+1)',stoke(ij+1)
end do

当我运行它时,它会将四个输出值的结果输出为 NaN。但如果我使用随机数 array(size=4) 代替 stokes(4) 它会显示数字。

【问题讨论】:

  • 那么,xx 是什么,diff_iq 是什么?
  • 不要使用sn-p功能,它只适用于javascript。
  • NaN 的来源很可能与 stoke(ij) 的值有关,如果它大于 1,将使 sqrt 的参数为负,因此 nan。在循环的入口打印stoke的值就可以看到了。
  • 感谢 Edmondo Giovannozzi。 soke 的值大于 1,所以我猜这就是值。
  • 您是否尝试将新生成的(独立的)高斯随机数(均值“mean”和标准偏差“sd”添加到数组“stroke(1:4)”中?那我猜参数日志等应该始终是统一的随机数。

标签: fortran


【解决方案1】:

我认为您可能正在使用Box-Muller 方法从两个均匀随机数生成两个高斯随机数。因此,如果要将高斯噪声添加到现有数组(此处为stoke),我猜代码会变成这样:

do ij = 1, size( stoke )-1, 2   !! assuming the array size is even...

    u = rand()   !! uniform random number in [0,1)
    v = rand()   !! same as above (another sample)

    stoke( ij   ) = stoke( ij   ) + sd * sqrt(-2.0*log( u )) * cos( 2*pi * v ) + mean
    stoke( ij+1 ) = stoke( ij+1 ) + sd * sqrt(-2.0*log( u )) * sin( 2*pi * v ) + mean
end do

【讨论】:

  • 为防止极少数情况下 u=0,最好附上代码以拒绝它并重新采样 u。
猜你喜欢
  • 1970-01-01
  • 2011-09-12
  • 2018-02-15
  • 2021-01-12
  • 2013-10-14
  • 1970-01-01
  • 2016-02-07
  • 2022-01-20
  • 1970-01-01
相关资源
最近更新 更多