如何在Python中绘制两个信号之间的连贯性?
Matplotlib是Python中用于数组二维图的惊人可视化库。 Matplotlib 是一个基于 NumPy 数组构建的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈配合使用。
什么是连贯性和相关性?
相干性:用于测量两个信号之间的相关性。
相关性:它定义了一个量相对于另一个量的依赖程度。如果一个量完全依赖于另一个量,则它们之间的相关性称为 1。如果两个量或变量彼此不相关,则它们的相关性为零。
Coherence 是归一化的交叉谱密度:
在Python中, Matplotlib.pyplot.cohere() 用于查找两个信号之间的相干性。
Syntax: matplotlib.pyplot.cohere(x, y, NFFT=256, Fs=2, Fc=0, detrend=, window=, noverlap=0, pad_to=None, sides=’default’, scale_by_freq=None, *, data=None, **kwargs)
Parameters: This method accepts the following parameters-
1)x, y : It is the sequence of data.
2) Fs: It is a scalar parameter and its default value is 2,
3) window: This parameter takes a data segment as an argument and returns the windowed version of the segment. Its default value is window_hanning()
4) sides: This parameter specifies which sides of the spectrum to return. This can have the following values: ‘default’, ‘one-sided’, and ‘two-sided’.
5) pad_to : This parameter contains the integer value to which the data segment is padded.
6) Fc: This parameter also contains the integer value to offsets the x extents of the plot to reflect the frequency range. Its default value is 0
7) NFFT: This parameter contains the number of data points used in each block for the FFT.
8) detrend: This parameter contains the function applied to each segment before fft-ing, designed to remove the mean or linear trend {‘none’, ‘mean’, ‘linear’}.
scale by freq: This parameter allows for integration over the returned frequency values.
9) overlap : This parameter is the number of points of overlap between blocks.
10) Fc : This parameter is the center frequency of x.
Returns : This method returns the following-
1) Cxy: This returns the coherence vector.
2) freqs: This returns the frequencies for the elements in Cxy.
The resultant is (Cxy, freqs)
让我们看看下面的例子,我们将使用上面的函数找到两个信号之间的一致性。
示例 1:
python3
import numpy as np
import matplotlib.pyplot as plt
# signal 1
time1=np.arange(0,100,0.1)
cossignal1= np.cos(time1)
plt.plot(cossignal1)
plt.title("Signal 1")
plt.show()
# signal 2
time2=np.arange(0,100,0.1)
cossignal2= np.cos(time2)
plt.plot(cossignal2)
plt.title("Signal 2")
plt.show()
# Store the value of correlation in a
# variable say 'cor' using the following code:
cor=plt.cohere(cossignal1,cossignal2)
# plot the coherence graph
plt.show()
Python3
import numpy as np
import matplotlib.pyplot as plt
# signal 1
time1 = np.arange(0, 100, 0.1)
sinsignal1 = np.sin(time1)
plt.plot(sinsignal1)
plt.title("Sine Signal")
plt.show()
# signal 2
time2 = np.arange(0, 100, 0.1)
cossignal2 = np.cos(time2)
plt.plot(cossignal2)
plt.title("Code Signal")
plt.show()
# Store the value of correlation in
# a variable say 'cor' using the
# following code
cor = plt.cohere(sinsignal1, cossignal2)
# Plot the coherence graph
plt.show()
输出:
示例 2:正弦和余弦信号之间的相干性
Python3
import numpy as np
import matplotlib.pyplot as plt
# signal 1
time1 = np.arange(0, 100, 0.1)
sinsignal1 = np.sin(time1)
plt.plot(sinsignal1)
plt.title("Sine Signal")
plt.show()
# signal 2
time2 = np.arange(0, 100, 0.1)
cossignal2 = np.cos(time2)
plt.plot(cossignal2)
plt.title("Code Signal")
plt.show()
# Store the value of correlation in
# a variable say 'cor' using the
# following code
cor = plt.cohere(sinsignal1, cossignal2)
# Plot the coherence graph
plt.show()
输出: