Using the Lomb-Scargle periodogram

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

Using the Lomb-Scargle periodogram

How to do it...

  1. The imports are as follows:
    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
  2. Load the sunspots data as follows:
    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)
  3. Plot a sine waveform as follows:
    sp = dl.plotting.Subplotter(2, 2, context)
    sp.ax.plot(t, sine)
    sp.label()
    
    sp.next_ax().plot(df['YEAR'], df['SUNACTIVITY'])
    sp.label()
  4. Apply the periodogram to the sine:
    pgram = signal.lombscargle(t, sine, f)
    sp.next_ax().plot(f, 2 * np.sqrt(pgram/size))
    sp.label()
  5. Apply the periodogram to the sunspots data:
    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:

How to do it...

The preceding code is a breakdown of the lomb_scargle.ipynb file in this book's code bundle.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset