使用 python 从小波系数创建二维数组
create 2D array from wavelets coefficients using python
我需要使用小波变换的细节系数创建类似于下图的二维数组。
我尝试了 2 周多的时间来寻找如何做到这一点。
此图表示不同级别(1、2、3、4 和 5)的小波变换的细节系数。细节系数(cA5、cD5、cD4、cD3、cD2、cD1=coeffs)是一维数组,每个数组都有不同的大小。我想使用小波系数创建类似于图像的二维数组,我该怎么做?
coeffs = wavedec(data, 'sym5', level=5)
cA5,cD5,cD4,cD3,cD2,cD1=coeffs
for i, ci in enumerate(coeffs):
plt.imshow(ci.reshape(1, -1), extent=[0, 2000, i + 0.5, i + 1.5], cmap='inferno', aspect='auto', interpolation='nearest')
plt.ylim(0.5, len(coeffs) + 0.5)
plt.yticks(range(1, len(coeffs) + 1), ['cA5', 'cD5', 'cD4', 'cD3', 'cD2', 'cD1'])
plt.show()
答案比您想象的要简单。
只需从 for
循环中取出 plt.show()
:
import pywt
import numpy as np
import matplotlib.pyplot as plt
data=np.random.rand(4000)
coeffs = pywt.wavedec(data, 'sym5', level=5)
cA5,cD5,cD4,cD3,cD2,cD1=coeffs
for i, ci in enumerate(coeffs):
plt.imshow(ci.reshape(1, -1), extent=[0, 2000, i + 0.5, i + 1.5], cmap='inferno', aspect='auto', interpolation='nearest')
plt.ylim(0.5, len(coeffs) + 0.5)
plt.yticks(range(1, len(coeffs) + 1), ['cA5', 'cD5', 'cD4', 'cD3', 'cD2', 'cD1'])
plt.show()
这将允许您更新您的身材:
编辑:
如果您将 plt.show()
留在循环中,它将为每个系数提供单独的图,如下所示:
引用这个 introduction to matplotlib:
One thing to be aware of: the plt.show() command should be used only
once per Python session, and is most often seen at the very end of the
script. Multiple show() commands can lead to unpredictable
backend-dependent behavior, and should mostly be avoided.
我需要使用小波变换的细节系数创建类似于下图的二维数组。 我尝试了 2 周多的时间来寻找如何做到这一点。
此图表示不同级别(1、2、3、4 和 5)的小波变换的细节系数。细节系数(cA5、cD5、cD4、cD3、cD2、cD1=coeffs)是一维数组,每个数组都有不同的大小。我想使用小波系数创建类似于图像的二维数组,我该怎么做?
coeffs = wavedec(data, 'sym5', level=5)
cA5,cD5,cD4,cD3,cD2,cD1=coeffs
for i, ci in enumerate(coeffs):
plt.imshow(ci.reshape(1, -1), extent=[0, 2000, i + 0.5, i + 1.5], cmap='inferno', aspect='auto', interpolation='nearest')
plt.ylim(0.5, len(coeffs) + 0.5)
plt.yticks(range(1, len(coeffs) + 1), ['cA5', 'cD5', 'cD4', 'cD3', 'cD2', 'cD1'])
plt.show()
答案比您想象的要简单。
只需从 for
循环中取出 plt.show()
:
import pywt
import numpy as np
import matplotlib.pyplot as plt
data=np.random.rand(4000)
coeffs = pywt.wavedec(data, 'sym5', level=5)
cA5,cD5,cD4,cD3,cD2,cD1=coeffs
for i, ci in enumerate(coeffs):
plt.imshow(ci.reshape(1, -1), extent=[0, 2000, i + 0.5, i + 1.5], cmap='inferno', aspect='auto', interpolation='nearest')
plt.ylim(0.5, len(coeffs) + 0.5)
plt.yticks(range(1, len(coeffs) + 1), ['cA5', 'cD5', 'cD4', 'cD3', 'cD2', 'cD1'])
plt.show()
这将允许您更新您的身材:
编辑:
如果您将 plt.show()
留在循环中,它将为每个系数提供单独的图,如下所示:
引用这个 introduction to matplotlib:
One thing to be aware of: the plt.show() command should be used only once per Python session, and is most often seen at the very end of the script. Multiple show() commands can lead to unpredictable backend-dependent behavior, and should mostly be avoided.