Created
May 13, 2019 06:53
-
-
Save jcheong0428/8ebcadcb0d1c46d52f791ed84e457a14 to your computer and use it in GitHub Desktop.
Instantaneous phase synchrony
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from scipy.signal import hilbert, butter, filtfilt | |
| from scipy.fftpack import fft,fftfreq,rfft,irfft,ifft | |
| import numpy as np | |
| import seaborn as sns | |
| import pandas as pd | |
| import scipy.stats as stats | |
| def butter_bandpass(lowcut, highcut, fs, order=5): | |
| nyq = 0.5 * fs | |
| low = lowcut / nyq | |
| high = highcut / nyq | |
| b, a = butter(order, [low, high], btype='band') | |
| return b, a | |
| def butter_bandpass_filter(data, lowcut, highcut, fs, order=5): | |
| b, a = butter_bandpass(lowcut, highcut, fs, order=order) | |
| y = filtfilt(b, a, data) | |
| return y | |
| lowcut = .01 | |
| highcut = .5 | |
| fs = 30. | |
| order = 1 | |
| d1 = df['S1_Joy'].interpolate().values | |
| d2 = df['S2_Joy'].interpolate().values | |
| y1 = butter_bandpass_filter(d1,lowcut=lowcut,highcut=highcut,fs=fs,order=order) | |
| y2 = butter_bandpass_filter(d2,lowcut=lowcut,highcut=highcut,fs=fs,order=order) | |
| al1 = np.angle(hilbert(y1),deg=False) | |
| al2 = np.angle(hilbert(y2),deg=False) | |
| phase_synchrony = 1-np.sin(np.abs(al1-al2)/2) | |
| N = len(al1) | |
| # Plot results | |
| f,ax = plt.subplots(3,1,figsize=(14,7),sharex=True) | |
| ax[0].plot(y1,color='r',label='y1') | |
| ax[0].plot(y2,color='b',label='y2') | |
| ax[0].legend(bbox_to_anchor=(0., 1.02, 1., .102),ncol=2) | |
| ax[0].set(xlim=[0,N], title='Filtered Timeseries Data') | |
| ax[1].plot(al1,color='r') | |
| ax[1].plot(al2,color='b') | |
| ax[1].set(ylabel='Angle',title='Angle at each Timepoint',xlim=[0,N]) | |
| phase_synchrony = 1-np.sin(np.abs(al1-al2)/2) | |
| ax[2].plot(phase_synchrony) | |
| ax[2].set(ylim=[0,1.1],xlim=[0,N],title='Instantaneous Phase Synchrony',xlabel='Time',ylabel='Phase Synchrony') | |
| plt.tight_layout() | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment