首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正确使用scipy.signal.spectral.lombscargle

正确使用scipy.signal.spectral.lombscargle
EN

Stack Overflow用户
提问于 2013-01-25 17:34:38
回答 1查看 4.4K关注 0票数 7

我指的是下面的帖子:Using scipy.signal.spectral.lombscargle for period discovery

我意识到在某些情况下给出的答案是正确的。

sin(x)的频率为1/(2* pi)

代码语言:javascript
复制
# imports the numerical array and scientific computing packages
import numpy as np
import scipy as sp
from scipy.signal import spectral

# generates 100 evenly spaced points between 1 and 1000
time = np.linspace(1, 1000, 100)

# computes the sine value of each of those points
mags = np.sin(time)

# scales the sine values so that the mean is 0 and the variance is 1 (the documentation specifies that this must be done)
scaled_mags = (mags-mags.mean())/mags.std()

# generates 1000 frequencies between 0.01 and 1
freqs = np.linspace(0.01, 1, 1000)

# computes the Lomb Scargle Periodogram of the time and scaled magnitudes using each frequency as a guess
periodogram = spectral.lombscargle(time, scaled_mags, freqs)

# returns the inverse of the frequence (i.e. the period) of the largest periodogram value
print "1/2pi = " + str(1/(2*np.pi))
print "Frequency = " + str(freqs[np.argmax(periodogram)] / 2.0 / np.pi)

将打印以下内容。很好。我想是的。我们将lombscargle结果除以2pi的原因是,我们需要将弧度转换为频率。(f =弧度/ 2pi)

代码语言:javascript
复制
1/2pi = 0.159154943092
Frequency = 0.159154943092

然而,对于下面的情况,事情似乎出了问题。

sin(2x)的频率为1/(pi)

代码语言:javascript
复制
# imports the numerical array and scientific computing packages
import numpy as np
import scipy as sp
from scipy.signal import spectral

# generates 100 evenly spaced points between 1 and 1000
time = np.linspace(1, 1000, 100)

# computes the sine value of each of those points
mags = np.sin(2 * time)

# scales the sine values so that the mean is 0 and the variance is 1 (the documentation specifies that this must be done)
scaled_mags = (mags-mags.mean())/mags.std()

# generates 1000 frequencies between 0.01 and 1
freqs = np.linspace(0.01, 1, 1000)

# computes the Lomb Scargle Periodogram of the time and scaled magnitudes using each frequency as a guess
periodogram = spectral.lombscargle(time, scaled_mags, freqs)

# returns the inverse of the frequence (i.e. the period) of the largest periodogram value
print "1/pi = " + str(1/(np.pi))
print "Frequency = " + str(freqs[np.argmax(periodogram)] / 2.0 / np.pi)

正在打印以下内容。

代码语言:javascript
复制
1/pi = 0.318309886184
Frequency = 0.0780862900972

似乎是不正确的。我遗漏了什么步骤吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-26 01:23:49

您理所当然地希望峰值出现在1 / pi上,但您测试的最高频率是1 / 2 / pi……尝试以下单个更改:

代码语言:javascript
复制
freqs = linspace(0.01, 3, 3000)

现在的输出是预期的:

代码语言:javascript
复制
1/pi = 0.318309886184
Frequency = 0.318311478264

但请注意,如果您绘制periodogramfreqs / 2 / np.pi的关系图,则该图如下所示:

因此,对于更复杂的信号,你不能仅仅依靠寻找周期图的max来找到主频,因为谐波可能会欺骗你。

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14518970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档