Ejemplo n.º 1
0
def limit_freqs_hht(imfs, freqs, fs, energy_thresh=2.):
    """Limit specparam frequency ranges using HH transform.

    Parameters
    ----------
    imfs : 1d array
        Sum of masked intrinsic mode functions.
    freqs : 1d array
        Frequncies to define the HHT.
    fs : float
        Sampling rate, in Hz.
    energy_thresh : float, optional, default: 1.
        Normalized energy threshold to define oscillatory frequencies.

    Returns
    -------
    freqs_min : 1d array
        Lower frequency bounds.
    freqs_max : 1d array
        Upper frequency bounds.
    """
    # Use HHT to identify frequency ranges to fit
    _, IF, IA = emd.spectra.frequency_transform(imfs.sum(axis=0).T, fs, 'nht')

    # Amplitude weighted HHT
    spec_weighted = emd.spectra.hilberthuang_1d(IF, IA, freqs).T[0]

    spec_weighted = normalize_variance(spec_weighted)

    # Split spectrum into separate superthresh segments
    idxs = np.where(spec_weighted > energy_thresh)[0]

    if len(idxs) == 0:
        return None, None

    # Split where non-continuous
    idxs = np.split(idxs, np.where(np.diff(idxs) != 1)[0]+1)

    # Require at least 3 freqs
    idxs = [idx for idx in idxs if len(idx) >= 3]

    # Get frequency ranges of segments
    freqs_min = np.zeros(len(idxs))
    freqs_max = np.zeros(len(idxs))

    for idx, seg in enumerate(idxs):
        freqs_min[idx] = freqs[np.min(seg)]
        freqs_max[idx] = freqs[np.max(seg)]

    return freqs_min, freqs_max
Ejemplo n.º 2
0
    def decorated(*args, **kwargs):

        # Grab variance & mean as possible kwargs, with default values if not
        variance = kwargs.pop('variance', 1.)
        mean = kwargs.pop('mean', 0.)

        # Call sim function, and unpack to get sig variable, if there are multiple returns
        out = func(*args, **kwargs)
        sig = out[0] if isinstance(out, tuple) else out

        # Apply variance & mean transformations
        if variance is not None:
            sig = normalize_variance(sig, variance=variance)
        if mean is not None:
            sig = demean(sig, mean=mean)

        # Return sig & other outputs, if there were any, or just sig otherwise
        return (sig, out[1:]) if isinstance(out, tuple) else sig
Ejemplo n.º 3
0
n_points = int(n_seconds * fs)

f_beta = (14, 28)
high_fq = 80.0  # Hz; carrier frequency
low_fq = 24.0  # Hz; driver frequency
low_fq_width = 2.0  # Hz

noise_level = 0.25

# Simulate beta-gamma pac
sig_pac = simulate_pac(n_points=n_points, fs=fs, high_fq=high_fq, low_fq=low_fq,
                       low_fq_width=low_fq_width, noise_level=noise_level)

# Simulate 10 Hz spiking which couples to about 60 Hz
spikes = sim_oscillation(n_seconds, fs, 10, cycle='gaussian', std=0.005)
noise = normalize_variance(pink_noise(n_points, slope=1.), variance=.5)
sig_spurious_pac = spikes + noise

# Simulate a sine wave that is the driver frequency
sig_low_fq = sim_oscillation(n_seconds, fs, low_fq)

# Add the sine wave to pink noise to make a control, no pac signal
sig_no_pac = sig_low_fq + noise

####################################################################################################
# Check comodulogram for PAC
# ~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Now, we will use the tools from pactools to compute the comodulogram which
# is a visualization of phase amplitude coupling. The stronger the driver
# phase couples with a particular frequency, the brighter the color value.