R中的快速傅立叶变换 [英] Fast Fourier Transform in R

查看:134
本文介绍了R中的快速傅立叶变换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,其中包含12个月内动物每小时造访的次数.我想使用快速傅立叶变换来检查周期性模式和周期性.过去,我曾为此使用Statistica.但是,我想使用R来获得频谱密度与周期的关系图.在R中有简单的方法吗?我想确定活动的12小时和24小时高峰.

解决方案

您可以考虑以下功能.

    TSA软件包中的
  • periodogram会立即绘制周期图.
  • GeneCycle中的
  • periodogram返回频率列表和估计的功率谱密度.它是stats::spectrum的包装函数,其中设置了一些特殊选项.
  • stats中的
  • spectrum允许您选择用于估计光谱密度的方法:周期图或使用自回归过程.
  • 中的
  • cpgram绘制了累积周期图以及置信区间.

请参见例如?cpgram?spectrum的所有详细信息,并请记住,当函数名称重合时,例如TSA::periodogramGeneCycle::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 from TSA package immediately plots a periodogram.
  • periodogram from GeneCycle returns a list of frequencies and estimated power spectral densities. It is a wrapper function for stats::spectrum with some special options set.
  • spectrum from stats allows you to choose the method used to estimate the spectral density: either periodogram or using autoregressive process.
  • cpgram from stats 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆