iron2man
iron2man

Reputation: 1837

Python - Plotting Fourier transform from text file

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.

Upvotes: 0

Views: 3236

Answers (2)

Dalen
Dalen

Reputation: 4236

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.

Upvotes: 2

Popher
Popher

Reputation: 131

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)

Upvotes: 1

Related Questions