I have data for Velocity vs time. The time steps are not uniform, but the Velocity data is a wave. How do I calculate the principal frequency of the velocity using FFT of Python? Most of the examples I am seeing online are for uniform time stepping.
My data is like
7.56683038E+02 2.12072850E-01
7.56703750E+02 2.13280844E-01
7.56724461E+02 2.14506402E-01
7.56745172E+02 2.15748934E-01
7.56765884E+02 2.17007907E-01
7.56786595E+02 2.18282753E-01
10000 lines like that.
Seeing some online responses, I wrote a code like the following, but it is not working:
#!/usr/bin/env python
import numpy as np
import scipy as sy
import scipy.fftpack as syfp
import pylab as pyl
# Calculate the number of data points
length = 0
for line in open("data.dat"):
length = length + 1
# Read in data from file here
t = np.zeros(shape=(length,1))
u = np.zeros(shape=(length,1))
length = 0
for line in open("data.dat"):
columns = line.split(' ')
t[length] = float(columns[0])
u[length] = float(columns[1])
length = length + 1
# Do FFT analysis of array
FFT = sy.fft(u)
# Getting the related frequencies
freqs = syfp.fftfreq(len(u))
# Create subplot windows and show plot
pyl.subplot(211)
pyl.plot(t, u)
pyl.xlabel('Time')
pyl.ylabel('Amplitude')
pyl.subplot(212)
pyl.plot(freqs, sy.log10(FFT), 'x')
pyl.show()
---------------------- edit ------------------------
with this code I am getting an output like the following figure. I am not sure what this figure shows. I was expecting just to see one peak in the FFT diagram
---------------------- edit ------------------------
My results with the mock data with the sin functions suggested in the comments below are here:
sy.log10(np.abs(FFT))
(ie, note the use of theabs
there). – Ephemeront = 0.02071*np.arange(100000)
andu = np.sin(2*np.pi*t*.01)
? – Ephemeron