我是Python和信号处理的新手,所以请原谅可能滥用行话。
我在Pandas数据帧x中有一个信号的离散值,间隔1秒。它看起来像这样:
2017-08-02 16:42:00 0.363657
2017-08-02 16:42:01 0.282907
2017-08-02 16:42:02 0.155929
...
2017-08-02 16:43:14 0.178522
2017-08-02 16:43:15 0.488507
2017-08-02 16:43:16 0.194987
...
我想计算30秒周期内信号的能量(通过能量,我指的是离散傅里叶系数的平方和,用加数的数量归一化)。我希望输出格式为
2017-08-02 16:42:00 x_1
2017-08-02 16:42:30 x_2
2017-08-02 16:43:00 x_3
...
其中每个x_i是对应于该30s周期的能量的标量。得到一个numpy数组我也很好,因为最终我只需要能量值而不是时间戳。
我试过这样做:
energy = x.resample('30S').apply(lambda x: (numpy.absolute(numpy.fft.fft(x))**2)/30)
然而,我得到的形式
2017-08-02 16:42:00 [[0.422450491863], [0.482244793857], [0.514463...
2017-08-02 16:42:30 [[0.345172558059], [0.554558388074], [0.461898...
2017-08-02 16:43:00 [[0.689816890284], [0.613620822242], [0.389962...
我做错了什么,怎么最好纠正它?
谢谢!
编辑:我稍微改变了时间,因为我只需要复制一小部分输出,所以不要介意这些值本身。
**编辑2:代码与此处写的完全相同,即:
x = 2017-08-02 16:42:00 0.363657
2017-08-02 16:42:01 0.282907
2017-08-02 16:42:02 0.155929
...
2017-08-02 16:43:14 0.178522
2017-08-02 16:43:15 0.488507
2017-08-02 16:43:16 0.194987
...
#This is imported as a dataframe, and indeed shows its type as pandas.core.frame.DataFrame.
energy = x.resample('30S').apply(lambda x: (numpy.absolute(numpy.fft.fft(x))**2)/30)
Out: energy = 2017-08-02 16:42:00 [[0.422450491863], [0.482244793857], [0.514463...
2017-08-02 16:42:30 [[0.345172558059], [0.554558388074], [0.461898...
2017-08-02 16:43:00 [[0.689816890284], [0.613620822242], [0.389962...
...
#type(energy) = object
答案 0 :(得分:1)
import scipy as sp
# Create input of real sine wave
fs = 1.0
fc = 0.25
n = sp.arange(0, 300)
x = sp.cos(2*sp.pi*n*fc/fs)
# Rearrange x into 10 30 second windows
x = sp.reshape(x, (-1, 30))
# Calculate power over each window [J/s]
p = sp.sum(x*x, 1)/x.size
# Calculate energy [J = J/s * 30 second]
e = p*x.size