为什么R中stats包中函数fft返回的常量在评估时没有返回信号的近似值?

问题描述 投票:0回答:1

我一直在尝试应用stats.:fft()函数获取一个信号的所有值的频率和振幅,我的真实信号如下图所示。enter image description here

信号是每小时测量一次的

这是用于创建信号指数的代码,我不知道为什么我必须总是从0到1进行积分,但我读到了这一点,所以这就是为什么我在这里使用它。

samp_freq <- 168

time_interval <- 1/samp_freq

signal_index <- seq(0, 1, by=time_interval)

plot(x = signal_index,y = dt$train.sensores_sum_entrada[1:169], type = "l")

在应用函数变换并评估每个频率分量的常数后,当绘制结果时,我得到类似于这样的结果。enter image description here

产生这个结果的代码如下:

f_component <- stats::fft(z = dt$train.sensores_sum_entrada[1:169])

f_coef <- abs(f_component)

plot(f_coef)
main_coef <- (abs(stats::fft(dt$train.sensores_sum_entrada))/(6966/2))[1:500]
normal_f_compon <- f_coef/ (168/2)
main_coef <- normal_f_compon[1:(168/2)]
coef_table <- data.table(frequency = 1:length(main_coef),amplitude = main_coef)
model_evaluated <- lapply(X = 1:length(coef_table$frequency),FUN = function(i) coef_table$amplitude[i]*sin(2*pi*coef_table$frequency[i]*signal_index))
plot(Reduce(f = `+`,model_evaluated),type = "l")

如果有人知道如何解决这个问题,我会很感激。

谢谢。

python r math fft statsmodels
1个回答
1
投票

看起来你是想从信号的fft中重建信号。你可以使用 fft(f_component, inverse = TRUE) 不过如果你想手动操作,你可以试试这个方法。

首先我将产生一些虚拟数据。

f <- function(x)
{
  (sin(x * pi * 12) + 1) * 2000 +
  cos(x * pi * 54) * 200 -
  sin(x * pi * 3.6) * 250
}

x <- seq(0, 1, length.out = 168)

plot(x, f(x), type = "l")

enter image description here

然后我得到傅里叶变换。

f_components <- fft(f(x))

然后我就可以像这样进行反转

freqs   <- seq_along(f_components) - 1
fourier <- pi * -2 * complex(imaginary = 1) * freqs/length(f_components)
y <- sapply(freqs, function(i) sum(f_components * exp(fourier * i)))
y <- c(y[1], rev(y[-1])) / length(f_components)

lines(x, y, col = "red")

创建于2020-05-28,作者: 重读包 (v0.3.0)

© www.soinside.com 2019 - 2024. All rights reserved.