Ejemplo n.º 1
0
def test_star1():
    """Test STAR 1."""
    # N channels,  1 sinusoidal target, N-3 noise sources, temporally local
    # artifacts on each channel.
    n_samples = 1000
    n_chans = 10
    f = 2

    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

    # Test SNR=1
    x, x0 = sim_data(n_samples, n_chans, f, SNR=np.sqrt(1))
    y, w, _ = star(x, 2, verbose='debug')
    assert_allclose(demean(y), x0)  # check that denoised signal ~ x0

    # Test more unfavourable SNR
    x, x0 = sim_data(n_samples, n_chans, f, SNR=np.sqrt(1e-7))
    y, w, _ = star(x, 2)
    assert_allclose(demean(y), x0)  # check that denoised signal ~ x0

    # Test an all-zero channel is properly handled
    x = x - x[:, 0][:, np.newaxis]
    y, w, _ = star(x, 2)
    assert_allclose(demean(y)[:, 0], x[:, 0])
Ejemplo n.º 2
0
      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
# -----------------------------------------------------------------------------
y, w, _ = star.star(x, 2)

###############################################################################
# Plot results
# -----------------------------------------------------------------------------
f, (ax1, ax2, ax3) = plt.subplots(3, 1)
ax1.plot(x, lw=.5)
ax1.set_title('Signal + Artifacts (SNR = {})'.format(SNR))
ax2.plot(y, lw=.5)
ax2.set_title('Denoised')
ax3.plot(demean(y) - x0, lw=.5)
ax3.set_title('Residual')
f.set_tight_layout(True)
plt.show()