0

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.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
Burak Nebioglu
  • 81
  • 2
  • 11
  • See http://dsp.stackexchange.com/questions/2970/how-to-make-frequency-axis-for-even-and-odd-fft-length – Mihai8 Apr 11 '14 at 13:27

2 Answers2

2

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()
Ffisegydd
  • 51,807
  • 15
  • 147
  • 125
  • 3
    Thanks for all replies. Ffisegydd I tried you code but I get File "/Users/buraknebioglu/Desktop/npfft_ex.py", line 31, in y = np.fft.rfftfreq(len(x)) # Frequency data AttributeError: 'module' object has no attribute 'rfftfreq' error my numpy version is '1.6.2' – Burak Nebioglu Apr 11 '14 at 14:42
  • @BurakNebioglu can you copy the code into a new Python file (on it's own) and test it? Does this work? If so then you should have no problem fitting the `numpy.fft.rfftfreq` method into your own code. – Ffisegydd Apr 11 '14 at 14:45
  • I did just like you said but it failed now I try pip install -U numpy. I installed with pip but version does not change :( – Burak Nebioglu Apr 11 '14 at 16:06
1

Solution

rfftfreq will work, as the other poster mentioned, if you have it in your system.

Caveat

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>

My Recommendation

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.

Community
  • 1
  • 1
heisenBug
  • 865
  • 9
  • 19