Note
Click here to download the full example code
Phase shift and temporal delay in PAC¶
This example disantangles the two distinct notions of phase shift and temporal delay in phase-amplitude coupling. The phase shift ($phi$) is the phase of the slow oscillation which corresponds to the maximum amplitude of the fast oscillation. The temporal delay ($tau$) is the delay between the two coupled components. The two notions would be identical should the driver be a perfect stationary sinusoid.
(1st line) When both are equal to zero, the high frequency bursts happen in the driver’s peaks.
(2nd line) When $tau = 0$ and $phi != 0$, the bursts are shifted in time with respect to the driver’s peaks, and this shift varies depending on the instantaneous frequency of the driver.
(3rd line) When $tau != 0$ and $phi = 0$, the bursts are shifted in time with respect to the driver’s peaks, and this shift is constant over the signal. In this case, note how the driver’s phase corresponding to the bursts varies depending on the instantaneous frequency of the driver.
(4th line) Both $tau$ and $phi$ can also be both non-zero.
The temporal delay is estimated maximizing the likelihood on DAR models. The phase shift is extracted from a DAR model fitted with the optimal temporal delay.
References¶
Dupre la Tour et al. (2017). Non-linear Auto-Regressive Models for Cross-Frequency Coupling in Neural Time Series. bioRxiv, 159731.
Out:
[ ] 0% | 0.00 sec | delays
[ ] 1% | 0.19 sec | delays
[ ] 1% | 0.35 sec | delays
[ ] 2% | 0.49 sec | delays
[ ] 2% | 0.61 sec | delays
[. ] 3% | 0.72 sec | delays
[. ] 4% | 0.86 sec | delays
[. ] 4% | 0.97 sec | delays
[. ] 5% | 1.10 sec | delays
[.. ] 5% | 1.29 sec | delays
[.. ] 6% | 1.43 sec | delays
[.. ] 7% | 1.56 sec | delays
[.. ] 7% | 1.67 sec | delays
[... ] 8% | 1.78 sec | delays
[... ] 8% | 1.96 sec | delays
[... ] 9% | 2.10 sec | delays
[... ] 10% | 2.25 sec | delays
[.... ] 10% | 2.38 sec | delays
[.... ] 11% | 2.48 sec | delays
[.... ] 11% | 2.61 sec | delays
[.... ] 12% | 2.76 sec | delays
[..... ] 13% | 2.89 sec | delays
[..... ] 13% | 3.00 sec | delays
[..... ] 14% | 3.11 sec | delays
[..... ] 14% | 3.22 sec | delays
[..... ] 15% | 3.34 sec | delays
[...... ] 16% | 3.45 sec | delays
[...... ] 16% | 3.56 sec | delays
[...... ] 17% | 3.67 sec | delays
[...... ] 17% | 3.78 sec | delays
[....... ] 18% | 3.89 sec | delays
[....... ] 19% | 4.07 sec | delays
[....... ] 19% | 4.20 sec | delays
[....... ] 20% | 4.36 sec | delays
[........ ] 20% | 4.47 sec | delays
[........ ] 21% | 4.60 sec | delays
[........ ] 22% | 4.71 sec | delays
[........ ] 22% | 4.85 sec | delays
[......... ] 23% | 4.95 sec | delays
[......... ] 23% | 5.08 sec | delays
[......... ] 24% | 5.19 sec | delays
[......... ] 25% | 5.33 sec | delays
[.......... ] 25% | 5.45 sec | delays
[.......... ] 26% | 5.60 sec | delays
[.......... ] 26% | 5.76 sec | delays
[.......... ] 27% | 5.89 sec | delays
[........... ] 28% | 6.01 sec | delays
[........... ] 28% | 6.14 sec | delays
[........... ] 29% | 6.29 sec | delays
[........... ] 29% | 6.43 sec | delays
[........... ] 30% | 6.56 sec | delays
[............ ] 31% | 6.67 sec | delays
[............ ] 31% | 6.86 sec | delays
[............ ] 32% | 6.97 sec | delays
[............ ] 32% | 7.09 sec | delays
[............. ] 33% | 7.25 sec | delays
[............. ] 34% | 7.40 sec | delays
[............. ] 34% | 7.51 sec | delays
[............. ] 35% | 7.69 sec | delays
[.............. ] 35% | 7.88 sec | delays
[.............. ] 36% | 8.03 sec | delays
[.............. ] 37% | 8.23 sec | delays
[.............. ] 37% | 8.46 sec | delays
[............... ] 38% | 8.61 sec | delays
[............... ] 38% | 8.74 sec | delays
[............... ] 39% | 8.96 sec | delays
[............... ] 40% | 9.19 sec | delays
[................ ] 40% | 9.33 sec | delays
[................ ] 41% | 9.60 sec | delays
[................ ] 41% | 9.80 sec | delays
[................ ] 42% | 10.04 sec | delays
[................. ] 43% | 10.25 sec | delays
[................. ] 43% | 10.49 sec | delays
[................. ] 44% | 10.68 sec | delays
[................. ] 44% | 10.86 sec | delays
[................. ] 45% | 11.01 sec | delays
[.................. ] 46% | 11.19 sec | delays
[.................. ] 46% | 11.32 sec | delays
[.................. ] 47% | 11.59 sec | delays
[.................. ] 47% | 11.79 sec | delays
[................... ] 48% | 12.03 sec | delays
[................... ] 49% | 12.17 sec | delays
[................... ] 49% | 12.43 sec | delays
[................... ] 50% | 12.65 sec | delays
[.................... ] 50% | 12.99 sec | delays
[.................... ] 51% | 13.11 sec | delays
[.................... ] 51% | 13.30 sec | delays
[.................... ] 52% | 13.57 sec | delays
[..................... ] 53% | 13.86 sec | delays
[..................... ] 53% | 14.11 sec | delays
[..................... ] 54% | 14.29 sec | delays
[..................... ] 54% | 14.61 sec | delays
[...................... ] 55% | 14.76 sec | delays
[...................... ] 56% | 14.96 sec | delays
[...................... ] 56% | 15.17 sec | delays
[...................... ] 57% | 15.31 sec | delays
[...................... ] 57% | 15.52 sec | delays
[....................... ] 58% | 15.72 sec | delays
[....................... ] 59% | 15.92 sec | delays
[....................... ] 59% | 16.16 sec | delays
[....................... ] 60% | 16.43 sec | delays
[........................ ] 60% | 16.66 sec | delays
[........................ ] 61% | 16.87 sec | delays
[........................ ] 62% | 17.08 sec | delays
[........................ ] 62% | 17.33 sec | delays
[......................... ] 63% | 17.58 sec | delays
[......................... ] 63% | 17.83 sec | delays
[......................... ] 64% | 18.00 sec | delays
[......................... ] 65% | 18.16 sec | delays
[.......................... ] 65% | 18.31 sec | delays
[.......................... ] 66% | 18.48 sec | delays
[.......................... ] 66% | 18.66 sec | delays
[.......................... ] 67% | 18.85 sec | delays
[........................... ] 68% | 18.97 sec | delays
[........................... ] 68% | 19.08 sec | delays
[........................... ] 69% | 19.20 sec | delays
[........................... ] 69% | 19.33 sec | delays
[............................ ] 70% | 19.70 sec | delays
[............................ ] 71% | 19.84 sec | delays
[............................ ] 71% | 19.96 sec | delays
[............................ ] 72% | 20.08 sec | delays
[............................ ] 72% | 20.20 sec | delays
[............................. ] 73% | 20.31 sec | delays
[............................. ] 74% | 20.44 sec | delays
[............................. ] 74% | 20.57 sec | delays
[............................. ] 75% | 20.68 sec | delays
[.............................. ] 75% | 20.80 sec | delays
[.............................. ] 76% | 20.92 sec | delays
[.............................. ] 77% | 21.06 sec | delays
[.............................. ] 77% | 21.23 sec | delays
[............................... ] 78% | 21.35 sec | delays
[............................... ] 78% | 21.47 sec | delays
[............................... ] 79% | 21.58 sec | delays
[............................... ] 80% | 21.69 sec | delays
[................................ ] 80% | 21.80 sec | delays
[................................ ] 81% | 21.91 sec | delays
[................................ ] 81% | 22.02 sec | delays
[................................ ] 82% | 22.13 sec | delays
[................................. ] 83% | 22.24 sec | delays
[................................. ] 83% | 22.35 sec | delays
[................................. ] 84% | 22.46 sec | delays
[................................. ] 84% | 22.57 sec | delays
[.................................. ] 85% | 22.68 sec | delays
[.................................. ] 86% | 22.80 sec | delays
[.................................. ] 86% | 22.91 sec | delays
[.................................. ] 87% | 23.02 sec | delays
[.................................. ] 87% | 23.13 sec | delays
[................................... ] 88% | 23.24 sec | delays
[................................... ] 89% | 23.35 sec | delays
[................................... ] 89% | 23.46 sec | delays
[................................... ] 90% | 23.57 sec | delays
[.................................... ] 90% | 23.68 sec | delays
[.................................... ] 91% | 23.79 sec | delays
[.................................... ] 92% | 23.90 sec | delays
[.................................... ] 92% | 24.01 sec | delays
[..................................... ] 93% | 24.12 sec | delays
[..................................... ] 93% | 24.23 sec | delays
[..................................... ] 94% | 24.34 sec | delays
[..................................... ] 95% | 24.45 sec | delays
[...................................... ] 95% | 24.57 sec | delays
[...................................... ] 96% | 24.69 sec | delays
[...................................... ] 96% | 24.79 sec | delays
[...................................... ] 97% | 24.90 sec | delays
[....................................... ] 98% | 25.02 sec | delays
[....................................... ] 98% | 25.12 sec | delays
[....................................... ] 99% | 25.23 sec | delays
[....................................... ] 99% | 25.34 sec | delays
[........................................] 100% | 25.49 sec | delays
[ ] 0% | 0.00 sec | delays
[ ] 1% | 0.11 sec | delays
[ ] 1% | 0.22 sec | delays
[ ] 2% | 0.32 sec | delays
[ ] 2% | 0.43 sec | delays
[. ] 3% | 0.54 sec | delays
[. ] 4% | 0.67 sec | delays
[. ] 4% | 0.79 sec | delays
[. ] 5% | 0.90 sec | delays
[.. ] 5% | 1.01 sec | delays
[.. ] 6% | 1.13 sec | delays
[.. ] 7% | 1.23 sec | delays
[.. ] 7% | 1.34 sec | delays
[... ] 8% | 1.46 sec | delays
[... ] 8% | 1.57 sec | delays
[... ] 9% | 1.70 sec | delays
[... ] 10% | 1.85 sec | delays
[.... ] 10% | 2.00 sec | delays
[.... ] 11% | 2.11 sec | delays
[.... ] 11% | 2.25 sec | delays
[.... ] 12% | 2.41 sec | delays
[..... ] 13% | 2.56 sec | delays
[..... ] 13% | 2.69 sec | delays
[..... ] 14% | 2.85 sec | delays
[..... ] 14% | 2.97 sec | delays
[..... ] 15% | 3.11 sec | delays
[...... ] 16% | 3.24 sec | delays
[...... ] 16% | 3.37 sec | delays
[...... ] 17% | 3.48 sec | delays
[...... ] 17% | 3.65 sec | delays
[....... ] 18% | 3.76 sec | delays
[....... ] 19% | 3.88 sec | delays
[....... ] 19% | 4.01 sec | delays
[....... ] 20% | 4.12 sec | delays
[........ ] 20% | 4.23 sec | delays
[........ ] 21% | 4.34 sec | delays
[........ ] 22% | 4.45 sec | delays
[........ ] 22% | 4.58 sec | delays
[......... ] 23% | 4.69 sec | delays
[......... ] 23% | 4.83 sec | delays
[......... ] 24% | 4.94 sec | delays
[......... ] 25% | 5.05 sec | delays
[.......... ] 25% | 5.20 sec | delays
[.......... ] 26% | 5.36 sec | delays
[.......... ] 26% | 5.55 sec | delays
[.......... ] 27% | 5.78 sec | delays
[........... ] 28% | 6.03 sec | delays
[........... ] 28% | 6.24 sec | delays
[........... ] 29% | 6.51 sec | delays
[........... ] 29% | 6.89 sec | delays
[........... ] 30% | 7.36 sec | delays
[............ ] 31% | 7.85 sec | delays
[............ ] 31% | 8.33 sec | delays
[............ ] 32% | 8.76 sec | delays
[............ ] 32% | 9.22 sec | delays
[............. ] 33% | 9.50 sec | delays
[............. ] 34% | 9.80 sec | delays
[............. ] 34% | 10.15 sec | delays
[............. ] 35% | 10.58 sec | delays
[.............. ] 35% | 10.90 sec | delays
[.............. ] 36% | 11.21 sec | delays
[.............. ] 37% | 11.53 sec | delays
[.............. ] 37% | 11.86 sec | delays
[............... ] 38% | 12.17 sec | delays
[............... ] 38% | 12.55 sec | delays
[............... ] 39% | 12.86 sec | delays
[............... ] 40% | 13.18 sec | delays
[................ ] 40% | 13.44 sec | delays
[................ ] 41% | 13.74 sec | delays
[................ ] 41% | 14.04 sec | delays
[................ ] 42% | 14.30 sec | delays
[................. ] 43% | 14.66 sec | delays
[................. ] 43% | 14.97 sec | delays
[................. ] 44% | 15.39 sec | delays
[................. ] 44% | 15.80 sec | delays
[................. ] 45% | 16.20 sec | delays
[.................. ] 46% | 16.50 sec | delays
[.................. ] 46% | 16.86 sec | delays
[.................. ] 47% | 17.29 sec | delays
[.................. ] 47% | 17.79 sec | delays
[................... ] 48% | 18.21 sec | delays
[................... ] 49% | 18.75 sec | delays
[................... ] 49% | 19.12 sec | delays
[................... ] 50% | 19.46 sec | delays
[.................... ] 50% | 19.78 sec | delays
[.................... ] 51% | 20.18 sec | delays
[.................... ] 51% | 20.42 sec | delays
[.................... ] 52% | 20.80 sec | delays
[..................... ] 53% | 21.20 sec | delays
[..................... ] 53% | 21.63 sec | delays
[..................... ] 54% | 21.95 sec | delays
[..................... ] 54% | 22.35 sec | delays
[...................... ] 55% | 22.75 sec | delays
[...................... ] 56% | 23.22 sec | delays
[...................... ] 56% | 23.73 sec | delays
[...................... ] 57% | 24.13 sec | delays
[...................... ] 57% | 24.33 sec | delays
[....................... ] 58% | 24.50 sec | delays
[....................... ] 59% | 24.95 sec | delays
[....................... ] 59% | 25.49 sec | delays
[....................... ] 60% | 25.85 sec | delays
[........................ ] 60% | 26.10 sec | delays
[........................ ] 61% | 26.42 sec | delays
[........................ ] 62% | 26.79 sec | delays
[........................ ] 62% | 27.20 sec | delays
[......................... ] 63% | 27.44 sec | delays
[......................... ] 63% | 27.68 sec | delays
[......................... ] 64% | 27.99 sec | delays
[......................... ] 65% | 28.40 sec | delays
[.......................... ] 65% | 28.79 sec | delays
[.......................... ] 66% | 29.16 sec | delays
[.......................... ] 66% | 29.57 sec | delays
[.......................... ] 67% | 29.95 sec | delays
[........................... ] 68% | 30.39 sec | delays
[........................... ] 68% | 30.84 sec | delays
[........................... ] 69% | 31.30 sec | delays
[........................... ] 69% | 31.71 sec | delays
[............................ ] 70% | 32.03 sec | delays
[............................ ] 71% | 32.38 sec | delays
[............................ ] 71% | 32.72 sec | delays
[............................ ] 72% | 33.13 sec | delays
[............................ ] 72% | 33.54 sec | delays
[............................. ] 73% | 33.94 sec | delays
[............................. ] 74% | 34.38 sec | delays
[............................. ] 74% | 34.85 sec | delays
[............................. ] 75% | 35.13 sec | delays
[.............................. ] 75% | 35.42 sec | delays
[.............................. ] 76% | 35.58 sec | delays
[.............................. ] 77% | 35.71 sec | delays
[.............................. ] 77% | 35.93 sec | delays
[............................... ] 78% | 36.08 sec | delays
[............................... ] 78% | 36.22 sec | delays
[............................... ] 79% | 36.52 sec | delays
[............................... ] 80% | 36.69 sec | delays
[................................ ] 80% | 36.90 sec | delays
[................................ ] 81% | 37.11 sec | delays
[................................ ] 81% | 37.47 sec | delays
[................................ ] 82% | 37.74 sec | delays
[................................. ] 83% | 38.04 sec | delays
[................................. ] 83% | 38.38 sec | delays
[................................. ] 84% | 38.86 sec | delays
[................................. ] 84% | 39.19 sec | delays
[.................................. ] 85% | 39.55 sec | delays
[.................................. ] 86% | 39.89 sec | delays
[.................................. ] 86% | 40.21 sec | delays
[.................................. ] 87% | 40.51 sec | delays
[.................................. ] 87% | 40.73 sec | delays
[................................... ] 88% | 40.98 sec | delays
[................................... ] 89% | 41.29 sec | delays
[................................... ] 89% | 41.53 sec | delays
[................................... ] 90% | 41.78 sec | delays
[.................................... ] 90% | 42.11 sec | delays
[.................................... ] 91% | 42.33 sec | delays
[.................................... ] 92% | 42.50 sec | delays
[.................................... ] 92% | 42.76 sec | delays
[..................................... ] 93% | 42.91 sec | delays
[..................................... ] 93% | 43.08 sec | delays
[..................................... ] 94% | 43.28 sec | delays
[..................................... ] 95% | 43.68 sec | delays
[...................................... ] 95% | 43.99 sec | delays
[...................................... ] 96% | 44.13 sec | delays
[...................................... ] 96% | 44.25 sec | delays
[...................................... ] 97% | 44.40 sec | delays
[....................................... ] 98% | 44.52 sec | delays
[....................................... ] 98% | 44.66 sec | delays
[....................................... ] 99% | 44.93 sec | delays
[....................................... ] 99% | 45.13 sec | delays
[........................................] 100% | 45.31 sec | delays
[ ] 0% | 0.00 sec | delays
[ ] 1% | 0.18 sec | delays
[ ] 1% | 0.36 sec | delays
[ ] 2% | 0.55 sec | delays
[ ] 2% | 0.76 sec | delays
[. ] 3% | 1.09 sec | delays
[. ] 4% | 1.31 sec | delays
[. ] 4% | 1.59 sec | delays
[. ] 5% | 1.83 sec | delays
[.. ] 5% | 2.07 sec | delays
[.. ] 6% | 2.32 sec | delays
[.. ] 7% | 2.53 sec | delays
[.. ] 7% | 2.74 sec | delays
[... ] 8% | 2.97 sec | delays
[... ] 8% | 3.14 sec | delays
[... ] 9% | 3.33 sec | delays
[... ] 10% | 3.50 sec | delays
[.... ] 10% | 3.79 sec | delays
[.... ] 11% | 3.97 sec | delays
[.... ] 11% | 4.20 sec | delays
[.... ] 12% | 4.43 sec | delays
[..... ] 13% | 4.58 sec | delays
[..... ] 13% | 4.87 sec | delays
[..... ] 14% | 5.22 sec | delays
[..... ] 14% | 5.44 sec | delays
[..... ] 15% | 5.63 sec | delays
[...... ] 16% | 5.80 sec | delays
[...... ] 16% | 6.12 sec | delays
[...... ] 17% | 6.31 sec | delays
[...... ] 17% | 6.55 sec | delays
[....... ] 18% | 6.88 sec | delays
[....... ] 19% | 7.18 sec | delays
[....... ] 19% | 7.32 sec | delays
[....... ] 20% | 7.61 sec | delays
[........ ] 20% | 7.87 sec | delays
[........ ] 21% | 8.09 sec | delays
[........ ] 22% | 8.25 sec | delays
[........ ] 22% | 8.44 sec | delays
[......... ] 23% | 8.57 sec | delays
[......... ] 23% | 8.77 sec | delays
[......... ] 24% | 8.93 sec | delays
[......... ] 25% | 9.07 sec | delays
[.......... ] 25% | 9.20 sec | delays
[.......... ] 26% | 9.36 sec | delays
[.......... ] 26% | 9.50 sec | delays
[.......... ] 27% | 9.68 sec | delays
[........... ] 28% | 9.88 sec | delays
[........... ] 28% | 10.16 sec | delays
[........... ] 29% | 10.31 sec | delays
[........... ] 29% | 10.51 sec | delays
[........... ] 30% | 10.64 sec | delays
[............ ] 31% | 10.79 sec | delays
[............ ] 31% | 10.96 sec | delays
[............ ] 32% | 11.14 sec | delays
[............ ] 32% | 11.30 sec | delays
[............. ] 33% | 11.58 sec | delays
[............. ] 34% | 11.71 sec | delays
[............. ] 34% | 11.90 sec | delays
[............. ] 35% | 12.09 sec | delays
[.............. ] 35% | 12.35 sec | delays
[.............. ] 36% | 12.61 sec | delays
[.............. ] 37% | 12.83 sec | delays
[.............. ] 37% | 13.03 sec | delays
[............... ] 38% | 13.21 sec | delays
[............... ] 38% | 13.37 sec | delays
[............... ] 39% | 13.54 sec | delays
[............... ] 40% | 13.67 sec | delays
[................ ] 40% | 13.83 sec | delays
[................ ] 41% | 13.95 sec | delays
[................ ] 41% | 14.08 sec | delays
[................ ] 42% | 14.24 sec | delays
[................. ] 43% | 14.46 sec | delays
[................. ] 43% | 14.99 sec | delays
[................. ] 44% | 15.20 sec | delays
[................. ] 44% | 15.40 sec | delays
[................. ] 45% | 15.55 sec | delays
[.................. ] 46% | 15.67 sec | delays
[.................. ] 46% | 15.81 sec | delays
[.................. ] 47% | 15.95 sec | delays
[.................. ] 47% | 16.09 sec | delays
[................... ] 48% | 16.22 sec | delays
[................... ] 49% | 16.35 sec | delays
[................... ] 49% | 16.51 sec | delays
[................... ] 50% | 16.69 sec | delays
[.................... ] 50% | 16.82 sec | delays
[.................... ] 51% | 16.93 sec | delays
[.................... ] 51% | 17.04 sec | delays
[.................... ] 52% | 17.14 sec | delays
[..................... ] 53% | 17.25 sec | delays
[..................... ] 53% | 17.36 sec | delays
[..................... ] 54% | 17.47 sec | delays
[..................... ] 54% | 17.58 sec | delays
[...................... ] 55% | 17.68 sec | delays
[...................... ] 56% | 17.79 sec | delays
[...................... ] 56% | 17.90 sec | delays
[...................... ] 57% | 18.01 sec | delays
[...................... ] 57% | 18.13 sec | delays
[....................... ] 58% | 18.25 sec | delays
[....................... ] 59% | 18.36 sec | delays
[....................... ] 59% | 18.47 sec | delays
[....................... ] 60% | 18.59 sec | delays
[........................ ] 60% | 18.71 sec | delays
[........................ ] 61% | 18.82 sec | delays
[........................ ] 62% | 18.97 sec | delays
[........................ ] 62% | 19.09 sec | delays
[......................... ] 63% | 19.26 sec | delays
[......................... ] 63% | 19.37 sec | delays
[......................... ] 64% | 19.50 sec | delays
[......................... ] 65% | 19.70 sec | delays
[.......................... ] 65% | 19.86 sec | delays
[.......................... ] 66% | 20.03 sec | delays
[.......................... ] 66% | 20.21 sec | delays
[.......................... ] 67% | 20.34 sec | delays
[........................... ] 68% | 20.52 sec | delays
[........................... ] 68% | 20.74 sec | delays
[........................... ] 69% | 21.00 sec | delays
[........................... ] 69% | 21.16 sec | delays
[............................ ] 70% | 21.31 sec | delays
[............................ ] 71% | 21.51 sec | delays
[............................ ] 71% | 21.82 sec | delays
[............................ ] 72% | 22.05 sec | delays
[............................ ] 72% | 22.25 sec | delays
[............................. ] 73% | 22.41 sec | delays
[............................. ] 74% | 22.64 sec | delays
[............................. ] 74% | 22.80 sec | delays
[............................. ] 75% | 23.13 sec | delays
[.............................. ] 75% | 23.34 sec | delays
[.............................. ] 76% | 23.58 sec | delays
[.............................. ] 77% | 23.78 sec | delays
[.............................. ] 77% | 24.08 sec | delays
[............................... ] 78% | 24.41 sec | delays
[............................... ] 78% | 24.61 sec | delays
[............................... ] 79% | 24.88 sec | delays
[............................... ] 80% | 25.04 sec | delays
[................................ ] 80% | 25.26 sec | delays
[................................ ] 81% | 25.45 sec | delays
[................................ ] 81% | 25.79 sec | delays
[................................ ] 82% | 25.93 sec | delays
[................................. ] 83% | 26.11 sec | delays
[................................. ] 83% | 26.29 sec | delays
[................................. ] 84% | 26.52 sec | delays
[................................. ] 84% | 26.73 sec | delays
[.................................. ] 85% | 27.02 sec | delays
[.................................. ] 86% | 27.17 sec | delays
[.................................. ] 86% | 27.32 sec | delays
[.................................. ] 87% | 27.55 sec | delays
[.................................. ] 87% | 27.74 sec | delays
[................................... ] 88% | 27.92 sec | delays
[................................... ] 89% | 28.24 sec | delays
[................................... ] 89% | 28.47 sec | delays
[................................... ] 90% | 28.76 sec | delays
[.................................... ] 90% | 28.97 sec | delays
[.................................... ] 91% | 29.16 sec | delays
[.................................... ] 92% | 29.32 sec | delays
[.................................... ] 92% | 29.51 sec | delays
[..................................... ] 93% | 29.78 sec | delays
[..................................... ] 93% | 30.17 sec | delays
[..................................... ] 94% | 30.33 sec | delays
[..................................... ] 95% | 30.48 sec | delays
[...................................... ] 95% | 30.76 sec | delays
[...................................... ] 96% | 30.87 sec | delays
[...................................... ] 96% | 31.01 sec | delays
[...................................... ] 97% | 31.19 sec | delays
[....................................... ] 98% | 31.39 sec | delays
[....................................... ] 98% | 31.64 sec | delays
[....................................... ] 99% | 31.83 sec | delays
[....................................... ] 99% | 32.02 sec | delays
[........................................] 100% | 32.17 sec | delays
[ ] 0% | 0.00 sec | delays
[ ] 1% | 0.15 sec | delays
[ ] 1% | 0.27 sec | delays
[ ] 2% | 0.41 sec | delays
[ ] 2% | 0.71 sec | delays
[. ] 3% | 1.05 sec | delays
[. ] 4% | 1.25 sec | delays
[. ] 4% | 1.40 sec | delays
[. ] 5% | 1.61 sec | delays
[.. ] 5% | 1.72 sec | delays
[.. ] 6% | 1.85 sec | delays
[.. ] 7% | 1.98 sec | delays
[.. ] 7% | 2.09 sec | delays
[... ] 8% | 2.21 sec | delays
[... ] 8% | 2.32 sec | delays
[... ] 9% | 2.42 sec | delays
[... ] 10% | 2.54 sec | delays
[.... ] 10% | 2.65 sec | delays
[.... ] 11% | 2.77 sec | delays
[.... ] 11% | 2.87 sec | delays
[.... ] 12% | 3.03 sec | delays
[..... ] 13% | 3.16 sec | delays
[..... ] 13% | 3.31 sec | delays
[..... ] 14% | 3.52 sec | delays
[..... ] 14% | 3.71 sec | delays
[..... ] 15% | 3.91 sec | delays
[...... ] 16% | 4.07 sec | delays
[...... ] 16% | 4.23 sec | delays
[...... ] 17% | 4.38 sec | delays
[...... ] 17% | 4.58 sec | delays
[....... ] 18% | 4.75 sec | delays
[....... ] 19% | 4.90 sec | delays
[....... ] 19% | 5.07 sec | delays
[....... ] 20% | 5.34 sec | delays
[........ ] 20% | 5.53 sec | delays
[........ ] 21% | 5.64 sec | delays
[........ ] 22% | 5.78 sec | delays
[........ ] 22% | 5.91 sec | delays
[......... ] 23% | 6.08 sec | delays
[......... ] 23% | 6.24 sec | delays
[......... ] 24% | 6.39 sec | delays
[......... ] 25% | 6.54 sec | delays
[.......... ] 25% | 6.70 sec | delays
[.......... ] 26% | 6.83 sec | delays
[.......... ] 26% | 6.97 sec | delays
[.......... ] 27% | 7.16 sec | delays
[........... ] 28% | 7.32 sec | delays
[........... ] 28% | 7.47 sec | delays
[........... ] 29% | 7.63 sec | delays
[........... ] 29% | 7.78 sec | delays
[........... ] 30% | 7.90 sec | delays
[............ ] 31% | 8.11 sec | delays
[............ ] 31% | 8.32 sec | delays
[............ ] 32% | 8.52 sec | delays
[............ ] 32% | 8.67 sec | delays
[............. ] 33% | 8.82 sec | delays
[............. ] 34% | 9.01 sec | delays
[............. ] 34% | 9.21 sec | delays
[............. ] 35% | 9.37 sec | delays
[.............. ] 35% | 9.51 sec | delays
[.............. ] 36% | 9.64 sec | delays
[.............. ] 37% | 9.79 sec | delays
[.............. ] 37% | 9.90 sec | delays
[............... ] 38% | 10.03 sec | delays
[............... ] 38% | 10.14 sec | delays
[............... ] 39% | 10.25 sec | delays
[............... ] 40% | 10.37 sec | delays
[................ ] 40% | 10.48 sec | delays
[................ ] 41% | 10.60 sec | delays
[................ ] 41% | 10.72 sec | delays
[................ ] 42% | 10.83 sec | delays
[................. ] 43% | 10.94 sec | delays
[................. ] 43% | 11.06 sec | delays
[................. ] 44% | 11.18 sec | delays
[................. ] 44% | 11.29 sec | delays
[................. ] 45% | 11.40 sec | delays
[.................. ] 46% | 11.57 sec | delays
[.................. ] 46% | 11.73 sec | delays
[.................. ] 47% | 11.88 sec | delays
[.................. ] 47% | 12.03 sec | delays
[................... ] 48% | 12.26 sec | delays
[................... ] 49% | 12.43 sec | delays
[................... ] 49% | 12.63 sec | delays
[................... ] 50% | 12.76 sec | delays
[.................... ] 50% | 12.90 sec | delays
[.................... ] 51% | 13.06 sec | delays
[.................... ] 51% | 13.18 sec | delays
[.................... ] 52% | 13.30 sec | delays
[..................... ] 53% | 13.45 sec | delays
[..................... ] 53% | 13.60 sec | delays
[..................... ] 54% | 13.82 sec | delays
[..................... ] 54% | 13.93 sec | delays
[...................... ] 55% | 14.05 sec | delays
[...................... ] 56% | 14.22 sec | delays
[...................... ] 56% | 14.45 sec | delays
[...................... ] 57% | 14.60 sec | delays
[...................... ] 57% | 14.74 sec | delays
[....................... ] 58% | 14.91 sec | delays
[....................... ] 59% | 15.06 sec | delays
[....................... ] 59% | 15.27 sec | delays
[....................... ] 60% | 15.44 sec | delays
[........................ ] 60% | 15.56 sec | delays
[........................ ] 61% | 15.68 sec | delays
[........................ ] 62% | 15.84 sec | delays
[........................ ] 62% | 15.98 sec | delays
[......................... ] 63% | 16.14 sec | delays
[......................... ] 63% | 16.36 sec | delays
[......................... ] 64% | 16.49 sec | delays
[......................... ] 65% | 16.61 sec | delays
[.......................... ] 65% | 16.72 sec | delays
[.......................... ] 66% | 16.84 sec | delays
[.......................... ] 66% | 17.00 sec | delays
[.......................... ] 67% | 17.18 sec | delays
[........................... ] 68% | 17.29 sec | delays
[........................... ] 68% | 17.41 sec | delays
[........................... ] 69% | 17.52 sec | delays
[........................... ] 69% | 17.64 sec | delays
[............................ ] 70% | 17.75 sec | delays
[............................ ] 71% | 17.87 sec | delays
[............................ ] 71% | 17.98 sec | delays
[............................ ] 72% | 18.10 sec | delays
[............................ ] 72% | 18.21 sec | delays
[............................. ] 73% | 18.36 sec | delays
[............................. ] 74% | 18.56 sec | delays
[............................. ] 74% | 18.71 sec | delays
[............................. ] 75% | 18.83 sec | delays
[.............................. ] 75% | 18.94 sec | delays
[.............................. ] 76% | 19.06 sec | delays
[.............................. ] 77% | 19.17 sec | delays
[.............................. ] 77% | 19.28 sec | delays
[............................... ] 78% | 19.44 sec | delays
[............................... ] 78% | 19.55 sec | delays
[............................... ] 79% | 19.67 sec | delays
[............................... ] 80% | 19.79 sec | delays
[................................ ] 80% | 19.91 sec | delays
[................................ ] 81% | 20.02 sec | delays
[................................ ] 81% | 20.14 sec | delays
[................................ ] 82% | 20.26 sec | delays
[................................. ] 83% | 20.37 sec | delays
[................................. ] 83% | 20.49 sec | delays
[................................. ] 84% | 20.61 sec | delays
[................................. ] 84% | 20.73 sec | delays
[.................................. ] 85% | 20.89 sec | delays
[.................................. ] 86% | 21.06 sec | delays
[.................................. ] 86% | 21.24 sec | delays
[.................................. ] 87% | 21.36 sec | delays
[.................................. ] 87% | 21.51 sec | delays
[................................... ] 88% | 21.62 sec | delays
[................................... ] 89% | 21.75 sec | delays
[................................... ] 89% | 21.89 sec | delays
[................................... ] 90% | 22.01 sec | delays
[.................................... ] 90% | 22.13 sec | delays
[.................................... ] 91% | 22.26 sec | delays
[.................................... ] 92% | 22.43 sec | delays
[.................................... ] 92% | 22.59 sec | delays
[..................................... ] 93% | 22.71 sec | delays
[..................................... ] 93% | 22.82 sec | delays
[..................................... ] 94% | 22.95 sec | delays
[..................................... ] 95% | 23.06 sec | delays
[...................................... ] 95% | 23.17 sec | delays
[...................................... ] 96% | 23.29 sec | delays
[...................................... ] 96% | 23.40 sec | delays
[...................................... ] 97% | 23.51 sec | delays
[....................................... ] 98% | 23.63 sec | delays
[....................................... ] 98% | 23.75 sec | delays
[....................................... ] 99% | 23.86 sec | delays
[....................................... ] 99% | 23.98 sec | delays
[........................................] 100% | 24.10 sec | delays
/home/tom/work/github/pactools/examples/plot_phase_delay.py:191: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from pactools.dar_model import DAR
from pactools.utils import peak_finder
from pactools.utils.viz import phase_string, SEABORN_PALETTES, set_style
from pactools.delay_estimator import DelayEstimator
plt.close('all')
set_style(font_scale=1.4)
blue, green, red, purple, yellow, cyan = SEABORN_PALETTES['deep']
fs = 500. # Hz
high_fq = 80.0 # Hz
low_fq = 3.0 # Hz
low_fq_mod_fq = 0.5 # Hz
plot_fq_range = [40., 120.] # Hz
bandwidth = 2.0 # Hz
high_fq_amp = 0.5
low_fq_mod_amp = 3.0
ratio = 1. / 6.
phi_0 = -2 * np.pi * ratio
delay = -1. / low_fq * ratio
offset = -1.
sharpness = 5.
noise_level = 0.1
n_points = 30000
t_plot = 1. # sec
def sigmoid(array, sharpness):
return 1. / (1. + np.exp(-sharpness * array))
def clean_peak_finder(sig):
"""Remove first peak if it is at t=0"""
peak_inds, _ = peak_finder(sig, thresh=None, extrema=1)
if peak_inds[0] == 0:
peak_inds = peak_inds[1:]
return peak_inds
def simulate_and_plot(phi_0, delay, ax, rng):
"""Simulate oscillations with frequency modulation"""
# create the slow oscillations
time = np.arange(n_points) / fs
phase = time * 2 * np.pi * low_fq + np.pi / 2
# add frequency modulation
phase += low_fq_mod_amp * np.sin(time * 2 * np.pi * low_fq_mod_fq)
theta = np.cos(phase)
# add the fast oscillations
gamma = np.cos(time * 2 * np.pi * high_fq)
modulation = sigmoid(offset + np.cos(phase - phi_0),
sharpness) * high_fq_amp
gamma *= modulation
# add a delay
delay_point = int(delay * fs)
gamma = np.roll(gamma, delay_point)
modulation = np.roll(modulation, delay_point)
# plot the beginning of the signal
sel = slice(int(t_plot * fs) + 1)
lines_theta = ax.plot(time[sel], theta[sel])
ax.plot(time[sel], gamma[sel])
lines_modulation = ax.plot(time[sel], modulation[sel])
# plot the horizontal line of phi_0
if delay == 0 and False:
ax.hlines(
np.cos(-phi_0), time[sel][0], time[sel][-1], color='k',
linestyle='--')
gamma_peak_inds = clean_peak_finder(modulation[sel])
theta_peak_inds = clean_peak_finder(theta[sel])
cosph_peak_inds = clean_peak_finder(np.cos(phase - phi_0)[sel])
# plot the vertical lines of the maximum amplitude
ax.vlines(time[sel][gamma_peak_inds], -1, 1, color='k', linestyle='--')
# fill vertical intervals between start_idx and stop_idx
start_idx = gamma_peak_inds
stop_idx = cosph_peak_inds
fill_zone = np.zeros_like(time[sel])
fill_zone[np.minimum(np.maximum(start_idx, 0), sel.stop - 1)] += 1
fill_zone[np.minimum(np.maximum(stop_idx, 0), sel.stop - 1)] += -1
ax.fill_between(time[sel], -1, 1, where=np.cumsum(fill_zone) != 0,
color=cyan, alpha=0.5)
# add annotations
if delay != 0:
for start, stop in zip(start_idx, stop_idx):
middle = 0.7 * time[sel][start] + 0.3 * time[sel][stop]
ax.annotate(r"$\tau_0$", (middle, -1), xycoords='data')
if phi_0 != 0:
# ax.annotate(r"$\cos(\phi_0)$", (0, np.cos(phi_0)), xycoords='data')
ticks = [-1, 0, np.cos(phi_0), 1]
ticklabels = ['-1', '0', r'$\cos(\phi_0)$', '1']
ax.set_yticks(ticks)
ax.set_yticklabels(ticklabels)
# fill the horizontal interval between cos(phi_0) and 1
ax.fill_between(time[sel], np.cos(phi_0), 1, color=cyan, alpha=0.5)
# plot the squares of the theta peaks
ax.plot(time[sel][theta_peak_inds], theta[sel][theta_peak_inds], 's',
color=lines_theta[0].get_color())
# plot the circles of maximum gamma amplitude
ax.plot(time[sel][gamma_peak_inds], modulation[sel][gamma_peak_inds], 'o',
color=lines_modulation[0].get_color())
ax.set_xlim([0, t_plot])
ax.set_xlabel('Time (s)')
ax.text(0.99, 0.22, r'$\phi_0 = %s$' % (phase_string(phi_0), ),
horizontalalignment='right', transform=ax.transAxes)
ax.text(0.99, 0.08, r'$\tau_0 = %.0f \;\mathrm{ms}$' % (delay * 1000, ),
horizontalalignment='right', transform=ax.transAxes)
return theta + gamma + noise_level * rng.randn(*gamma.shape)
def fit_dar_and_plot(sig, ax_logl, ax_phase, phi_0, random_state=None):
dar_model = DAR(ordar=10, ordriv=2)
est = DelayEstimator(fs, dar_model=dar_model, low_fq=low_fq,
low_fq_width=bandwidth,
random_state=random_state)
est.fit(sig)
est.plot(ax=ax_logl)
# plot the modulation of the best model
est.best_model_.plot(ax=ax_phase, mode='c', frange=plot_fq_range)
ax_phase.set_title('')
ticks = [-np.pi, phi_0, np.pi]
ax_phase.set_xticks(ticks)
ax_phase.set_xticklabels([r'$%s$' % phase_string(d) for d in ticks])
ax_phase.grid('on')
ax_phase.grid(color=(0.5, 0.5, 0.5))
# initialize the plots
rng = np.random.RandomState(3)
fig, axs = plt.subplots(4, 3, figsize=(18, 12),
gridspec_kw={'width_ratios': [3, 1, 1]})
# loop over the four conditions
for phi_0_, delay_, axs_ in zip([0, phi_0, 0, phi_0], [0, 0, delay, delay],
axs):
sig = simulate_and_plot(phi_0=phi_0_, delay=delay_, ax=axs_[0], rng=rng)
fit_dar_and_plot(sig, axs_[1], axs_[2], phi_0=phi_0_, random_state=rng)
plt.tight_layout()
plt.show()
Total running time of the script: ( 2 minutes 9.210 seconds)