Two signals can be fully synchronized, not synchronized, or somewhere in between. We usually measure phase synchronization in radians. The related quantity of instantaneous phase can be measured with the NumPy angle()
function. For real-valued data, we need to obtain the analytic representation of the signal, which is given by the Hilbert transform. The Hilbert transform is also available in SciPy and NumPy.
Cross-correlation measures the correlation between two signals using a sliding inner product. We can use cross-correlation to measure the time delay between two signals. NumPy offers the correlate()
function, which calculates the cross-correlation between two arrays.
import dautil as dl import matplotlib.pyplot as plt import numpy as np from IPython.display import HTML
df = dl.data.Weather.load().dropna() df = dl.ts.groupby_yday(df).mean().dropna() ws_phase = dl.ts.instant_phase(df['WIND_SPEED']) wd_phase = dl.ts.instant_phase(df['WIND_DIR'])
sp = dl.plotting.Subplotter(2, 2, context) cp = dl.plotting.CyclePlotter(sp.ax) cp.plot(df.index, dl.stats.zscores(df['WIND_DIR'].values), label='Wind direction') cp.plot(df.index, dl.stats.zscores(df['WIND_SPEED'].values), label='Wind speed') sp.label()
cp = dl.plotting.CyclePlotter(sp.next_ax()) cp.plot(df.index, ws_phase, label='Wind speed') cp.plot(df.index, wd_phase, label='Wind direction') sp.label()
sp.label(advance=True) sp.ax.plot(np.correlate(df['WIND_SPEED'], df['WIND_DIR'], 'same'))
sp.label(advance=True) sp.ax.plot(np.angle(np.fft.fft(df['WIND_SPEED'])/np.fft.fft(df['WIND_DIR']))) HTML(sp.exit())
Refer to the following screenshot for the end result:
The code for this recipe is in the phase_synchrony.ipynb
file in this book's code bundle.
angle()
function at https://docs.scipy.org/doc/numpy/reference/generated/numpy.angle.html (retrieved September 2015)correlate()
function at https://docs.scipy.org/doc/numpy/reference/generated/numpy.correlate.html (retrieved September 2015)