.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_misc_plot_itc.py: ================================================ Compute and plot the Inter-Trial Coherence (ITC) ================================================ This example illustrate how to compute and plot the Inter-Trial Coherence (ITC). The ITC can be used to inspect if phases are aligned across trials or said differently, it provides a measure of the consistency across trials. .. code-block:: default import numpy as np from tensorpac.utils import ITC, PSD import matplotlib.pyplot as plt Generate a random data of shape (n_epochs, n_times) ############################################################################## the dataset used in this example is composed of pure sines and noise. All sines across epochs are going to have a unique frequency so that there is no synchronization between them except around 0 second .. code-block:: default # Let's start by creating a random dataset n_epochs = 100 # number of trials n_pts = 1000 # number of time points sf = 512. # sampling frequency f_min = 10 # minimum sine frequency f_max = 15 # maximum sine frequency # create sines time = np.linspace(-n_pts / 2, n_pts / 2, n_pts) / sf freqs = np.linspace(f_min, f_max, n_epochs) data = np.sin(2 * np.pi * freqs.reshape(-1, 1) * time.reshape(1, -1)) data += .1 * np.random.rand(n_epochs, n_pts) # plot some trials and see how sines are synchronized around 0 trials = np.linspace(0, n_epochs - 1, 10).astype(int) plt.figure(0) plt.plot(time, data[trials, :].T, alpha=.5) plt.xlabel('Time (seconds)'), plt.ylabel('Amplitude (V)') .. image:: /auto_examples/misc/images/sphx_glr_plot_itc_001.png :class: sphx-glr-single-img Compute and plot the Power Spectrum Density (PSD) ############################################################################## the PSD can also be used to inspect how frequencies are distributed across trials .. code-block:: default psd = PSD(data, sf) plt.figure(1) psd.plot(f_max=30, confidence=None) .. image:: /auto_examples/misc/images/sphx_glr_plot_itc_002.png :class: sphx-glr-single-img Compute and plot the Inter-Trial Coherence (ITC) ############################################################################## finally, compute and plot the ITC sphinx_gallery_thumbnail_number = 3 .. code-block:: default edges = 10 # remove 10 points to remove edge effects due to filtering cycle = 6 # number of cycles to use to extract the phase plt.figure(2, figsize=(14, 4)) # compute ITC for phases between [2, 30]Hz itc = ITC(data, sf, f_pha=[f_min, f_max], edges=edges, cycle=cycle, n_jobs=1) plt.subplot(121) itc.plot(times=time) plt.axvline(0, linestyle='--', color='black', lw=2) # compute ITC for phases between [2, 30]Hz with frequency steps itc = ITC(data, sf, f_pha=(2, 30, 1, .5), edges=edges, cycle=cycle, n_jobs=1) plt.subplot(122) itc.plot(times=time, cmap='viridis') plt.axvline(0, linestyle='--', color='white', lw=2) itc.show() .. image:: /auto_examples/misc/images/sphx_glr_plot_itc_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 3.965 seconds) .. _sphx_glr_download_auto_examples_misc_plot_itc.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: plot_itc.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: plot_itc.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_