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 – data set X from time domain.
data_2 – data set Y from time domain.
nperseg – number of samples in a fft segment.
pad_type – padding type to be applied.
fs – Sampling frequency.
nfft – Size of fft window.
window – FFT window type. Supported window types are listed at https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.get_window.html.
- Returns:
Complex coherency.
- sfc.fd.run_cc(data_1, data_2)#
Calculate complex coherency from frequency domain data.
- Parameters:
f_data_1 – data set X from the complex frequency domain.
f_data_2 – data set Y from the compley frequency domain.
- Returns:
Complex coherency
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()