I have a time series data and I capture some part of it then use numpy.fft.rfft
command after that I use
plt.plot(2*abs(result)/len(result))
to show rfft
result how can I calculate freq axis? My samplefreq is 65536 Hz.
I have a time series data and I capture some part of it then use numpy.fft.rfft
command after that I use
plt.plot(2*abs(result)/len(result))
to show rfft
result how can I calculate freq axis? My samplefreq is 65536 Hz.
You can use the numpy.fft.rfftfreq
method to generate the frequency data corresponding to the rfft
method. Below I have generated a noisy sin wave and Fourier transformed it as well as generated the frequency data.
import numpy as np
import matplotlib.pyplot as plt
N = 2048
x = np.sin(2*np.pi*10*np.linspace(0,10,N)) + np.random.random(N)*0.1
z = np.fft.rfft(x) # FFT
y = np.fft.rfftfreq(len(x)) # Frequency data
fig, ax = plt.subplots()
ax.plot(y, z)
plt.show()
rfftfreq
will work, as the other poster mentioned, if you have it in your system.
However, I think you are using an outdated version of numpy. Numpy 1.6.1 does not have rfftfreq in the package, so you will see the "not found" error for numpy.fft.rfftfreq
.
If I use an older version of python/numpy:
Speak 'Friend' and Enter: python2.7
Python 2.7.2 (default, Oct 11 2012, 20:14:37)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> numpy.version.version
'1.6.1'
>>> numpy.fft.rfftfreq
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'rfftfreq'
Now the same commands in an updated version with the new numpy:
Speak 'Friend' and Enter: python3.3
Python 3.3.2 (v3.3.2:d047928ae3f6, May 13 2013, 13:52:24)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> numpy.version.version
'1.8.1'
>>> numpy.fft.fftfreq
<function fftfreq at 0x10114edd0>
Update numpy. You may also need to update python.
Simply way to accomplish this (just tested it): install python3.3, then install pip ( Installing pip for python3.3 ). After you install pip, it will be a simple sudo pip3.3 install numpy
and you will get the most updated version (1.8.1 at the time of writing). There are other ways to updated python/numpy (e.g. brew or apt depending on your system ), but pip works pretty well ihmo.