我试图了解 matplotlib.mlab.psd() 函数返回的频率箱。
使用以下代码我可以检查返回的频率,但我不相信它们是正确的。
import matplotlib.mlab as ml
import numpy as np
sampf=500.
nfft=2**4
testdat=np.random.randn(10000,)
p2,f2=ml.psd(testdat, nfft,sampf,sides='twosided')
p1,f1=ml.psd(testdat, nfft,sampf,sides='onesided')
print testdat.shape
print "Twosided"
print "\tbin1 : {:f} ".format(f2[0])
print "\tbin2 : {:f} ".format(f2[1])
print "\tbinlast : {:f} ".format(f2[-1])
print "onesided"
print "\tbin1 : {:f} ".format(f1[0])
print "\tbin2 : {:f} ".format(f1[1])
print "\tbinlast : {:f} ".format(f1[-1])
print "recreate"
f3=np.arange(nfft)*(sampf/2.)/nfft
print "\tbin1 : {:f} ".format(f3[0])
print "\tbin2 : {:f} ".format(f3[1])
print "\tbinlast : {:f} ".format(f3[-1])
给出了这个输出:
Twosided
bin1 : -250.000000
bin2 : -218.750000
binlast : 218.750000
onesided
bin1 : 0.000000
bin2 : 31.250000
binlast : 250.000000
recreate
bin1 : 0.000000
bin2 : 15.625000
binlast : 234.375000
我认为 2 面情况的最大频率 (binlast) 应该是采样频率的一半,对吗?
已关注 this SO post我认为它的范围应该是 sampf/2。
最佳答案
单方面所做的只是不返回负方面。
因为您正在提交一个实数信号f_hat(w) = conj(f_hat(-w))
(即负欧米伽处的傅立叶分量是欧米伽处分量的复共轭)因此它们将具有相同的幅度,因此就功率谱而言是冗余的。
如果您缺少确切的 sampf/2
,这是因为与具有偶数步数但需要奇数个点(如果您要包含)相关的逐一问题0 并且完全对称。请注意,在您的两侧情况下,最负频率是 -sampf/2
并且您的最大错过 sampf/2
是一个 bin 步骤。您的重建 bin 最后是 (nfft-1)/nfft * (sampf/2)
并且由于我怀疑浮点舍入错误而错过了该值。
关于python - matplotlib 返回的最大 PSD 频率箱是否错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17552009/