如何模拟R中的粉红噪声

时间:2012-01-02 05:04:17

标签: r signal-processing noise noise-generator

我知道可以通过将rnorm()的输出视为时间序列来实现白噪声。有关如何模拟粉红噪音的任何建议吗?

2 个答案:

答案 0 :(得分:10)

tuneR具有noise功能,可生成白色或粉红色噪音的波形对象:

require(tuneR)
w <- noise(kind = c("white"))
p <- noise(kind = c("pink"))
par(mfrow=c(2,1))
plot(w,main="white noise")
plot(p,main="pink noise")
编辑:我意识到上面的方法不生成向量(doh)。将其转换为向量的残酷方法是添加以下代码:

writeWave(p,"p.wav")#writes pink noise on your hard drive
require(audio)#loads `audio` package to use `load.wave` function
p.vec <- load.wave("path/to/p.wav")#this will load pink noise as a vector

enter image description here

答案 1 :(得分:0)

正如@mbq所说,您可以使用p @ left来获取向量,而不是保存并读取wav文件。另一方面,您可以直接使用在tuneR中生成时间序列的函数:

min: function() {
    return minqty;
}

那很完美:

TK95 <- function(N, alpha = 1){ 
    f <- seq(from=0, to=pi, length.out=(N/2+1))[-c(1,(N/2+1))] # Fourier frequencies
    f_ <- 1 / f^alpha # Power law
    RW <- sqrt(0.5*f_) * rnorm(N/2-1) # for the real part
    IW <- sqrt(0.5*f_) * rnorm(N/2-1) # for the imaginary part
    fR <- complex(real = c(rnorm(1), RW, rnorm(1), RW[(N/2-1):1]), 
                  imaginary = c(0, IW, 0, -IW[(N/2-1):1]), length.out=N)
     # Those complex numbers that are to be back transformed for Fourier Frequencies 0, 2pi/N, 2*2pi/N, ..., pi, ..., 2pi-1/N 
     # Choose in a way that frequencies are complex-conjugated and symmetric around pi 
     # 0 and pi do not need an imaginary part
    reihe <- fft(fR, inverse=TRUE) # go back into time domain
    return(Re(reihe)) # imaginary part is 0
}

enter image description here