2012-11-12 27 views
4

新的Scipy v0.11提供了一個用於頻譜分析的軟件包。不幸的是,文檔很少,並沒有很多可用的例子。使用scipy.signal.spectral.lombscargle發現週期

作爲一個嬰兒的例子,我試圖做一個正弦波的時期發現。不幸的是,它預測的時間段爲1,而不是預期的2pi。有任何想法嗎?

# 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 
1/freqs[np.argmax(periodogram)] 

這將返回1代替2pi ~= 1/0.6366預產期。有任何想法嗎?

+1

注意:不要'從scipy.signal進口lombscargle',不'從scipy.signal進口spectral' - - 請參閱http://docs.scipy.org/doc/scipy/reference/api.html#api-definition –

回答

6

請注意的spectral.lombscargle的最後一個參數是根據docstring角頻率:

Parameters 
---------- 
x : array_like 
Sample times. 
y : array_like 
Measurement values. 
freqs : array_like 
Angular frequencies for output periodogram. 
+0

這是否意味着結果應乘以2pi? – rhombidodecahedron

+1

是的,角頻率以弧度/秒給出。你可以在[wikipedia](http://en.wikipedia.org/wiki/Angular_frequency)上閱讀更多內容。 – btel

+0

@EarlBellinger如果你滿意的話,你也可以接受答案。 – btel