Plotting Coherence of Two Signals

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This lab demonstrates how to plot the coherence of two signals using Python's Matplotlib library. The coherence of two signals is a measure of their linear relationship, with a value of 1 indicating perfect coherence and a value of 0 indicating no coherence.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/PlotCustomizationGroup -.-> matplotlib/grid_config("`Grid Configuration`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48603{{"`Plotting Coherence of Two Signals`"}} matplotlib/importing_matplotlib -.-> lab-48603{{"`Plotting Coherence of Two Signals`"}} matplotlib/figures_axes -.-> lab-48603{{"`Plotting Coherence of Two Signals`"}} matplotlib/line_plots -.-> lab-48603{{"`Plotting Coherence of Two Signals`"}} matplotlib/grid_config -.-> lab-48603{{"`Plotting Coherence of Two Signals`"}} python/booleans -.-> lab-48603{{"`Plotting Coherence of Two Signals`"}} python/for_loops -.-> lab-48603{{"`Plotting Coherence of Two Signals`"}} python/lists -.-> lab-48603{{"`Plotting Coherence of Two Signals`"}} python/tuples -.-> lab-48603{{"`Plotting Coherence of Two Signals`"}} python/importing_modules -.-> lab-48603{{"`Plotting Coherence of Two Signals`"}} python/standard_libraries -.-> lab-48603{{"`Plotting Coherence of Two Signals`"}} python/math_random -.-> lab-48603{{"`Plotting Coherence of Two Signals`"}} python/numerical_computing -.-> lab-48603{{"`Plotting Coherence of Two Signals`"}} python/data_visualization -.-> lab-48603{{"`Plotting Coherence of Two Signals`"}} python/build_in_functions -.-> lab-48603{{"`Plotting Coherence of Two Signals`"}} end

Import Libraries

The first step is to import the necessary libraries. We will be using NumPy and Matplotlib.

import matplotlib.pyplot as plt
import numpy as np

Generate Signals

Next, we will generate two signals with a coherent part at 10 Hz and a random part. We will also add white noise to the signals.

## Fixing random state for reproducibility
np.random.seed(19680801)

dt = 0.01
t = np.arange(0, 30, dt)
nse1 = np.random.randn(len(t))                 ## white noise 1
nse2 = np.random.randn(len(t))                 ## white noise 2

s1 = np.sin(2 * np.pi * 10 * t) + nse1
s2 = np.sin(2 * np.pi * 10 * t) + nse2

Plot Signals

We can now plot the two signals in the time domain using Matplotlib.

fig, axs = plt.subplots(2, 1)
axs[0].plot(t, s1, t, s2)
axs[0].set_xlim(0, 2)
axs[0].set_xlabel('Time')
axs[0].set_ylabel('s1 and s2')
axs[0].grid(True)

Plot Coherence

We can now plot the coherence of the two signals using Matplotlib's cohere function.

cxy, f = axs[1].cohere(s1, s2, 256, 1. / dt)
axs[1].set_ylabel('Coherence')

Display Plot

Finally, we can display the plot using Matplotlib's show function.

fig.tight_layout()
plt.show()

Summary

This lab demonstrated how to plot the coherence of two signals using Python's Matplotlib library. We generated two signals with a coherent part at 10 Hz and a random part, added white noise to the signals, and plotted the signals in the time domain and their coherence using Matplotlib's cohere function.

Other Python Tutorials you may like