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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.