The Lomb-Scargle periodogram is a frequency spectrum estimation method that fits sines to data, and it is frequently used with unevenly sampled data. The method is named after Nicholas R. Lomb and Jeffrey D. Scargle. The algorithm was published around 1976 and has been improved since then. Scargle introduced a time delay parameter, which separates the sine and cosine waveforms. The following equations define the time delay (6.7) and periodogram (6.8).
from scipy import signal import numpy as np import matplotlib.pyplot as plt import dautil as dl import statsmodels.api as sm from IPython.display import HTML
df = sm.datasets.sunspots.load_pandas().data sunspots = df['SUNACTIVITY'].values size = len(sunspots) t = np.linspace(-2 * np.pi, 2 * np.pi, size) sine = dl.ts.sine_like(sunspots) f = np.linspace(0.01, 2, 10 * size)
sp = dl.plotting.Subplotter(2, 2, context) sp.ax.plot(t, sine) sp.label() sp.next_ax().plot(df['YEAR'], df['SUNACTIVITY']) sp.label()
pgram = signal.lombscargle(t, sine, f) sp.next_ax().plot(f, 2 * np.sqrt(pgram/size)) sp.label()
pgram = signal.lombscargle(np.arange(size, dtype=float), sunspots, f) sp.next_ax().plot(f, 2 * np.sqrt(pgram/size)) sp.label() HTML(sp.exit())
Refer to the following screenshot for the end result:
The preceding code is a breakdown of the lomb_scargle.ipynb
file in this book's code bundle.
lombscargle()
function documented at https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.lombscargle.html (retrieved September 2015)