Beispiel #1
0
def test_compute_fluctuations_dfa():

    # Note: these tests need a high sampling rate, so we use local simulations

    # Test white noise: expected DFA of 0.5
    fs = 1000
    white = sim_powerlaw(N_SECONDS, fs, exponent=0)
    t_scales, flucs, exp = compute_fluctuations(white, fs, method='dfa')
    assert np.isclose(exp, 0.5, atol=0.1)

    # Test brown noise: expected DFA of 1.5
    brown = sim_powerlaw(N_SECONDS, fs, exponent=-2)
    t_scales, flucs, exp = compute_fluctuations(brown, fs, method='dfa')
    assert np.isclose(exp, 1.5, atol=0.1)
def test_sim_peak_oscillation():

    sig_ap = sim_powerlaw(N_SECONDS, FS)
    sig = sim_peak_oscillation(sig_ap, FS, FREQ1, bw=5, height=10)

    check_sim_output(sig)

    # Ensure the peak is at or close (+/- 5hz) to FREQ1
    _, powers_ap = compute_spectrum(sig_ap, FS)
    _, powers = compute_spectrum(sig, FS)

    assert abs(np.argmax(powers - powers_ap) - FREQ1) < 5
Beispiel #3
0
#
# Neural signals display 1/f-like activity, whereby power decreases linearly across
# increasing frequencies, when plotted in log-log.
#
# Let's start with a powerlaw signal, specifically a brown noise process, or a signal
# for which the power spectrum is distributed as 1/f^2.
#

###################################################################################################

# Setting for the simulation
exponent = -2

# Simulate powerlaw activity, specifically brown noise
times = create_times(n_seconds, fs)
br_noise = sim.sim_powerlaw(n_seconds, fs, exponent)

###################################################################################################

# Plot the simulated data, in the time domain
plot_time_series(times, br_noise)

###################################################################################################

# Plot the simulated data, in the frequency domain
freqs, psd = spectral.compute_spectrum(br_noise, fs)
plot_power_spectra(freqs, psd)

###################################################################################################
#
# Simulate Filtered 1/f Activity
###################################################################################################

# Set time and sampling rate
n_seconds_burst = 1
n_seconds_noise = 2
fs = 1000

# Create a times vector
times = create_times(n_seconds_burst + n_seconds_noise, fs)

# Simulate a signal component with an oscillation
components = {'sim_powerlaw': {'exponent': 0}, 'sim_oscillation': {'freq': 10}}
s1 = sim_combined(n_seconds_burst, fs, components, [0.1, 1])

# Simulate a signal component with just noise
s2 = sim_powerlaw(n_seconds_noise, fs, 0, variance=0.1)

# Join signals together to approximate a 'burst'
sig = np.append(s1, s2)

###################################################################################################

# Plot example signal
plot_time_series(times, sig)

###################################################################################################
# Compute lagged coherence for an alpha oscillation
# -------------------------------------------------
#
# We can compute lagged coherence with the :func:`~.compute_lagged_coherence` function.
#
Beispiel #5
0
#

###################################################################################################
# Autocorrelation of Aperiodic Signals
# ------------------------------------
#
# Next, lets consider the autocorrelation of aperiodic signals.
#
# Here we will use white noise, as an example of a signal without autocorrelation, and
# pink noise, which does, by definition, have temporal auto-correlations.
#

###################################################################################################

# Simulate a white noise signal
sig_wn = sim_powerlaw(n_seconds, fs, exponent=0)

# Simulate a pink noise signal
sig_pn = sim_powerlaw(n_seconds, fs, exponent=-1)

###################################################################################################

# Compute autocorrelation on the aperiodic time series
timepoints_wn, autocorrs_wn = compute_autocorr(sig_wn)
timepoints_pn, autocorrs_pn = compute_autocorr(sig_pn)

###################################################################################################

# Plot the autocorrelations of the aperiodic signals
_, ax = plt.subplots(figsize=(5, 4))
ax.plot(timepoints_wn, autocorrs_wn, label='White Noise')
Beispiel #6
0
                                     osc_freq_rand,
                                     cycle='asine',
                                     rdsym=asine_rdsym)

        simulation_features['asine_rdsym'][aa] = asine_rdsym

    # roll signal to have different phase every simulation
    signal = np.roll(signal, random.randint(0, signal_length))

    # get random exponential
    exp_rand = random.uniform(exp[0], exp[1])

    # simulate 50x brown noise on top to get reliable slope
    n_brown_noise = 50
    for br in range(n_brown_noise):
        br_noise = sim.sim_powerlaw(n_seconds, fs, exp_rand)
        signal = signal + br_noise

    try:

        # run FOOOF
        # compute frequency spectrum
        freq_mean, psd_mean = spectral.compute_spectrum(signal,
                                                        fs,
                                                        method='welch',
                                                        avg_type='mean',
                                                        nperseg=fs * 2)

        # Initialize FOOOF model
        fm = FOOOF(peak_width_limits=bw_lims,
                   background_mode='fixed',
Beispiel #7
0
###################################################################################################

# Simulate a signal component with an oscillation
components = {
    'sim_oscillation': {
        'freq': freq
    },
    'sim_powerlaw': {
        'exponent': exp,
        'f_range': (1, None)
    }
}
s1 = sim_combined(t_osc, fs, components, [1, 0.5])

# Simulate a signal component with only aperiodic activity
s2 = sim_powerlaw(t_ap, fs, exp, variance=0.5)

# Join signals together to approximate a 'burst'
sig = np.append(s1, s2)

###################################################################################################

# Plot example signal
plot_time_series(times, sig)

###################################################################################################
# Compute lagged coherence on simulated data
# ------------------------------------------
#
# We can compute lagged coherence with the :func:`~.compute_lagged_coherence` function.
#
#

###################################################################################################

# Simulation settings
s_rate = 1000
n_seconds = 4
times = create_times(n_seconds, s_rate)

# Set random seed, for consistency generating simulated data
set_random_seed(21)

###################################################################################################

# Simulate a signal of aperiodic activity: pink noise
sig = sim_powerlaw(n_seconds, s_rate, exponent=-1)

###################################################################################################

# Plot our simulated time series
plot_time_series(times, sig)

###################################################################################################
# Filtering Aperiodic Signals
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Now that we have a simulated signal, let's filter it into each of our frequency bands.
#
# To do so, we will loop across our band definitions, and plot the filtered version
# of the signal.
#
Beispiel #9
0
###################################################################################################
# Pink Noise
# ^^^^^^^^^^
#
# Other 'colors' of noise refer to different patterns of power distributions
# in the power spectrum.
#
# For example, pink noise is a signal where power systematically decreases across
# frequencies in the power spectrum.
#

###################################################################################################

# Generate a pink noise signal
pink_sig = sim.sim_powerlaw(n_seconds, s_rate, exponent=-1)

###################################################################################################

# Plot the pink noise time series
plot_time_series(times, pink_sig)

###################################################################################################

# Compute the power spectrum of the pink noise signal
freqs, powers = compute_spectrum_welch(pink_sig, s_rate)

###################################################################################################

# Visualize the power spectrum of the pink noise signal
plot_power_spectra(freqs, powers)
Beispiel #10
0
def tsig_brown():

    yield sim_powerlaw(N_SECONDS_LONG, FS_HIGH, exponent=-2)
Beispiel #11
0
def tsig_white():

    yield sim_powerlaw(N_SECONDS_LONG, FS_HIGH, exponent=0)