We can think of periodic signals as being composed of multiple frequencies. For instance, sound is composed of multiple tones and light is composed of multiple colors. The range of frequencies is called the frequency spectrum. When we analyze the frequency spectrum of a signal, it's natural to take a look at the result of the Fourier Transform of the signal. The periodogram extends this and is equal to the squared magnitude of the Fourier Transform, as follows:
We will look at the periodograms of the following variables:
from scipy import signal import matplotlib.pyplot as plt import dautil as dl import numpy as np import pandas as pd from IPython.display import HTML
fs = 365 rain = dl.data.Weather.load()['RAIN'].dropna()
def plot_periodogram(arr, ax): f, Pxx_den = signal.periodogram(arr, fs) ax.set_xlabel('Frequency (Hz)') ax.set_ylabel('PSD') ax.semilogy(f, Pxx_den)
sp = dl.plotting.Subplotter(2, 2, context) sp.label() plot_periodogram(rain, sp.ax) sp.label(advance=True) plot_periodogram(np.diff(rain, 2), sp.ax) sp.label(advance=True) plot_periodogram(pd.rolling_sum(rain, fs).dropna(), sp.ax) sp.label(advance=True) plot_periodogram(pd.rolling_mean(rain, fs).dropna(), sp.ax) HTML(sp.exit())
Refer to the following screenshot for the end result:
The code is from the periodograms.ipynb
file in this book's code bundle demonstrates periodograms.
periodogram()
function at https://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.signal.periodogram.html#scipy.signal.periodogram (retrieved September 2015)