Exemple #1
0
    def sim_data(n_samples, n_chans, f, SNR):
        target = np.sin(np.arange(n_samples) / n_samples * 2 * np.pi * f)
        target = target[:, np.newaxis]
        noise = np.random.randn(n_samples, n_chans - 3)

        x0 = (
            normcol(np.dot(noise, np.random.randn(noise.shape[1], n_chans))) +
            SNR * target * np.random.randn(1, n_chans))
        x0 = demean(x0)
        artifact = np.zeros(x0.shape)
        for k in np.arange(n_chans):
            artifact[k * 100 + np.arange(20), k] = 1
        x = x0 + 20 * artifact
        return x, x0
# Create simulated data
# -----------------------------------------------------------------------------
# Simulated data consist of N channels, 1 sinusoidal target, N-3 noise sources,
# with temporally local artifacts on each channel.

# Create simulated data
nchans = 10
n_samples = 1000
f = 2
target = np.sin(np.arange(n_samples) / n_samples * 2 * np.pi * f)
target = target[:, np.newaxis]
noise = np.random.randn(n_samples, nchans - 3)

# Create artifact signal
SNR = np.sqrt(1)
x0 = (normcol(np.dot(noise, np.random.randn(noise.shape[1], nchans))) +
      SNR * target * np.random.randn(1, nchans))
x0 = demean(x0)
artifact = np.zeros(x0.shape)
for k in np.arange(nchans):
    artifact[k * 100 + np.arange(20), k] = 1
x = x0 + 10 * artifact

# This is to compare with matlab numerically
# from scipy.io import loadmat
# mat = loadmat('/Users/nicolas/Toolboxes/NoiseTools/TEST/X.mat')
# x = mat['x']
# x0 = mat['x0']

###############################################################################
# Apply STAR
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

from meegkit import star, dss  # noqa:E402
from meegkit.utils import demean, normcol, tscov  # noqa:E402

# Create simulated data
np.random.seed(9)
n_chans, n_samples = 10, 1000
f = 2
target = np.sin(np.arange(n_samples) / n_samples * 2 * np.pi * f)
target = target[:, np.newaxis]
noise = np.random.randn(n_samples, n_chans - 3)

# Create artifact signal
SNR = np.sqrt(1)
x0 = (normcol(np.dot(noise, np.random.randn(noise.shape[1], n_chans))) +
      SNR * target * np.random.randn(1, n_chans))
x0 = demean(x0)
artifact = np.zeros(x0.shape)
for k in np.arange(n_chans):
    artifact[k * 100 + np.arange(20), k] = 1
x = x0 + 10 * artifact


# Basic function to fit a sinusoidal trend for DSS
def _sine_fit(x):
    guess_mean = np.mean(x)
    guess_std = np.std(x)
    guess_phase = 0
    t = np.linspace(0, 4 * np.pi, x.shape[0])