微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Python中绘制DWT比例图

如何解决在Python中绘制DWT比例图

我有一个来自磁检测器的信号,我不感兴趣进行分析,我已经使用wavedec()进行了信号分解

coeffs = pywt.wavedec(dane_K180_40['CH1[uV]'],'coif5',level=5)

我收到的分解系数如下:

cA1,cD5,cD4,cD3,cD2,cD1 = coeffs

这些是具有各种长度的ndarrays对象。 cD1为(1519,)cD2为(774,)依此类推。我遇到的主要障碍是阵列的长度不同。 coefficients

我的问题:

我必须制作 DWT比例图 ,但我不能过分强调自己已尽力而为。

什么是最好的方法?如下使用matpllotlib的 imshow()

plt.imshow(np.abs([cD5,cD1]),cmap='bone',interpolation='none',aspect='auto')

给我一​​个错误

TypeError:dtype对象的图像数据无法转换为float

由于我不是python专家,因此我尝试用Google搜索它,并且尝试将ndarrays更改为float。

绘制比例尺图,matshow,pcolormesh的最佳方法是什么? ; D

解决方法

基本上,每个 cDi 数组的样本数量是前一个数组的一半(这不是每个母小波的情况!),所以我创建了一个 2D numpy 数组,其中第一个元素是“完整”数量的样本,并且对于每个后续级别,我重复样本 2^level 次,以便最终结果是一个矩形块。您可以选择要将 Y 轴绘制为线性还是对数刻度。

# Create signal
xc = np.linspace(0,t_n,num=N)
xd = np.linspace(0,num=32)
sig = np.sin(2*np.pi * 64 * xc[:32]) * (1 - xd)
composite_signal3 = np.concatenate([np.zeros(32),sig[:32],np.zeros(N-32-32)])

# Use the Daubechies wavelet
w = pywt.Wavelet('db1')
# Perform Wavelet transform up to log2(N) levels
lvls = ceil(log2(N))
coeffs = pywt.wavedec(composite_signal3,w,level=lvls)
# Each level of the WT will split the frequency band in two and apply a
# WT on the highest band. The lower band then gets split into two again,# and a WT is applied on the higher band of that split. This repeats
# 'lvls' times.
#
# Since the amount of samples in each step decreases,we need to make
# sure that we repeat the samples 2^i times where i is the level so
# that at each level,we have the same amount of transformed samples
# as in the first level. This is only necessary because of plotting.
cc = np.abs(np.array([coeffs[-1]]))
for i in range(lvls - 1):
    cc = np.concatenate(np.abs([cc,np.array([np.repeat(coeffs[lvls - 1 - i],pow(2,i + 1))])]))

plt.figure()
plt.xlabel('Time (s)')
plt.ylabel('Frequency (Hz)')
plt.title('Discrete Wavelet Transform')
# X-axis has a linear scale (time)
x = np.linspace(start=0,stop=1,num=N//2)
# Y-axis has a logarithmic scale (frequency)
y = np.logspace(start=lvls-1,stop=0,num=lvls,base=2)
X,Y = np.meshgrid(x,y)
plt.pcolormesh(X,Y,cc)

use_log_scale = False

if use_log_scale:
    plt.yscale('log')
else:
    yticks = [pow(2,i) for i in range(lvls)]
    plt.yticks(yticks)

plt.tight_layout()
plt.show()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。