简体   繁体   English

从多维FFT中提取频率

[英]Extracting frequencies from multidimensional FFT

I've written a python code to take a 2D signal and FFT it, and now I want to extract the frequencies associated with the FFT. 我写了一个python代码来获取2D信号并对其进行FFT,现在我想提取与FFT相关的频率。 The np.fft.fftfreq fails, giving me the error np.fft.fftfreq失败,给我错误

File "/usr/lib64/python2.7/site-packages/numpy/fft/helper.py", line 153, in fftfreq
    assert isinstance(n,types.IntType) or isinstance(n, integer)
AssertionError

My code is : 我的代码是:

import numpy as np
import scipy as sp
import pylab
import sys
import math

filename = sys.argv[1]  # Get name of file to open 

ifp = open(filename, "r")
ifp.seek(0)

nrows = 0
ncols = 0

nrows = sum(1 for line in ifp) # Sum over all the lines in the file ptr

ifp.seek(0) # Set the fptr back to beginning of file
for line in ifp:
   ncols = len(line.split()) #Split and count number of words in a line
   if ncols > 0:
      break

OrigData = np.zeros([nrows, ncols], dtype=np.float32) #Allocate numpy array
FFTData = np.zeros([nrows, ncols], dtype=complex)
IFFTData = np.zeros([nrows, ncols], dtype=complex)
FreqComp = np.zeros([nrows, ncols], dtype=np.float32)

ii = 0
jj = 0
ifp.seek(0)
for line in ifp:
   linedata = line.split()
   jj = 0
   for el in linedata:
      OrigData[ii,jj] = float(el)
      jj+=1
   ii+=1
ifp.close()

FFTData = np.fft.fft2(OrigData)
FreqComp = np.fft.fftfreq(FFTData, d=2)

#--- Continue with more code ---#

I know that everything else works except the np.fft.fftfreq line, because I added that in last. 我知道除了np.fft.fftfreq行之外其他一切都有效,因为我在最后添加了它。 How does one extract 2 dimensional frequency components? 如何提取二维频率成分?

You are passing in an invalid parameter: np.fft.fftfreq takes the size of the signal data as first parameter (an integer) and the timestep as the second parameter. 您传入的参数无效: np.fft.fftfreq将信号数据的大小作为第一个参数(整数),将时间步长作为第二个参数。 You are passing in an array as the first parameter. 您将传入一个数组作为第一个参数。

You need to perform an np.fft.fft on the signal first though. 您需要先在信号上执行np.fft.fft

Hate to point out the obvious, but read np.fft.fftfreq ... the example code is very pretty clear. 讨厌指出明显的,但读取np.fft.fftfreq ...示例代码非常清楚。


Having performed a 2D FFT, you can obtain the sample frequencies along each dimension as follows: 执行2D FFT后,您可以获得每个维度的采样频率,如下所示:

FreqCompRows = np.fft.fftfreq(FFTData.shape[0],d=2)
FreqCompCols = np.fft.fftfreq(FFTData.shape[1],d=2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM