我已经编写了一个 python 代码来获取二维信号并对它进行 FFT,现在我想提取与 FFT 相关的频率。 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
我的代码是:
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 ---#
我知道除了 np.fft.fftfreq
行之外的其他所有内容都有效,因为我在最后添加了它。如何提取二维频率分量?
最佳答案
您传递的参数无效:np.fft.fftfreq
将信号数据的大小作为第一个参数(整数),将时间步长作为第二个参数。您将传入一个数组作为第一个参数。
不过,您需要先对信号执行 np.fft.fft
。
不想指出明显的,但请阅读 np.fft.fftfreq
...示例代码非常清晰。
执行 2D FFT 后,您可以获得沿每个维度的采样频率,如下所示:
FreqCompRows = np.fft.fftfreq(FFTData.shape[0],d=2)
FreqCompCols = np.fft.fftfreq(FFTData.shape[1],d=2)
关于python - 从多维 FFT 中提取频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14582543/