【问题标题】:Correct way to use scipy.signal.spectral.lombscargle使用 scipy.signal.spectral.lombscargle 的正确方法
【发布时间】:2013-01-09 05:59:15
【问题描述】:

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

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

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

# 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)

1/2pi = 0.159154943092
Frequency = 0.159154943092

但是,以下情况似乎出了问题。

sin(2x) 的频率,即 1/(pi)

# 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)

正在打印以下内容。

1/pi = 0.318309886184
Frequency = 0.0780862900972

似乎不正确。我错过了什么步骤?

【问题讨论】:

    标签: python numpy scipy signal-processing scientific-computing


    【解决方案1】:

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

    freqs = linspace(0.01, 3, 3000)
    

    现在输出是预期的:

    1/pi = 0.318309886184
    Frequency = 0.318311478264
    

    但请注意,如果您将 periodogram 与 freqs / 2 / np.pi 绘制成图,则图表如下所示:

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

    【讨论】:

    • 感谢您的信息。顺便说一句,我在 0.01 到 3 freqs = np.linspace(0.01, 3, 6000) 之间生成更多频率。我预计结果会更接近1/pi = 0.318309886184。但是,当我跑步时,情况并非如此。我得到Frequency = 0.417415475576。有什么经验法则可以遵循吗?谢谢
    • 原来我没有提供彼此足够接近的采样点:time = np.linspace(1, 1000, 100)
    • 如果跨度是几个数量级,例如 0.25 Hz 到 10kHz,它还有助于以几何方式扩展频率:ang_freq = np.geomspace(start_af, end_af, 10000)
    猜你喜欢
    • 1970-01-01
    • 2012-11-01
    • 2015-12-17
    • 2012-02-23
    • 2010-12-28
    • 2012-04-26
    • 2014-10-24
    • 2013-07-29
    • 2016-08-03
    相关资源
    最近更新 更多