用python实现小波系数的可视化

2024-09-29 06:35:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要用小波变换的细节系数准备一个类似下图的图。我花了两个多星期的时间试图找到我能做到这一点的方法

enter image description here

此图表示不同级别(1、2、3和4)的小波变换细节系数。细节系数(cA4、cD4、cD3、cD2、cD1=系数)是一个一维数组,每个都有不同的大小

<

def wavelet(data):
    waveletname = 'sym5'
    coeffs = wavedec(data, 'sym5', level=5)
    cA5,cD5,cD4,cD3,cD2,cD1=coeffs

>


Tags: 方法ltdata时间数组级别细节系数
1条回答
网友
1楼 · 发布于 2024-09-29 06:35:05

一种可能的方法是将每个阵列绘制为一维图像,每个阵列位于不同的y位置

plt.imshow需要一个2D数组,因此将数组的形状改为1作为第一维,将原始大小作为第二维,可以得到一个水平图像。(如果它还不是numpy数组,则需要通过np.array(ci).reshape(1, -1)进行转换)。通过extent参数,可以设置边界x和y值interpolation='nearest'显示每个像素之间的硬边界aspect='auto'需要防止imshow设置固定的纵横比

from matplotlib import pyplot as plt
import numpy as np

# generate six random 1d arrays of different sizes
coeffs = [np.random.uniform(0, 1, np.random.randint(30, 100)) for i in range(6)]
# cA5, cD5, cD4, cD3, cD2, cD1 = coeffs

for i, ci in enumerate(coeffs):
    plt.imshow(ci.reshape(1, -1), extent=[0, 1000, i + 0.5, i + 1.5], cmap='inferno', aspect='auto', interpolation='nearest')

plt.ylim(0.5, len(coeffs) + 0.5) # set the y-lim to include the six horizontal images
# optionally relabel the y-axis (the given labeling is 1,2,3,...)
plt.yticks(range(1, len(coeffs) + 1), ['cA5', 'cD5', 'cD4', 'cD3', 'cD2', 'cD1'])

plt.show()

resulting plot

相关问题 更多 >