简体   繁体   English

FFT不计算傅立叶变换

[英]FFT not computing fourier transform

I have a csv file that I read in (using python 3 on a Jupyter notebook but get the same results from terminal). 我有一个读入的csv文件(在Jupyter笔记本上使用python 3,但从终端获得了相同的结果)。 I am computing the fft via numpy.fft.fft module and am getting the strange result that the fft of the data returns back the original data - ie a complex vector with real part exactly equal to the (real) input data and the imaginary part identically equal to 0. The code is shown below: 我正在通过numpy.fft.fft模块计算fft,并得到奇怪的结果,即数据的fft返回原始数据-即一个复数矢量,其实数部分等于(实数)输入数据和虚数部分等于0。代码如下所示:

with open('/Users/amacrae/Documents/PMDi/MCT/Jan10/msin287.csv', 'r') as f:
    c = csv.reader(f)
    y = np.array(list(c),dtype=float)
YF = np.fft.fft(y)
print(np.sum(YF.real-y))
print(np.sum(YF.imag))
> 0.0
> 0.0

To ensure that it's not just the data, I plotted the identical data in matlab with the correct results (the data is designed so that the magnitude of the fft is constant over a window in frequency space and has a real iffy.) The corresponding matlab code is: 为了确保不仅是数据,我在matlab中绘制了具有正确结果的相同数据(数据经过精心设计,使得fft的幅度在频率空间中的一个窗口上是恒定的,并且具有真实的不确定性。)代码是:

y = csvread('/Users/amacrae/Documents/PMDi/MCT/Jan10/msin287.csv');
plot(abs(fft(y)))

As far as I can tell the results should be the same in either language ... the real parts of the imported data match in both cases (same length and values) but the fft's do not. 据我所知,两种语言的结果都应该相同……两种情况下,导入数据的实际部分都匹配(长度和值相同),而fft则不然。 The data are quite long - 100,000 samples, but if I create a random 100,000 sample array in python I get a real + imaginary fft. 数据非常长-100,000个样本,但是如果我在python中创建随机的100,000个样本数组,则会得到实数+虚数fft。 Does anyone have an idea what might be causing this? 有谁知道这可能是什么原因?

图像1在Python中。 fft'd数据正是输入数据

图片2是相同的数据,但在Matlab中

Reagarding your code 重新获取代码

print(np.sum(YF.real-y))
print(np.sum(YF.imag))

The first part is true because of Parseval's theorem The second is true since the first half of the spectrum and the second half of the spectrum are conjugate. 第一部分是正确的,因为有Parseval定理 。第二部分是正确的,因为频谱的前半部分和频谱的后半部分是共轭的。

Try comparing the absolute value in Python with your Matlab version. 尝试将Python中的绝对值与您的Matlab版本进行比较。 Absolute value differs from real. 绝对值不同于真实值。 In both cases consider defining the length of the transform ie 1024, 2048 (since e^(j*1e-5) may cause problem) 在这两种情况下,请考虑定义转换的长度,即1024、2048(因为e ^(j * 1e-5)可能会引起问题)

Thanks for the input Radu. 感谢您输入Radu。 In the end it turned out to be something else (see comment above.) 最后,事实证明那是另一回事(请参阅上面的评论。)

The issue was that the method of loading the file produced an array of arrays, ie a column vector. 问题在于,加载文件的方法产生了一个数组数组,即列向量。 When I called fft, it just returned the original column vector back as it performed the fft on each row individually. 当我调用fft时,它只返回了原始列向量,因为它分别对每一行执行了fft。

with open('/Users/amacrae/Documents/PMDi/MCT/Jan10/msin287.csv', 'r') as f:
    c = csv.reader(f)
    y = np.array(list(c),dtype=float)
    # y = [[y0],[y1],[y2],...]] fft(y) = [[y0+0.j],[y1+0.j],[y2+0.j],...]]

The solution was to either flatten the array: 解决方案是将数组展平:

with open('/Users/amacrae/Documents/PMDi/MCT/Jan10/msin287.csv', 'r') as f:
    c = csv.reader(f)
    y = np.array(list(c),dtype=float)
    y2 = flatten(y)
    # produces [y0,y1,y2,....]

or use a different method for reading the file: 或使用其他方法读取文件:

y2 = np.loadtxt('/Users/amacrae/Documents/PMDi/MCT/Jan10/msin287.csv')
# also produces [y0,y1,y2,....]

Both produced the correct FFT. 两者都产生了正确的FFT。

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

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