计算两个角度之间的相对相位 - python

Calculate relative phase between two angles - python

我正在尝试计算两个角度的时间序列之间的相对相位。使用下面的方法,角度是通过从与 Label ALabel B 相关联的 xy 点导出的旋转来测量的。前 3 个时间点角度向相似方向移动,然后在其余 3 个时间点偏离。

我的理解是,使用希尔伯特变换的相对相位计算表示接近 0° 的值表示协调或同相模式。相反,接近 180° 的值表示异步模式或反相。然而,当我导出下面的结果时,我没有看到这个?

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import hilbert

df = pd.DataFrame({      
    'Time' : [1,1,2,2,3,3,4,4,5,5,6,6],    
    'Label' : ['A','B','A','B','A','B','A','B','A','B','A','B'],
    'x' : [-2.0,-1.0,-1.0,0.0,0.0,1.0,0.0,1.0,0.0,1.0,0.0,1.0],
    'y' : [-2.0,-1.0,-2.0,-1.0,-2.0,-1.0,-3.0,0.0,-4.0,1.0,-5.0,2.0],              
   })

x = df.groupby('Label')['x'].diff().fillna(0).astype(float)
y = df.groupby('Label')['y'].diff().fillna(0).astype(float) 

df['Rotation'] = np.arctan2(y, x)
df['Angle'] = np.degrees(df['Rotation'])

df_A = df[df['Label'] == 'A'].reset_index(drop = True)
df_B = df[df['Label'] == 'B'].reset_index(drop = True)

y1 = df_A['Angle'].values
y2 = df_B['Angle'].values

ang1 = np.angle(hilbert(y1),deg=False)
ang2 = np.angle(hilbert(y2),deg=False)

f,ax = plt.subplots(3,1,figsize=(20,5),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[1].plot(ang1,color='r')
ax[1].plot(ang2,color='b')
ax[1].set(title='Angle at each Timepoint')
phase_synchrony = 1-np.sin(np.abs(ang1-ang2)/2)
ax[2].plot(phase_synchrony)
ax[2].set(ylim=[0,1.1],title='Instantaneous Phase Synchrony',xlabel='Time',ylabel='Phase Synchrony')
plt.tight_layout()
plt.show()

根据你的描述,我会简单地使用

phase_synchrony = 1-np.sin(np.abs(y1-y2)/2)

通过希尔伯特变换的 analytic representation 适用于只有您知道(或根据合理原则假设)要分析的信号的实部,在这种情况下,您可以找到一个虚部,使结果函数解析。

但是在你的情况下你已经有了 xy,所以你可以像你已经做的那样直接计算角度。

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import hilbert

df = pd.DataFrame({      
    'Time' : [1,1,2,2,3,3,4,4,5,5,6,6],    
    'Label' : ['A','B','A','B','A','B','A','B','A','B','A','B'],
    'x' : [-2.0,-1.0,-1.0,0.0,0.0,1.0,0.0,1.0,0.0,1.0,0.0,1.0],
    'y' : [-2.0,-1.0,-2.0,-1.0,-2.0,-1.0,-3.0,0.0,-4.0,1.0,-5.0,2.0],              
   })

x = df.groupby('Label')['x'].diff().fillna(0).astype(float)
y = df.groupby('Label')['y'].diff().fillna(0).astype(float) 

df['Rotation'] = np.arctan2(y, x)
df['Angle'] = np.degrees(df['Rotation'])

df_A = df[df['Label'] == 'A'].reset_index(drop = True)
df_B = df[df['Label'] == 'B'].reset_index(drop = True)

y1 = df_A['Angle'].values
y2 = df_B['Angle'].values

# no need to compute the hilbert transforms here

f,ax = plt.subplots(3,1,figsize=(20,5),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[1].plot(ang1,color='r')
ax[1].plot(ang2,color='b')
ax[1].set(title='Angle at each Timepoint')
# all I changed
phase_synchrony = 1-np.sin(np.abs(y1-y2)/2)
ax[2].plot(phase_synchrony)
ax[2].set(ylim=[0,1.1],title='Instantaneous Phase Synchrony',xlabel='Time',ylabel='Phase Synchrony')
plt.tight_layout()
plt.show()