gpt4 book ai didi

python - python中连续小波变换图(比例图)中的频率轴

转载 作者:太空狗 更新时间:2023-10-30 00:18:57 27 4
gpt4 key购买 nike

我有一个 EEG 信号,我有兴趣在时域和频域中对其进行分析。我已经使用过 scipy.signal.spectrogram 函数,但我认为使用小波可以产生更好的特征提取结果。我尝试对我创建的人工信号运行连续小波变换,如下所示:

fs = 128.0
sampling_period = 1/fs
t = np.linspace(0, 2, 2*fs)
x = chirp(t,10,2,40,'quadratic')

coef, freqs = pywt.cwt(x, np.arange(1,50),'morl',
sampling_period=sampling_period)

然后我绘制了系数矩阵:

plt.matshow(coef)
plt.show()

the resuls for the code above

我的问题是如何调整比例和时间轴?

最佳答案

函数 plt.matshow(coef) 不使用时间和频率数组来创建轴(但它创建基于样本索引的轴)。

我建议使用 plt.pcolormesh(t, freqs, coef),因此时间和频率用于轴。然后你可以玩比例尺——比如,将频率轴放在对数比例尺中——并产生类似的东西:

enter image description here

这是生成图像的代码,源自您的示例:

import pywt
import numpy as np
import matplotlib.pyplot as plt

from scipy.signal import chirp

# Define signal
fs = 128.0
sampling_period = 1 / fs
t = np.linspace(0, 2, 2 * fs)
x = chirp(t, 10, 2, 40, 'quadratic')

# Calculate continuous wavelet transform
coef, freqs = pywt.cwt(x, np.arange(1, 50), 'morl',
sampling_period=sampling_period)

# Show w.r.t. time and frequency
plt.figure(figsize=(5, 2))
plt.pcolor(t, freqs, coef)

# Set yscale, ylim and labels
plt.yscale('log')
plt.ylim([1, 100])
plt.ylabel('Frequency (Hz)')
plt.xlabel('Time (sec)')
plt.savefig('egg.png', dpi=150)

关于python - python中连续小波变换图(比例图)中的频率轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43587314/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com