Exemplo n.º 1
0
def test_plot_instantaneous_measure(tsig_comb):

    times = create_times(N_SECONDS, FS)

    plot_instantaneous_measure(
        times,
        amp_by_time(tsig_comb, FS, F_RANGE),
        'amplitude',
        save_fig=True,
        file_path=TEST_PLOTS_PATH,
        file_name='test_plot_instantaneous_measure_amplitude.png')

    plot_instantaneous_measure(
        times,
        phase_by_time(tsig_comb, FS, F_RANGE),
        'phase',
        save_fig=True,
        file_path=TEST_PLOTS_PATH,
        file_name='test_plot_instantaneous_measure_phase.png')

    plot_instantaneous_measure(
        times,
        freq_by_time(tsig_comb, FS, F_RANGE),
        'frequency',
        save_fig=True,
        file_path=TEST_PLOTS_PATH,
        file_name='test_plot_instantaneous_measure_frequency.png')

    # Check the error for bad measure
    with raises(ValueError):
        plot_instantaneous_measure(times, tsig_comb, 'BAD')
#
# It is measured as the temporal derivative of the instantaneous phase.
#
# Instantaneous frequency measures can exhibit abrupt shifts. Sometimes, a transform,
# such as applying a median filter, is used to make it smoother.
#
# For example of this, see Samaha & Postle, 2015.
#
# Instantaneous frequency can be analyzed with the
# :func:`~neurodsp.timefrequency.hilbert.freq_by_time` function.
#

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

# Compute instantaneous frequency from a signal
i_f = freq_by_time(sig, fs, f_range)

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

# Plot example signal
_, axs = plt.subplots(3, 1, figsize=(15, 9))
plot_time_series(times,
                 sig,
                 'Raw Voltage',
                 xlim=[4, 5],
                 xlabel=None,
                 ax=axs[0])
plot_time_series(times,
                 sig_filt_true,
                 labels='Beta Filtered Voltage',
                 colors='b',
Exemplo n.º 3
0
def instantaneous_frequency(time_series, Fs, filter_range):
    inst_freq = freq_by_time(time_series, Fs, filter_range)
    return np.nanmean(inst_freq)
Exemplo n.º 4
0
def get_inst_data(data, fs, f_range):
    i_phase = tf.phase_by_time(data, fs, f_range)
    i_amp = tf.amp_by_time(data, fs, f_range)
    i_freq = tf.freq_by_time(data, fs, f_range)
    return i_phase, i_amp, i_freq
Exemplo n.º 5
0
def get_alpha_instantaneous_statistics(eeg_epoch_full_df):
    # alpha_range = (7, 12)
    alpha_amps = {}
    alpha_pha = {}
    alpha_if = {}
    power_range = [(4, 7), (7, 12), (12, 30)]
    for power_i in range(len(power_range)):
        alpha_range = power_range[power_i]
        for i in range(0, len(eeg_epoch_full_df)):

            for ch in all_chans:
                sig = eeg_epoch_full_df[ch][i][:]
                key = ch + "_" + str(alpha_range)

                amp = amp_by_time(
                    sig, eeg_fs,
                    alpha_range)  # Amplitude by time (instantaneous amplitude)
                if key + "_amp_med" not in alpha_amps:
                    alpha_amps[key + "_amp_med"] = list()
                    alpha_amps[key + "_amp_avg"] = list()
                    alpha_amps[key + "_amp_std"] = list()
                    alpha_amps[key + "_amp_gradmax"] = list()
                    alpha_amps[key + "_amp_gradmin"] = list()
                alpha_amps[key + "_amp_med"].append(np.nanmedian(amp))
                alpha_amps[key + "_amp_avg"].append(np.nanmean(amp))
                alpha_amps[key + "_amp_std"].append(np.nanstd(amp))
                alpha_amps[key + "_amp_gradmax"].append(
                    np.nanmax(np.gradient(amp)))
                alpha_amps[key + "_amp_gradmin"].append(
                    np.nanmin(np.gradient(amp)))

                pha = phase_by_time(
                    sig, eeg_fs,
                    alpha_range)  # Phase by time (instantaneous phase)
                if key + "_pha_med" not in alpha_pha:
                    alpha_pha[key + "_pha_med"] = list()
                    alpha_pha[key + "_pha_avg"] = list()
                    alpha_pha[key + "_pha_std"] = list()
                    alpha_pha[key + "_pha_gradmax"] = list()
                    alpha_pha[key + "_pha_gradmin"] = list()
                alpha_pha[key + "_pha_med"].append(np.nanmedian(pha))
                alpha_pha[key + "_pha_avg"].append(np.nanmean(pha))
                alpha_pha[key + "_pha_std"].append(np.nanstd(pha))
                alpha_pha[key + "_pha_gradmax"].append(
                    np.nanmax(np.gradient(pha)))
                alpha_pha[key + "_pha_gradmin"].append(
                    np.nanmin(np.gradient(pha)))

                i_f = freq_by_time(
                    sig, eeg_fs,
                    alpha_range)  # Frequency by time (instantaneous frequency)
                if key + "_freq_med" not in alpha_if:
                    alpha_if[key + "_freq_med"] = list()
                    alpha_if[key + "_freq_avg"] = list()
                    alpha_if[key + "_freq_std"] = list()
                    alpha_if[key + "_freq_gradmax"] = list()
                    alpha_if[key + "_freq_gradmin"] = list()
                alpha_if[key + "_freq_med"].append(np.nanmedian(i_f))
                alpha_if[key + "_freq_avg"].append(np.nanmean(i_f))
                alpha_if[key + "_freq_std"].append(np.nanstd(i_f))
                alpha_if[key + "_freq_gradmax"].append(
                    np.nanmax(np.gradient(i_f)))
                alpha_if[key + "_freq_gradmin"].append(
                    np.nanmin(np.gradient(i_f)))

    alpha_med_df = pd.DataFrame(alpha_amps)
    alpha_pha_df = pd.DataFrame(alpha_pha)
    alpha_if_df = pd.DataFrame(alpha_if)
    insta_stat_df = pd.concat([alpha_med_df, alpha_pha_df, alpha_if_df],
                              axis=1)
    print(list(insta_stat_df.columns))
    return insta_stat_df