我不是傅立叶分析专家,完全不明白 R 的函数 fft() 的作用。即使交叉阅读了很多之后我也无法弄清楚。我建立了一个例子。
require(ggplot2)
freq <- 200 #sample frequency in Hz
duration <- 3 # length of signal in seconds
#arbitrary sine wave
x <- seq(-4*pi,4*pi, length.out = freq*duration)
y <- sin(0.25*x) + sin(0.5*x) + sin(x)
Run Code Online (Sandbox Code Playgroud)
看起来像:

fourier <- fft(y)
#frequency "amounts" and associated frequencies
amo <- Mod(fft(y))
freqvec <- 1:length(amo)
Run Code Online (Sandbox Code Playgroud)
我假设 fft 期望在 1 秒的时间跨度内记录向量,因此我除以时间跨度
freqvec <- freqvec/duration
#and put this into a data.frame
df <- data.frame(freq = freqvec, ammount = amo)
Run Code Online (Sandbox Code Playgroud)
现在我可以/必须省略 data.frame 的后半部分,因为由于奈奎斯特的原因,频率“量”仅对采样率的一半有意义。
df <- df[(1:as.integer(0.5*freq*duration)),]
Run Code Online (Sandbox Code Playgroud)
为了绘图,我离散了一点
df.disc <- data.frame(freq = 1:100)
cum.amo <- numeric(100)
for (i in 1:100){
cum.amo[i] <- sum(df$ammount[c(3*i-2,3*i-1,3*i)])
}
df.disc$ammount <- cum.amo
Run Code Online (Sandbox Code Playgroud)
前 20 个频率的绘图函数:
df.disc$freq <- as.factor(df.disc$freq)
ggplot(df.disc[1:20,], aes(x=freq, y=ammount)) + geom_bar(stat = "identity")
Run Code Online (Sandbox Code Playgroud)
结果:

这真的是上述函数的正确频谱图吗?我的两个假设正确吗?我的错误在哪里?如果没有,这个情节现在告诉我什么?
编辑:这是一张没有离散化的图片:

感谢大家,
米夏.
好吧好吧。由于我的错误通常性质较差,所以解决方案非常简单。我写了 freq = 200 和持续时间 = 3。但实际持续时间是从 -4pi 到 4 pi,因此 8pi 导致“真实”采样频率为 1/ ((8*pi)/600) = 23.87324,这不等于200. 将示例代码中的相应行替换为
freq <- 200 #sample frequency in Hz
duration <- 6 # length of signal in seconds
x <- seq(0,duration, length.out = freq*duration)
y <- sin(4*pi*x) + sin(6*pi*x) + sin(8*pi*x)
Run Code Online (Sandbox Code Playgroud)
(具有更具说明性的功能)产生正确的频率,如下图所示(仅限于频域的重要部分):

| 归档时间: |
|
| 查看次数: |
7787 次 |
| 最近记录: |