当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Python cusignal.spectral_analysis.spectral.coherence用法及代码示例

用法:

cusignal.spectral_analysis.spectral.coherence(x, y, fs=1.0, window='hann', nperseg=None, noverlap=None, nfft=None, detrend='constant', axis=- 1)

使用 Welch 方法估计 discrete-time 信号 X 和 Y 的幅度平方相干估计值 Cxy。

Cxy = abs(Pxy)**2/(Pxx*Pyy) ,其中 PxxPyy 是 X 和 Y 的功率谱密度估计,Pxy 是 X 和 Y 的交叉谱密度估计。

参数

xarray_like

测量值的时间序列

yarray_like

测量值的时间序列

fs浮点数,可选

xy 时间序列的采样频率。默认为 1.0。

windowstr 或 tuple 或 数组,可选

想要使用的窗口。如果window是字符串或元组,则传递给get_window生成窗口值,默认为DFT-even。有关窗口和所需参数的列表,请参阅get_window。如果window 是数组,它将直接用作窗口,其长度必须为nperseg。默认为 Hann 窗口。

nperseg整数,可选

每个段的长度。默认为 None,但如果 window 是 str 或 tuple,则设置为 256,如果 window 是 数组,则设置为窗口的长度。

noverlap: int, optional

段之间重叠的点数。如果 None , noverlap = nperseg // 2 。默认为 None

nfft整数,可选

如果需要零填充 FFT,则使用的 FFT 的长度。如果 None ,则 FFT 长度为 nperseg 。默认为 None

detrendstr 或 function 或 False ,可选

指定如何去除每个段的趋势。如果detrend 是一个字符串,它作为type 参数传递给detrend 函数。如果它是一个函数,它接受一个段并返回一个去趋势的段。如果 detrendFalse ,则不进行去趋势。默认为‘constant’。

axis整数,可选

为两个输入计算相干性的轴;默认值在最后一个轴上(即 axis=-1 )。

返回

fndarray

采样频率数组。

Cxyndarray

x 和 y 的幅度平方相干性。

注意

适当的重叠量取决于窗口的选择和您的要求。对于默认的 Hann 窗口,50% 的重叠是在准确估计信号功率和不过度计算任何数据之间的合理权衡。较窄的窗口可能需要较大的重叠。

参考

1

P. Welch, “The use of the fast Fourier transform for the estimation of power spectra: A method based on time averaging over short, modified periodograms”, IEEE Trans. Audio Electroacoust. vol. 15, pp. 70-73, 1967.

2

Stoica, Petre, and Randolph Moses, “Spectral Analysis of Signals” Prentice Hall, 2005

例子

>>> import cusignal
>>> import cupy as cp
>>> import matplotlib.pyplot as plt

生成两个具有一些共同特征的测试信号。

>>> fs = 10e3
>>> N = 1e5
>>> amp = 20
>>> freq = 1234.0
>>> noise_power = 0.001 * fs / 2
>>> time = cp.arange(N) / fs
>>> b, a = cusignal.butter(2, 0.25, 'low')
>>> x = cp.random.normal(scale=cp.sqrt(noise_power), size=time.shape)
>>> # lfilter not implemented in cuSignal
>>> y = cusignal.lfilter(b, a, x)
>>> x += amp*cp.sin(2*cp.pi*freq*time)
>>> y += cp.random.normal(scale=0.1*cp.sqrt(noise_power), size=time.shape)

计算并绘制相干性。

>>> f, Cxy = cusignal.coherence(x, y, fs, nperseg=1024)
>>> plt.semilogy(cp.asnumpy(f), cp.asnumpy(Cxy))
>>> plt.xlabel('frequency [Hz]')
>>> plt.ylabel('Coherence')
>>> plt.show()

相关用法


注:本文由纯净天空筛选整理自rapids.ai大神的英文原创作品 cusignal.spectral_analysis.spectral.coherence。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。