我目前正在为一项类(class)作业做一些频谱分析,尽管我们还没有明确教授傅立叶变换。我一直在使用 scipy 和 numpy 中的各种 fft 算法处理一些我知道答案应该是什么样的数据
在这种情况下,它是一个 8kHz 载波频率的 AM 信号和顶部的 1kHz 调制正弦波,因此在 fft 上应该有 3 个清晰的峰值
当应用 scipy.fftpack.rfft
和 numpy.fft.rfft
我分别得到以下图:
科学:
Numpy:
虽然 2 个 FFT 的形状与峰值之间的正确比率大致相同,但 numpy
看起来平滑得多,而 scipy
稍微小一些最大峰值,并且有更多的噪音。
我假设这主要归因于离散傅里叶变换算法的不同应用,并且看过其他关于 scipy
实现如何在运行时更快的文章。但我一直在想具体是什么导致了差异,哪个更准确?
编辑:用于生成图的代码:
data = pd.read_csv("./Waveforms/AM waveform Sine.csv", sep = ',', dtype = float)
data = data.as_matrix()
time = data[:,0]
voltage = data[:,1]/data[:,1].max() # normalise the values
#scipy plot:
plt.figure()
magnitude = scipy.fftpack.rfft(voltage)
freq = scipy.fftpack.rfftfreq(len(time),np.diff(time)[0])
plt.figure()
plt.plot(freq, np.absolute(magnitude), lw = 1)
plt.ylim(0,2500)
plt.xlim(0,15)
#numpy plot
magnitude = np.fft.rfft(voltage)
freq = np.fft.rfftfreq(len(time),np.diff(time)[0])
plt.figure()
plt.plot(freq, np.absolute(magnitude), lw = 1)
plt.ylim(0,2500)
plt.xlim(0,15)
最佳答案
来自 NumPy 的文档 rfft :
Returns:
out : complex ndarray
The truncated or zero-padded input, transformed along the axis indicated by axis, or the last one if axis is not specified. If n is even, the length of the transformed axis is (n/2)+1. If n is odd, the length is (n+1)/2.
它没有明确写入,但“转换后的数据”在这里很复杂。
来自 SciPy 的文档 rfft
z : real ndarray
The returned real array contains:
[y(0),Re(y(1)),Im(y(1)),...,Re(y(n/2))] if n is even [y(0),Re(y(1)),Im(y(1)),...,Re(y(n/2)),Im(y(n/2))] if n is odd
结论:存储方式不同
首先,看一下magnitude
的长度,两种情况下都会有所不同。为了清楚起见,我在下面举了一个例子:
In [33]: data = np.random.random(size=8)
In [34]: np.fft.rfft(data)
Out[34]:
array([ 3.33822983+0.j , 0.15879369+0.48542266j,
0.00614876+0.03590621j, -0.67376592-0.69793372j, 1.51730861+0.j ])
In [35]: scipy.fftpack.rfft(data)
Out[35]:
array([ 3.33822983, 0.15879369, 0.48542266, 0.00614876, 0.03590621,
-0.67376592, -0.69793372, 1.51730861])
两种情况下的第一个元素都是所谓的“直流分量”(信号的平均值)。
然后,您可以在 SciPy 版本中识别 NumPy 版本的实部和虚部的连续性。
关于python - 为什么 scipy 和 numpy fft 图看起来不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47307862/