如何使用 R 的 fft() 函数计算正确的振幅?

How does one calculate the correct amplitude with R's fft()-function?

在 R 中,我使用 rnorm() 在时域中生成不相关的值。然后我将 fft() 应用于这些值,但是,我只得到 0.88 而不是 1 的值。有什么我不知道的吗?

这是一个 MWE:

# dt <- 0.01 # time stesp
nSteps <- 100000 # Number of time steps
# df <- 1/(nSteps*dt) # frequency resolution

# t <- 0:(nSteps-1)*dt # 
y <- rnorm(nSteps, mean=0, sd=1) # generate uncorrelated data. Should result in a white noise spectrum with sd=1
y_sq_sum <- sum(y^2)

# We ignore cutting to the Nyquist frequency.
# f <- 0:(nSteps-1)*df
fft_y <- abs(fft(y))/sqrt(length(y))
fft_y_sq_sum <- sum(fft_y^2)

print(paste("Check for Parseval's theorem: y_sq_sum = ", y_sq_sum, "; fft_y_sq_sum = ", fft_y_sq_sum, sep=""))

print(paste("Mean amplitude of my fft spectrum: ", mean(fft_y)))
print(paste("The above is typically around 0.88, why is it not 1?"))

这个问题不属于 Whosebug,它更像是 Cross-validated 类的问题。但无论如何这里有一个答案:

Parseval 定理说 fft_y^2 的均值应该是 1。平方根函数是凹函数,所以 Jensen 不等式说 sqrt(fft_y^2) 的均值将小于 1。由于 fft_y 在你的定义中是肯定的,fft_y = sqrt(fft_y^2).