matplotlib中小波分析输出的基本作图

2024-06-01 08:08:48 发布

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

由于python模块pywt,我在实践中发现了小波。

我浏览过some examples of the pywt module usage,但我无法掌握关键步骤:基本上,我不知道如何用matplotlib来显示小波分析的多维输出。

这就是我所尝试的,(给定一个pyplot斧头ax):

import pywt

data_1_dimension_series = [0,0.1,0.2,0.4,-0.1,-0.1,-0.3,-0.4,1.0,1.0,1.0,0] 
# indeed my data_1_dimension_series is much longer

cA, cD = pywt.dwt(data_1_dimension_series, 'haar')

ax.set_xlabel('seconds')
ax.set_ylabel('wavelet affinity by scale factor')

ax.plot(axe_wt_time, zip(cA,cD))

或者也

data_wt_analysis = pywt.dwt(data_1_dimension_series, 'haar')
ax.plot(axe_wt_time, data_wt_analysis) 

ax.plot(axe_wt_time, data_wt_analysis)ax.plot(axe_wt_time, zip(cA,cD))都不合适,并返回错误。两者都抛出x and y must have the same first dimension

问题是data_wt_analysis包含几个一维序列,每个小波尺度因子一个。 我当然可以显示尽可能多的图表,因为有比例因素。但我希望它们都在同一张图中。

我怎么能用matplotlib只在一个图形中显示这样的数据呢?

像下面五颜六色的正方形:

enter image description here


Tags: thedatatimeplotmatplotlibcdanalysisax
1条回答
网友
1楼 · 发布于 2024-06-01 08:08:48

您应该从感兴趣的数组中提取不同的1D序列,并使用matplotlib作为最简单的示例

import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()

from doc.

您希望叠加1D图(或线图)。所以,如果你有列表l1,l2,l3,你会

import matplotlib.pyplot as plt
plt.plot(l1)
plt.plot(l2)
plt.plot(l3)
plt.show()

对于scalegram:我使用的是imshow()。这不是针对小波,而是同一个ID:一个颜色映射。

我发现了this sample用于imshow()小波,没想过

from pylab import *
import pywt
import scipy.io.wavfile as wavfile

# Find the highest power of two less than or equal to the input.
def lepow2(x):
    return 2 ** floor(log2(x))

# Make a scalogram given an MRA tree.
def scalogram(data):
    bottom = 0

    vmin = min(map(lambda x: min(abs(x)), data))
    vmax = max(map(lambda x: max(abs(x)), data))

    gca().set_autoscale_on(False)

    for row in range(0, len(data)):
        scale = 2.0 ** (row - len(data))

        imshow(
            array([abs(data[row])]),
            interpolation = 'nearest',
            vmin = vmin,
            vmax = vmax,
            extent = [0, 1, bottom, bottom + scale])

        bottom += scale

# Load the signal, take the first channel, limit length to a power of 2 for simplicity.
rate, signal = wavfile.read('kitten.wav')
signal = signal[0:lepow2(len(signal)),0]
tree = pywt.wavedec(signal, 'db5')

# Plotting.
gray()
scalogram(tree)
show()

相关问题 更多 >