################################################################################################### # # Load neural signal # ------------------ # ################################################################################################### # Load example data sig = np.load('../data/sample_data_1.npy') fs = 1000 times = create_times(len(sig)/fs, fs) f_range = (13, 30) # Plot example signal plot_time_series(times, sig) ################################################################################################### # # Apply sliding window matching to neural signal # ---------------------------------------------- # # Because we define the window length to be about 1 cycle, this should roughly extract # the waveform shape of the neural oscillation. Notice that the beta cycles have sharper # troughs than peaks, and the average window is a beta cycle with a sharp trough. # # However, notice that these results change dramatically by changing the random seed. # Using more data and increasing the number of iterations helps the robustness of the algorithm. # ###################################################################################################
# ################################################################################################### # Simulation settings n_seconds = 1 osc_freq = 6.6 # Simulate a sinusoidal oscillation osc_sine = sim.sim_oscillation(n_seconds, fs, osc_freq, cycle='sine') # Create a times vector for our simulation times = create_times(n_seconds, fs) # Plot the simulated data, in the time domain plot_time_series(times, osc_sine) ################################################################################################### # # Cycle Kernels # ------------- # # To simulate oscillations, we can use a sinuisoidal kernel, as above, or any of a selection # of other cycle kernals, that represent different shapes and properties that may be useful # to simulate different aspects of periodic neural activity. # # Cycle kernel options include: # # - sine: a sine wave cycle # - asine: an asymmetric sine wave # - sawtooth: a sawtooth wave
# Let's start with a power law signal, specifically a brown noise process, or a signal # for which the power spectrum is distributed as 1/f^2. # ################################################################################################### # Set the exponent for brown noise, which is -2 exponent = -2 # Simulate powerlaw activity br_noise = sim_powerlaw(n_seconds, fs, exponent) ################################################################################################### # Plot the simulated data, in the time domain plot_time_series(times, br_noise, title='Brown Noise') ################################################################################################### # Plot the simulated data, in the frequency domain freqs, psd = compute_spectrum(br_noise, fs) plot_power_spectra(freqs, psd) ################################################################################################### # Simulate Filtered 1/f Activity # ------------------------------ # # The power law simulation function is also integrated with a filter. This can be useful # for filtering out some low frequencies, as is often done with neural signals, # to remove the very slow drifts that we see in the pure 1/f simulations. #
# power decreases roughly linearly with frequency. # ################################################################################################### # Simulate brown noise n_seconds = 10 fs = 1000 exponent = -2 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 brown noise # ----------------------------- # # However, brown noise has a lot of power in very slow frequnecies, # whereas these slow frequencies are often not present or filtered out # in neural signals. Therefore, we may desire our brown noise to be
fs = 1000 n_seconds = 5 # Define simulation components components = {'sim_synaptic_current' : {'n_neurons':1000, 'firing_rate':2, 't_ker':1.0, 'tau_r':0.002, 'tau_d':0.02}, 'sim_bursty_oscillation' : {'freq' : 10, 'enter_burst' : .2, 'leave_burst' : .2}} # Simulate a signal with a bursty oscillation with an aperiodic component & a time vector sig = sim_combined(n_seconds, fs, components) times = create_times(n_seconds, fs) ################################################################################################### # Plot the simulated data plot_time_series(times, sig, 'Simulated EEG') ################################################################################################### # # In the simulated signal above, we can see some bursty 10 Hz oscillations. # ################################################################################################### # Dual Amplitude Threshold Algorithm # ---------------------------------- # # First, let's use the dual-amplitude threshold algorithm for burst detection, which # we can use with the :func:`~.detect_bursts_dual_threshold` function. # # This algorithm first computes the amplitude at each point in time for a given # frequency range. This amplitude is then normalized by the average (default: median)
# 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_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 = compute_spectrum(br_noise, fs) plot_power_spectra(freqs, psd) ################################################################################################### # Simulate Filtered 1/f Activity # ------------------------------ # # The power law simulation function is also integrated with a filter. This can be useful # for filtering out some low frequencies, as is often done with neural signals, # to remove the very slow drifts that we see in the pure 1/f simulations. #
# Generate an oscillation with noise fs = 1000 times = create_times(4, 1000) sig = np.random.randn(len(times)) + 5*np.sin(times*2*np.pi*6) ################################################################################################### # Filter the data, across a frequency band of interest f_range = (4, 8) sig_filt = filt.filter_signal(sig, fs, 'bandpass', f_range) ################################################################################################### # Plot filtered signal plot_time_series(times, [sig, sig_filt], ['Raw', 'Filtered']) ################################################################################################### # # Notice that the edges of the filtered signal are clipped (no red). # # Edge artifact removal is done by default in :func:`filter_signal`, because # the signal samples at the edges only experienced part of the filter. # # To bypass this feature, set `remove_edges=False`, but at your own risk! # ################################################################################################### # # 2. Highpass, lowpass, and bandstop filters # ------------------------------------------
# Relevant publication: Mizuseki et al, 2012, Nature Neuro # ################################################################################################### # Load example data signal sig = load_ndsp_data('sample_data_2.npy', folder='data') # Set sampling rate, and create a times vector for plotting fs = 1000 times = create_times(len(sig) / fs, fs) ################################################################################################### # Plot the loaded signal plot_time_series(times, sig, xlim=[0, 3]) ################################################################################################### # # Plotting the data, we observe a strong theta oscillation (~6-8 Hz) # ################################################################################################### # Computing the power spectral density (PSD) # ------------------------------------------ # # A PSD is a frequency domain representation of a time series. # # Using the Fourier transform, the signal is split into orthogonal components # of different frequencies, and amount of power in each frequency is estimated. #
# Load a neural signal, as well as a filtered version of the same signal sig = np.load('../data/sample_data_1.npy') sig_filt_true = np.load('../data/sample_data_1_filt.npy') # Set the sampling rate and create a times vector for the signal fs = 1000 times = create_times(len(sig) / fs, fs) # Set the frequency range to be used f_range = (13, 30) ################################################################################################### # Plot signal plot_time_series(times, sig) ################################################################################################### # # Instantaneous Phase # ------------------- # # Instantaneous phase is a measure of the phase of a signal, over time. # ################################################################################################### # Compute instaneous phase from a signal pha = phase_by_time(sig, fs, f_range) ###################################################################################################