Skip to content

Instantly share code, notes, and snippets.

@mbillingr
Last active April 4, 2016 15:36
Show Gist options
  • Save mbillingr/1c03de8ed0bb5e47e39eade935a2c587 to your computer and use it in GitHub Desktop.
Save mbillingr/1c03de8ed0bb5e47e39eade935a2c587 to your computer and use it in GitHub Desktop.
Toy example that shows what happens to the DTF when a sinusoid is added to white noise signals
import numpy as np
import matplotlib.pyplot as plt
import seaborn
from scot.var import VAR
from scot.connectivity import connectivity
fs = 100
n = 10001
disturbance = np.sin(np.arange(n) * 2 * np.pi * 20 / fs)
for i in range(3):
np.random.seed(42)
if i == 0:
# three white noise signals
x = np.random.randn(3, n)
elif i == 1:
# a sinusoid added to the three signals
x = np.random.randn(3, n)
x += disturbance
elif i == 2:
# in addition to adding the sinusoid, now we also include a fourth
# signal that contains only the sinousoid (and a little noise)
x = np.random.randn(4, n)
x[3, :] *= 0.03
x += disturbance
plt.figure()
var = VAR(15).fit(x)
S = connectivity('S', var.coef, var.rescov)
C = connectivity('DTF', var.coef, var.rescov)
f = np.abs(np.linspace(0, fs / 2, S.shape[-1]))
for i in range(x.shape[0]):
for j in range(x.shape[0]):
plt.subplot(x.shape[0], x.shape[0], i * x.shape[0] + j + 1)
if i == j:
plt.fill_between(f, 0, S[i, j, :], color=seaborn.color_palette()[1])
else:
plt.fill_between(f, 0, C[i, j, :], color = seaborn.color_palette()[0])
plt.ylim(0, 1)
plt.show()
@mbillingr
Copy link
Author

Results:
image
image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment