我试图在R中复制下图:(改编自http://link.springer.com/article/10.1007/PL00011669)
该图的基本概念是显示DFT的前几个分量,在时域中绘制,然后仅使用相对于原始数据的这些分量(X')显示时域中的重建波( X)。我想略微修改上图,使得所有显示的行都叠加在一个图上。
我一直在尝试使用60 Hz采样的一些实际数据来调整数字。例如:
## 3 second sample where: time is in seconds and var is the variable of interest
temp = data.frame(time=seq(from=0,to=3,by=1/60),
var = c(0.054,0.054,0.054,0.072,0.072,0.072,0.072,0.09,0.09,0.108,0.126,0.126,
0.126,0.126,0.126,0.144,0.144,0.144,0.144,0.144,0.162,0.162,0.144,0.126,
0.126,0.108,0.144,0.162,0.18,0.162,0.126,0.126,0.108,0.108,0.126,0.144,
0.162,0.144,0.144,0.144,0.144,0.162,0.162,0.126,0.108,0.09,0.09,0.072,
0.054,0.054,0.054,0.036,0.036,0.018,0.018,0.018,0.018,0,0.018,0,
0,0,-0.018,0,0,0,-0.018,0,-0.018,-0.018,0,-0.018,
-0.018,-0.018,-0.018,-0.036,-0.036,-0.054,-0.054,-0.072,-0.072,-0.072,-0.072,-0.072,
-0.09,-0.09,-0.108,-0.126,-0.126,-0.126,-0.144,-0.144,-0.144,-0.162,-0.162,-0.18,
-0.162,-0.162,-0.162,-0.162,-0.144,-0.144,-0.144,-0.126,-0.126,-0.108,-0.108,-0.09,
-0.072,-0.054,-0.036,-0.018,0,0,0,0,0.018,0.018,0.036,0.054,
0.054,0.054,0.054,0.054,0.054,0.054,0.054,0.054,0.054,0.072,0.054,0.072,
0.072,0.072,0.072,0.072,0.072,0.054,0.054,0.054,0.036,0.036,0.036,0.036,
0.036,0.054,0.054,0.072,0.09,0.072,0.036,0.036,0.018,0.018,0.018,0.018,
0.036,0.036,0.036,0.036,0.018,0,-0.018,-0.018,-0.018,-0.018,-0.018,0,
-0.018,-0.036,-0.036,-0.018,-0.018,-0.018,-0.036,0,0,-0.018,-0.018,-0.018,-0.018))
##plot the original data
ggplot(temp, aes(x=time, y=var))+geom_line()
我相信我可以使用fft()
来最终实现这一目标,但是从fft()
的输出到我的目标的跳跃有点不清楚。
我意识到这个问题有点类似于:How do I calculate amplitude and phase angle of fft() output from real-valued input?但我对上面特定数据的实际代码更感兴趣。
请注意我对时间序列分析相对较新,所以你可以提供任何清晰度w.r.t.将fft()的输出放在上下文中,或者您可以推荐的任何可以有效完成此任务的包将不胜感激。
谢谢
答案 0 :(得分:2)
Matlab是您最好的工具,具体功能只是fft()。要使用它,首先要确定时域数据的几个基本参数:
1,持续时间(T),等于3s。
2,采样间隔T_s,等于1/60秒。
3,频域旋转f_s,其等于两个相邻傅立叶基础之间的频率差。您可以根据需要定义f_s。然而,最小可能的f_s等于1 / T = 0.333Hz。因此,如果您想要更好的频域旋转(更小的f_s),则需要更长的时域数据。4,最大频率f_M,根据香农抽样理论,等于1 /(2T_s)= 30。
5,DFT长度N,等于2 * f_M / f_s。
然后找出您想要用来近似数据的四个傅里叶基的特定频率。例如,3,6,9和12 Hz。所以f_s = 3 Hz。然后N = 2 * f_M / f_s = 20。
您的Matlab代码如下所示:
var=[0.054,0.054,0.054 ...]; % input all your data points here
f_full=fft(var,20); % Do 20-point fft
f_useful=f_full(2:5); % You are interested with the lowest four frequencies except DC
这里f_useful包含四个傅立叶基的四个复系数。要重建var,请执行以下操作:
% Generate basis functions
dt=0:1/60:3;
df=[3:3:12];
basis1=exp(1j*2*pi*df(1)*dt);
basis2=exp(1j*2*pi*df(2)*dt);
basis3=exp(1j*2*pi*df(3)*dt);
basis4=exp(1j*2*pi*df(4)*dt);
% Reconstruct var
var_recon=basis1*f_useful(1)+...
basis2*f_useful(2)+...
basis3*f_useful(3)+...
basis4*f_useful(4);
var_recon=real(var_recon);
% Plot both curves
figure;
plot(var);
hold on;
plot(var_recon);
将此代码改编为您的论文:)
答案 1 :(得分:0)
从Signal Processing调整我自己的帖子。我认为它仍然适用于Python中的那些。
我不是这个主题的专家,但有一些有用的例子可供分享。
您保留的傅里叶组件越多,您就越接近模仿原始信号。
此示例显示了将10,20,......保存到 n 组件时会发生什么。假设x
和y
是您的数据向量。
import numpy
from matplotlib import pyplot as plt
n = len(y)
COMPONENTS = [10, 20, n]
for c in COMPONENTS:
colors = numpy.linspace(start=100, stop=255, num=c)
for i in range(c):
Y = numpy.fft.fft(y)
numpy.put(Y, range(i+1, n), 0.0)
ifft = numpy.fft.ifft(Y)
plt.plot(x, ifft, color=plt.cm.Reds(int(colors[i])), alpha=.70)
plt.title("First {c} fourier components".format(c=c))
plt.plot(x,y, label="Original dataset", linewidth=2.0)
plt.grid(linestyle='dashed')
plt.legend()
plt.show()
对于本书的数据集,最多可保留4,10和 n 组件:
对于您的数据集,最多可保留4,10和 n 组件: