Reputation: 109
I have time associated data that I would like to perform a Fourier transform on. Data is located at http://pastebin.com/2i0UGJW9. The problem is that the data is not uniformly spaced. To solve this, I attempted to interpolate the data and then perform the Fast Fourier Transform.
import numpy as np
from scipy.fftpack import fft, fftfreq, fftshift
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
x = np.linspace(min(times), max(times), len(times))
y = interp1d(times, data)(x)
yf = fft(y)
xf = fftfreq(len(times), (max(times)-min(times))/len(times))
xf = fftshift(xf)
yplot = fftshift(yf)
plt.figure()
plt.plot(xf, 1.0/len(times) * np.abs(yplot))
plt.grid()
plt.show()
However, this gives a single spike centered on zero instead of an expected frequency graph. How can I get this to give accurate results?
Upvotes: 1
Views: 4027
Reputation: 1345
As I don't have enough reputation to post a comment, I'll post my suggestions as an answer and hope one of them does lead to answer.
It's probably wiser to interpolate onto a grid that is quite a bit finer than what you are doing. Otherwise your interpolation will smooth the noisy data in an unpredictable fashion. If you want to smooth the data, you'd better do this via the FFT (this might be the whole point of the exercise...)
The time data has a minimum interval of 24, you should probably use an interpolation grid of about half that. Better still, the time intervals are not constant, but they are very regular. After typing print times % 24
it seems a good grid to use would be np.arange(min(times), max(times)+1, 24)
. Note that the +1
is just to include the last time too.
Your data is not periodic, but the FFT treats it as if it were. This means it sees a large jump between the first and last data points. You should look at the FFT documentation on how to tell it to perform an expansion of the data.
The spike at frequency zero is just a consequence of the fact that your signal does not have mean zero.
Hope this was of help.
Upvotes: 7