0

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()
6
  • Asking "why doesn't this work" isn't going to get very far on SO. What kind of error are you getting? Commented Oct 2, 2020 at 17:51
  • No error, there is no data in the plot and the fft is full of nan. Sorry about my poor post etiquette. Commented Oct 2, 2020 at 17:58
  • Could you try to plot, np.absolute(fft) just to make sure this isn't a complex number problem? Something like this, to get the magnitude of the fft, should almost always be done anyway. Commented Oct 2, 2020 at 18:08
  • I tried np.absolute(fft) still the same results. printing fft yields [nan+nanj nan+nanj nan+nanj ... nan+nanj nan+nanj nan+nanj] Commented Oct 2, 2020 at 18:10
  • I don't know what the input frequency is. Commented Oct 2, 2020 at 18:10

1 Answer 1

1

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)

enter image description here

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)

enter image description here

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.