Skip to content

Instantly share code, notes, and snippets.

@jcheong0428
Last active February 7, 2022 04:25
Show Gist options
  • Select an option

  • Save jcheong0428/7d5759f78145fc0dc979337f82c6ea33 to your computer and use it in GitHub Desktop.

Select an option

Save jcheong0428/7d5759f78145fc0dc979337f82c6ea33 to your computer and use it in GitHub Desktop.

Revisions

  1. jcheong0428 revised this gist Mar 13, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion synchrony03.py
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ def crosscorr(datax, datay, lag=0, wrap=False):
    seconds = 5
    fps = 30
    rs = [crosscorr(d1,d2, lag) for lag in range(-int(seconds*fps),int(seconds*fps+1))]
    offset = np.ceil(len(rs)/2)-np.argmax(rs)
    offset = np.floor(len(rs)/2)-np.argmax(rs)
    f,ax=plt.subplots(figsize=(14,3))
    ax.plot(rs)
    ax.axvline(np.ceil(len(rs)/2),color='k',linestyle='--',label='Center')
  2. jcheong0428 revised this gist Aug 20, 2019. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions synchrony03.py
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,7 @@ def crosscorr(datax, datay, lag=0, wrap=False):
    ax.plot(rs)
    ax.axvline(np.ceil(len(rs)/2),color='k',linestyle='--',label='Center')
    ax.axvline(np.argmax(rs),color='r',linestyle='--',label='Peak synchrony')
    ax.set(title=f'Offset = {offset} frames\nS1 leads <> S2 leads',ylim=[.1,.31],xlim=[0,300], xlabel='Offset',ylabel='Pearson r')
    ax.set_xticklabels([int(item-150) for item in ax.get_xticks()])
    ax.set(title=f'Offset = {offset} frames\nS1 leads <> S2 leads',ylim=[.1,.31],xlim=[0,301], xlabel='Offset',ylabel='Pearson r')
    ax.set_xticks([0, 50, 100, 151, 201, 251, 301])
    ax.set_xticklabels([-150, -100, -50, 0, 50, 100, 150]);
    plt.legend()
  3. jcheong0428 revised this gist Aug 20, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion synchrony03.py
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ def crosscorr(datax, datay, lag=0, wrap=False):
    d2 = df['S2_Joy']
    seconds = 5
    fps = 30
    rs = [crosscorr(d1,d2, lag) for lag in range(-int(seconds*fps-1),int(seconds*fps))]
    rs = [crosscorr(d1,d2, lag) for lag in range(-int(seconds*fps),int(seconds*fps+1))]
    offset = np.ceil(len(rs)/2)-np.argmax(rs)
    f,ax=plt.subplots(figsize=(14,3))
    ax.plot(rs)
  4. jcheong0428 revised this gist May 13, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion synchrony03.py
    Original file line number Diff line number Diff line change
    @@ -28,5 +28,6 @@ def crosscorr(datax, datay, lag=0, wrap=False):
    ax.plot(rs)
    ax.axvline(np.ceil(len(rs)/2),color='k',linestyle='--',label='Center')
    ax.axvline(np.argmax(rs),color='r',linestyle='--',label='Peak synchrony')
    ax.set(title=f'Offset = {offset} frames\nS1 leads <> S2 leads',ylim=[.1,.31])
    ax.set(title=f'Offset = {offset} frames\nS1 leads <> S2 leads',ylim=[.1,.31],xlim=[0,300], xlabel='Offset',ylabel='Pearson r')
    ax.set_xticklabels([int(item-150) for item in ax.get_xticks()])
    plt.legend()
  5. jcheong0428 created this gist May 13, 2019.
    32 changes: 32 additions & 0 deletions synchrony03.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    def crosscorr(datax, datay, lag=0, wrap=False):
    """ Lag-N cross correlation.
    Shifted data filled with NaNs
    Parameters
    ----------
    lag : int, default 0
    datax, datay : pandas.Series objects of equal length
    Returns
    ----------
    crosscorr : float
    """
    if wrap:
    shiftedy = datay.shift(lag)
    shiftedy.iloc[:lag] = datay.iloc[-lag:].values
    return datax.corr(shiftedy)
    else:
    return datax.corr(datay.shift(lag))

    d1 = df['S1_Joy']
    d2 = df['S2_Joy']
    seconds = 5
    fps = 30
    rs = [crosscorr(d1,d2, lag) for lag in range(-int(seconds*fps-1),int(seconds*fps))]
    offset = np.ceil(len(rs)/2)-np.argmax(rs)
    f,ax=plt.subplots(figsize=(14,3))
    ax.plot(rs)
    ax.axvline(np.ceil(len(rs)/2),color='k',linestyle='--',label='Center')
    ax.axvline(np.argmax(rs),color='r',linestyle='--',label='Peak synchrony')
    ax.set(title=f'Offset = {offset} frames\nS1 leads <> S2 leads',ylim=[.1,.31])
    plt.legend()