Complex coherency#

This function may be called for data in the time domain or the frequency domain.

sfc.td.run_cc(data_1, data_2, nperseg, pad_type, fs, nfft, window)#

Calculate complex coherency from time domain data.

Parameters:
  • data_1 (np.ndarray or list, len(n_samples)) – First dataset from the complex frequency domain; vector of samples.

  • data_2 (np.ndarray or list, len(n_samples)) – Second dataset from the complex frequency domain; vector of samples.

  • nperseg (float) – Size of individual segments in fft.

  • pad_type (str) – Padding type, currently only “zero” padding is supported.

  • fs (float) – Sampling frequency

  • nfft (float) – FFT window size.

  • window (str) – FFT window type. Supported window types are listed at https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.get_window.html.

Returns:

  • binslist

    Frequency bins.

  • connlist

    Coherence values of the respective frequency bins measured via complex coherence.

Return type:

tuple of (list, list)

sfc.fd.run_cc(data_1, data_2)#

Calculate complex coherency from frequency domain data.

Parameters:
  • data_1 (np.ndarray or list, len(n_samples)) – First dataset from the complex frequency domain; vector of samples.

  • data_2 (np.ndarray or list, len(n_samples)) – Second dataset from the complex frequency domain; vector of samples.

Returns:

Connectivity between data_1 and data_2 measured using complex coherence.

Return type:

list

The following code example shows how to calcualte complex coherency, a precurses to several connectivity metrics.

import numpy as np

import finn.sfc.td as td
import finn.sfc.fd as fd

import finn.sfc._misc as misc
import demo_data.demo_data_paths as paths

def main():
    data = np.load(paths.fct_sfc_data)
    frequency_sampling = 5500
    frequency_peak = 30

    noise_weight = 0.2

    phase_shift = 153

    nperseg = frequency_sampling
    nfft = frequency_sampling

    #Generate data
    offset = int(np.ceil(frequency_sampling/frequency_peak))
    loc_data = data[offset:]
    signal_1 = np.zeros((loc_data).shape)
    signal_1 += loc_data
    signal_1 += np.random.random(len(loc_data)) * noise_weight

    loc_offset = offset - int(np.ceil(frequency_sampling/frequency_peak * phase_shift/360))
    loc_data = data[(loc_offset):]
    signal_2 = np.zeros(loc_data.shape)
    signal_2 += loc_data
    signal_2 += np.random.random(len(loc_data)) * noise_weight

    (bins, cc_td) = calc_from_time_domain(signal_1, signal_2, frequency_sampling, nperseg, nfft)
    cc_fd = calc_from_frequency_domain(signal_1, signal_2, frequency_sampling, nperseg, nfft)

    if ((cc_fd == cc_td).all() == False):
        print("Error")

def calc_from_time_domain(signal_1, signal_2, frequency_sampling, nperseg, nfft, window = "hann", pad_type = "zero"):
    return td.run_cc(signal_1, signal_2, nperseg, pad_type, frequency_sampling, nfft, window)

def calc_from_frequency_domain(signal_1, signal_2, frequency_sampling, nperseg, nfft, window = "hann", pad_type = "zero"):
    seg_data_X = misc._segment_data(signal_1, nperseg, pad_type)
    seg_data_Y = misc._segment_data(signal_2, nperseg, pad_type)

    (bins, fd_signal_1) = misc._calc_FFT(seg_data_X, frequency_sampling, nfft)
    (_,    fd_signal_2) = misc._calc_FFT(seg_data_Y, frequency_sampling, nfft)

    return fd.run_cc(fd_signal_1, fd_signal_2)


main()