Measuring phase synchronization

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.

How to do it...

  1. The imports are as follows:
    import dautil as dl
    import matplotlib.pyplot as plt
    import numpy as np
    from IPython.display import HTML
  2. Load the data and calculate the instantaneous phase:
    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'])
  3. Plot the wind direction and speed z-scores:
    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()
  4. Plot the instantaneous phase as follows:
    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()
  5. Plot the correlation of wind speed and direction:
    sp.label(advance=True)
    sp.ax.plot(np.correlate(df['WIND_SPEED'], df['WIND_DIR'], 'same'))
  6. Plot the phase shift with the fast Fourier Transform:
    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:

How to do it...

The code for this recipe is in the phase_synchrony.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