R中的快速傅立叶变换 [英] Fast Fourier Transform in R
问题描述
我有一个数据集,其中包含12个月内动物每小时造访的次数.我想使用快速傅立叶变换来检查周期性模式和周期性.过去,我曾为此使用Statistica.但是,我想使用R来获得频谱密度与周期的关系图.在R中有简单的方法吗?我想确定活动的12小时和24小时高峰.
您可以考虑以下功能.
-
periodogram
会立即绘制周期图. -
periodogram
返回频率列表和估计的功率谱密度.它是stats::spectrum
的包装函数,其中设置了一些特殊选项. -
spectrum
允许您选择用于估计光谱密度的方法:周期图或使用自回归过程. -
cpgram
绘制了累积周期图以及置信区间.
TSA
软件包中的GeneCycle
中的stats
中的请参见例如?cpgram
或?spectrum
的所有详细信息,并请记住,当函数名称重合时,例如TSA::periodogram
和GeneCycle::periodogram
.
在线上也有许多示例和教程,介绍如何使用这些功能.参见此处,了解fft
和此处,以获取更广泛的教程.
此外,您可能已经知道,给定的时间序列必须被逆趋势化.因此,请使用例如diff(x)
代替x
.最后,时间序列的长度必须能够被12整除,以便能够识别12和24小时的频率,这可以通过例如x[-(1:(length(x) %% 12))]
来实现,其中x
是去趋势的时间序列.>
I have a dataset with the number of hourly visits an animal made during a period of 12 months. I want to use the Fast Fourier Transform to examine cyclical patterns and periodicity. In the past, I have used Statistica for this this; however, I would like to use R to get a plot of the spectral density vs. period. Is there an easy way to do this in R? I would like to identify 12 and 24 hr peak in activity if possible.
You may consider the following functions.
periodogram
fromTSA
package immediately plots a periodogram.periodogram
fromGeneCycle
returns a list of frequencies and estimated power spectral densities. It is a wrapper function forstats::spectrum
with some special options set.spectrum
fromstats
allows you to choose the method used to estimate the spectral density: either periodogram or using autoregressive process.cpgram
fromstats
plots a cumulative periodogram along with a confidence interval.
See, e.g., ?cpgram
or ?spectrum
for all the details and keep in mind that it is, e.g., TSA::periodogram
and GeneCycle::periodogram
when names of the functions coincide.
There are also plenty of examples and tutorials online on how to use those functions. See here for the usage of fft
and here for an even more extensive tutorial.
Also, as you probably already know, a given time series must be detrended. Hence, use, e.g., diff(x)
instead of x
. And finally, the length of your time series must be divisible by 12 as to be able to identify 12 and 24 hours frequencies, it can be achieved by, e.g., x[-(1:(length(x) %% 12))]
, where x
is a detrended time series.
这篇关于R中的快速傅立叶变换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!