Reputation: 213
I'm new both to Python and to signal processing, so pardon possible misuse of jargon.
I have the discrete values of a signal in a Pandas dataframe x, spaced 1 second apart. It looks like this:
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
...
I would like to compute the energy of the signal over 30 second periods (by energy I mean the sum of squares of the discrete Fourier coefficients, normalized by the number of summands). I would like the output to be of the form
2017-08-02 16:42:00 x_1
2017-08-02 16:42:30 x_2
2017-08-02 16:43:00 x_3
...
where each x_i is a scalar corresponding to the energy for that 30s period. I'm also fine with getting a numpy array, as eventually I only need the energy values and not the time stamps.
I tried doing this:
energy = x.resample('30S').apply(lambda x: (numpy.absolute(numpy.fft.fft(x))**2)/30)
however, I get something of the form
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...
What did I do wrong, and how would be best to correct it?
Thanks!
EDIT: I changed the time a bit as I needed to copy only a small portion of the output, so don't mind the values themselves.
**EDIT 2: The code appears exactly as written here, namely:
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
Upvotes: 3
Views: 14723
Reputation: 1032
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
Upvotes: 5