Reputation: 471
I have a csv file that contains time and torque data. https://pastebin.com/MAT2rG3U This data set is truncated because size limit.
I am trying to find the FFT of the data to find the frequency of a vibration.
Here is my code (here is the example I used Fast Fourier Transform in Python ), it does not produce any results. I've researched many online resources and can not find my error
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
data = pd.read_csv('data.csv',index_col=0)
data = data['Torque'].astype(float).values
print(data)
N = data.shape[0] #number of elements
t = np.linspace(0, 300, N)
#t=np.arange(N)
s = data
fft = np.fft.fft(s)
fftfreq = np.fft.fftfreq(len(s))
T = t[1] - t[0]
print(T)
f = np.linspace(0, 1 / T, N)
plt.ylabel("Amplitude")
plt.xlabel("Frequency [Hz]")
plt.plot(fftfreq,fft)
#plt.xlim(0,100)
plt.show()
Upvotes: 0
Views: 4558
Reputation: 69192
What you've posted works for me, but your data isn't valid for an FFT because the timesteps aren't consistent. That is, you don't have a well defined sample rate.
data = pd.read_csv('torque_data.txt',index_col=0)
data = data['Torque'].astype(float).values
print(data)
N = data.shape[0] #number of elements
t = np.linspace(0, 300, N)
#t=np.arange(N)
s = data
fft = np.fft.fft(s)
fftfreq = np.fft.fftfreq(len(s))
T = t[1] - t[0]
print(T)
f = np.linspace(0, 1 / T, N)
plt.ylabel("Amplitude")
plt.xlabel("Frequency [Hz]")
plt.plot(fftfreq, np.absolute(fft))
#plt.xlim(0,100)
There's probably something wrong with the data that you didn't include in the sample that's giving you the NaNs.
Still, in the data you provided, the sample rate isn't consistent, which is required for an FFT. To see this, plot a histogram of the time steps:
# read in the data like this to get the times
data = pd.read_csv('torque_data.txt')
time = data['Seconds'].astype(float).values
data = data['Torque'].astype(float).values
# now look at the timesteps
fig, axs = plt.subplots()
time_deltas = t[1:]-t[:-1]
h = axs.hist(time_deltas, bins=50)
Because so many of the timesteps have different values, I'd be worried about trusting the FFT. (When I first looked at your data, most of the earlier points seemed to have the 0.004s timestep, so I wonder if your data collection is changing over time and not just randomly, but whatever, you need to sort this out too.) There are solutions to this, like interpolation/resampling, or down-sampling, but one can't reliably trust the FFT results without a fix.
Upvotes: 1