0

I have this text file that has columns of different recorded values to where the first column is of values of time and columns 2, 3, and 4, are of position x, y, and z, respectively, to where that if I were to plot time vs its position of x, y, or z, it will be shown to oscillate.

I want to take Fourier transform of this data and plot it to where the x-axis is frequency.

I'm have trouble following along from examples from other posts, so maybe somebody can give me advice to go in the correct direction.

Having my text file,

with open('SampleData.txt') as f:
    data = f.read()
data = data.split('\n')


t = [float(row.split()[0]) for row in data]  
x1 = [float(row.split()[1]) for row in data] 

Now using, the numpy function of the Fourier Transform, I have no idea where to go from there.

1
  • What does oscillate? What do you mean by time, what by position x? Please, do be more precise and post an example of your file. My answer now reflects what I thought you asked for, but... Commented Jun 3, 2016 at 23:03

2 Answers 2

2
from matplotlib.pyplot import *
import numpy

spectrum =numpy.fft.fft(x1)
spectrum = abs(spectrum[:len(spectrum)/2]) # Just first half of the spectrum, as the second is the negative copy
figure()
plot(spectrum)
show()

I'll edit the answer according to your need, as your question is not very clear.

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

Comments

1

Fast Fourier Transforms in Numpy are pretty straightforward:

fft = np.fft.fft(x)

See here for more details - Link

Plotting a simple line is straightforward too:

import matplotlib.pyplot as plt
plt.plot(fft)

See more here - Click

Edit - may be worth reading your files in in a more efficient way - numpy has a text reader which will save you a bit of time and effort. Click Essentially;

x = np.loadtxt(fname, dtype=<type 'float'>, delimiter=None)

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.