0

I have a lot of data files (testfft.dat) whit three columns like

trash | x | f(x)

100, 1, 0.129

100, 2, 0.198

etc.

where ',' is the separator. I was trying to do the Fast Fourier Transform over the f(x) column and then plot the spectrum vs. x. I was trying to do that following another answer but I can't get that working. More over, I just don't know python at all, I was doing the FFT with xmgrace but it isn't efficient.

I did the following (on ipython)

import numpy as np
import scipy as sy
import scipy.fftpack as syfp

array = np.loadtxt("testfft.dat")

but I get this error when I do the last line

ValueError                                Traceback (most recent call last)
<ipython-input-5-f42979d3ce2b> in <module>()
----> 1 array = np.loadtxt("testfft.dat")

~/.local/lib/python3.5/site-packages/numpy/lib/npyio.py in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin)
   1022 
   1023             # Convert each value according to its column and store
-> 1024             items = [conv(val) for (conv, val) in zip(converters, vals)]
   1025             # Then pack it according to the dtype's nesting
   1026             items = pack_items(items, packing)

~/.local/lib/python3.5/site-packages/numpy/lib/npyio.py in <listcomp>(.0)
   1022 
   1023             # Convert each value according to its column and store
-> 1024             items = [conv(val) for (conv, val) in zip(converters, vals)]
   1025             # Then pack it according to the dtype's nesting
   1026             items = pack_items(items, packing)

~/.local/lib/python3.5/site-packages/numpy/lib/npyio.py in floatconv(x)
    723         if b'0x' in x:
    724             return float.fromhex(asstr(x))
--> 725         return float(x)
    726 
    727     typ = dtype.type

ValueError: could not convert string to float: b'10,'

As I don't really know Python, I don't know what to do from here.

Is there a way to modify the script from the answer to do what I need?

Thanks.

2
  • Welcome to stackoverflow! The answer you cited offered a Minimal, Complete, and Verifiable example: stackoverflow.com/help/mcve . Please edit your question to do the same. What specific code / data did you try, and what diagnostic error appeared when it wasn't working for you? We want to help you. Help us to help you. Commented Oct 28, 2017 at 0:56
  • @J_H Thanks for the advice, I edited my question with all that I did. Commented Oct 28, 2017 at 1:11

1 Answer 1

2

Your header has a different separator (|) than for the rows. So we first need to skip the header and then give the columns names (trash,x,f). Then use scipy's fft to get the discrete variant:

import numpy as np
from scipy.fftpack import fft

df = np.genfromtxt('testfft.dat', skip_header=1, names=['trash','x','f'], delimiter=',')
print(fft(df['f']))
[ 0.327+0.j -0.069+0.j]

You can find documentation here: FFT

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

5 Comments

That works great. Now I will try to do a script that loop this over my 56 files. Thanks @sharatpc
@sempiternal happy to help. glad if you can accept the answer.
Why did you print fft(fft(df['f'])) and not just fft(df['f'])? @sharatpc
Edited. Wanted to show second order was also possible.
Oh I understand. So I'm doing It ok

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.